Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 29 | class RestXmlCollectionLoader extends XmlFileLoader |
||
| 30 | { |
||
| 31 | protected $collectionParents = []; |
||
| 32 | private $processor; |
||
| 33 | private $includeFormat; |
||
| 34 | private $formats; |
||
| 35 | private $defaultFormat; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param string[] $formats |
||
| 39 | */ |
||
| 40 | 14 | View Code Duplication | public function __construct( |
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | 9 | protected function parseNode(RouteCollection $collection, \DOMElement $node, $path, $file) |
|
| 59 | { |
||
| 60 | 9 | switch ($node->tagName) { |
|
| 61 | 9 | case 'route': |
|
| 62 | 6 | $this->parseRoute($collection, $node, $path); |
|
| 63 | |||
| 64 | 6 | break; |
|
| 65 | 3 | case 'import': |
|
| 66 | 3 | $name = (string) $node->getAttribute('id'); |
|
| 67 | 3 | $resource = (string) $node->getAttribute('resource'); |
|
| 68 | 3 | $prefix = (string) $node->getAttribute('prefix'); |
|
| 69 | 3 | $namePrefix = (string) $node->getAttribute('name-prefix'); |
|
| 70 | 3 | $parent = (string) $node->getAttribute('parent'); |
|
| 71 | 3 | $type = (string) $node->getAttribute('type'); |
|
| 72 | 3 | $host = isset($config['host']) ? $config['host'] : null; |
|
| 73 | 3 | $currentDir = dirname($path); |
|
| 74 | |||
| 75 | 3 | $parents = []; |
|
| 76 | 3 | View Code Duplication | if (!empty($parent)) { |
| 77 | 1 | if (!isset($this->collectionParents[$parent])) { |
|
| 78 | 1 | throw new \InvalidArgumentException(sprintf('Cannot find parent resource with name %s', $parent)); |
|
| 79 | } |
||
| 80 | |||
| 81 | $parents = $this->collectionParents[$parent]; |
||
| 82 | } |
||
| 83 | |||
| 84 | 2 | $imported = $this->processor->importResource($this, $resource, $parents, $prefix, $namePrefix, $type, $currentDir); |
|
| 85 | |||
| 86 | if (!empty($name) && $imported instanceof RestRouteCollection) { |
||
| 87 | $parents[] = (!empty($prefix) ? $prefix.'/' : '').$imported->getSingularName(); |
||
| 88 | $prefix = null; |
||
| 89 | |||
| 90 | $this->collectionParents[$name] = $parents; |
||
| 91 | } |
||
| 92 | |||
| 93 | if (!empty($host)) { |
||
| 94 | $imported->setHost($host); |
||
| 95 | } |
||
| 96 | |||
| 97 | $imported->addPrefix((string) $prefix); |
||
| 98 | $collection->addCollection($imported); |
||
| 99 | |||
| 100 | break; |
||
| 101 | default: |
||
| 102 | throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName)); |
||
| 103 | } |
||
| 104 | 6 | } |
|
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | 6 | protected function parseRoute(RouteCollection $collection, \DOMElement $node, $path) |
|
| 180 | |||
| 181 | 6 | private function getOptions(\DOMElement $node) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | */ |
||
| 202 | 5 | public function supports($resource, $type = null) |
|
| 208 | |||
| 209 | 10 | protected function validate(\DOMDocument $dom) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * {@inheritdoc} |
||
| 239 | * |
||
| 240 | * @internal |
||
| 241 | */ |
||
| 242 | 10 | protected function loadFile($file) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Retrieves libxml errors and clears them. |
||
| 256 | * |
||
| 257 | * Note: The underscore postfix on the method name is to ensure compatibility with versions |
||
| 258 | * before 2.0.16 while working around a bug in PHP https://bugs.php.net/bug.php?id=62956 |
||
| 259 | */ |
||
| 260 | 1 | private function getXmlErrors_(bool $internalErrors): array |
|
| 280 | } |
||
| 281 |
If you suppress an error, we recommend checking for the error condition explicitly: