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 |
||
5 | class Rgb implements Color |
||
6 | { |
||
7 | /** @var int */ |
||
8 | protected $red, $green, $blue; |
||
|
|||
9 | |||
10 | View Code Duplication | public function __construct(int $red, int $green, int $blue) |
|
20 | |||
21 | View Code Duplication | public static function fromString(string $string) |
|
33 | |||
34 | public function red(): int |
||
38 | |||
39 | public function green(): int |
||
43 | |||
44 | public function blue(): int |
||
48 | |||
49 | public function toHex(): Hex |
||
57 | |||
58 | public function toRgb(): Rgb |
||
62 | |||
63 | public function toRgba(float $alpha = 1): Rgba |
||
67 | |||
68 | public function __toString(): string |
||
72 | } |
||
73 |
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.