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 File 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 File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class File extends \yii\base\Object |
||
23 | { |
||
24 | /** |
||
25 | * @var Goal |
||
26 | */ |
||
27 | public $goal; |
||
28 | |||
29 | /** |
||
30 | * @var file hanler: renderer and parser |
||
31 | */ |
||
32 | protected $_handler; |
||
33 | |||
34 | /** |
||
35 | * @var string absolute path |
||
36 | */ |
||
37 | protected $_path; |
||
38 | |||
39 | /** |
||
40 | * @var string directory |
||
41 | */ |
||
42 | protected $_dirname; |
||
43 | |||
44 | /** |
||
45 | * @var string name with extension |
||
46 | */ |
||
47 | protected $_basename; |
||
48 | |||
49 | /** |
||
50 | * @var string name only, without extension |
||
51 | */ |
||
52 | protected $_filename; |
||
53 | |||
54 | /** |
||
55 | * @var string extension |
||
56 | */ |
||
57 | protected $_extension; |
||
58 | |||
59 | /** |
||
60 | * @var array file stat |
||
61 | */ |
||
62 | protected $_stat; |
||
63 | |||
64 | /** |
||
65 | * @var array possible types |
||
66 | */ |
||
67 | public $types = []; |
||
68 | |||
69 | /** |
||
70 | * @var string type |
||
71 | */ |
||
72 | public $type; |
||
73 | |||
74 | /** |
||
75 | * @var string template |
||
76 | */ |
||
77 | public $template; |
||
78 | |||
79 | /** |
||
80 | * @var mixed data |
||
81 | */ |
||
82 | protected $data; |
||
83 | |||
84 | /** |
||
85 | * @var string path to minimal example file |
||
86 | */ |
||
87 | public $minimal; |
||
88 | |||
89 | /** |
||
90 | * Create file object. |
||
91 | * @param string|array $path or config |
||
92 | * @return File |
||
93 | */ |
||
94 | public static function create($path) |
||
95 | { |
||
96 | $config = is_array($path) ? $path : compact('path'); |
||
97 | $config['class'] = get_called_class(); |
||
98 | |||
99 | return Yii::createObject($config); |
||
100 | } |
||
101 | |||
102 | public function getMinimalPath() |
||
106 | |||
107 | /** |
||
108 | * @var array type to extension correspondance |
||
109 | */ |
||
110 | protected static $_extension2type = [ |
||
111 | 'json' => 'json', |
||
112 | 'yml' => 'yaml', /// first one is preferred |
||
113 | 'yaml' => 'yaml', |
||
114 | 'xml' => 'xml', |
||
115 | 'xml.dist' => 'xml', |
||
116 | ]; |
||
117 | |||
118 | public function getExtensionByType($type) |
||
131 | |||
132 | public function getTypeByExtension($extension) |
||
136 | |||
137 | public function findType() |
||
141 | |||
142 | public function setPath($path) |
||
153 | |||
154 | public function getPath() |
||
158 | |||
159 | public function setBasename($basename) |
||
163 | |||
164 | public function getBasename() |
||
168 | |||
169 | public function setDirname($dirname) |
||
173 | |||
174 | public function getDirname() |
||
178 | |||
179 | public function setFilename($filename) |
||
183 | |||
184 | public function getFilename() |
||
188 | |||
189 | public function setExtension($extension) |
||
193 | |||
194 | public function getExtension() |
||
198 | |||
199 | public function getCtype() |
||
203 | |||
204 | /** |
||
205 | * Save file. |
||
206 | * @param mixed $data |
||
207 | * @return bool true if file was changed |
||
208 | */ |
||
209 | public function save($data = null) |
||
217 | |||
218 | public function write($content) |
||
222 | |||
223 | public function load() |
||
227 | |||
228 | public function read() |
||
232 | |||
233 | public function readArray() |
||
237 | |||
238 | public function getHandler() |
||
250 | |||
251 | public static function file_exists($path) |
||
255 | |||
256 | public function exists() |
||
260 | |||
261 | public function find(array $types = []) |
||
279 | |||
280 | public function get($name) |
||
284 | |||
285 | public function getStat($field = null) |
||
293 | |||
294 | public function getUid() |
||
298 | |||
299 | View Code Duplication | public function getOwner() |
|
307 | |||
308 | public function getGid() |
||
312 | |||
313 | View Code Duplication | public function getGroup() |
|
321 | |||
322 | public function getPermissions() |
||
326 | |||
327 | public static function formatOctal($value) |
||
331 | |||
332 | public function chmod($value) |
||
342 | |||
343 | public function chown($value) |
||
353 | |||
354 | public function chgrp($value) |
||
363 | } |
||
364 |
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.