Complex classes like AttributeDecorator 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 AttributeDecorator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class AttributeDecorator |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * Server. |
||
| 25 | * |
||
| 26 | * @var Server |
||
| 27 | */ |
||
| 28 | protected $server; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Filesystem. |
||
| 32 | * |
||
| 33 | * @var Filesystem |
||
| 34 | */ |
||
| 35 | protected $fs; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Acl. |
||
| 39 | * |
||
| 40 | * @var Acl |
||
| 41 | */ |
||
| 42 | protected $acl; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Custom attributes. |
||
| 46 | * |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $custom = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Init. |
||
| 53 | * |
||
| 54 | * @param Server $server |
||
| 55 | * @param Acl $acl |
||
| 56 | */ |
||
| 57 | public function __construct(Server $server, Acl $acl) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Decorate attributes. |
||
| 66 | * |
||
| 67 | * @param NodeInterface $node |
||
| 68 | * @param array $attributes |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | public function decorate(NodeInterface $node, ?array $attributes): array |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Add decorator. |
||
| 97 | * |
||
| 98 | * @param string $attribute |
||
| 99 | * @param Closure $decorator |
||
| 100 | * |
||
| 101 | * @return AttributeDecorator |
||
| 102 | */ |
||
| 103 | public function addDecorator(string $attribute, Closure $decorator): self |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Prepare requested attributes. |
||
| 112 | * |
||
| 113 | * @param NodeInterface $node |
||
| 114 | * @param array $attributes |
||
| 115 | * |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | protected function prepare(NodeInterface $node, ?array $attributes): array |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get Attributes. |
||
| 161 | * |
||
| 162 | * @param NodeInterface |
||
| 163 | * @param array $attributes |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | protected function getAttributes(NodeInterface $node, array $attributes): array |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get Attributes. |
||
| 215 | * |
||
| 216 | * @param NodeInterface |
||
| 217 | * @param array $attributes |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | protected function getTimeAttributes(NodeInterface $node, array $attributes): array |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Get Attributes. |
||
| 249 | * |
||
| 250 | * @param NodeInterface |
||
| 251 | * @param array $attributes |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | protected function getTypeAttributes(NodeInterface $node, array $attributes): array |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Execute closures. |
||
| 305 | * |
||
| 306 | * @param NodeInterface |
||
| 307 | * @param array $attributes |
||
| 308 | * @param array $requested |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | protected function translateAttributes(NodeInterface $node, array $attributes, array $requested): array |
||
| 328 | } |
||
| 329 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.