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:
Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
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 | 88 | 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 | 77 | public function register($type, $schema) |
|
109 | |||
110 | /** |
||
111 | * Register providers for resource types. |
||
112 | * |
||
113 | * @param array $schemas |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | 88 | public function registerArray(array $schemas) |
|
123 | |||
124 | /** |
||
125 | * @inheritdoc |
||
126 | */ |
||
127 | 66 | public function getSchema($resource) |
|
133 | |||
134 | /** |
||
135 | * @inheritdoc |
||
136 | */ |
||
137 | 68 | public function getSchemaByType($type) |
|
164 | |||
165 | /** |
||
166 | * @inheritdoc |
||
167 | */ |
||
168 | 49 | public function getSchemaByResourceType($resourceType) |
|
196 | |||
197 | /** |
||
198 | * @return SchemaFactoryInterface |
||
199 | */ |
||
200 | 66 | protected function getFactory() |
|
204 | |||
205 | /** |
||
206 | * @return array |
||
207 | */ |
||
208 | 1 | protected function getProviderMappings() |
|
212 | |||
213 | /** |
||
214 | * @param string $type |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | 75 | protected function hasProviderMapping($type) |
|
222 | |||
223 | /** |
||
224 | * @param string $type |
||
225 | * |
||
226 | * @return mixed |
||
227 | */ |
||
228 | 66 | protected function getProviderMapping($type) |
|
232 | |||
233 | /** |
||
234 | * @param string $type |
||
235 | * @param string|Closure $schema |
||
236 | * |
||
237 | * @return void |
||
238 | */ |
||
239 | 75 | protected function setProviderMapping($type, $schema) |
|
243 | |||
244 | /** |
||
245 | * @param string $type |
||
246 | * |
||
247 | * @return bool |
||
248 | */ |
||
249 | 68 | protected function hasCreatedProvider($type) |
|
253 | |||
254 | /** |
||
255 | * @param string $type |
||
256 | * |
||
257 | * @return SchemaProviderInterface |
||
258 | */ |
||
259 | 59 | protected function getCreatedProvider($type) |
|
263 | |||
264 | /** |
||
265 | * @param string $type |
||
266 | * @param SchemaProviderInterface $provider |
||
267 | * |
||
268 | * @return void |
||
269 | */ |
||
270 | 68 | protected function setCreatedProvider($type, SchemaProviderInterface $provider) |
|
274 | |||
275 | /** |
||
276 | * @param string $resourceType |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | 49 | protected function hasResourceToJsonTypeMapping($resourceType) |
|
284 | |||
285 | /** |
||
286 | * @param string $resourceType |
||
287 | * |
||
288 | * @return string |
||
289 | */ |
||
290 | 49 | protected function getJsonType($resourceType) |
|
294 | |||
295 | /** |
||
296 | * @param string $resourceType |
||
297 | * @param string $jsonType |
||
298 | * |
||
299 | * @return void |
||
300 | */ |
||
301 | 68 | protected function setResourceToJsonTypeMapping($resourceType, $jsonType) |
|
305 | |||
306 | /** |
||
307 | * @param object $resource |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | 66 | protected function getResourceType($resource) |
|
315 | |||
316 | /** |
||
317 | * @deprecated Use `createSchemaFromCallable` method instead. |
||
318 | * @param Closure $closure |
||
319 | * |
||
320 | * @return SchemaProviderInterface |
||
321 | */ |
||
322 | protected function createSchemaFromClosure(Closure $closure) |
||
328 | |||
329 | /** |
||
330 | * @param callable $callable |
||
331 | * |
||
332 | * @return SchemaProviderInterface |
||
333 | */ |
||
334 | 32 | protected function createSchemaFromCallable(callable $callable) |
|
341 | |||
342 | /** |
||
343 | * @param string $className |
||
344 | * |
||
345 | * @return SchemaProviderInterface |
||
346 | */ |
||
347 | 43 | protected function createSchemaFromClassName($className) |
|
353 | } |
||
354 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.