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 namespace Neomerx\JsonApi\Schema; |
||
32 | class Container implements ContainerInterface, LoggerAwareInterface |
||
33 | { |
||
34 | use LoggerAwareTrait; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $providerMapping = []; |
||
40 | |||
41 | /** |
||
42 | * @var SchemaProviderInterface[] |
||
43 | */ |
||
44 | private $createdProviders = []; |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $resType2JsonType = []; |
||
50 | |||
51 | /** |
||
52 | * @var SchemaFactoryInterface |
||
53 | */ |
||
54 | private $factory; |
||
55 | |||
56 | /** |
||
57 | * @param SchemaFactoryInterface $factory |
||
58 | * @param array $schemas |
||
59 | */ |
||
60 | 79 | public function __construct(SchemaFactoryInterface $factory, array $schemas = []) |
|
65 | |||
66 | /** |
||
67 | * Register provider for resource type. |
||
68 | * |
||
69 | * @param string $type |
||
70 | * @param string|Closure $schema |
||
71 | * |
||
72 | * @return void |
||
73 | */ |
||
74 | 71 | public function register($type, $schema) |
|
75 | { |
||
76 | // Type must be non-empty string |
||
77 | 71 | $isOk = (is_string($type) === true && empty($type) === false); |
|
78 | 71 | if ($isOk === false) { |
|
79 | 1 | throw new InvalidArgumentException(T::t('Type must be non-empty string.')); |
|
80 | } |
||
81 | |||
82 | 70 | $isOk = ((is_string($schema) === true && empty($schema) === false) || $schema instanceof Closure); |
|
83 | 70 | if ($isOk === false) { |
|
84 | 1 | throw new InvalidArgumentException(T::t( |
|
85 | 1 | 'Schema for type \'%s\' must be non-empty string or Closure.', |
|
86 | 1 | [$type] |
|
87 | 1 | )); |
|
88 | } |
||
89 | |||
90 | 69 | View Code Duplication | if ($this->hasProviderMapping($type) === true) { |
91 | 1 | throw new InvalidArgumentException(T::t( |
|
92 | 1 | 'Type should not be used more than once to register a schema (\'%s\').', |
|
93 | 1 | [$type] |
|
94 | 2 | )); |
|
95 | } |
||
96 | |||
97 | 69 | $this->setProviderMapping($type, $schema); |
|
98 | 69 | } |
|
99 | |||
100 | /** |
||
101 | * Register providers for resource types. |
||
102 | * |
||
103 | * @param array $schemas |
||
104 | * |
||
105 | * @return void |
||
106 | */ |
||
107 | 79 | public function registerArray(array $schemas) |
|
113 | |||
114 | /** |
||
115 | * @inheritdoc |
||
116 | */ |
||
117 | 60 | public function getSchema($resource) |
|
123 | |||
124 | /** |
||
125 | * @inheritdoc |
||
126 | */ |
||
127 | 62 | public function getSchemaByType($type) |
|
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | */ |
||
157 | 45 | public function getSchemaByResourceType($resourceType) |
|
185 | |||
186 | /** |
||
187 | * @return SchemaFactoryInterface |
||
188 | */ |
||
189 | 62 | protected function getFactory() |
|
193 | |||
194 | /** |
||
195 | * @return array |
||
196 | */ |
||
197 | 1 | protected function getProviderMappings() |
|
201 | |||
202 | /** |
||
203 | * @param string $type |
||
204 | * |
||
205 | * @return bool |
||
206 | */ |
||
207 | 69 | protected function hasProviderMapping($type) |
|
211 | |||
212 | /** |
||
213 | * @param string $type |
||
214 | * |
||
215 | * @return mixed |
||
216 | */ |
||
217 | 62 | protected function getProviderMapping($type) |
|
221 | |||
222 | /** |
||
223 | * @param string $type |
||
224 | * @param string|Closure $schema |
||
225 | * |
||
226 | * @return void |
||
227 | */ |
||
228 | 69 | protected function setProviderMapping($type, $schema) |
|
232 | |||
233 | /** |
||
234 | * @param string $type |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | 62 | protected function hasCreatedProvider($type) |
|
242 | |||
243 | /** |
||
244 | * @param string $type |
||
245 | * |
||
246 | * @return SchemaProviderInterface |
||
247 | */ |
||
248 | 54 | protected function getCreatedProvider($type) |
|
252 | |||
253 | /** |
||
254 | * @param string $type |
||
255 | * @param SchemaProviderInterface $provider |
||
256 | * |
||
257 | * @return void |
||
258 | */ |
||
259 | 62 | protected function setCreatedProvider($type, SchemaProviderInterface $provider) |
|
263 | |||
264 | /** |
||
265 | * @param string $resourceType |
||
266 | * |
||
267 | * @return bool |
||
268 | */ |
||
269 | 45 | protected function hasResourceToJsonTypeMapping($resourceType) |
|
273 | |||
274 | /** |
||
275 | * @param string $resourceType |
||
276 | * |
||
277 | * @return string |
||
278 | */ |
||
279 | 45 | protected function getJsonType($resourceType) |
|
283 | |||
284 | /** |
||
285 | * @param string $resourceType |
||
286 | * @param string $jsonType |
||
287 | * |
||
288 | * @return void |
||
289 | */ |
||
290 | 62 | protected function setResourceToJsonTypeMapping($resourceType, $jsonType) |
|
294 | |||
295 | /** |
||
296 | * @param object $resource |
||
297 | * |
||
298 | * @return string |
||
299 | */ |
||
300 | 60 | protected function getResourceType($resource) |
|
304 | |||
305 | /** |
||
306 | * @param Closure $closure |
||
307 | * |
||
308 | * @return SchemaProviderInterface |
||
309 | */ |
||
310 | 28 | protected function createSchemaFromClosure(Closure $closure) |
|
316 | |||
317 | /** |
||
318 | * @param string $className |
||
319 | * |
||
320 | * @return SchemaProviderInterface |
||
321 | */ |
||
322 | 42 | protected function createSchemaFromClassName($className) |
|
328 | } |
||
329 |