Complex classes like TagNode 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 TagNode, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class TagNode extends ANode implements ArrayAccess |
||
17 | { |
||
18 | /** |
||
19 | * Name of this node. |
||
20 | * |
||
21 | * @access private |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | private $name; |
||
26 | |||
27 | /** |
||
28 | * The html tag name of this node. |
||
29 | * |
||
30 | * @access private |
||
31 | * |
||
32 | * @var string |
||
33 | */ |
||
34 | private $tagName; |
||
35 | |||
36 | /** |
||
37 | * Whether the html tag is empty. |
||
38 | * |
||
39 | * @access private |
||
40 | * |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $isEmpty; |
||
44 | |||
45 | /** |
||
46 | * The attributes of this node. |
||
47 | * |
||
48 | * @access private |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $attributes=[]; |
||
53 | |||
54 | /** |
||
55 | * Real constructor. |
||
56 | * |
||
57 | * @access protected |
||
58 | * |
||
59 | * @return \Htsl\Parser\Node\Contracts\ANode |
||
60 | */ |
||
61 | protected function construct():parent |
||
75 | |||
76 | /** |
||
77 | * Opening this tag node, and returning node opener. |
||
78 | * |
||
79 | * @access public |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | public function open():string |
||
112 | |||
113 | /** |
||
114 | * Close this tag node, and returning node closer. |
||
115 | * |
||
116 | * @access public |
||
117 | * |
||
118 | * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | public function close( Line$closerLine ):string |
||
126 | |||
127 | /** |
||
128 | * Getting whether this is embedding node and embeding type. |
||
129 | * |
||
130 | * @access public |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | public function getEmbed():string |
||
138 | |||
139 | /** |
||
140 | * Getting whether this node contains a scope and scope name. |
||
141 | * |
||
142 | * @access public |
||
143 | * |
||
144 | * @return string | null |
||
145 | */ |
||
146 | public function getScope() |
||
150 | |||
151 | /** |
||
152 | * Parsing node parameters if needed. |
||
153 | * |
||
154 | * @access protected |
||
155 | * |
||
156 | * @return \Htsl\Parser\Node\TagNode |
||
157 | */ |
||
158 | protected function parseParams():self |
||
168 | |||
169 | /** |
||
170 | * Parsing <name|value> attributes. |
||
171 | * |
||
172 | * @access protected |
||
173 | * |
||
174 | * @return \Htsl\Parser\Node\TagNode |
||
175 | */ |
||
176 | protected function parseNameValue():self |
||
184 | |||
185 | /** |
||
186 | * Getting tag section with given leader. |
||
187 | * |
||
188 | * @access protected |
||
189 | * |
||
190 | * @param string $leader |
||
191 | * @param boool $allowSpace |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | protected function sectionLedBy( string$leader, bool$allowSpace=false ):string |
||
212 | |||
213 | /** |
||
214 | * Parsing @links. |
||
215 | * |
||
216 | * @access protected |
||
217 | * |
||
218 | * @return \Htsl\Parser\Node\TagNode |
||
219 | */ |
||
220 | protected function parseLink():self |
||
234 | |||
235 | /** |
||
236 | * Parsing >target. |
||
237 | * |
||
238 | * @access protected |
||
239 | * |
||
240 | * @return \Htsl\Parser\Node\TagNode |
||
241 | */ |
||
242 | protected function parseTarget():self |
||
246 | |||
247 | /** |
||
248 | * Parsing _placeholders. |
||
249 | * |
||
250 | * @access protected |
||
251 | * |
||
252 | * @return \Htsl\Parser\Node\TagNode |
||
253 | */ |
||
254 | protected function parseAlt():self |
||
258 | |||
259 | /** |
||
260 | * Setting attribute whitch has same name in HTSL but different name in HTML. |
||
261 | * |
||
262 | * @access private |
||
263 | * |
||
264 | * @param string $name Attribute name of HTSL |
||
265 | * @param string $value Attribute value |
||
266 | * @param callable|null $processer |
||
267 | * |
||
268 | * @return \Htsl\Parser\Node\TagNode |
||
269 | */ |
||
270 | private function setMultinameAttribute( string$name, string$value, callable$processer=null ):self |
||
278 | |||
279 | /** |
||
280 | * Parsing #ids .classes ^titles [styles] %event{>listeners<} and {other attributes} |
||
281 | * |
||
282 | * @access protected |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | protected function parseCommonAttributes():string |
||
318 | |||
319 | /** |
||
320 | * Checking and parse PHP expressions. |
||
321 | * |
||
322 | * @access protected |
||
323 | * |
||
324 | * @param string $value |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | protected function checkExpression( string$value ):string |
||
332 | |||
333 | /** |
||
334 | * Getting attribute string with HTML syntax. |
||
335 | * |
||
336 | * @access protected |
||
337 | * |
||
338 | * @return string |
||
339 | */ |
||
340 | protected function getAttributesString():string |
||
351 | |||
352 | /** |
||
353 | * Setting attribute. |
||
354 | * |
||
355 | * @access protected |
||
356 | * |
||
357 | * @param string $key Attribute name. |
||
358 | * @param string $value Attribute value |
||
359 | * @param string|null $condition Optional condition, If given, attribute will seted only when condition is true. |
||
360 | */ |
||
361 | protected function setAttribute( string$key, string$value, string$condition=null ):self |
||
373 | |||
374 | |||
375 | /* *\ |
||
376 | ArrayAccess |
||
377 | \* */ |
||
378 | |||
379 | /** |
||
380 | * Whether the attribute isset. |
||
381 | * |
||
382 | * @access public |
||
383 | * |
||
384 | * @param mixed $offset |
||
385 | * |
||
386 | * @return bool |
||
387 | */ |
||
388 | public function offsetExists( $offset ):bool |
||
392 | |||
393 | /** |
||
394 | * Getting attribute with array access. |
||
395 | * |
||
396 | * @access public |
||
397 | * |
||
398 | * @param mixed $offset |
||
399 | * |
||
400 | * @return mixed |
||
401 | */ |
||
402 | public function offsetGet( $offset ) |
||
406 | |||
407 | /** |
||
408 | * Setting Attribute with array access. |
||
409 | * |
||
410 | * @access public |
||
411 | * |
||
412 | * @param mixed $offset |
||
413 | * @param mixed $value |
||
414 | */ |
||
415 | public function offsetSet( $offset, $value ) |
||
419 | |||
420 | /** |
||
421 | * Unset an attribute with array access. |
||
422 | * |
||
423 | * @access public |
||
424 | * |
||
425 | * @param mixed $offset |
||
426 | */ |
||
427 | public function offsetUnset( $offset ) |
||
432 | } |
||
433 |
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: