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 |
||
12 | class MarathonApiClient implements ApiClientInterface |
||
13 | { |
||
14 | |||
15 | /** @var HttpClientInterface */ |
||
16 | private $oHttpClient; |
||
17 | |||
18 | 9 | public function __construct( |
|
24 | |||
25 | /** |
||
26 | * @link: https://mesos.github.io/chronos/docs/api.html#listing-jobs |
||
27 | * @return array |
||
28 | */ |
||
29 | 1 | public function listingJobs() |
|
39 | |||
40 | /** |
||
41 | * @param JobEntityInterface $oJobEntity |
||
42 | * @return bool |
||
43 | */ |
||
44 | 1 | public function addingJob(JobEntityInterface $oJobEntity) |
|
51 | |||
52 | /** |
||
53 | * @param JobEntityInterface|ChronosJobEntity $oJobEntity |
||
54 | * @return bool |
||
55 | */ |
||
56 | 1 | public function updatingJob(JobEntityInterface $oJobEntity) |
|
64 | |||
65 | /** |
||
66 | * @param string $sJobName |
||
67 | * @return bool |
||
68 | */ |
||
69 | 1 | public function removeJob($sJobName) |
|
76 | |||
77 | /** |
||
78 | * @param string $sJobName |
||
79 | * @return array |
||
80 | */ |
||
81 | public function getJobStats($sJobName) |
||
85 | |||
86 | /** |
||
87 | * Returns true if the client can be connected to. |
||
88 | * @return bool |
||
89 | */ |
||
90 | 5 | View Code Duplication | public function ping() |
106 | } |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.