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 | 82 | public function __construct( |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Create encoder instance. |
||
| 86 | * |
||
| 87 | * @param array $schemas Schema providers. |
||
| 88 | * |
||
| 89 | * @return EncoderInterface |
||
| 90 | */ |
||
| 91 | 82 | public static function instance(array $schemas = []): EncoderInterface |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @inheritdoc |
||
| 102 | */ |
||
| 103 | 65 | 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 | 75 | 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 | 66 | protected function encodeDataToArray($data): array |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @param object|iterable|null $data Data to encode. |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | * |
||
| 226 | * @SuppressWarnings(PHPMD.ElseExpression) |
||
| 227 | */ |
||
| 228 | 6 | protected function encodeIdentifiersToArray($data): array |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param ErrorInterface $error |
||
| 290 | * |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | 4 | protected function encodeErrorToArray(ErrorInterface $error): array |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @param iterable $errors |
||
| 313 | * |
||
| 314 | * @return array |
||
| 315 | */ |
||
| 316 | 4 | protected function encodeErrorsToArray(iterable $errors): array |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @param $meta |
||
| 340 | * |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | 2 | protected function encodeMetaToArray($meta): array |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @param BaseWriterInterface $writer |
||
| 365 | * |
||
| 366 | * @return void |
||
| 367 | */ |
||
| 368 | 80 | protected function writeHeader(BaseWriterInterface $writer): void |
|
| 390 | |||
| 391 | /** |
||
| 392 | * @param BaseWriterInterface $writer |
||
| 393 | * |
||
| 394 | * @return void |
||
| 395 | */ |
||
| 396 | 78 | protected function writeFooter(BaseWriterInterface $writer): void |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Encode array to JSON. |
||
| 403 | * |
||
| 404 | * @param array $document |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | 74 | protected function encodeToJson(array $document): string |
|
| 412 | |||
| 413 | /** |
||
| 414 | * @return DocumentWriterInterface |
||
| 415 | */ |
||
| 416 | 71 | private function createDocumentWriter(): DocumentWriterInterface |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @return ErrorWriterInterface |
||
| 426 | */ |
||
| 427 | 7 | private function createErrorWriter(): ErrorWriterInterface |
|
| 434 | } |
||
| 435 |
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: