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 |
||
| 10 | class PermissionsProvider extends AbstractProvider |
||
| 11 | { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Registers services on the given app. |
||
| 15 | * |
||
| 16 | * @param Application $app An Application instance |
||
| 17 | */ |
||
| 18 | public function register(Application $app) |
||
| 19 | { |
||
| 20 | $app['permission'] = $this; |
||
| 21 | $this->app = $app; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param string $groupName The group name |
||
| 26 | */ |
||
| 27 | public function createGroupIfNeeded($groupName) |
||
| 28 | { |
||
| 29 | if (PHP_OS == "Darwin") { |
||
| 30 | $this->processProvider->executeSudoCommand('dscl . create /groups/' . $groupName); |
||
| 31 | $this->processProvider->executeSudoCommand('dscl . create /groups/' . $groupName . ' RealName ' . $groupName); |
||
| 32 | $this->processProvider->executeSudoCommand('dscl . create /groups/' . $groupName . " name " . $groupName); |
||
| 33 | $this->processProvider->executeSudoCommand('dscl . create /groups/' . $groupName . ' passwd "*"'); |
||
| 34 | $this->processProvider->executeSudoCommand('dscl . create /groups/' . $groupName . ' PrimaryGroupID 20'); |
||
| 35 | } else { |
||
| 36 | if (!$this->isGroup($groupName)) { |
||
|
|
|||
| 37 | $this->processProvider->executeSudoCommand('addgroup ' . $groupName); |
||
| 38 | } |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param string $groupName The group name |
||
| 44 | * |
||
| 45 | * @return bool|string |
||
| 46 | */ |
||
| 47 | private function isGroup($groupName) |
||
| 48 | { |
||
| 49 | if (PHP_OS == "Darwin") { |
||
| 50 | return $this->processProvider->executeSudoCommand('dscl . -list /groups | grep ^' . $groupName . '$', true); |
||
| 51 | } else { |
||
| 52 | return $this->processProvider->executeSudoCommand('cat /etc/group | egrep ^' . $groupName . ':', true); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param string $userName The user name |
||
| 58 | * @param string $groupName The group name |
||
| 59 | */ |
||
| 60 | public function createUserIfNeeded($userName, $groupName) |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param string $userName The user name |
||
| 85 | * |
||
| 86 | * @return mixed |
||
| 87 | */ |
||
| 88 | private function isUser($userName) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param \ArrayObject $project The project |
||
| 95 | */ |
||
| 96 | public function applyOwnership(\ArrayObject $project) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param \ArrayObject $project The project |
||
| 126 | */ |
||
| 127 | public function applyPermissions(\ArrayObject $project) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $userName The user name |
||
| 164 | */ |
||
| 165 | public function killProcesses($userName) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $userName The user name |
||
| 172 | * @param string $groupName The group name |
||
| 173 | */ |
||
| 174 | public function removeUser($userName, $groupName) |
||
| 185 | } |
||
| 186 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: