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 |
||
| 8 | class Negotiation |
||
| 9 | { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @var Parser |
||
| 13 | */ |
||
| 14 | private $parser; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | private $stack; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var \Illuminate\Database\Eloquent\Model |
||
| 23 | */ |
||
| 24 | private $model; |
||
| 25 | |||
| 26 | public function __construct() |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Negotiate |
||
| 33 | * |
||
| 34 | * @param string $query |
||
| 35 | * @return Collection |
||
| 36 | */ |
||
| 37 | public function negotiate(string $query) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Return only selected elements |
||
| 56 | * |
||
| 57 | * @param Collection $collection |
||
| 58 | * @param array $fields |
||
| 59 | * @return Collection |
||
| 60 | */ |
||
| 61 | private function only(Collection $collection, array $fields) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Get relations |
||
| 76 | * |
||
| 77 | * @return Negotiation |
||
| 78 | */ |
||
| 79 | private function relations() |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Set filters |
||
| 102 | * |
||
| 103 | * @return Negotiation |
||
| 104 | */ |
||
| 105 | private function filters() |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Set the sort |
||
| 129 | * |
||
| 130 | * @return Negotiation |
||
| 131 | */ |
||
| 132 | private function orderBy() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Set limit |
||
| 143 | * |
||
| 144 | * @return Negotiation |
||
| 145 | */ |
||
| 146 | private function limit() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Set groups |
||
| 157 | * |
||
| 158 | * @return Negotiation |
||
| 159 | */ |
||
| 160 | View Code Duplication | private function groupBy() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Get data |
||
| 170 | * |
||
| 171 | * @return Collection |
||
| 172 | */ |
||
| 173 | View Code Duplication | private function get() |
|
| 181 | } |
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..