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); |
||
| 40 | class Encoder implements EncoderInterface |
||
| 41 | { |
||
| 42 | use EncoderPropertiesTrait; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Default value. |
||
| 46 | */ |
||
| 47 | const DEFAULT_URL_PREFIX = ''; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Default value. |
||
| 51 | */ |
||
| 52 | const DEFAULT_INCLUDE_PATHS = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Default value. |
||
| 56 | */ |
||
| 57 | const DEFAULT_FIELD_SET_FILTERS = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Default encode options. |
||
| 61 | * |
||
| 62 | * @link http://php.net/manual/en/function.json-encode.php |
||
| 63 | */ |
||
| 64 | const DEFAULT_JSON_ENCODE_OPTIONS = 0; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Default encode depth. |
||
| 68 | * |
||
| 69 | * @link http://php.net/manual/en/function.json-encode.php |
||
| 70 | */ |
||
| 71 | const DEFAULT_JSON_ENCODE_DEPTH = 512; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param FactoryInterface $factory |
||
| 75 | * @param SchemaContainerInterface $container |
||
| 76 | */ |
||
| 77 | 87 | public function __construct( |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Create encoder instance. |
||
| 86 | * |
||
| 87 | * @param array $schemas Schema providers. |
||
| 88 | * |
||
| 89 | * @return EncoderInterface |
||
| 90 | */ |
||
| 91 | 87 | public static function instance(array $schemas = []): EncoderInterface |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @inheritdoc |
||
| 102 | */ |
||
| 103 | 70 | public function encodeData($data): string |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @inheritdoc |
||
| 114 | */ |
||
| 115 | 5 | public function encodeIdentifiers($data): string |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @inheritdoc |
||
| 126 | */ |
||
| 127 | 3 | public function encodeError(ErrorInterface $error): string |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @inheritdoc |
||
| 138 | */ |
||
| 139 | 3 | public function encodeErrors(iterable $errors): string |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @inheritdoc |
||
| 150 | */ |
||
| 151 | 1 | public function encodeMeta($meta): string |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @return FactoryInterface |
||
| 162 | */ |
||
| 163 | 77 | protected static function createFactory(): FactoryInterface |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @param object|iterable|null $data Data to encode. |
||
| 170 | * |
||
| 171 | * @return array |
||
| 172 | * |
||
| 173 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 174 | * @SuppressWarnings(PHPMD.CyclomaticComplexity) |
||
| 175 | */ |
||
| 176 | 71 | protected function encodeDataToArray($data): array |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @param object|iterable|null $data Data to encode. |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | * |
||
| 227 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 228 | */ |
||
| 229 | 6 | protected function encodeIdentifiersToArray($data): array |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param ErrorInterface $error |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | 4 | protected function encodeErrorToArray(ErrorInterface $error): array |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param iterable $errors |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | 4 | protected function encodeErrorsToArray(iterable $errors): array |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param $meta |
||
| 342 | * |
||
| 343 | * @return array |
||
| 344 | */ |
||
| 345 | 2 | protected function encodeMetaToArray($meta): array |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @param BaseWriterInterface $writer |
||
| 367 | * |
||
| 368 | * @return void |
||
| 369 | */ |
||
| 370 | 85 | protected function writeHeader(BaseWriterInterface $writer): void |
|
| 392 | |||
| 393 | /** |
||
| 394 | * @param BaseWriterInterface $writer |
||
| 395 | * |
||
| 396 | * @return void |
||
| 397 | */ |
||
| 398 | 83 | protected function writeFooter(BaseWriterInterface $writer): void |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Encode array to JSON. |
||
| 405 | * |
||
| 406 | * @param array $document |
||
| 407 | * |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | 79 | protected function encodeToJson(array $document): string |
|
| 414 | |||
| 415 | /** |
||
| 416 | * @return DocumentWriterInterface |
||
| 417 | */ |
||
| 418 | 76 | private function createDocumentWriter(): DocumentWriterInterface |
|
| 425 | |||
| 426 | /** |
||
| 427 | * @return ErrorWriterInterface |
||
| 428 | */ |
||
| 429 | 7 | private function createErrorWriter(): ErrorWriterInterface |
|
| 436 | } |
||
| 437 |
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: