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 |
||
24 | class GuzzleHttpAdapter implements AdapterInterface |
||
25 | { |
||
26 | |||
27 | /** |
||
28 | * @var ClientInterface |
||
29 | */ |
||
30 | protected $client; |
||
31 | |||
32 | /** |
||
33 | * @var Response|ResponseInterface |
||
34 | */ |
||
35 | protected $response; |
||
36 | |||
37 | /** |
||
38 | * @param string $username |
||
39 | * @param string $password |
||
40 | * @param string $accept |
||
41 | * @param ClientInterface|null $client |
||
42 | */ |
||
43 | public function __construct($username, $password, $accept, ClientInterface $client = null) |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | View Code Duplication | public function get($url) |
|
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | View Code Duplication | public function delete($url) |
|
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | View Code Duplication | public function put($url, $content = '') |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | View Code Duplication | public function post($url, $content = '') |
|
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | View Code Duplication | public function getLatestResponseHeaders() |
|
133 | |||
134 | /** |
||
135 | * @throws HttpException |
||
136 | */ |
||
137 | View Code Duplication | protected function handleError() |
|
144 | |||
145 | } |
||
146 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.