Complex classes like UrlFilters 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 UrlFilters, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class UrlFilters extends Filters |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * @var SourceCodeHighlighter |
||
31 | */ |
||
32 | private $highlighter; |
||
33 | |||
34 | /** |
||
35 | * @var Markup |
||
36 | */ |
||
37 | private $markup; |
||
38 | |||
39 | /** |
||
40 | * @var ElementResolverInterface |
||
41 | */ |
||
42 | private $elementResolver; |
||
43 | |||
44 | /** |
||
45 | * @var Configuration |
||
46 | */ |
||
47 | private $configuration; |
||
48 | |||
49 | /** |
||
50 | * @var LinkBuilder |
||
51 | */ |
||
52 | private $linkBuilder; |
||
53 | |||
54 | /** |
||
55 | * @var ElementLinkFactory |
||
56 | */ |
||
57 | private $elementLinkFactory; |
||
58 | |||
59 | |||
60 | public function __construct( |
||
61 | Configuration $configuration, |
||
62 | SourceCodeHighlighter $highlighter, |
||
63 | Markup $markup, |
||
64 | ElementResolverInterface $elementResolver, |
||
65 | LinkBuilder $linkBuilder, |
||
66 | ElementLinkFactory $elementLinkFactory |
||
67 | ) { |
||
68 | $this->highlighter = $highlighter; |
||
69 | $this->markup = $markup; |
||
70 | $this->elementResolver = $elementResolver; |
||
71 | $this->configuration = $configuration; |
||
72 | $this->linkBuilder = $linkBuilder; |
||
73 | $this->elementLinkFactory = $elementLinkFactory; |
||
74 | } |
||
75 | |||
76 | |||
77 | /** |
||
78 | * Tries to parse a definition of a class/method/property/constant/function |
||
79 | * and returns the appropriate link if successful. |
||
80 | * |
||
81 | * @param string $definition |
||
82 | * @param ElementReflectionInterface $reflectionElement |
||
83 | * @return string|NULL |
||
84 | */ |
||
85 | public function resolveLink($definition, ElementReflectionInterface $reflectionElement) |
||
86 | { |
||
87 | if (empty($definition)) { |
||
88 | return null; |
||
89 | } |
||
90 | |||
91 | $suffix = ''; |
||
92 | if (substr($definition, -2) === '[]') { |
||
93 | $definition = substr($definition, 0, -2); |
||
94 | $suffix = '[]'; |
||
95 | } |
||
96 | |||
97 | $element = $this->elementResolver->resolveElement($definition, $reflectionElement, $expectedName); |
||
|
|||
98 | if ($element === null || $element instanceof FunctionReflectionInterface) { |
||
99 | return $expectedName; |
||
100 | } |
||
101 | |||
102 | $classes = []; |
||
103 | if ($element->isDeprecated()) { |
||
104 | $classes[] = 'deprecated'; |
||
105 | } |
||
106 | |||
107 | /** @var FunctionReflectionInterface $element */ |
||
108 | if (! $element->isValid()) { |
||
109 | $classes[] = 'invalid'; |
||
110 | } |
||
111 | |||
112 | $link = $this->createLinkForElement($element, $classes); |
||
113 | return '<code>' . $link . $suffix . '</code>'; |
||
114 | } |
||
115 | |||
116 | |||
117 | /** |
||
118 | * @param string $value |
||
119 | * @param string $name |
||
120 | * @param ElementReflectionInterface $reflectionElement |
||
121 | * @return string |
||
122 | */ |
||
123 | public function annotation($value, $name, ElementReflectionInterface $reflectionElement) |
||
124 | { |
||
125 | $annotationProcessors = [ |
||
126 | 'return' => $this->processReturnAnnotations($value, $reflectionElement), |
||
127 | 'throws' => $this->processThrowsAnnotations($value, $reflectionElement), |
||
128 | 'license' => $this->processLicenseAnnotations($value), |
||
129 | 'link' => $this->processLinkAnnotations($value), |
||
130 | 'see' => $this->processSeeAnnotations($value, $reflectionElement), |
||
131 | 'uses' => $this->processUsesAndUsedbyAnnotations($value, $reflectionElement), |
||
132 | 'usedby' => $this->processUsesAndUsedbyAnnotations($value, $reflectionElement), |
||
133 | ]; |
||
134 | |||
135 | if (isset($annotationProcessors[$name])) { |
||
136 | return $annotationProcessors[$name]; |
||
137 | } |
||
138 | |||
139 | return $this->doc($value, $reflectionElement); |
||
140 | } |
||
141 | |||
142 | |||
143 | /** |
||
144 | * Returns links for types. |
||
145 | * |
||
146 | * @param string $annotation |
||
147 | * @param ElementReflectionInterface $reflectionElement |
||
148 | * @return string |
||
149 | */ |
||
150 | public function typeLinks($annotation, ElementReflectionInterface $reflectionElement) |
||
151 | { |
||
152 | $links = []; |
||
153 | |||
154 | list($types) = Strings::split($annotation); |
||
155 | if (! empty($types) && $types[0] === '$') { |
||
156 | $types = null; |
||
157 | } |
||
158 | |||
159 | foreach (explode('|', $types) as $type) { |
||
160 | $type = $this->getTypeName($type, false); |
||
161 | $links[] = $this->resolveLink($type, $reflectionElement) ?: LatteFilters::escapeHtml(ltrim($type, '\\')); |
||
162 | } |
||
163 | |||
164 | return implode('|', $links); |
||
165 | } |
||
166 | |||
167 | |||
168 | /********************* description *********************/ |
||
169 | |||
170 | |||
171 | /** |
||
172 | * @param string $annotation |
||
173 | * @param ElementReflectionInterface $reflectionElement |
||
174 | * @return string |
||
175 | */ |
||
176 | public function description($annotation, ElementReflectionInterface $reflectionElement) |
||
177 | { |
||
178 | $description = trim(strpbrk($annotation, "\n\r\t $")) ?: $annotation; |
||
179 | return $this->doc($description, $reflectionElement); |
||
180 | } |
||
181 | |||
182 | |||
183 | /** |
||
184 | * @param ElementReflectionInterface $reflectionElement |
||
185 | * @param bool $block |
||
186 | * @return string |
||
187 | */ |
||
188 | public function shortDescription(ElementReflectionInterface $reflectionElement, $block = false) |
||
192 | |||
193 | |||
194 | /** |
||
195 | * @return string |
||
196 | */ |
||
197 | public function longDescription(ElementReflectionInterface $element) |
||
210 | |||
211 | |||
212 | /********************* text formatter *********************/ |
||
213 | |||
214 | |||
215 | /** |
||
216 | * @param string $text |
||
217 | * @param ElementReflectionInterface $reflectionElement |
||
218 | * @param bool $block |
||
219 | * @return string |
||
220 | */ |
||
221 | public function doc($text, ElementReflectionInterface $reflectionElement, $block = false) |
||
234 | |||
235 | |||
236 | /** |
||
237 | * @param string $text |
||
238 | * @return string |
||
239 | */ |
||
240 | private function resolveInternalAnnotation($text) |
||
255 | |||
256 | |||
257 | /** |
||
258 | * @param string $text |
||
259 | * @param ElementReflectionInterface $reflectionElement |
||
260 | * @return string |
||
261 | */ |
||
262 | private function resolveLinkAndSeeAnnotation($text, ElementReflectionInterface $reflectionElement) |
||
278 | |||
279 | |||
280 | /********************* highlight *********************/ |
||
281 | |||
282 | |||
283 | /** |
||
284 | * @param string $source |
||
285 | * @param ElementReflectionInterface $reflectionElement |
||
286 | * @return string |
||
287 | */ |
||
288 | public function highlightPhp($source, ElementReflectionInterface $reflectionElement) |
||
293 | |||
294 | |||
295 | /** |
||
296 | * @param string $definition |
||
297 | * @param ElementReflectionInterface $reflectionElement |
||
298 | * @return string |
||
299 | */ |
||
300 | public function highlightValue($definition, ElementReflectionInterface $reflectionElement) |
||
304 | |||
305 | |||
306 | /** |
||
307 | * @return string |
||
308 | */ |
||
309 | private function createLinkForElement($reflectionElement, array $classes) |
||
313 | |||
314 | |||
315 | /** |
||
316 | * @param string $value |
||
317 | * @param ElementReflectionInterface $reflectionElement |
||
318 | * @return string |
||
319 | */ |
||
320 | private function processReturnAnnotations($value, ElementReflectionInterface $reflectionElement) |
||
326 | |||
327 | |||
328 | /** |
||
329 | * @param string $value |
||
330 | * @param ElementReflectionInterface $elementReflection |
||
331 | * @return string |
||
332 | */ |
||
333 | private function processThrowsAnnotations($value, ElementReflectionInterface $elementReflection) |
||
334 | { |
||
335 | $description = $this->getDescriptionFromValue($value, $elementReflection); |
||
336 | $typeLinks = $this->typeLinks($value, $elementReflection); |
||
337 | return $typeLinks . $description; |
||
338 | } |
||
339 | |||
340 | |||
341 | /** |
||
342 | * @param mixed $value |
||
343 | * @param ElementReflectionInterface $elementReflection |
||
344 | * @return string |
||
345 | */ |
||
346 | private function getDescriptionFromValue($value, ElementReflectionInterface $elementReflection) |
||
347 | { |
||
348 | $description = trim(strpbrk($value, "\n\r\t $")) ?: null; |
||
349 | if ($description) { |
||
350 | $description = '<br>' . $this->doc($description, $elementReflection); |
||
351 | } |
||
352 | return $description; |
||
353 | } |
||
354 | |||
355 | |||
356 | /** |
||
357 | * @param string $value |
||
358 | * @return string |
||
359 | */ |
||
360 | private function processLicenseAnnotations($value) |
||
365 | |||
366 | |||
367 | /** |
||
368 | * @param string $value |
||
369 | * @return string |
||
370 | */ |
||
371 | private function processLinkAnnotations($value) |
||
379 | |||
380 | |||
381 | /** |
||
382 | * @param string $value |
||
383 | * @param ElementReflectionInterface $reflectionElement |
||
384 | * @return string |
||
385 | */ |
||
386 | private function processSeeAnnotations($value, ElementReflectionInterface $reflectionElement) |
||
398 | |||
399 | |||
400 | /** |
||
401 | * @param string $value |
||
402 | * @param ElementReflectionInterface $reflectionElement |
||
403 | * @return string |
||
404 | */ |
||
405 | private function processUsesAndUsedbyAnnotations($value, ElementReflectionInterface $reflectionElement) |
||
415 | } |
||
416 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: