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 |
||
| 24 | class RootUnit implements UnitInterface |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var TreeConverter |
||
| 28 | */ |
||
| 29 | protected $tree; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $name; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | */ |
||
| 39 | 10 | public function getName() |
|
| 43 | |||
| 44 | /** @return UnitInterface */ |
||
| 45 | 7 | public function getParent() |
|
| 49 | |||
| 50 | /** @return UnitInterface */ |
||
| 51 | 10 | public function getRoot() |
|
| 55 | |||
| 56 | 8 | public function getFactor() |
|
| 60 | |||
| 61 | 7 | public function getMeasure() |
|
| 65 | |||
| 66 | /** |
||
| 67 | * @param TreeConverter $tree |
||
| 68 | * @param string $name |
||
| 69 | */ |
||
| 70 | 5 | public function __construct(TreeConverter $tree, $name) |
|
| 75 | |||
| 76 | protected $calculator; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Returns calculator for this unit. |
||
| 80 | * Units can have different calculators. |
||
| 81 | * @return CalculatorInterface |
||
| 82 | */ |
||
| 83 | 7 | public function getCalculator() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | 9 | public function equals(UnitInterface $other) |
|
| 99 | |||
| 100 | 9 | public function isAlias(UnitInterface $other) |
|
| 111 | |||
| 112 | 8 | public function getCanon() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritdoc} |
||
| 119 | */ |
||
| 120 | 7 | public function assertConvertible(UnitInterface $other) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * {@inheritdoc} |
||
| 129 | */ |
||
| 130 | 9 | final public function isConvertible(UnitInterface $other) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | */ |
||
| 138 | 7 | public function convert(UnitInterface $other, $quantity) |
|
| 158 | |||
| 159 | 7 | View Code Duplication | public function multiplyByFactor($quantity) |
| 167 | |||
| 168 | 7 | View Code Duplication | public function divideByFactor($quantity) |
| 176 | |||
| 177 | 9 | public function getNode(UnitInterface $unit) |
|
| 181 | } |
||
| 182 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: