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 RestYamlCollectionLoader extends YamlFileLoader |
||
30 | { |
||
31 | protected $collectionParents = []; |
||
32 | private $processor; |
||
33 | private $includeFormat; |
||
34 | private $formats; |
||
35 | private $defaultFormat; |
||
36 | |||
37 | /** |
||
38 | * @param string[] $formats |
||
39 | */ |
||
40 | 21 | View Code Duplication | public function __construct( |
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | 17 | public function load($file, $type = null) |
|
59 | { |
||
60 | 17 | $path = $this->locator->locate($file); |
|
61 | |||
62 | try { |
||
63 | 17 | $config = Yaml::parse(file_get_contents($path)); |
|
64 | 1 | } catch (ParseException $e) { |
|
65 | 1 | throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e); |
|
66 | } |
||
67 | |||
68 | 16 | $collection = new RouteCollection(); |
|
69 | 16 | $collection->addResource(new FileResource($path)); |
|
70 | |||
71 | // empty file |
||
72 | 16 | if (null === $config) { |
|
73 | 1 | return $collection; |
|
74 | } |
||
75 | |||
76 | // not an array |
||
77 | 15 | if (!is_array($config)) { |
|
78 | 1 | throw new \InvalidArgumentException(sprintf('The file "%s" must contain a Yaml mapping (an array).', $path)); |
|
79 | } |
||
80 | |||
81 | // process routes and imports |
||
82 | 14 | foreach ($config as $name => $config) { |
|
83 | 14 | if (isset($config['resource'])) { |
|
84 | 8 | $resource = $config['resource']; |
|
85 | 8 | $prefix = isset($config['prefix']) ? $config['prefix'] : null; |
|
86 | 8 | $namePrefix = isset($config['name_prefix']) ? $config['name_prefix'] : null; |
|
87 | 8 | $parent = isset($config['parent']) ? $config['parent'] : null; |
|
88 | 8 | $type = isset($config['type']) ? $config['type'] : null; |
|
89 | 8 | $host = isset($config['host']) ? $config['host'] : null; |
|
90 | 8 | $requirements = isset($config['requirements']) ? $config['requirements'] : []; |
|
91 | 8 | $defaults = isset($config['defaults']) ? $config['defaults'] : []; |
|
92 | 8 | $options = isset($config['options']) ? $config['options'] : []; |
|
93 | 8 | $currentDir = dirname($path); |
|
94 | |||
95 | 8 | $parents = []; |
|
96 | 8 | View Code Duplication | if (!empty($parent)) { |
97 | 1 | if (!isset($this->collectionParents[$parent])) { |
|
98 | 1 | throw new \InvalidArgumentException(sprintf('Cannot find parent resource with name %s', $parent)); |
|
99 | } |
||
100 | |||
101 | $parents = $this->collectionParents[$parent]; |
||
102 | } |
||
103 | |||
104 | 7 | $imported = $this->processor->importResource($this, $resource, $parents, $prefix, $namePrefix, $type, $currentDir); |
|
105 | |||
106 | 3 | if ($imported instanceof RestRouteCollection) { |
|
107 | 3 | $parents[] = ($prefix ? $prefix.'/' : '').$imported->getSingularName(); |
|
108 | 3 | $prefix = null; |
|
109 | 3 | $namePrefix = null; |
|
110 | |||
111 | 3 | $this->collectionParents[$name] = $parents; |
|
112 | } |
||
113 | |||
114 | 3 | $imported->addRequirements($requirements); |
|
115 | 3 | $imported->addDefaults($defaults); |
|
116 | 3 | $imported->addOptions($options); |
|
117 | |||
118 | 3 | if (!empty($host)) { |
|
119 | $imported->setHost($host); |
||
120 | } |
||
121 | |||
122 | 3 | $imported->addPrefix((string) $prefix); |
|
123 | |||
124 | // Add name prefix from parent config files |
||
125 | 3 | $imported = $this->addParentNamePrefix($imported, $namePrefix); |
|
126 | |||
127 | 3 | $collection->addCollection($imported); |
|
128 | 6 | } elseif (isset($config['pattern']) || isset($config['path'])) { |
|
129 | // the YamlFileLoader of the Routing component only checks for |
||
130 | // the path option |
||
131 | 6 | if (!isset($config['path'])) { |
|
132 | 1 | $config['path'] = $config['pattern']; |
|
133 | |||
134 | 1 | @trigger_error(sprintf('The "pattern" option at "%s" in file "%s" is deprecated. Use the "path" option instead.', $name, $path), E_USER_DEPRECATED); |
|
135 | } |
||
136 | |||
137 | 6 | if ($this->includeFormat) { |
|
138 | // append format placeholder if not present |
||
139 | 5 | if (false === strpos($config['path'], '{_format}')) { |
|
140 | 5 | $config['path'] .= '.{_format}'; |
|
141 | } |
||
142 | |||
143 | // set format requirement if configured globally |
||
144 | 5 | if (!isset($config['requirements']['_format']) && !empty($this->formats)) { |
|
145 | 5 | $config['requirements']['_format'] = implode('|', array_keys($this->formats)); |
|
146 | } |
||
147 | } |
||
148 | |||
149 | // set the default format if configured |
||
150 | 6 | if (null !== $this->defaultFormat) { |
|
151 | 1 | $config['defaults']['_format'] = $this->defaultFormat; |
|
152 | } |
||
153 | |||
154 | 6 | $this->parseRoute($collection, $name, $config, $path); |
|
155 | } else { |
||
156 | throw new \InvalidArgumentException(sprintf('Unable to parse the "%s" route.', $name)); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | 9 | return $collection; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | 10 | public function supports($resource, $type = null) |
|
171 | |||
172 | /** |
||
173 | * @param string $namePrefix |
||
174 | * |
||
175 | * @return RouteCollection |
||
176 | */ |
||
177 | 3 | public function addParentNamePrefix(RouteCollection $collection, $namePrefix) |
|
192 | } |
||
193 |
If you suppress an error, we recommend checking for the error condition explicitly: