Complex classes like ClassDefinition 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 ClassDefinition, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class ClassDefinition extends ParentDefinition |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | protected $type; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Class methods |
||
| 26 | * |
||
| 27 | * @var ClassMethod[] |
||
| 28 | */ |
||
| 29 | protected $methods = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Class properties |
||
| 33 | * |
||
| 34 | * @var Node\Stmt\Property[] |
||
| 35 | */ |
||
| 36 | protected $properties = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Class constants |
||
| 40 | * |
||
| 41 | * @var Node\Stmt\Const_[] |
||
| 42 | */ |
||
| 43 | protected $constants = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @todo Use Finder |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $filepath; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string|null |
||
| 54 | */ |
||
| 55 | protected $extendsClass; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var ClassDefinition|null |
||
| 59 | */ |
||
| 60 | protected $extendsClassDefinition; |
||
|
|
|||
| 61 | |||
| 62 | /** |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | protected $interfaces = array(); |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $name |
||
| 69 | * @param integer $type |
||
| 70 | */ |
||
| 71 | 393 | public function __construct($name, $type) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @param ClassMethod $methodDefintion |
||
| 79 | */ |
||
| 80 | 15 | public function addMethod(ClassMethod $methodDefintion) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param Node\Stmt\Property $property |
||
| 87 | */ |
||
| 88 | 2 | public function addProperty(Node\Stmt\Property $property) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param Node\Stmt\ClassConst $const |
||
| 95 | */ |
||
| 96 | 2 | public function addConst(Node\Stmt\ClassConst $const) |
|
| 97 | { |
||
| 98 | 2 | $this->constants[$const->consts[0]->name] = $const; |
|
| 99 | 2 | } |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @param Context $context |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | 14 | public function compile(Context $context) |
|
| 106 | { |
||
| 107 | 14 | if ($this->compiled) { |
|
| 108 | return true; |
||
| 109 | } |
||
| 110 | |||
| 111 | 14 | $this->compiled = true; |
|
| 112 | 14 | $context->setFilepath($this->filepath); |
|
| 113 | 14 | $context->setScope($this); |
|
| 114 | |||
| 115 | 14 | foreach ($this->methods as $method) { |
|
| 116 | 14 | $context->clearSymbols(); |
|
| 117 | |||
| 118 | 14 | if (!$method->isStatic()) { |
|
| 119 | 14 | $thisPtr = new Variable('this', $this, CompiledExpression::OBJECT); |
|
| 120 | 14 | $thisPtr->incGets(); |
|
| 121 | 14 | $context->addVariable($thisPtr); |
|
| 122 | 14 | } |
|
| 123 | |||
| 124 | 14 | $method->compile($context); |
|
| 125 | |||
| 126 | 14 | $symbols = $context->getSymbols(); |
|
| 127 | 14 | if (count($symbols) > 0) { |
|
| 128 | 14 | foreach ($symbols as $name => $variable) { |
|
| 129 | 14 | if ($variable->isUnused()) { |
|
| 130 | 1 | $context->warning( |
|
| 131 | 1 | 'unused-' . $variable->getSymbolType(), |
|
| 132 | 1 | sprintf( |
|
| 133 | 1 | 'Unused ' . $variable->getSymbolType() . ' $%s in method %s()', |
|
| 134 | 1 | $variable->getName(), |
|
| 135 | 1 | $method->getName() |
|
| 136 | 1 | ) |
|
| 137 | 1 | ); |
|
| 138 | 1 | } |
|
| 139 | 14 | } |
|
| 140 | 14 | } |
|
| 141 | 14 | } |
|
| 142 | |||
| 143 | 14 | return $this; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $name |
||
| 148 | * @param boolean|false $inherit |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | 1 | public function hasMethod($name, $inherit = false) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @param string $name |
||
| 167 | * @param bool $inherit |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | 2 | public function hasConst($name, $inherit = false) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @param $name |
||
| 181 | * @param boolean|false $inherit |
||
| 182 | * @return ClassMethod|null |
||
| 183 | */ |
||
| 184 | 1 | public function getMethod($name, $inherit = false) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @param $name |
||
| 199 | * @param bool $inherit |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | 1 | public function hasProperty($name, $inherit = false) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $name |
||
| 213 | * @param bool $inherit |
||
| 214 | * @return Node\Stmt\Property |
||
| 215 | */ |
||
| 216 | public function getProperty($name, $inherit = false) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | 14 | public function getFilepath() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $filepath |
||
| 241 | */ |
||
| 242 | 14 | public function setFilepath($filepath) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @return bool |
||
| 249 | */ |
||
| 250 | public function isAbstract() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param null|string $extendsClass |
||
| 257 | */ |
||
| 258 | public function setExtendsClass($extendsClass) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @return null|ClassDefinition |
||
| 265 | */ |
||
| 266 | public function getExtendsClassDefinition() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param ClassDefinition $extendsClassDefinition |
||
| 273 | */ |
||
| 274 | 1 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param array $interface |
||
| 281 | */ |
||
| 282 | public function addInterface($interface) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return null|string |
||
| 289 | */ |
||
| 290 | 14 | public function getExtendsClass() |
|
| 294 | } |
||
| 295 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.