Complex classes like TemplateAssetsResolver 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 TemplateAssetsResolver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class TemplateAssetsResolver extends CollectionResolver implements MimeResolverAwareInterface |
||
19 | { |
||
20 | |||
21 | /** |
||
22 | * The assets paths |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $assetsPaths = array(); |
||
27 | |||
28 | /** |
||
29 | * The mime resolver. |
||
30 | * |
||
31 | * @var MimeResolver |
||
32 | */ |
||
33 | protected $mimeResolver; |
||
34 | |||
35 | /** |
||
36 | * The templates paths |
||
37 | * |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $templatesPathStack = array(); |
||
41 | |||
42 | /** |
||
43 | * The name of the folder inside the assets paths that will generate the global.css and global.js |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $globalFolderName; |
||
48 | |||
49 | /* |
||
50 | * Regex Pattern to match (first group) the template name |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $patternTemplateName; |
||
55 | |||
56 | /* |
||
57 | * Regex Pattern to match (in any group) if a filename is inside a global assets folder |
||
58 | * |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $patternGlobalAssets; |
||
62 | |||
63 | /** |
||
64 | * Path to the public dir |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $publicPath; |
||
69 | |||
70 | /** |
||
71 | * Get the Paths where assets are allowed |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | 5 | public function getAssetsPaths() |
|
79 | |||
80 | /** |
||
81 | * Set the Paths where assets are allowed |
||
82 | * |
||
83 | * @param array $assetsPaths |
||
84 | */ |
||
85 | 13 | public function setAssetsPaths(Array $assetsPaths = null) |
|
89 | |||
90 | /** |
||
91 | * Set the mime resolver |
||
92 | * |
||
93 | * @param MimeResolver $resolver |
||
94 | */ |
||
95 | 4 | public function setMimeResolver(MimeResolver $resolver) |
|
99 | |||
100 | /** |
||
101 | * Get the mime resolver |
||
102 | * |
||
103 | * @return MimeResolver |
||
104 | */ |
||
105 | 3 | public function getMimeResolver() |
|
109 | |||
110 | /** |
||
111 | * Retrieve paths to templates |
||
112 | * |
||
113 | * @return array |
||
114 | */ |
||
115 | 6 | public function getTemplatesPathStack() |
|
119 | |||
120 | /** |
||
121 | * Set the templates paths |
||
122 | * |
||
123 | * @param array $templatesPathStack |
||
124 | */ |
||
125 | 13 | public function setTemplatesPathStack(Array $templatesPathStack) |
|
129 | |||
130 | /** |
||
131 | * Get the name of the global folder |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | 1 | public function getGlobalFolderName() |
|
139 | |||
140 | /** |
||
141 | * Set the name of the global folder |
||
142 | * |
||
143 | * @param string $globalFolderName |
||
144 | */ |
||
145 | 13 | public function setGlobalFolderName($globalFolderName = '') |
|
149 | |||
150 | /** |
||
151 | * Get the pattern to match the template name |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | 7 | public function getPatternTemplateName() |
|
159 | |||
160 | /** |
||
161 | * Get the pattern to match if a file is a global asset |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | 2 | public function getPatternGlobalAssets() |
|
169 | |||
170 | /** |
||
171 | * Set the pattern to match the template name |
||
172 | * |
||
173 | * @param string $patternTemplateName |
||
174 | */ |
||
175 | 13 | public function setPatternTemplateName($patternTemplateName) |
|
179 | |||
180 | /** |
||
181 | * Set the pattern to match if a file is a global asset |
||
182 | * |
||
183 | * @param string $patternGlobalAssets |
||
184 | */ |
||
185 | 13 | public function setPatternGlobalAssets($patternGlobalAssets) |
|
189 | |||
190 | /** |
||
191 | * Returns the public path |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | 1 | public function getPublicPath() |
|
199 | |||
200 | /** |
||
201 | * Sets the public path |
||
202 | * |
||
203 | * @param string $publicPath |
||
204 | */ |
||
205 | public function setPublicPath($publicPath) |
||
209 | |||
210 | |||
211 | /** |
||
212 | * Constructor |
||
213 | * |
||
214 | * Instantiate, set the assets paths, templates paths and the current template |
||
215 | * @param array $assetsPaths |
||
216 | * @param array $templatesPathStack |
||
217 | */ |
||
218 | 13 | public function __construct(Array $assetsPaths, Array $templatesPathStack) |
|
224 | |||
225 | /** |
||
226 | * Adds a collection of assets with an alias |
||
227 | * |
||
228 | * @param string $alias |
||
229 | * @param array $assets |
||
230 | */ |
||
231 | 4 | public function addToCollections($alias, Array $assets) |
|
237 | |||
238 | /** |
||
239 | * Returns the template name if the requested asset belongs to a template |
||
240 | * |
||
241 | * @param string $alias |
||
242 | * @return boolean|array |
||
243 | */ |
||
244 | 7 | public function matchTemplateName($alias) |
|
253 | |||
254 | /** |
||
255 | * Returns true if the asset belongs to a template |
||
256 | * |
||
257 | * @param string $alias |
||
258 | * @return boolean |
||
259 | */ |
||
260 | 4 | public function isTemplateAsset($alias) |
|
268 | |||
269 | /** |
||
270 | * Returns true if the asset is used globally |
||
271 | * |
||
272 | * @param string $name |
||
273 | * @return boolean |
||
274 | */ |
||
275 | 2 | public function isGlobalAsset($name) |
|
280 | |||
281 | /** |
||
282 | * Generates the collection of global assets by iterating over the assets |
||
283 | * in the global assets directory and adds it to the Resolver |
||
284 | * |
||
285 | * @param string $alias |
||
286 | */ |
||
287 | 1 | public function loadGlobalCollection($alias) |
|
295 | |||
296 | /** |
||
297 | * Generates the collection of the template assets by iterating over the assets |
||
298 | * in the template directory and adds it to the Resolver |
||
299 | * |
||
300 | * @param string $alias |
||
301 | */ |
||
302 | 3 | public function loadTemplateCollection($alias) |
|
318 | |||
319 | /** |
||
320 | * Resolves assets with absolute path |
||
321 | * |
||
322 | * @param string $path |
||
323 | * @return FileAsset |
||
324 | */ |
||
325 | 4 | public function resolveAbsolutePath($path) |
|
339 | |||
340 | /** |
||
341 | * {@inheritDoc} |
||
342 | */ |
||
343 | 2 | public function resolve($name) |
|
364 | |||
365 | /** |
||
366 | * Return the FIRST paths that contain a template with the specified name |
||
367 | * (There should not be more than one posible template path) |
||
368 | * |
||
369 | * @param string $templateName |
||
370 | * @return string |
||
371 | */ |
||
372 | 3 | public function getExistantTemplatePath($templateName) |
|
383 | |||
384 | /** |
||
385 | * Returns true if it is a valid template |
||
386 | * |
||
387 | * @param string $templatePath |
||
388 | * @return boolean |
||
389 | */ |
||
390 | 3 | public function validTemplate($templatePath) |
|
399 | |||
400 | /** |
||
401 | * Returns true if the asset is in an allowed path |
||
402 | * |
||
403 | * @param string $name The path to the asset |
||
404 | * @return boolean If the asset is in an allowed path will return true. |
||
405 | */ |
||
406 | 4 | public function inAllowedPaths($name) |
|
416 | |||
417 | /** |
||
418 | * Returns an array with the global assets path |
||
419 | * |
||
420 | * @return array |
||
421 | */ |
||
422 | 1 | public function getGlobalAssetsPaths() |
|
432 | |||
433 | /** |
||
434 | * Generate the collections of assets for the a template. |
||
435 | * @param string $extension |
||
436 | * @return array|Traversable collections of assets |
||
437 | */ |
||
438 | 1 | public function generateCollection($paths, $extension) |
|
451 | } |
||
452 |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.