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 |
||
| 41 | class SMB extends Common { |
||
| 42 | /** |
||
| 43 | * @var \Icewind\SMB\Server |
||
| 44 | */ |
||
| 45 | protected $server; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Icewind\SMB\Share |
||
| 49 | */ |
||
| 50 | protected $share; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $root; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var \Icewind\SMB\FileInfo[] |
||
| 59 | */ |
||
| 60 | protected $statCache; |
||
| 61 | |||
| 62 | public function __construct($params) { |
||
| 63 | if (isset($params['host']) && isset($params['user']) && isset($params['password']) && isset($params['share'])) { |
||
| 64 | if (Server::NativeAvailable()) { |
||
| 65 | $this->server = new NativeServer($params['host'], $params['user'], $params['password']); |
||
|
|
|||
| 66 | } else { |
||
| 67 | $this->server = new Server($params['host'], $params['user'], $params['password']); |
||
| 68 | } |
||
| 69 | $this->share = $this->server->getShare(trim($params['share'], '/')); |
||
| 70 | |||
| 71 | $this->root = isset($params['root']) ? $params['root'] : '/'; |
||
| 72 | View Code Duplication | if (!$this->root || $this->root[0] != '/') { |
|
| 73 | $this->root = '/' . $this->root; |
||
| 74 | } |
||
| 75 | if (substr($this->root, -1, 1) != '/') { |
||
| 76 | $this->root .= '/'; |
||
| 77 | } |
||
| 78 | } else { |
||
| 79 | throw new \Exception('Invalid configuration'); |
||
| 80 | } |
||
| 81 | $this->statCache = new CappedMemoryCache(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public function getId() { |
||
| 88 | // FIXME: double slash to keep compatible with the old storage ids, |
||
| 89 | // failure to do so will lead to creation of a new storage id and |
||
| 90 | // loss of shares from the storage |
||
| 91 | return 'smb::' . $this->server->getUser() . '@' . $this->server->getHost() . '//' . $this->share->getName() . '/' . $this->root; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $path |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | protected function buildPath($path) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $path |
||
| 104 | * @return \Icewind\SMB\IFileInfo |
||
| 105 | */ |
||
| 106 | protected function getFileInfo($path) { |
||
| 107 | $path = $this->buildPath($path); |
||
| 108 | if (!isset($this->statCache[$path])) { |
||
| 109 | $this->statCache[$path] = $this->share->stat($path); |
||
| 110 | } |
||
| 111 | return $this->statCache[$path]; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $path |
||
| 116 | * @return \Icewind\SMB\IFileInfo[] |
||
| 117 | */ |
||
| 118 | protected function getFolderContents($path) { |
||
| 119 | $path = $this->buildPath($path); |
||
| 120 | $files = $this->share->dir($path); |
||
| 121 | foreach ($files as $file) { |
||
| 122 | $this->statCache[$path . '/' . $file->getName()] = $file; |
||
| 123 | } |
||
| 124 | return $files; |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param \Icewind\SMB\IFileInfo $info |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | protected function formatInfo($info) { |
||
| 132 | return array( |
||
| 133 | 'size' => $info->getSize(), |
||
| 134 | 'mtime' => $info->getMTime() |
||
| 135 | ); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string $path |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | public function stat($path) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @param string $path |
||
| 148 | * @return bool |
||
| 149 | */ |
||
| 150 | public function unlink($path) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * check if a file or folder has been updated since $time |
||
| 167 | * |
||
| 168 | * @param string $path |
||
| 169 | * @param int $time |
||
| 170 | * @return bool |
||
| 171 | */ |
||
| 172 | public function hasUpdated($path, $time) { |
||
| 182 | |||
| 183 | /** |
||
| 184 | * @param string $path |
||
| 185 | * @param string $mode |
||
| 186 | * @return resource |
||
| 187 | */ |
||
| 188 | public function fopen($path, $mode) { |
||
| 240 | |||
| 241 | public function rmdir($path) { |
||
| 258 | |||
| 259 | public function touch($path, $time = null) { |
||
| 267 | |||
| 268 | public function opendir($path) { |
||
| 276 | |||
| 277 | public function filetype($path) { |
||
| 284 | |||
| 285 | public function mkdir($path) { |
||
| 294 | |||
| 295 | public function file_exists($path) { |
||
| 303 | |||
| 304 | /** |
||
| 305 | * check if smbclient is installed |
||
| 306 | */ |
||
| 307 | public static function checkDependencies() { |
||
| 313 | } |
||
| 314 |
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..