4/20/2023 0 Comments Equally downcast![]() We see next() is the target of our refactoring. I just implemented the most basic case of advancement of a plate (it will overflow after 26 next() calls), to keep this code simple and to the point. This object models a license plate used in a Car. In the example, we target the most useful case: a Value Object whose creation from its primitive value should be encapsulated in a method. This mean modifying both the call and for objects, while you can do one step at the time in the case of scalar casting due to the (type) operation being idempotent.įinally, remember to update the docblock of the modified methods accordingly. The second step is moving the down cast into the method. Ultimately (the other way around), any method returning an array or scalar value is a suspect.Value Objects having many getters for their fields, or calculated fields, can often return another Value Object look for duplicated logic on the client code.new M圜lass(.) statements often executed on the result of a method.The most important step in applying this refactoring is to identify the cases where it would work. The standard however, is the annotation: /** While the information is embodied into the code in statically typed languages, PHP gives you freedom to return whichever type you need but does not provide a formal way to document the choices. ** Topic */ $filteredTopic = $topic->getSelectedPostsForQuery("refactoring") ĭocblocks applied to methods have also to be modified to reflect into the API documentation what you're returning (an object, a Traversable, an IteratorAggregate or a MyIterator). The result is that conversions to and from the primitive structure flourish what we want to target with this refactoring is to encapsulate the initial conversions as much as possible, an only work with a closed algebra of objects in all the rest of the code: /** Topic */ $topic = $object->getAllPosts() However, it is not introduced in all the code, since it will take long for such a refactoring to be completed. $firstPage = new Topic($topic->getPart($offset = 0, $limit = 10)) Ī simple reason for why this happens in this little example is that the the Topic object, representing a collection of Posts, gets introduced into the codebase to host some new methods that make sense only on a set of Posts. Sometimes this conversion gets duplicated like for the case of casting primitives: $topic = new Topic($object->getAllPosts()) Value ObjectsĪnother form of casting/conversion that we need in hybrid languages like PHP is the conversion of a primitive (scalar or array) value into an object (Value Object usually). The logic of the refactoring is the same as for the casts on objects: encapsulating them into the method results in a cleaner API and the lack of duplication of the cast in all the client code. The (bool) cast will be spread throughout all the client code calling $object. $result = (bool) $theater->isASeatAvailable() ![]() Return $this->numberOfSeats - 42 // 42 is a total of sold tickets For example, we want to avoid this smelly code: public function isASeatAvailable() ![]() PHP rules tell us how they are evaluated in its specification for type juggling.Įncapsulate Downcast is equally valid when we want to convert a type and only want to expose the right one. Scalarsįirst of all, you may need to cast scalar variables to other types (e.g. Since this casting is absent in PHP and the type coincide with the current content of the variable, we will talk about conversions of variables to different types. Note that in Fowler's book downcasting is applied only to objects: the same instance just aquires a new (larger) interface. Casting with (ClassName) is not even supported by the language (while casting a non-object with (object) will give you a stdClass.)ĭowncasting however means to promote a variable from a stricter interface to a richer one: we will apply this refactoring to scalars and their OO equivalents, Value Objects. You'll never need to downcast objects: variables can contain handlers to objects or even scalars without compile-time checks. ![]() This problem was really diffused in old Java code (before Generics introduction) and still today in some cases. A Java example can be a collection of Object instances where some of them are actually a String: to obtain a String instance to be able to call string.startsWith("prefix_"), the code using the collection needs a down cast: String myString = (String) myObject Statically typed languages sometimes encounter the problem of downcasting: the compiler is only able to guarantee a basic type, and the object contained instead is an instance of a richer subtype. PHP instead, a is dynamic language where variable can contain any object, and the only enforcement of an interface can be performed on method parameters via type hinting. In statically typed languages, each variable must have a minimal type known at compile time.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |