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 |
||
| 33 | class Quota extends Wrapper { |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int $quota |
||
| 37 | */ |
||
| 38 | protected $quota; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string $sizeRoot |
||
| 42 | */ |
||
| 43 | protected $sizeRoot; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param array $parameters |
||
| 47 | */ |
||
| 48 | public function __construct($parameters) { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @return int quota value |
||
| 56 | */ |
||
| 57 | public function getQuota() { |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $path |
||
| 63 | * @param \OC\Files\Storage\Storage $storage |
||
| 64 | */ |
||
| 65 | protected function getSize($path, $storage = null) { |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get free space as limited by the quota |
||
| 81 | * |
||
| 82 | * @param string $path |
||
| 83 | * @return int |
||
| 84 | */ |
||
| 85 | public function free_space($path) { |
||
| 86 | if ($this->quota < 0 || strpos($path, 'cache') === 0) { |
||
| 87 | return $this->storage->free_space($path); |
||
| 88 | } else { |
||
| 89 | $used = $this->getSize($this->sizeRoot); |
||
| 90 | if ($used < 0) { |
||
| 91 | return \OCP\Files\FileInfo::SPACE_NOT_COMPUTED; |
||
| 92 | } else { |
||
| 93 | $free = $this->storage->free_space($path); |
||
| 94 | $quotaFree = max($this->quota - $used, 0); |
||
| 95 | // if free space is known |
||
| 96 | if ($free >= 0) { |
||
| 97 | $free = min($free, $quotaFree); |
||
| 98 | } else { |
||
| 99 | $free = $quotaFree; |
||
| 100 | } |
||
| 101 | return $free; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * see http://php.net/manual/en/function.file_put_contents.php |
||
| 108 | * |
||
| 109 | * @param string $path |
||
| 110 | * @param string $data |
||
| 111 | * @return bool |
||
| 112 | */ |
||
| 113 | public function file_put_contents($path, $data) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * see http://php.net/manual/en/function.copy.php |
||
| 124 | * |
||
| 125 | * @param string $source |
||
| 126 | * @param string $target |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | View Code Duplication | public function copy($source, $target) { |
|
| 137 | |||
| 138 | /** |
||
| 139 | * see http://php.net/manual/en/function.fopen.php |
||
| 140 | * |
||
| 141 | * @param string $path |
||
| 142 | * @param string $mode |
||
| 143 | * @return resource |
||
| 144 | */ |
||
| 145 | public function fopen($path, $mode) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Checks whether the given path is a part file |
||
| 163 | * |
||
| 164 | * @param string $path Path that may identify a .part file |
||
| 165 | * @return string File path without .part extension |
||
| 166 | * @note this is needed for reusing keys |
||
| 167 | */ |
||
| 168 | private function isPartFile($path) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param IStorage $sourceStorage |
||
| 176 | * @param string $sourceInternalPath |
||
| 177 | * @param string $targetInternalPath |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function copyFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 188 | |||
| 189 | /** |
||
| 190 | * @param IStorage $sourceStorage |
||
| 191 | * @param string $sourceInternalPath |
||
| 192 | * @param string $targetInternalPath |
||
| 193 | * @return bool |
||
| 194 | */ |
||
| 195 | View Code Duplication | public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) { |
|
| 203 | |||
| 204 | public function mkdir($path) { |
||
| 211 | } |
||
| 212 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.