Complex classes like Generic often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Generic, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | final class Generic implements Message |
||
17 | { |
||
18 | private $contentType; |
||
19 | private $contentEncoding; |
||
20 | private $headers; |
||
21 | private $deliveryMode; |
||
22 | private $priority; |
||
23 | private $correlationId; |
||
24 | private $replyTo; |
||
25 | private $expiration; |
||
26 | private $id; |
||
27 | private $timestamp; |
||
28 | private $type; |
||
29 | private $userId; |
||
30 | private $appId; |
||
31 | private $body; |
||
32 | |||
33 | 22 | public function __construct(Str $body) |
|
37 | |||
38 | 9 | public function hasContentType(): bool |
|
42 | |||
43 | 2 | public function contentType(): ContentType |
|
47 | |||
48 | 2 | public function withContentType(ContentType $contentType): Message |
|
55 | |||
56 | 9 | public function hasContentEncoding(): bool |
|
60 | |||
61 | 2 | public function contentEncoding(): ContentEncoding |
|
65 | |||
66 | 2 | public function withContentEncoding(ContentEncoding $contentEncoding): Message |
|
73 | |||
74 | 9 | public function hasHeaders(): bool |
|
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | 2 | public function headers(): MapInterface |
|
86 | |||
87 | 3 | public function withHeaders(MapInterface $headers): Message |
|
101 | |||
102 | 9 | public function hasDeliveryMode(): bool |
|
106 | |||
107 | 2 | public function deliveryMode(): DeliveryMode |
|
111 | |||
112 | 2 | public function withDeliveryMode(DeliveryMode $deliveryMode): Message |
|
119 | |||
120 | 9 | public function hasPriority(): bool |
|
124 | |||
125 | 2 | public function priority(): Priority |
|
129 | |||
130 | 2 | public function withPriority(Priority $priority): Message |
|
137 | |||
138 | 9 | public function hasCorrelationId(): bool |
|
142 | |||
143 | 2 | public function correlationId(): CorrelationId |
|
147 | |||
148 | 2 | public function withCorrelationId(CorrelationId $correlationId): Message |
|
155 | |||
156 | 9 | public function hasReplyTo(): bool |
|
160 | |||
161 | 2 | public function replyTo(): ReplyTo |
|
165 | |||
166 | 2 | public function withReplyTo(ReplyTo $replyTo): Message |
|
173 | |||
174 | 9 | public function hasExpiration(): bool |
|
178 | |||
179 | 2 | public function expiration(): ElapsedPeriod |
|
183 | |||
184 | 2 | public function withExpiration(ElapsedPeriod $expiration): Message |
|
191 | |||
192 | 9 | public function hasId(): bool |
|
196 | |||
197 | 2 | public function id(): Id |
|
201 | |||
202 | 2 | public function withId(Id $id): Message |
|
209 | |||
210 | 9 | public function hasTimestamp(): bool |
|
214 | |||
215 | 2 | public function timestamp(): PointInTimeInterface |
|
219 | |||
220 | 2 | public function withTimestamp(PointInTimeInterface $timestamp): Message |
|
227 | |||
228 | 9 | public function hasType(): bool |
|
232 | |||
233 | 2 | public function type(): Type |
|
237 | |||
238 | 2 | public function withType(Type $type): Message |
|
245 | |||
246 | 9 | public function hasUserId(): bool |
|
250 | |||
251 | 2 | public function userId(): UserId |
|
255 | |||
256 | 2 | public function withUserId(UserId $userId): Message |
|
263 | |||
264 | 9 | public function hasAppId(): bool |
|
268 | |||
269 | 2 | public function appId(): AppId |
|
273 | |||
274 | 2 | public function withAppId(AppId $appId): Message |
|
281 | |||
282 | 8 | public function body(): Str |
|
286 | } |
||
287 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.