| Total Complexity | 42 |
| Total Lines | 199 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like UserFileReader 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.
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 UserFileReader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class UserFileReader |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * The constructor |
||
| 23 | * |
||
| 24 | * @param AuthInterface $auth |
||
| 25 | * @param string $configFile |
||
| 26 | */ |
||
| 27 | public function __construct(private AuthInterface $auth, private string $configFile) |
||
| 28 | {} |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string $value |
||
| 32 | * |
||
| 33 | * @return bool |
||
| 34 | */ |
||
| 35 | private function callsEnvVar(string $value): bool |
||
| 36 | { |
||
| 37 | return preg_match('/^env\(.*\)$/', $value) !== false; |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param array $server |
||
| 42 | * |
||
| 43 | * @return bool |
||
| 44 | */ |
||
| 45 | private function checkPortNumber(array $server): bool |
||
| 46 | { |
||
| 47 | if (!isset($server['port']) || is_numeric($server['port'])) { |
||
| 48 | return true; |
||
| 49 | } |
||
| 50 | if (!is_string($server['port'])) { |
||
| 51 | return false; |
||
| 52 | } |
||
| 53 | return $this->callsEnvVar($server['port']); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @param array $server |
||
| 58 | * |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | private function checkServer(array $server): bool |
||
| 62 | { |
||
| 63 | if (!isset($server['name']) || !isset($server['driver']) || |
||
| 64 | !is_string($server['name']) || !is_string($server['driver'])) { |
||
| 65 | return false; |
||
| 66 | } |
||
| 67 | if ($server['driver'] === 'sqlite') { |
||
| 68 | return isset($server['directory']) && is_string($server['directory']); |
||
| 69 | } |
||
| 70 | return isset($server['username']) && isset($server['password']) && |
||
| 71 | isset($server['host']) && is_string($server['username']) && |
||
| 72 | is_string($server['password']) && is_string($server['host']) && |
||
| 73 | $this->checkPortNumber($server); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param array $options |
||
| 78 | * |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | private function checkUser(array $options): bool |
||
| 82 | { |
||
| 83 | $user = $options['id']['user'] ?? null; |
||
| 84 | return is_string($user) && $this->auth->user() === $user; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param array $options |
||
| 89 | * |
||
| 90 | * @return bool |
||
| 91 | */ |
||
| 92 | private function checkUsers(array $options): bool |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param array $options |
||
| 100 | * |
||
| 101 | * @return bool |
||
| 102 | */ |
||
| 103 | private function checkRole(array $options): bool |
||
| 104 | { |
||
| 105 | $role = $options['id']['role'] ?? null; |
||
| 106 | return is_string($role) && $this->auth->role() === $role; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param array $options |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | private function checkRoles(array $options): bool |
||
| 115 | { |
||
| 116 | $roles = $options['id']['roles'] ?? null; |
||
| 117 | return is_array($roles) && in_array($this->auth->role(), $roles); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @param array $options |
||
| 122 | * |
||
| 123 | * @return bool |
||
| 124 | */ |
||
| 125 | private function userMatches(array $options): bool |
||
| 126 | { |
||
| 127 | return $this->checkUser($options) || $this->checkUsers($options) || |
||
| 128 | $this->checkRole($options) || $this->checkRoles($options); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $value |
||
| 133 | * |
||
| 134 | * @return mixed |
||
| 135 | */ |
||
| 136 | private function getOptionValue(string $value): mixed |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Replace options with values from the .env config. |
||
| 145 | * |
||
| 146 | * @param array $values |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | private function getOptionValues(array $values): array |
||
| 151 | { |
||
| 152 | // Filter the servers list on valid entries |
||
| 153 | $values['servers'] = array_filter($values['servers'] ?? [], |
||
| 154 | fn(array $server) => $this->checkServer($server)); |
||
| 155 | // The values in the server options are the names of the |
||
| 156 | // corresponding options in the .env file. |
||
| 157 | $values['servers'] = array_map(function(array $server) { |
||
| 158 | if ($server['driver'] === 'sqlite') { |
||
| 159 | return $server; |
||
| 160 | } |
||
| 161 | |||
| 162 | $server['host'] = $this->getOptionValue($server['host']); |
||
| 163 | $server['username'] = $this->getOptionValue($server['username']); |
||
| 164 | $server['password'] = $this->getOptionValue($server['password']); |
||
| 165 | if (isset($server['port']) && is_string($server['port'])) { |
||
| 166 | $server['port'] = $this->getOptionValue($server['port']); |
||
| 167 | } |
||
| 168 | return $server; |
||
| 169 | }, $values['servers'] ?? []); |
||
| 170 | |||
| 171 | return $values; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get the options for the authenticated user. |
||
| 176 | * |
||
| 177 | * @param array $defaultOptions |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function getOptions(array $defaultOptions): array |
||
| 218 | } |
||
| 219 | } |
||
| 220 |