Complex classes like YamlReferenceDumper 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 YamlReferenceDumper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class YamlReferenceDumper |
||
| 28 | { |
||
| 29 | private $reference; |
||
| 30 | |||
| 31 | public function dump(ConfigurationInterface $configuration) |
||
| 35 | |||
| 36 | public function dumpAtPath(ConfigurationInterface $configuration, $path) |
||
| 61 | |||
| 62 | public function dumpNode(NodeInterface $node) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param NodeInterface $node |
||
| 74 | * @param int $depth |
||
| 75 | * @param bool $prototypedArray |
||
| 76 | */ |
||
| 77 | private function writeNode(NodeInterface $node, $depth = 0, $prototypedArray = false) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Outputs a single config reference line. |
||
| 176 | * |
||
| 177 | * @param string $text |
||
| 178 | * @param int $indent |
||
| 179 | */ |
||
| 180 | private function writeLine($text, $indent = 0) |
||
| 187 | |||
| 188 | private function writeArray(array $array, $depth) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param PrototypedArrayNode $node |
||
| 213 | * |
||
| 214 | * @return array |
||
| 215 | */ |
||
| 216 | private function getPrototypeChildren(PrototypedArrayNode $node) |
||
| 217 | { |
||
| 218 | $prototype = $node->getPrototype(); |
||
| 219 | $key = $node->getKeyAttribute(); |
||
| 220 | |||
| 221 | // Do not expand prototype if it isn't an array node nor uses attribute as key |
||
| 222 | if (!$key && !$prototype instanceof ArrayNode) { |
||
| 223 | return $node->getChildren(); |
||
| 224 | } |
||
| 225 | |||
| 226 | if ($prototype instanceof ArrayNode) { |
||
| 227 | $keyNode = new ArrayNode($key, $node); |
||
| 228 | $children = $prototype->getChildren(); |
||
| 229 | |||
| 230 | if ($prototype instanceof PrototypedArrayNode && $prototype->getKeyAttribute()) { |
||
| 231 | $children = $this->getPrototypeChildren($prototype); |
||
| 232 | } |
||
| 233 | |||
| 234 | // add children |
||
| 235 | foreach ($children as $childNode) { |
||
| 236 | $keyNode->addChild($childNode); |
||
| 237 | } |
||
| 238 | } else { |
||
| 239 | $keyNode = new ScalarNode($key, $node); |
||
| 240 | } |
||
| 241 | |||
| 242 | $info = 'Prototype'; |
||
| 243 | if (null !== $prototype->getInfo()) { |
||
| 244 | $info .= ': '.$prototype->getInfo(); |
||
| 245 | } |
||
| 246 | $keyNode->setInfo($info); |
||
| 247 | |||
| 248 | return array($key => $keyNode); |
||
| 249 | } |
||
| 250 | } |
||
| 251 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.