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:
1 | <?php |
||
28 | abstract class AbstractCollectionNormalizer implements NormalizerInterface, NormalizerAwareInterface |
||
29 | { |
||
30 | use ContextTrait { initContext as protected; } |
||
31 | use NormalizerAwareTrait; |
||
32 | |||
33 | const FORMAT = self::FORMAT; |
||
34 | |||
35 | protected $resourceClassResolver; |
||
36 | protected $pageParameterName; |
||
37 | |||
38 | public function __construct(ResourceClassResolverInterface $resourceClassResolver, string $pageParameterName) |
||
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | public function supportsNormalization($data, $format = null) |
||
51 | |||
52 | /** |
||
53 | * {@inheritdoc} |
||
54 | * |
||
55 | * @param iterable $object |
||
56 | */ |
||
57 | public function normalize($object, $format = null, array $context = []) |
||
79 | |||
80 | /** |
||
81 | * Gets the pagination configuration. |
||
82 | * |
||
83 | * @param iterable $object |
||
84 | * @param array $context |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | protected function getPaginationConfig($object, array $context = []): array |
||
108 | |||
109 | /** |
||
110 | * Gets the pagination data. |
||
111 | * |
||
112 | * @param iterable $object |
||
113 | * @param array $context |
||
114 | * |
||
115 | * @return array |
||
116 | */ |
||
117 | abstract protected function getPaginationData($object, array $context = []): array; |
||
118 | |||
119 | /** |
||
120 | * Gets items data. |
||
121 | * |
||
122 | * @param iterable $object |
||
123 | * @param string|null $format |
||
124 | * @param array $context |
||
125 | * |
||
126 | * @return array |
||
127 | */ |
||
128 | abstract protected function getItemsData($object, string $format = null, array $context = []): array; |
||
129 | } |
||
130 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.