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 |
||
| 10 | final class Resource implements ResourceNode |
||
| 11 | { |
||
| 12 | /** @var array */ |
||
| 13 | private $fields = []; |
||
| 14 | |||
| 15 | /** @var Link[] */ |
||
| 16 | private $links = []; |
||
| 17 | |||
| 18 | /** @var Resource[] */ |
||
| 19 | private $resources = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @param FieldNode[] $fields |
||
| 23 | * @param Link[] $links |
||
| 24 | * @param ResourceNode[] $resources |
||
| 25 | */ |
||
| 26 | public function __construct(array $fields, array $links = [], array $resources = []) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param string $name |
||
| 39 | * |
||
| 40 | * @return FieldNode |
||
| 41 | */ |
||
| 42 | public function __get($name) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $name |
||
| 49 | * |
||
| 50 | * @return FieldNode |
||
| 51 | * |
||
| 52 | * @throws FieldNotFoundException |
||
| 53 | */ |
||
| 54 | View Code Duplication | public function getField($name) |
|
| 62 | |||
| 63 | /** @return string[] */ |
||
| 64 | public function getLinks() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @return Link |
||
| 71 | * |
||
| 72 | * @throws LinkNotFoundException |
||
| 73 | */ |
||
| 74 | public function getLink($name) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return Resource |
||
| 85 | */ |
||
| 86 | public function getResource($name) |
||
| 94 | |||
| 95 | public function matches($criteria) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string|int $name |
||
| 108 | * @param mixed $value |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | private function matchesItem($name, $value) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string|int $name |
||
| 123 | * @param mixed $value |
||
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | private function isResourceSearchCriteria($name, $value) |
||
| 134 | |||
| 135 | /** @return bool */ |
||
| 136 | private function matchesResource(array $value) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $name |
||
| 150 | * @param mixed $value |
||
| 151 | * |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | public function matchField($name, $value) |
||
| 162 | } |
||
| 163 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..