Complex classes like AbstractElement 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 AbstractElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 5 | abstract class AbstractElement implements GenerableInterface |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * @var string |
||
| 9 | */ |
||
| 10 | protected $name; |
||
| 11 | /** |
||
| 12 | * @var AbstractElement[]|mixed[] |
||
| 13 | */ |
||
| 14 | protected $children; |
||
| 15 | /** |
||
| 16 | * @var int |
||
| 17 | */ |
||
| 18 | protected $indentation; |
||
| 19 | /** |
||
| 20 | * @param string $name |
||
| 21 | */ |
||
| 22 | 572 | public function __construct($name) |
|
| 23 | { |
||
| 24 | 572 | $this->setName($name); |
|
| 25 | 572 | $this->children = array(); |
|
| 26 | 572 | $this->indentation = 0; |
|
| 27 | 572 | } |
|
| 28 | /** |
||
| 29 | * @param string $name |
||
| 30 | * @return AbstractElement |
||
| 31 | */ |
||
| 32 | 572 | public function setName($name) |
|
| 33 | { |
||
| 34 | 572 | if (!self::nameIsValid($name)) { |
|
| 35 | throw new \InvalidArgumentException(sprintf('Name "%s" is invalid, please provide a valid name', $name)); |
||
| 36 | } |
||
| 37 | 572 | $this->name = $name; |
|
| 38 | 572 | return $this; |
|
| 39 | } |
||
| 40 | /** |
||
| 41 | * @return string |
||
| 42 | */ |
||
| 43 | 404 | public function getName() |
|
| 47 | /** |
||
| 48 | * @param string $name |
||
| 49 | * @param bool $allowBackslash |
||
| 50 | * @return bool |
||
| 51 | */ |
||
| 52 | 580 | public static function nameIsValid($name, $allowBackslash = false) |
|
| 53 | { |
||
| 54 | 580 | $pattern = '/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/'; |
|
| 55 | 580 | if ($allowBackslash === true) { |
|
| 56 | 88 | $pattern = '/[a-zA-Z_\x7f-\xff\\\][a-zA-Z0-9_\x7f-\xff\\\]*/'; |
|
| 57 | 66 | } |
|
| 58 | 580 | return preg_match($pattern, $name); |
|
| 59 | } |
||
| 60 | /** |
||
| 61 | * @param mixed $string |
||
| 62 | * @param bool $checkName |
||
| 63 | * @param bool $allowBackslash |
||
| 64 | * @return bool |
||
| 65 | */ |
||
| 66 | 224 | public static function stringIsValid($string, $checkName = true, $allowBackslash = false) |
|
| 70 | /** |
||
| 71 | * @param mixed $object |
||
| 72 | * @return bool |
||
| 73 | */ |
||
| 74 | 172 | public static function objectIsValid($object, $checkClass = null) |
|
| 78 | /** |
||
| 79 | * @see \WsdlToPhp\PhpGenerator\Element\GenerableInterface::toString() |
||
| 80 | * @param int $indentation |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | 156 | public function toString($indentation = null) |
|
| 84 | { |
||
| 85 | $lines = array( |
||
| 86 | 156 | $this->getToStringDeclaration($indentation), |
|
| 87 | 156 | $this->getToStringBeforeChildren($indentation), |
|
| 88 | 117 | ); |
|
| 89 | 156 | foreach ($this->getChildren() as $child) { |
|
| 90 | 100 | $lines[] = $this->getChildContent($child, $indentation + ($this->useBracketsForChildren() ? 1 : 0)); |
|
| 91 | 117 | } |
|
| 92 | 156 | $lines[] = $this->getToStringAfterChildren($indentation); |
|
| 93 | 156 | return implode(self::BREAK_LINE_CHAR, self::cleanArrayToString($lines)); |
|
| 94 | } |
||
| 95 | /** |
||
| 96 | * @param int $indentation |
||
| 97 | * @return string|null |
||
| 98 | */ |
||
| 99 | 156 | private function getToStringDeclaration($indentation = null) |
|
| 100 | { |
||
| 101 | 156 | $declaration = $this->getPhpDeclaration(); |
|
| 102 | 156 | if (!empty($declaration)) { |
|
| 103 | 156 | return $this->getIndentedString($declaration, $indentation); |
|
| 104 | } |
||
| 105 | 52 | return null; |
|
| 106 | } |
||
| 107 | /** |
||
| 108 | * @param string $indentation |
||
| 109 | * @return string|null |
||
| 110 | */ |
||
| 111 | 156 | private function getToStringBeforeChildren($indentation = null) |
|
| 112 | { |
||
| 113 | 156 | $before = $this->getContextualLineBeforeChildren($indentation); |
|
| 114 | 156 | if (!empty($before)) { |
|
| 115 | 104 | return $before; |
|
| 116 | } |
||
| 117 | 128 | return null; |
|
| 118 | } |
||
| 119 | /** |
||
| 120 | * @param string $indentation |
||
| 121 | * @return string|null |
||
| 122 | */ |
||
| 123 | 156 | private function getToStringAfterChildren($indentation = null) |
|
| 124 | { |
||
| 125 | 156 | $after = $this->getContextualLineAfterChildren($indentation); |
|
| 126 | 156 | if (!empty($after)) { |
|
| 127 | 104 | return $after; |
|
| 128 | } |
||
| 129 | 128 | return null; |
|
| 130 | } |
||
| 131 | /** |
||
| 132 | * @param array $array |
||
| 133 | * @return array |
||
| 134 | */ |
||
| 135 | 156 | private static function cleanArrayToString($array) |
|
| 136 | { |
||
| 137 | 156 | $newArray = array(); |
|
| 138 | 156 | foreach ($array as $line) { |
|
| 139 | 156 | if ($line !== null) { |
|
| 140 | 156 | $newArray[] = $line; |
|
| 141 | 117 | } |
|
| 142 | 117 | } |
|
| 143 | 156 | return $newArray; |
|
| 144 | } |
||
| 145 | /** |
||
| 146 | * @throws \InvalidArgumentException |
||
| 147 | * @param string|AbstractElement $child |
||
| 148 | * @param int $indentation |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | 100 | protected function getChildContent($child, $indentation = null) |
|
| 161 | /** |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | 380 | public function getPhpName() |
|
| 168 | /** |
||
| 169 | * @throws \InvalidArgumentException |
||
| 170 | * @param mixed $child |
||
| 171 | * @return AbstractElement |
||
| 172 | */ |
||
| 173 | 164 | public function addChild($child) |
|
| 186 | /** |
||
| 187 | * @param mixed $child |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | 188 | protected function childIsValid($child) |
|
| 201 | /** |
||
| 202 | * @return AbstractElement[]|mixed[] |
||
| 203 | */ |
||
| 204 | 220 | public function getChildren() |
|
| 208 | /** |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | abstract public function getPhpDeclaration(); |
||
| 212 | /** |
||
| 213 | * defines authorized children element types |
||
| 214 | * @return string[] |
||
| 215 | */ |
||
| 216 | abstract public function getChildrenTypes(); |
||
| 217 | /** |
||
| 218 | * @param int $indentation |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | 156 | private function getContextualLineBeforeChildren($indentation = null) |
|
| 222 | { |
||
| 223 | 156 | if ($this->useBracketsForChildren()) { |
|
| 224 | 72 | $line = $this->getBracketBeforeChildren($indentation); |
|
| 225 | 54 | } else { |
|
| 226 | 128 | $line = $this->getLineBeforeChildren($indentation); |
|
| 227 | } |
||
| 228 | 156 | return $line; |
|
| 229 | } |
||
| 230 | /** |
||
| 231 | * @param int $indentation |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | 156 | private function getContextualLineAfterChildren($indentation = null) |
|
| 235 | { |
||
| 236 | 156 | if ($this->useBracketsForChildren()) { |
|
| 237 | 72 | $line = $this->getBracketAfterChildren($indentation); |
|
| 238 | 54 | } else { |
|
| 239 | 128 | $line = $this->getLineAfterChildren($indentation); |
|
| 240 | } |
||
| 241 | 156 | return $line; |
|
| 242 | } |
||
| 243 | /** |
||
| 244 | * Allows to generate content before children content is generated |
||
| 245 | * @param int $indentation |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | 128 | public function getLineBeforeChildren($indentation = null) |
|
| 252 | /** |
||
| 253 | * Allows to generate content after children content is generated |
||
| 254 | * @param int $indentation |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | 128 | public function getLineAfterChildren($indentation = null) |
|
| 261 | /** |
||
| 262 | * Allows to indicate that children are contained by brackets, |
||
| 263 | * in the case the method returns true, getBracketBeforeChildren |
||
| 264 | * is called instead of getLineBeforeChildren and getBracketAfterChildren |
||
| 265 | * is called instead of getLineAfterChildren, but be aware that these methods |
||
| 266 | * call the two others |
||
| 267 | * @return boolean |
||
| 268 | */ |
||
| 269 | 124 | public function useBracketsForChildren() |
|
| 273 | /** |
||
| 274 | * Allows to generate content before children content is generated |
||
| 275 | * @param int $indentation |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | 72 | public function getBracketBeforeChildren($indentation = null) |
|
| 279 | { |
||
| 280 | 72 | $line = $this->getIndentedString(self::OPEN_BRACKET, $indentation); |
|
| 281 | 72 | $this->setIndentation(($indentation === null ? $this->getIndentation() : $indentation) + 1); |
|
| 282 | 72 | return $line; |
|
| 283 | } |
||
| 284 | /** |
||
| 285 | * Allows to generate content after children content is generated |
||
| 286 | * @param int $indentation |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | 72 | public function getBracketAfterChildren($indentation = null) |
|
| 290 | { |
||
| 291 | 72 | $this->setIndentation(($indentation === null ? $this->getIndentation() : $indentation) - 1); |
|
| 292 | 72 | return $this->getIndentedString(self::CLOSE_BRACKET, $indentation); |
|
| 293 | } |
||
| 294 | /** |
||
| 295 | * @param int $indentation |
||
| 296 | * @return AbstractElement |
||
| 297 | */ |
||
| 298 | 72 | public function setIndentation($indentation) |
|
| 299 | { |
||
| 300 | 72 | $this->indentation = $indentation; |
|
| 301 | 72 | return $this; |
|
| 302 | } |
||
| 303 | /** |
||
| 304 | * @return int |
||
| 305 | */ |
||
| 306 | 156 | public function getIndentation() |
|
| 310 | /** |
||
| 311 | * @see \WsdlToPhp\PhpGenerator\Element\GenerableInterface::getIndentationString() |
||
| 312 | * @param int $indentation |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | 156 | public function getIndentationString($indentation = null) |
|
| 319 | /** |
||
| 320 | * @param string $string |
||
| 321 | * @param int $indentation |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | 156 | public function getIndentedString($string, $indentation = null) |
|
| 325 | { |
||
| 326 | 156 | $strings = explode(self::BREAK_LINE_CHAR, $string); |
|
| 327 | 156 | foreach ($strings as $i=>$s) { |
|
| 328 | 156 | $strings[$i] = sprintf('%s%s', $this->getIndentationString($indentation), $s); |
|
| 329 | 117 | } |
|
| 330 | 156 | return implode(self::BREAK_LINE_CHAR, $strings); |
|
| 331 | } |
||
| 332 | } |
||
| 333 |