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 | final class TrackInfo |
||
9 | { |
||
10 | private $name; |
||
11 | |||
12 | private $artists; |
||
13 | |||
14 | 18 | public function __construct($name, array $artists = []) |
|
21 | |||
22 | 18 | View Code Duplication | private function assertValidName($name) |
31 | |||
32 | 15 | View Code Duplication | private function assertThatTheseAreAllArtists(array $artists) |
48 | |||
49 | public function getName() |
||
53 | |||
54 | public function getArtists() |
||
58 | |||
59 | public function toString() |
||
67 | |||
68 | public function __toString() |
||
72 | } |
||
73 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.