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 Encoder 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 Encoder, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
38 | class Encoder implements EncoderInterface |
||
39 | { |
||
40 | use EncoderPropertiesTrait; |
||
41 | |||
42 | /** |
||
43 | * Default value. |
||
44 | */ |
||
45 | const DEFAULT_URL_PREFIX = ''; |
||
46 | |||
47 | /** |
||
48 | * Default value. |
||
49 | */ |
||
50 | const DEFAULT_INCLUDE_PATHS = []; |
||
51 | |||
52 | /** |
||
53 | * Default value. |
||
54 | */ |
||
55 | const DEFAULT_FIELD_SET_FILTERS = []; |
||
56 | |||
57 | /** |
||
58 | * Default encode options. |
||
59 | * |
||
60 | * @link http://php.net/manual/en/function.json-encode.php |
||
61 | */ |
||
62 | const DEFAULT_JSON_ENCODE_OPTIONS = 0; |
||
63 | |||
64 | /** |
||
65 | * Default encode depth. |
||
66 | * |
||
67 | * @link http://php.net/manual/en/function.json-encode.php |
||
68 | */ |
||
69 | const DEFAULT_JSON_ENCODE_DEPTH = 512; |
||
70 | |||
71 | /** |
||
72 | * @param FactoryInterface $factory |
||
73 | * @param SchemaContainerInterface $container |
||
74 | */ |
||
75 | 81 | public function __construct( |
|
81 | |||
82 | /** |
||
83 | * Create encoder instance. |
||
84 | * |
||
85 | * @param array $schemas Schema providers. |
||
86 | * |
||
87 | * @return EncoderInterface |
||
88 | */ |
||
89 | 81 | public static function instance(array $schemas = []): EncoderInterface |
|
97 | |||
98 | /** |
||
99 | * @inheritdoc |
||
100 | */ |
||
101 | 64 | public function encodeData($data): string |
|
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | */ |
||
115 | 5 | public function encodeIdentifiers($data): string |
|
125 | |||
126 | /** |
||
127 | * @inheritdoc |
||
128 | */ |
||
129 | 3 | public function encodeError(ErrorInterface $error): string |
|
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | 3 | public function encodeErrors(iterable $errors): string |
|
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | */ |
||
157 | 1 | public function encodeMeta($meta): string |
|
167 | |||
168 | /** |
||
169 | * @return FactoryInterface |
||
170 | */ |
||
171 | 74 | protected static function createFactory(): FactoryInterface |
|
175 | |||
176 | /** |
||
177 | * @param object|iterable|null $data Data to encode. |
||
178 | * |
||
179 | * @return array |
||
180 | * |
||
181 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
182 | */ |
||
183 | 65 | protected function encodeDataToArray($data): array |
|
227 | |||
228 | /** |
||
229 | * @param object|iterable|null $data Data to encode. |
||
230 | * |
||
231 | * @return array |
||
232 | * |
||
233 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
234 | */ |
||
235 | 6 | protected function encodeIdentifiersToArray($data): array |
|
294 | |||
295 | /** |
||
296 | * @param ErrorInterface $error |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | 4 | protected function encodeErrorToArray(ErrorInterface $error): array |
|
317 | |||
318 | /** |
||
319 | * @param iterable $errors |
||
320 | * |
||
321 | * @return array |
||
322 | */ |
||
323 | 4 | protected function encodeErrorsToArray(iterable $errors): array |
|
344 | |||
345 | /** |
||
346 | * @param $meta |
||
347 | * |
||
348 | * @return array |
||
349 | */ |
||
350 | 2 | protected function encodeMetaToArray($meta): array |
|
369 | |||
370 | /** |
||
371 | * @param BaseWriterInterface $writer |
||
372 | * |
||
373 | * @return void |
||
374 | */ |
||
375 | 79 | protected function writeHeader(BaseWriterInterface $writer): void |
|
397 | |||
398 | /** |
||
399 | * @param BaseWriterInterface $writer |
||
400 | * |
||
401 | * @return void |
||
402 | */ |
||
403 | 77 | protected function writeFooter(BaseWriterInterface $writer): void |
|
407 | |||
408 | /** |
||
409 | * Encode array to JSON. |
||
410 | * |
||
411 | * @param array $document |
||
412 | * |
||
413 | * @return string |
||
414 | */ |
||
415 | 73 | protected function encodeToJson(array $document): string |
|
419 | |||
420 | /** |
||
421 | * @return DocumentWriterInterface |
||
422 | */ |
||
423 | 70 | private function createDocumentWriter(): DocumentWriterInterface |
|
430 | |||
431 | /** |
||
432 | * @return ErrorWriterInterface |
||
433 | */ |
||
434 | 7 | private function createErrorWriter(): ErrorWriterInterface |
|
441 | |||
442 | /** |
||
443 | * @return self |
||
444 | */ |
||
445 | 81 | private function resetPropertiesToDefaults(): self |
|
455 | } |
||
456 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: