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 |
||
23 | class BuzzAdapter implements AdapterInterface |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var Browser |
||
28 | */ |
||
29 | protected $browser; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $username; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $password; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $accept; |
||
45 | |||
46 | /** |
||
47 | * @param string $username |
||
48 | * @param string $password |
||
49 | * @param string $accept |
||
50 | * @param Browser|null $browser |
||
51 | */ |
||
52 | public function __construct($username, $password, $accept, Browser $browser = null) |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function get($url) |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | public function delete($url) |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | View Code Duplication | public function put($url, $content = '') |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | View Code Duplication | public function post($url, $content = '') |
|
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function getLatestResponseHeaders() |
||
136 | |||
137 | /** |
||
138 | * @param Response $response |
||
139 | * |
||
140 | * @throws HttpException |
||
141 | */ |
||
142 | protected function handleResponse(Response $response) |
||
149 | |||
150 | /** |
||
151 | * @param Response $response |
||
152 | * |
||
153 | * @throws HttpException |
||
154 | */ |
||
155 | protected function handleError(Response $response) |
||
162 | } |
||
163 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.