| Total Complexity | 43 |
| Total Lines | 220 |
| Duplicated Lines | 0 % |
| Changes | 8 | ||
| 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 | * @var string |
||
| 23 | */ |
||
| 24 | private string $compareRegex = '/^env\(.*\)$/'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private string $captureRegex = '/^env\((.*)\)$/'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The constructor |
||
| 33 | * |
||
| 34 | * @param AuthInterface $auth |
||
| 35 | */ |
||
| 36 | public function __construct(private AuthInterface $auth) |
||
| 37 | {} |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param string $value |
||
| 41 | * |
||
| 42 | * @return bool |
||
| 43 | */ |
||
| 44 | private function callsEnvVar(string $value): bool |
||
| 45 | { |
||
| 46 | return preg_match($this->compareRegex, $value) !== false; |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param array $server |
||
| 51 | * |
||
| 52 | * @return bool |
||
| 53 | */ |
||
| 54 | private function checkPortNumber(array $server): bool |
||
| 55 | { |
||
| 56 | if (!isset($server['port']) || is_numeric($server['port'])) { |
||
| 57 | return true; |
||
| 58 | } |
||
| 59 | if (!is_string($server['port'])) { |
||
| 60 | return false; |
||
| 61 | } |
||
| 62 | return $this->callsEnvVar($server['port']); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param array $server |
||
| 67 | * |
||
| 68 | * @return bool |
||
| 69 | */ |
||
| 70 | private function checkServer(array $server): bool |
||
| 71 | { |
||
| 72 | if (!isset($server['name']) || !isset($server['driver']) || |
||
| 73 | !is_string($server['name']) || !is_string($server['driver'])) { |
||
| 74 | return false; |
||
| 75 | } |
||
| 76 | if ($server['driver'] === 'sqlite') { |
||
| 77 | return isset($server['directory']) && is_string($server['directory']); |
||
| 78 | } |
||
| 79 | return isset($server['username']) && isset($server['password']) && |
||
| 80 | isset($server['host']) && is_string($server['username']) && |
||
| 81 | is_string($server['password']) && is_string($server['host']) && |
||
| 82 | $this->checkPortNumber($server); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param array $options |
||
| 87 | * |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | private function checkUser(array $options): bool |
||
| 91 | { |
||
| 92 | $user = $options['id']['user'] ?? null; |
||
| 93 | return is_string($user) && $this->auth->user() === $user; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param array $options |
||
| 98 | * |
||
| 99 | * @return bool |
||
| 100 | */ |
||
| 101 | private function checkUsers(array $options): bool |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param array $options |
||
| 109 | * |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | private function checkRole(array $options): bool |
||
| 113 | { |
||
| 114 | $role = $options['id']['role'] ?? null; |
||
| 115 | return is_string($role) && $this->auth->role() === $role; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param array $options |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | private function checkRoles(array $options): bool |
||
| 124 | { |
||
| 125 | $roles = $options['id']['roles'] ?? null; |
||
| 126 | return is_array($roles) && in_array($this->auth->role(), $roles); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param array $options |
||
| 131 | * |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | private function userMatches(array $options): bool |
||
| 135 | { |
||
| 136 | return $this->checkUser($options) || $this->checkUsers($options) || |
||
| 137 | $this->checkRole($options) || $this->checkRoles($options); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param string $value |
||
| 142 | * |
||
| 143 | * @return mixed |
||
| 144 | */ |
||
| 145 | private function getOptionValue(string $value): mixed |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param array $server |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public function getServerOptions(array $server): array |
||
| 158 | { |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Replace options with values from the .env config. |
||
| 175 | * |
||
| 176 | * @param array $values |
||
| 177 | * |
||
| 178 | * @return array |
||
| 179 | */ |
||
| 180 | private function getOptionValues(array $values): array |
||
| 181 | { |
||
| 182 | // Callback to filter the servers list on valid entries. |
||
| 183 | $check = fn(array $server) => $this->checkServer($server); |
||
| 184 | // Callback to get the server options final values. |
||
| 185 | $convert = fn(array $server) => $this->getServerOptions($server); |
||
| 186 | $values['servers'] = array_map($convert, |
||
| 187 | array_filter($values['servers'] ?? [], $check)); |
||
| 188 | |||
| 189 | return $values; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get the options for the authenticated user. |
||
| 194 | * |
||
| 195 | * @param string $configFile |
||
| 196 | * @param array $defaultOptions |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | public function getOptions(string $configFile, array $defaultOptions = []): array |
||
| 239 | } |
||
| 240 | } |
||
| 241 |