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 |
||
| 7 | class NodePath implements \Countable, \Iterator |
||
| 8 | { |
||
| 9 | private |
||
| 10 | $path, |
||
| 11 | $valid; |
||
| 12 | |||
| 13 | public function __construct(array $path) |
||
| 25 | |||
| 26 | public function count() |
||
| 30 | |||
| 31 | public function current() |
||
| 35 | |||
| 36 | public function key() |
||
| 40 | |||
| 41 | public function next() |
||
| 45 | |||
| 46 | public function rewind() |
||
| 51 | |||
| 52 | public function valid() |
||
| 56 | |||
| 57 | public function getKey($key) |
||
| 61 | |||
| 62 | public function getKeys() |
||
| 66 | |||
| 67 | View Code Duplication | public function computeLength(Distance $distance) |
|
| 85 | |||
| 86 | public function contains(Node $needleNode) |
||
| 98 | |||
| 99 | public function getStartNode() |
||
| 103 | |||
| 104 | public function getEndNode() |
||
| 108 | |||
| 109 | public function toString() |
||
| 119 | } |
||
| 120 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.