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 Project extends Model |
||
8 | { |
||
9 | /** |
||
10 | * @var int |
||
11 | */ |
||
12 | protected $id; |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $name; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $page; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $domain; |
||
28 | |||
29 | public function __construct(array $projectData = []) |
||
44 | |||
45 | /** |
||
46 | * @return bool |
||
47 | * @throws LPTrackerSDKException |
||
48 | */ |
||
49 | View Code Duplication | public function validate() |
|
|
|||
50 | { |
||
51 | if (empty($this->id)) { |
||
52 | throw new LPTrackerSDKException('Project ID is required'); |
||
53 | } |
||
54 | |||
55 | if (empty($this->name)) { |
||
56 | throw new LPTrackerSDKException('Project name is required'); |
||
57 | } |
||
58 | |||
59 | return true; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return array |
||
64 | */ |
||
65 | public function toArray() |
||
74 | |||
75 | /** |
||
76 | * @return int |
||
77 | */ |
||
78 | public function getId() |
||
82 | |||
83 | /** |
||
84 | * @return string |
||
85 | */ |
||
86 | public function getName() |
||
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getPage() |
||
98 | |||
99 | /** |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getDomain() |
||
106 | } |
||
107 |
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.