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; |
||
31 | class Container implements ContainerInterface, LoggerAwareInterface |
||
32 | { |
||
33 | use LoggerAwareTrait; |
||
34 | |||
35 | /** |
||
36 | * Message code. |
||
37 | */ |
||
38 | const MSG_INVALID_TYPE = 0; |
||
39 | |||
40 | /** |
||
41 | * Message code. |
||
42 | */ |
||
43 | const MSG_INVALID_SCHEME = self::MSG_INVALID_TYPE + 1; |
||
44 | |||
45 | /** |
||
46 | * Message code. |
||
47 | */ |
||
48 | const MSG_TYPE_REUSE_FORBIDDEN = self::MSG_INVALID_SCHEME + 1; |
||
49 | |||
50 | /** |
||
51 | * Message code. |
||
52 | */ |
||
53 | const MSG_UNREGISTERED_SCHEME_FOR_TYPE = self::MSG_TYPE_REUSE_FORBIDDEN + 1; |
||
54 | |||
55 | /** |
||
56 | * Message code. |
||
57 | */ |
||
58 | const MSG_UNREGISTERED_SCHEME_FOR_RESOURCE_TYPE = self::MSG_UNREGISTERED_SCHEME_FOR_TYPE + 1; |
||
59 | |||
60 | /** |
||
61 | * Default messages. |
||
62 | */ |
||
63 | const MESSAGES = [ |
||
64 | self::MSG_INVALID_TYPE => 'Type must be non-empty string.', |
||
65 | self::MSG_INVALID_SCHEME => |
||
66 | 'Schema for type \'%s\' must be non-empty string, callable or SchemaInterface instance.', |
||
67 | self::MSG_TYPE_REUSE_FORBIDDEN => |
||
68 | 'Type should not be used more than once to register a schema (\'%s\').', |
||
69 | self::MSG_UNREGISTERED_SCHEME_FOR_TYPE => 'Schema is not registered for type \'%s\'.', |
||
70 | self::MSG_UNREGISTERED_SCHEME_FOR_RESOURCE_TYPE => 'Schema is not registered for resource type \'%s\'.', |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * @var array |
||
75 | */ |
||
76 | private $providerMapping = []; |
||
77 | |||
78 | /** |
||
79 | * @var SchemaInterface[] |
||
80 | */ |
||
81 | private $createdProviders = []; |
||
82 | |||
83 | /** |
||
84 | * @var array |
||
85 | */ |
||
86 | private $resType2JsonType = []; |
||
87 | |||
88 | /** |
||
89 | * @var SchemaFactoryInterface |
||
90 | */ |
||
91 | private $factory; |
||
92 | |||
93 | /** |
||
94 | * @var array |
||
95 | */ |
||
96 | private $messages; |
||
97 | |||
98 | /** |
||
99 | * @param SchemaFactoryInterface $factory |
||
100 | * @param iterable $schemas |
||
|
|||
101 | * @param array $messages |
||
102 | */ |
||
103 | 96 | public function __construct(SchemaFactoryInterface $factory, iterable $schemas = [], $messages = self::MESSAGES) |
|
109 | |||
110 | /** |
||
111 | * Register provider for resource type. |
||
112 | * |
||
113 | * @param string $type |
||
114 | * @param string|Closure $schema |
||
115 | * |
||
116 | * @return void |
||
117 | * |
||
118 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
119 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
120 | */ |
||
121 | 85 | public function register(string $type, $schema): void |
|
122 | { |
||
123 | 85 | if (empty($type) === true || (class_exists($type) === false && interface_exists($type) === false)) { |
|
124 | 1 | throw new InvalidArgumentException(_($this->messages[self::MSG_INVALID_TYPE])); |
|
125 | } |
||
126 | |||
127 | $isOk = ( |
||
128 | 84 | (is_string($schema) === true && empty($schema) === false) || |
|
129 | 41 | is_callable($schema) || |
|
130 | 84 | $schema instanceof SchemaInterface |
|
131 | ); |
||
132 | 84 | if ($isOk === false) { |
|
133 | 1 | throw new InvalidArgumentException(_($this->messages[self::MSG_INVALID_SCHEME], $type)); |
|
134 | } |
||
135 | |||
136 | 83 | if ($this->hasProviderMapping($type) === true) { |
|
137 | 1 | throw new InvalidArgumentException(_($this->messages[self::MSG_TYPE_REUSE_FORBIDDEN], $type)); |
|
138 | } |
||
139 | |||
140 | 83 | if ($schema instanceof SchemaInterface) { |
|
141 | 4 | $this->setProviderMapping($type, get_class($schema)); |
|
142 | 4 | $this->setResourceToJsonTypeMapping($schema->getResourceType(), $type); |
|
143 | 4 | $this->setCreatedProvider($type, $schema); |
|
144 | } else { |
||
145 | 79 | $this->setProviderMapping($type, $schema); |
|
146 | } |
||
147 | 83 | } |
|
148 | |||
149 | /** |
||
150 | * Register providers for resource types. |
||
151 | * |
||
152 | * @param iterable $schemas |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | 96 | public function registerArray(iterable $schemas): void |
|
162 | |||
163 | /** |
||
164 | * @inheritdoc |
||
165 | */ |
||
166 | 74 | public function getSchema($resource): ?SchemaInterface |
|
176 | |||
177 | /** |
||
178 | * @inheritdoc |
||
179 | */ |
||
180 | 74 | public function hasSchema($resourceObject): bool |
|
185 | |||
186 | /** |
||
187 | * @inheritdoc |
||
188 | * |
||
189 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
190 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
191 | */ |
||
192 | 75 | public function getSchemaByType(string $type): SchemaInterface |
|
217 | |||
218 | /** |
||
219 | * @inheritdoc |
||
220 | * |
||
221 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
222 | * @SuppressWarnings(PHPMD.UnusedLocalVariable) |
||
223 | */ |
||
224 | 48 | public function getSchemaByResourceType(string $resourceType): SchemaInterface |
|
252 | |||
253 | /** |
||
254 | * @return SchemaFactoryInterface |
||
255 | */ |
||
256 | 70 | protected function getFactory(): SchemaFactoryInterface |
|
260 | |||
261 | /** |
||
262 | * @return array |
||
263 | */ |
||
264 | 75 | protected function getProviderMappings(): array |
|
268 | |||
269 | /** |
||
270 | * @param string $type |
||
271 | * |
||
272 | * @return bool |
||
273 | */ |
||
274 | 83 | protected function hasProviderMapping(string $type): bool |
|
278 | |||
279 | /** |
||
280 | * @param string $type |
||
281 | * |
||
282 | * @return mixed |
||
283 | */ |
||
284 | 70 | protected function getProviderMapping(string $type) |
|
288 | |||
289 | /** |
||
290 | * @param string $type |
||
291 | * @param string|Closure $schema |
||
292 | * |
||
293 | * @return void |
||
294 | */ |
||
295 | 83 | protected function setProviderMapping(string $type, $schema): void |
|
299 | |||
300 | /** |
||
301 | * @param string $type |
||
302 | * |
||
303 | * @return bool |
||
304 | */ |
||
305 | 75 | protected function hasCreatedProvider(string $type): bool |
|
309 | |||
310 | /** |
||
311 | * @param string $type |
||
312 | * |
||
313 | * @return SchemaInterface |
||
314 | */ |
||
315 | 63 | protected function getCreatedProvider(string $type): SchemaInterface |
|
319 | |||
320 | /** |
||
321 | * @param string $type |
||
322 | * @param SchemaInterface $provider |
||
323 | * |
||
324 | * @return void |
||
325 | */ |
||
326 | 74 | protected function setCreatedProvider(string $type, SchemaInterface $provider): void |
|
330 | |||
331 | /** |
||
332 | * @param string $resourceType |
||
333 | * |
||
334 | * @return bool |
||
335 | */ |
||
336 | 48 | protected function hasResourceToJsonTypeMapping(string $resourceType): bool |
|
340 | |||
341 | /** |
||
342 | * @param string $resourceType |
||
343 | * |
||
344 | * @return string |
||
345 | */ |
||
346 | 48 | protected function getJsonType(string $resourceType): string |
|
350 | |||
351 | /** |
||
352 | * @param string $resourceType |
||
353 | * @param string $jsonType |
||
354 | * |
||
355 | * @return void |
||
356 | */ |
||
357 | 74 | protected function setResourceToJsonTypeMapping(string $resourceType, string $jsonType): void |
|
361 | |||
362 | /** |
||
363 | * @param object $resource |
||
364 | * |
||
365 | * @return string |
||
366 | */ |
||
367 | 74 | protected function getResourceType($resource): string |
|
382 | |||
383 | /** |
||
384 | * @param callable $callable |
||
385 | * |
||
386 | * @return SchemaInterface |
||
387 | */ |
||
388 | 33 | protected function createSchemaFromCallable(callable $callable): SchemaInterface |
|
394 | |||
395 | /** |
||
396 | * @param string $className |
||
397 | * |
||
398 | * @return SchemaInterface |
||
399 | */ |
||
400 | 47 | protected function createSchemaFromClassName(string $className): SchemaInterface |
|
406 | } |
||
407 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
array
and suggests a stricter type likearray<String>
.Most often this is a case of a parameter that can be null in addition to its declared types.