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 SMB 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 SMB, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class SMB extends Common { |
||
43 | /** |
||
44 | * @var \Icewind\SMB\Server |
||
45 | */ |
||
46 | protected $server; |
||
47 | |||
48 | /** |
||
49 | * @var \Icewind\SMB\Share |
||
50 | */ |
||
51 | protected $share; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $root; |
||
57 | |||
58 | /** |
||
59 | * @var \Icewind\SMB\FileInfo[] |
||
60 | */ |
||
61 | protected $statCache; |
||
62 | |||
63 | public function __construct($params) { |
||
64 | if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
||
65 | if (Server::NativeAvailable()) { |
||
66 | $this->server = new NativeServer($params['host'], $params['user'], $params['password']); |
||
|
|||
67 | } else { |
||
68 | $this->server = new Server($params['host'], $params['user'], $params['password']); |
||
69 | } |
||
70 | $this->share = $this->server->getShare(trim($params['share'], '/')); |
||
71 | |||
72 | $this->root = isset($params['root']) ? $params['root'] : '/'; |
||
73 | View Code Duplication | if (!$this->root || $this->root[0] != '/') { |
|
74 | $this->root = '/' . $this->root; |
||
75 | } |
||
76 | if (substr($this->root, -1, 1) != '/') { |
||
77 | $this->root .= '/'; |
||
78 | } |
||
79 | } else { |
||
80 | throw new \Exception('Invalid configuration'); |
||
81 | } |
||
82 | $this->statCache = new CappedMemoryCache(); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getId() { |
||
94 | |||
95 | /** |
||
96 | * @param string $path |
||
97 | * @return string |
||
98 | */ |
||
99 | protected function buildPath($path) { |
||
102 | |||
103 | /** |
||
104 | * @param string $path |
||
105 | * @return \Icewind\SMB\IFileInfo |
||
106 | */ |
||
107 | protected function getFileInfo($path) { |
||
114 | |||
115 | /** |
||
116 | * @param string $path |
||
117 | * @return \Icewind\SMB\IFileInfo[] |
||
118 | */ |
||
119 | protected function getFolderContents($path) { |
||
127 | |||
128 | /** |
||
129 | * @param \Icewind\SMB\IFileInfo $info |
||
130 | * @return array |
||
131 | */ |
||
132 | protected function formatInfo($info) { |
||
138 | |||
139 | /** |
||
140 | * @param string $path |
||
141 | * @return array |
||
142 | */ |
||
143 | public function stat($path) { |
||
146 | |||
147 | /** |
||
148 | * @param string $path |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function unlink($path) { |
||
167 | |||
168 | /** |
||
169 | * check if a file or folder has been updated since $time |
||
170 | * |
||
171 | * @param string $path |
||
172 | * @param int $time |
||
173 | * @return bool |
||
174 | */ |
||
175 | public function hasUpdated($path, $time) { |
||
185 | |||
186 | /** |
||
187 | * @param string $path |
||
188 | * @param string $mode |
||
189 | * @return resource |
||
190 | */ |
||
191 | public function fopen($path, $mode) { |
||
245 | |||
246 | public function rmdir($path) { |
||
265 | |||
266 | public function touch($path, $time = null) { |
||
274 | |||
275 | public function opendir($path) { |
||
289 | |||
290 | View Code Duplication | public function filetype($path) { |
|
299 | |||
300 | public function mkdir($path) { |
||
309 | |||
310 | View Code Duplication | public function file_exists($path) { |
|
320 | |||
321 | /** |
||
322 | * check if smbclient is installed |
||
323 | */ |
||
324 | public static function checkDependencies() { |
||
330 | } |
||
331 |
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..