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 |
||
| 17 | class TagNode extends ANode implements ArrayAccess |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Name of this node. |
||
| 21 | * |
||
| 22 | * @access private |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $name; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The html tag name of this node. |
||
| 30 | * |
||
| 31 | * @access private |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | private $tagName; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Whether the html tag is empty. |
||
| 39 | * |
||
| 40 | * @access private |
||
| 41 | * |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | private $isEmpty; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The attributes of this node. |
||
| 48 | * |
||
| 49 | * @access private |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $attributes=[]; |
||
|
|
|||
| 54 | |||
| 55 | /** |
||
| 56 | * Real constructor. |
||
| 57 | * |
||
| 58 | * @access protected |
||
| 59 | * |
||
| 60 | * @return \Htsl\Parser\Node\Contracts\ANode |
||
| 61 | */ |
||
| 62 | protected function construct():parent |
||
| 63 | { |
||
| 64 | |||
| 65 | $name= $this->line->pregGet('/(?<=^-)[\w-:]+/'); |
||
| 66 | $this->name=$name; |
||
| 67 | |||
| 68 | $this->loadConfig($name,$this->document); |
||
| 69 | |||
| 70 | $this->tagName=$this->config['name']??$name; |
||
| 71 | $this->isEmpty= $this->line->getChar(-1)==='/' || $this->document->getConfig('empty_tags',$this->tagName); |
||
| 72 | isset($this->config['default_attributes']) and array_walk($this->config['default_attributes'],function( $value,$key ){ $this->setAttribute($key,$value); }); |
||
| 73 | |||
| 74 | return $this; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Opening this tag node, and returning node opener. |
||
| 79 | * |
||
| 80 | * @access public |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | public function open():string |
||
| 85 | { |
||
| 86 | if( isset($this->config['opener']) ) |
||
| 87 | { return $this->config['opener']; } |
||
| 88 | |||
| 89 | $this->parsePrivateAttributes(); |
||
| 90 | $this->parseCommonAttributes(); |
||
| 91 | |||
| 92 | if( isset($this->config['in_scope']) && isset($this->config['scope_function']) && is_callable($this->config['scope_function']) ) |
||
| 93 | { $this->config['scope_function']->call($this,$this->document->scope); } |
||
| 94 | |||
| 95 | $finisher= $this->isEmpty ? ' />' : '>'; |
||
| 96 | |||
| 97 | return "<{$this->tagName}{$this->attributesString}{$finisher}"; |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Close this tag node, and returning node closer. |
||
| 102 | * |
||
| 103 | * @access public |
||
| 104 | * |
||
| 105 | * @param \Htsl\ReadingBuffer\Line $closerLine The line when node closed. |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function close( Line$closerLine ):string |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Getting whether this is embedding node and embeding type. |
||
| 116 | * |
||
| 117 | * @access public |
||
| 118 | * |
||
| 119 | * @return string |
||
| 120 | */ |
||
| 121 | public function getEmbed():string |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Getting whether this node contains a scope and scope name. |
||
| 128 | * |
||
| 129 | * @access public |
||
| 130 | * |
||
| 131 | * @return string | null |
||
| 132 | */ |
||
| 133 | public function getScope() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Parsing node parameters if needed. |
||
| 140 | * |
||
| 141 | * @access protected |
||
| 142 | * |
||
| 143 | * @return \Htsl\Parser\Node\TagNode |
||
| 144 | */ |
||
| 145 | protected function parsePrivateAttributes():self |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Private attributes of tag. |
||
| 156 | * |
||
| 157 | * @access public |
||
| 158 | * |
||
| 159 | * @const array |
||
| 160 | * |
||
| 161 | * @todo Make this const private when php 7.1 |
||
| 162 | */ |
||
| 163 | const PRIVATE_ATTRIBUTES= [ |
||
| 164 | 'params', |
||
| 165 | 'name_value', |
||
| 166 | 'link', |
||
| 167 | 'target', |
||
| 168 | 'alt', |
||
| 169 | ]; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Parsing node parameters if needed. |
||
| 173 | * |
||
| 174 | * @access protected |
||
| 175 | * |
||
| 176 | * @return \Htsl\Parser\Node\TagNode |
||
| 177 | */ |
||
| 178 | protected function parseParams():self |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Parsing <name|value> attributes. |
||
| 191 | * |
||
| 192 | * @access protected |
||
| 193 | * |
||
| 194 | * @return \Htsl\Parser\Node\TagNode |
||
| 195 | */ |
||
| 196 | protected function parseNameValue():self |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Getting tag section with given leader. |
||
| 207 | * |
||
| 208 | * @access protected |
||
| 209 | * |
||
| 210 | * @param string $leader |
||
| 211 | * @param boool $allowSpace |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | protected function sectionLedBy( string$leader, bool$allowSpace=false ):string |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Parsing @links. |
||
| 235 | * |
||
| 236 | * @access protected |
||
| 237 | * |
||
| 238 | * @return \Htsl\Parser\Node\TagNode |
||
| 239 | */ |
||
| 240 | protected function parseLink():self |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Parsing >target. |
||
| 257 | * |
||
| 258 | * @access protected |
||
| 259 | * |
||
| 260 | * @return \Htsl\Parser\Node\TagNode |
||
| 261 | */ |
||
| 262 | protected function parseTarget():self |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Parsing _placeholders. |
||
| 269 | * |
||
| 270 | * @access protected |
||
| 271 | * |
||
| 272 | * @return \Htsl\Parser\Node\TagNode |
||
| 273 | */ |
||
| 274 | protected function parseAlt():self |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Setting attribute whitch has same name in HTSL but different name in HTML. |
||
| 281 | * |
||
| 282 | * @access private |
||
| 283 | * |
||
| 284 | * @param string $name Attribute name of HTSL |
||
| 285 | * @param string $value Attribute value |
||
| 286 | * @param callable|null $processer |
||
| 287 | * |
||
| 288 | * @return \Htsl\Parser\Node\TagNode |
||
| 289 | */ |
||
| 290 | private function setMultinameAttribute( string$name, string$value, callable$processer=null ):self |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Parsing #ids .classes ^titles [styles] %event{>listeners<} and {other attributes} |
||
| 301 | * |
||
| 302 | * @access protected |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | protected function parseCommonAttributes():string |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Checking and parse PHP expressions. |
||
| 341 | * |
||
| 342 | * @access protected |
||
| 343 | * |
||
| 344 | * @param string $value |
||
| 345 | * |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | protected function checkExpression( string$value ):string |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Getting attribute string with HTML syntax. |
||
| 355 | * |
||
| 356 | * @access protected |
||
| 357 | * |
||
| 358 | * @return string |
||
| 359 | */ |
||
| 360 | protected function getAttributesString():string |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Setting attribute. |
||
| 374 | * |
||
| 375 | * @access protected |
||
| 376 | * |
||
| 377 | * @param string $key Attribute name. |
||
| 378 | * @param string $value Attribute value |
||
| 379 | * @param string|null $condition Optional condition, If given, attribute will seted only when condition is true. |
||
| 380 | */ |
||
| 381 | protected function setAttribute( string$key, string$value, string$condition=null ):self |
||
| 393 | |||
| 394 | |||
| 395 | /* *\ |
||
| 396 | ArrayAccess |
||
| 397 | \* */ |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Whether the attribute isset. |
||
| 401 | * |
||
| 402 | * @access public |
||
| 403 | * |
||
| 404 | * @param mixed $offset |
||
| 405 | * |
||
| 406 | * @return bool |
||
| 407 | */ |
||
| 408 | public function offsetExists( $offset ):bool |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Getting attribute with array access. |
||
| 415 | * |
||
| 416 | * @access public |
||
| 417 | * |
||
| 418 | * @param mixed $offset |
||
| 419 | * |
||
| 420 | * @return mixed |
||
| 421 | */ |
||
| 422 | public function offsetGet( $offset ) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Setting Attribute with array access. |
||
| 429 | * |
||
| 430 | * @access public |
||
| 431 | * |
||
| 432 | * @param mixed $offset |
||
| 433 | * @param mixed $value |
||
| 434 | */ |
||
| 435 | public function offsetSet( $offset, $value ) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Unset an attribute with array access. |
||
| 442 | * |
||
| 443 | * @access public |
||
| 444 | * |
||
| 445 | * @param mixed $offset |
||
| 446 | */ |
||
| 447 | public function offsetUnset( $offset ) |
||
| 452 | } |
||
| 453 |
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.