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 |
||
| 13 | class Flysystem implements Adapter, ListKeysAware |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var AdapterInterface |
||
| 17 | */ |
||
| 18 | private $adapter; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var Config |
||
| 22 | */ |
||
| 23 | private $config; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @param AdapterInterface $adapter |
||
| 27 | * @param Config|array|null $config |
||
| 28 | */ |
||
| 29 | public function __construct(AdapterInterface $adapter, $config = null) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * {@inheritdoc} |
||
| 37 | */ |
||
| 38 | View Code Duplication | public function read($key) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | public function write($key, $content) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * {@inheritdoc} |
||
| 82 | */ |
||
| 83 | public function exists($key) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | public function keys() |
||
| 105 | |||
| 106 | /** |
||
| 107 | * {@inheritdoc} |
||
| 108 | */ |
||
| 109 | public function listKeys($prefix = '') |
||
| 110 | { |
||
| 111 | $dirs = []; |
||
| 112 | $keys = []; |
||
| 113 | |||
| 114 | try { |
||
| 115 | foreach ($this->adapter->listContents() as $content) { |
||
| 116 | if (empty($prefix) || 0 === strpos($content['path'], $prefix)) { |
||
| 117 | if ('dir' === $content['type']) { |
||
| 118 | $dirs[] = $content['path']; |
||
| 119 | } else { |
||
| 120 | $keys[] = $content['path']; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } catch (\Exception $e) { |
||
| 125 | throw StorageFailure::unexpectedFailure( |
||
| 126 | 'listKeys', |
||
| 127 | ['prefix' => $prefix], |
||
| 128 | $e |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | return [ |
||
| 133 | 'keys' => $keys, |
||
| 134 | 'dirs' => $dirs, |
||
| 135 | ]; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | */ |
||
| 141 | View Code Duplication | public function mtime($key) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | View Code Duplication | public function delete($key) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | public function rename($sourceKey, $targetKey) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * {@inheritdoc} |
||
| 209 | */ |
||
| 210 | public function isDirectory($key) |
||
| 214 | } |
||
| 215 |
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..