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 Collection implements Interfaces\CollectionInterface |
||
6 | { |
||
7 | |||
8 | // Private loaded data |
||
9 | private $data = null; |
||
10 | private $conf = null; |
||
11 | private $id = null; |
||
12 | private $tmdb = null; |
||
13 | |||
14 | /** |
||
15 | * Constructor |
||
16 | * @param \vfalies\tmdb\Tmdb $tmdb |
||
17 | * @throws \Exception |
||
18 | */ |
||
19 | 15 | View Code Duplication | public function __construct(\vfalies\tmdb\Tmdb $tmdb, $collection_id, array $options = array()) |
35 | |||
36 | /** |
||
37 | * Get collection ID |
||
38 | * @return int |
||
39 | * @throws \Exception |
||
40 | */ |
||
41 | 2 | public function getId(): int |
|
45 | |||
46 | /** |
||
47 | * Get collection name |
||
48 | * @return string |
||
49 | * @throws \Exception |
||
50 | */ |
||
51 | 2 | public function getName(): string |
|
59 | |||
60 | /** |
||
61 | * Get collection poster |
||
62 | * @param string $size |
||
63 | * @return string |
||
64 | * @throws \Exception |
||
65 | */ |
||
66 | 4 | View Code Duplication | public function getPoster($size = 'w185'): string |
82 | |||
83 | /** |
||
84 | * Get collection backdrop |
||
85 | * @param string $size |
||
86 | * @return string |
||
87 | * @throws \Exception |
||
88 | */ |
||
89 | 4 | View Code Duplication | public function getBackdrop($size = 'w780'): string |
105 | |||
106 | /** |
||
107 | * Get collection parts |
||
108 | * @return Generator|SearchMovieResult |
||
109 | */ |
||
110 | 2 | public function getParts(): \Generator |
|
121 | |||
122 | } |
||
123 |
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.