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 |
||
| 18 | class ClassDefinition extends ParentDefinition |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | protected $type; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Class methods |
||
| 27 | * |
||
| 28 | * @var ClassMethod[] |
||
| 29 | */ |
||
| 30 | protected $methods = array(); |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Class properties |
||
| 34 | * |
||
| 35 | * @var Node\Stmt\Property[] |
||
| 36 | */ |
||
| 37 | protected $properties = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Class constants |
||
| 41 | * |
||
| 42 | * @var Node\Stmt\Const_[] |
||
| 43 | */ |
||
| 44 | protected $constants = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @todo Use Finder |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $filepath; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var Node\Stmt\Class_|null |
||
| 55 | */ |
||
| 56 | protected $statement; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var string|null |
||
| 60 | */ |
||
| 61 | protected $extendsClass; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var ClassDefinition|null |
||
| 65 | */ |
||
| 66 | protected $extendsClassDefinition; |
||
|
|
|||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $interfaces = array(); |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param string $name |
||
| 75 | * @param Node\Stmt\Class_ $statement |
||
| 76 | * @param integer $type |
||
| 77 | */ |
||
| 78 | 820 | public function __construct($name, Node\Stmt\Class_ $statement = null, $type = 0) |
|
| 79 | { |
||
| 80 | 820 | $this->name = $name; |
|
| 81 | 820 | $this->statement = $statement; |
|
| 82 | 820 | $this->type = $type; |
|
| 83 | 820 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param ClassMethod $methodDefintion |
||
| 87 | */ |
||
| 88 | 24 | public function addMethod(ClassMethod $methodDefintion) |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @param Node\Stmt\Property $property |
||
| 95 | */ |
||
| 96 | 3 | public function addProperty(Node\Stmt\Property $property) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param Node\Stmt\ClassConst $const |
||
| 105 | */ |
||
| 106 | 3 | public function addConst(Node\Stmt\ClassConst $const) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param Context $context |
||
| 113 | * @return $this |
||
| 114 | */ |
||
| 115 | 24 | public function compile(Context $context) |
|
| 116 | { |
||
| 117 | 24 | if ($this->compiled) { |
|
| 118 | 1 | return true; |
|
| 119 | } |
||
| 120 | |||
| 121 | 24 | $context->getEventManager()->fire( |
|
| 122 | 24 | Event\StatementBeforeCompile::EVENT_NAME, |
|
| 123 | 24 | new Event\StatementBeforeCompile( |
|
| 124 | 24 | $this->statement, |
|
| 125 | $context |
||
| 126 | 24 | ) |
|
| 127 | 24 | ); |
|
| 128 | |||
| 129 | 24 | $this->compiled = true; |
|
| 130 | 24 | $context->setFilepath($this->filepath); |
|
| 131 | 24 | $context->setScope($this); |
|
| 132 | |||
| 133 | // Compile event for properties |
||
| 134 | 24 | foreach ($this->properties as $property) { |
|
| 135 | 2 | if (!$property->default) { |
|
| 136 | 1 | continue; |
|
| 137 | } |
||
| 138 | |||
| 139 | // fire expression event for property default |
||
| 140 | 1 | $context->getEventManager()->fire( |
|
| 141 | 1 | Event\ExpressionBeforeCompile::EVENT_NAME, |
|
| 142 | 1 | new Event\ExpressionBeforeCompile( |
|
| 143 | 1 | $property->default, |
|
| 144 | $context |
||
| 145 | 1 | ) |
|
| 146 | 1 | ); |
|
| 147 | 24 | } |
|
| 148 | |||
| 149 | // Compile event for constants |
||
| 150 | 24 | foreach ($this->constants as $const) { |
|
| 151 | 1 | $context->getEventManager()->fire( |
|
| 152 | 1 | Event\StatementBeforeCompile::EVENT_NAME, |
|
| 153 | 1 | new Event\StatementBeforeCompile( |
|
| 154 | 1 | $const, |
|
| 155 | $context |
||
| 156 | 1 | ) |
|
| 157 | 1 | ); |
|
| 158 | 24 | } |
|
| 159 | |||
| 160 | // Compile each method |
||
| 161 | 24 | foreach ($this->methods as $method) { |
|
| 162 | 23 | $context->clearSymbols(); |
|
| 163 | |||
| 164 | 23 | if (!$method->isStatic()) { |
|
| 165 | 23 | $thisPtr = new Variable('this', $this, CompiledExpression::OBJECT); |
|
| 166 | 23 | $thisPtr->incGets(); |
|
| 167 | 23 | $context->addVariable($thisPtr); |
|
| 168 | 23 | } |
|
| 169 | |||
| 170 | 23 | $method->compile($context); |
|
| 171 | |||
| 172 | 23 | $symbols = $context->getSymbols(); |
|
| 173 | 23 | if (count($symbols) > 0) { |
|
| 174 | 23 | foreach ($symbols as $name => $variable) { |
|
| 175 | 23 | if ($variable->isUnused()) { |
|
| 176 | 3 | $context->warning( |
|
| 177 | 3 | 'unused-' . $variable->getSymbolType(), |
|
| 178 | 3 | sprintf( |
|
| 179 | 3 | 'Unused ' . $variable->getSymbolType() . ' $%s in method %s()', |
|
| 180 | 3 | $variable->getName(), |
|
| 181 | 3 | $method->getName() |
|
| 182 | 3 | ) |
|
| 183 | 3 | ); |
|
| 184 | 3 | } |
|
| 185 | 23 | } |
|
| 186 | 23 | } |
|
| 187 | 24 | } |
|
| 188 | |||
| 189 | 24 | return $this; |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $name |
||
| 194 | * @param boolean|false $inherit |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | 1 | public function hasMethod($name, $inherit = false) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $name |
||
| 213 | * @param bool $inherit |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | 2 | public function hasConst($name, $inherit = false) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @param $name |
||
| 227 | * @param boolean|false $inherit |
||
| 228 | * @return ClassMethod|null |
||
| 229 | */ |
||
| 230 | 1 | public function getMethod($name, $inherit = false) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param $name |
||
| 245 | * @param bool $inherit |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | 2 | public function hasProperty($name, $inherit = false) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $name |
||
| 259 | * @param bool $inherit |
||
| 260 | * @return Node\Stmt\Property |
||
| 261 | */ |
||
| 262 | public function getProperty($name, $inherit = false) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | 24 | public function getFilepath() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @param string $filepath |
||
| 287 | */ |
||
| 288 | 24 | public function setFilepath($filepath) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | public function isAbstract() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param null|string $extendsClass |
||
| 303 | */ |
||
| 304 | public function setExtendsClass($extendsClass) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @return null|ClassDefinition |
||
| 311 | */ |
||
| 312 | public function getExtendsClassDefinition() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param ClassDefinition $extendsClassDefinition |
||
| 319 | */ |
||
| 320 | 1 | public function setExtendsClassDefinition(ClassDefinition $extendsClassDefinition) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * @param string $interface |
||
| 327 | */ |
||
| 328 | public function addInterface($interface) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @return null|string |
||
| 335 | */ |
||
| 336 | 24 | public function getExtendsClass() |
|
| 340 | } |
||
| 341 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.