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 declare(strict_types=1); |
||
| 14 | class Hydrator |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $options; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $hydrators = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $annotations = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var HandlerInterface[] |
||
| 33 | */ |
||
| 34 | protected $annotationHandlers = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var AnnotationReader |
||
| 38 | */ |
||
| 39 | protected $annotationReader; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param array $options |
||
| 43 | */ |
||
| 44 | 4 | public function __construct(array $options) |
|
| 62 | |||
| 63 | 4 | protected function setUpAnnotations() |
|
| 73 | |||
| 74 | 4 | protected function addSelfToExtraProperties() |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @param string $class |
||
| 81 | * @param array $json |
||
| 82 | * @return ResourceInterface |
||
| 83 | */ |
||
| 84 | 4 | View Code Duplication | public function hydrate(string $class, array $json): ResourceInterface |
| 96 | |||
| 97 | /** |
||
| 98 | * @param string $class |
||
| 99 | * @param array $json |
||
| 100 | * @return ResourceInterface |
||
| 101 | */ |
||
| 102 | 4 | public function hydrateFQCN(string $class, array $json): ResourceInterface |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param array $json |
||
| 116 | * @param ResourceInterface $object |
||
| 117 | * @return array |
||
| 118 | */ |
||
| 119 | 4 | View Code Duplication | protected function hydrateApplyAnnotations(array $json, ResourceInterface $object): array |
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $class |
||
| 135 | * @param ResourceInterface $object |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | 2 | View Code Duplication | public function extract(string $class, ResourceInterface $object): array |
| 150 | |||
| 151 | /** |
||
| 152 | * Takes a fully qualified class name and extracts the data for that class from the given $object |
||
| 153 | * @param string $class |
||
| 154 | * @param ResourceInterface $object |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | 2 | public function extractFQCN(string $class, ResourceInterface $object): array |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @param array $json |
||
| 166 | * @param ResourceInterface $object |
||
| 167 | * @return array |
||
| 168 | */ |
||
| 169 | 2 | View Code Duplication | protected function extractApplyAnnotations(ResourceInterface $object, array $json): array |
| 182 | |||
| 183 | /** |
||
| 184 | * @param ResourceInterface $object |
||
| 185 | * @param string $annotationClass |
||
| 186 | * @return null|AnnotationInterface |
||
| 187 | */ |
||
| 188 | 4 | protected function getAnnotation(ResourceInterface $object, string $annotationClass) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $resource |
||
| 222 | * @param ResourceInterface $object |
||
| 223 | * @return ResourceInterface |
||
| 224 | */ |
||
| 225 | 1 | public function buildAsyncFromSync(string $resource, ResourceInterface $object): ResourceInterface |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $class |
||
| 238 | * @return HydratorInterface |
||
| 239 | */ |
||
| 240 | 4 | protected function getHydrator(string $class): HydratorInterface |
|
| 258 | } |
||
| 259 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.