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 Rgba implements Color |
||
| 6 | { |
||
| 7 | /** @var int */ |
||
| 8 | protected $red, $green, $blue; |
||
|
|
|||
| 9 | |||
| 10 | /** @var float */ |
||
| 11 | protected $alpha; |
||
| 12 | |||
| 13 | View Code Duplication | public function __construct(int $red, int $green, int $blue, float $alpha) |
|
| 25 | |||
| 26 | View Code Duplication | public static function fromString(string $string) |
|
| 38 | |||
| 39 | public function red(): int |
||
| 43 | |||
| 44 | public function green(): int |
||
| 48 | |||
| 49 | public function blue(): int |
||
| 53 | |||
| 54 | public function alpha(): float |
||
| 58 | |||
| 59 | public function toHex(): Hex |
||
| 63 | |||
| 64 | public function toRgb(): Rgb |
||
| 68 | |||
| 69 | public function toRgba(float $alpha = 1): Rgba |
||
| 73 | |||
| 74 | public function __toString(): string |
||
| 80 | } |
||
| 81 |
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.