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 | abstract class AbstractVector implements VectorInterface |
||
| 9 | { |
||
| 10 | /** |
||
| 11 | * @var Element[] |
||
| 12 | */ |
||
| 13 | protected $elements = []; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var Matrix|null |
||
| 17 | */ |
||
| 18 | protected $matrix; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var int|null |
||
| 22 | */ |
||
| 23 | protected $matrixOffset; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param Element[] $elements |
||
| 27 | * @param Matrix $matrix |
||
| 28 | * @param int|null $matrixOffset |
||
| 29 | */ |
||
| 30 | 34 | public function __construct(array $elements = [], Matrix $matrix = null, $matrixOffset = null) |
|
| 44 | |||
| 45 | /** |
||
| 46 | * @param int $offset |
||
| 47 | * |
||
| 48 | * @return Element |
||
| 49 | */ |
||
| 50 | 14 | View Code Duplication | public function getElement($offset) |
| 61 | |||
| 62 | /** |
||
| 63 | * @return Element[] |
||
| 64 | */ |
||
| 65 | 18 | public function getElements() |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @param array $values |
||
| 72 | * |
||
| 73 | * @return Row|Column |
||
| 74 | */ |
||
| 75 | 6 | public static function createFromArray(array $values) |
|
| 88 | } |
||
| 89 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: