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 |
||
10 | class GoogleAnalytics |
||
11 | { |
||
12 | use Utils\HtmlInjectorTrait; |
||
13 | |||
14 | /** |
||
15 | * @var string|null The site's ID |
||
16 | */ |
||
17 | private $siteId; |
||
18 | |||
19 | /** |
||
20 | * Constructor.Set the site's ID. |
||
21 | * |
||
22 | * @param string $id |
||
|
|||
23 | */ |
||
24 | public function __construct($siteId = null) |
||
30 | |||
31 | /** |
||
32 | * Set the site's id. |
||
33 | * |
||
34 | * @param string $siteId |
||
35 | * |
||
36 | * @return self |
||
37 | */ |
||
38 | public function siteId($siteId) |
||
44 | |||
45 | /** |
||
46 | * Execute the middleware. |
||
47 | * |
||
48 | * @param ServerRequestInterface $request |
||
49 | * @param ResponseInterface $response |
||
50 | * @param callable $next |
||
51 | * |
||
52 | * @return ResponseInterface |
||
53 | */ |
||
54 | View Code Duplication | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
64 | |||
65 | /** |
||
66 | * Returns the google code. |
||
67 | * https://github.com/h5bp/html5-boilerplate/blob/master/src/index.html. |
||
68 | * |
||
69 | * @return string |
||
70 | */ |
||
71 | private function getCode() |
||
84 | } |
||
85 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.