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