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:
Complex classes like DotEnvStorage often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DotEnvStorage, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class DotEnvStorage extends \Dotenv\Loader implements Storage |
||
22 | { |
||
23 | use \Teto\Object\ReadOnly; |
||
24 | |||
25 | /** @var string|resource */ |
||
26 | private $file_path; |
||
27 | |||
28 | /** @var array */ |
||
29 | private $key_names = [ |
||
30 | 'app_name' => 'APP_NAME', |
||
31 | 'client_id' => 'CLIENT_ID', |
||
32 | 'client_secret' => 'CLIENT_SECRET', |
||
33 | 'username' => 'USERNAME', |
||
34 | 'password' => 'PASSWORD', |
||
35 | 'access_token' => 'ACCESS_TOKEN', |
||
36 | 'scope' => 'SCOPE', |
||
37 | 'created_at' => 'CREATED_AT', |
||
38 | ]; |
||
39 | |||
40 | /** @var bool */ |
||
41 | private $read_only = true; |
||
42 | |||
43 | /** @var array */ |
||
44 | private $save_values = []; |
||
45 | |||
46 | /** |
||
47 | * @param string $file_path |
||
48 | * @param array $options |
||
49 | * @throws \Dotenv\Exception\InvalidPathException |
||
50 | */ |
||
51 | 13 | public function __construct($file_path, $options = []) |
|
69 | |||
70 | /** |
||
71 | * @return array |
||
72 | */ |
||
73 | 5 | public function getAppName() |
|
83 | |||
84 | /** |
||
85 | * @param string $app_name |
||
86 | * @return void |
||
87 | */ |
||
88 | 7 | public function setAppName($app_name) |
|
93 | |||
94 | /** |
||
95 | * @return array |
||
96 | */ |
||
97 | public function getAuthorization() |
||
111 | |||
112 | /** |
||
113 | * @param string $access_token |
||
114 | * @param string|string[]|Scope $scope |
||
115 | * @param int $created_at |
||
116 | * @return void |
||
117 | */ |
||
118 | public function setAuthorization($access_token, $scope = null, $created_at = null) |
||
126 | |||
127 | /** |
||
128 | * @param Authorization $authorization |
||
129 | * @return void |
||
130 | */ |
||
131 | public function setAuthorizationFromObject(Authorization $authorization) |
||
132 | { |
||
133 | $this->setAuthorization( |
||
134 | $authorization->access_token, |
||
135 | $authorization->scope, |
||
136 | $authorization->created_at ? $authorization->created_at->getTimestamp() : time() |
||
137 | ); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @Return array |
||
142 | */ |
||
143 | 5 | View Code Duplication | public function getClientIdAndSecret() |
155 | |||
156 | /** |
||
157 | * @param string $client_id |
||
158 | * @param string $client_secret |
||
159 | */ |
||
160 | 3 | View Code Duplication | public function setClientIdAndSecret($client_id, $client_secret) |
167 | |||
168 | /** |
||
169 | * @return array |
||
170 | */ |
||
171 | 5 | View Code Duplication | public function getUsernameAndPassword() |
182 | |||
183 | /** |
||
184 | * @param string $username |
||
185 | * @param string $password |
||
186 | */ |
||
187 | 1 | View Code Duplication | public function setUsernameAndPassword($username, $password) |
194 | |||
195 | /** |
||
196 | * Returns all stored key-value pairs |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | 5 | public function getValues() |
|
213 | |||
214 | /** |
||
215 | * @param resource $fp |
||
216 | * @return string[] |
||
217 | */ |
||
218 | 13 | public function getLines($fp = null) |
|
230 | |||
231 | /** |
||
232 | * @return void |
||
233 | */ |
||
234 | 8 | public function save() |
|
288 | |||
289 | /** |
||
290 | * @param string $value |
||
291 | */ |
||
292 | 7 | public static function quoteValue($value) |
|
298 | } |
||
299 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..