Complex classes like CookieJar 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 CookieJar, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class CookieJar implements CookieJarInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var CookieFactoryInterface |
||
27 | */ |
||
28 | private $cookieFactory; |
||
29 | |||
30 | /** |
||
31 | * @var CookieInterface[] |
||
32 | */ |
||
33 | private $cookies = []; |
||
34 | |||
35 | /** |
||
36 | * @param CookieFactoryInterface|null $cookieFactory |
||
37 | */ |
||
38 | 387 | public function __construct(CookieFactoryInterface $cookieFactory = null) |
|
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 126 | public function getCookieFactory() |
|
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | 387 | public function setCookieFactory(CookieFactoryInterface $cookieFactory) |
|
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 9 | public function clean() |
|
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | 207 | public function clear($domain = null, $path = null, $name = null) |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 189 | public function hasCookies() |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 288 | public function getCookies() |
|
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | 117 | public function setCookies(array $cookies) |
|
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 117 | public function addCookies(array $cookies) |
|
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | 9 | public function removeCookies(array $cookies) |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 261 | public function hasCookie(CookieInterface $cookie) |
|
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 261 | public function addCookie(CookieInterface $cookie) |
|
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | 108 | public function removeCookie(CookieInterface $cookie) |
|
194 | |||
195 | /** |
||
196 | * {@inheritdoc} |
||
197 | */ |
||
198 | 9 | public function populate(InternalRequestInterface $request) |
|
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | 9 | public function extract(InternalRequestInterface $request, ResponseInterface $response) |
|
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | 162 | public function count() |
|
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | 162 | public function getIterator() |
|
240 | } |
||
241 |
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.