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 |
||
9 | class Piwik |
||
10 | { |
||
11 | use Utils\HtmlInjectorTrait; |
||
12 | use Utils\AttributeTrait; |
||
13 | |||
14 | private $options = [ |
||
15 | ['trackPageView'], |
||
16 | ['enableLinkTracking'], |
||
17 | ]; |
||
18 | |||
19 | /** |
||
20 | * @var int|null The site's ID |
||
21 | */ |
||
22 | private $siteId = 1; |
||
23 | |||
24 | /** |
||
25 | * @var string|null The Piwik url |
||
26 | */ |
||
27 | private $piwikUrl; |
||
28 | |||
29 | /** |
||
30 | * Constructor.Set the Piwik's url. |
||
31 | * |
||
32 | * @param string $piwikUrl |
||
33 | */ |
||
34 | public function __construct($piwikUrl = null) |
||
40 | |||
41 | /** |
||
42 | * Set the site's id. |
||
43 | * |
||
44 | * @param int $siteId |
||
45 | * |
||
46 | * @return self |
||
47 | */ |
||
48 | public function siteId($siteId) |
||
54 | |||
55 | /** |
||
56 | * Set the piwik url. |
||
57 | * |
||
58 | * @param string $url |
||
59 | * |
||
60 | * @return self |
||
61 | */ |
||
62 | public function piwikUrl($url) |
||
73 | |||
74 | /** |
||
75 | * Add an option. |
||
76 | * |
||
77 | * ... |
||
78 | * |
||
79 | * @return self |
||
80 | */ |
||
81 | public function addOption() |
||
87 | |||
88 | /** |
||
89 | * Execute the middleware. |
||
90 | * |
||
91 | * @param ServerRequestInterface $request |
||
92 | * @param ResponseInterface $response |
||
93 | * @param callable $next |
||
94 | * |
||
95 | * @return ResponseInterface |
||
96 | */ |
||
97 | View Code Duplication | public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
|
98 | { |
||
99 | $response = $next($request, $response); |
||
100 | |||
101 | if (Utils\Helpers::getMimeType($response) === 'text/html' && !Utils\Helpers::isAjax($request)) { |
||
102 | return $this->inject($response, $this->getCode()); |
||
103 | } |
||
104 | |||
105 | return $response; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Returns the piwik code. |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | private function getCode() |
||
137 | } |
||
138 |