Complex classes like Modules 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Modules, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | final class Modules extends \SimpleSAML\Module\monitor\TestSuiteFactory |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private $requiredApacheModules = array(); |
||
15 | |||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | private $requiredPhpModules = array(); |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | // Important!! Modules-names are handled case-sensitive!! |
||
25 | private $storeApacheDependencies = array(); |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $storePhpDependencies = array( |
||
31 | 'memcache' => 'memcached|memcache', |
||
32 | 'phpsession' => 'session', |
||
33 | 'redis' => 'redis', |
||
34 | 'redissentinel' => 'redis', |
||
35 | 'riak:Store' => 'riak', |
||
36 | 'sql' => 'PDO' |
||
37 | ); |
||
38 | |||
39 | /** |
||
40 | * @var array |
||
41 | */ |
||
42 | private $moduleApacheDependencies = array( |
||
43 | 'negotiateext' => 'mod_auth_kerb|mod_auth_gssapi' |
||
44 | ); |
||
45 | |||
46 | /** |
||
47 | * @var array |
||
48 | */ |
||
49 | private $modulePhpDependencies = array( |
||
50 | 'authfacebook' => array('curl', 'json'), |
||
51 | 'authYubiKey' => 'curl', |
||
52 | // TODO: consent only requires pdo when database backend is used.. Should probably add this to required-list when processing metadata |
||
53 | // 'consent' => 'PDO', |
||
54 | 'consentAdmin' => 'PDO', |
||
55 | 'ldap' => 'ldap', |
||
56 | 'memcacheMonitor' => 'memcached|memcache', |
||
57 | 'negotiate' => 'krb5', |
||
58 | 'radius' => 'radius', |
||
59 | 'riak' => 'riak', |
||
60 | 'sqlauth' => 'PDO' |
||
61 | ); |
||
62 | |||
63 | /** |
||
64 | * @param TestConfiguration|null $configuration |
||
65 | */ |
||
66 | public function __construct($configuration = null) |
||
70 | |||
71 | /** |
||
72 | * @param TestData|null $testData |
||
73 | * |
||
74 | * @return void |
||
75 | */ |
||
76 | protected function initialize($testData = null) |
||
82 | |||
83 | /** |
||
84 | * @return void |
||
85 | */ |
||
86 | private function addRequiredApacheModule($module) |
||
92 | |||
93 | /** |
||
94 | * @return void |
||
95 | */ |
||
96 | private function addRequiredPhpModule($module) |
||
102 | |||
103 | /** |
||
104 | * @return void |
||
105 | */ |
||
106 | private function setRequiredApacheModules() |
||
124 | |||
125 | /** |
||
126 | * @return void |
||
127 | */ |
||
128 | private function setRequiredPhpModules() |
||
150 | |||
151 | /** |
||
152 | * @return void |
||
153 | */ |
||
154 | private function setRequiredSspModules() |
||
174 | |||
175 | /** |
||
176 | * @return array<string,array> |
||
177 | */ |
||
178 | public function getAvailableModules() |
||
179 | { |
||
180 | $configuration = $this->getConfiguration(); |
||
181 | return array( |
||
182 | 'Apache' => $configuration->getAvailableApacheModules(), |
||
183 | 'Php' => $configuration->getAvailablePhpModules() |
||
184 | ); |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @return array<string,array> |
||
189 | */ |
||
190 | private function getRequiredModules() |
||
194 | |||
195 | /** |
||
196 | * @return array<string,array> |
||
197 | */ |
||
198 | private function getModuleDependencies() |
||
202 | |||
203 | /** |
||
204 | * @return void |
||
205 | */ |
||
206 | protected function invokeTestSuite() |
||
207 | { |
||
208 | $availableModules = $this->getAvailableModules(); |
||
209 | $requiredModules = $this->getRequiredModules(); |
||
210 | $moduleDependencies = $this->getModuleDependencies(); |
||
211 | $output = array(); |
||
212 | |||
213 | // Test for the availability of required modules |
||
214 | foreach ($availableModules as $category => $available) { |
||
215 | if (is_null($available)) { |
||
216 | $output[$category][] = array(State::SKIPPED, $category, implode(', ', $requiredModules[$category]), 'Unable to verify installed modules'); |
||
217 | } else { |
||
218 | $dependencies = array_key_exists($category, $moduleDependencies) ? $moduleDependencies[$category] : array(); |
||
219 | $required = array_key_exists($category, $requiredModules) ? $requiredModules[$category] : array(); |
||
220 | $available = array_key_exists($category, $availableModules) ? $availableModules[$category] : array(); |
||
221 | |||
222 | foreach ($required as $require) { |
||
223 | $testData = new TestData( |
||
224 | array( |
||
225 | 'require' => $require, |
||
226 | 'available' => $available |
||
227 | ) |
||
228 | ); |
||
229 | $this->testRequirement($testData, $category, $dependencies, $output); |
||
230 | } |
||
231 | } |
||
232 | } |
||
233 | |||
234 | $this->processTestResults($output); |
||
235 | |||
236 | $this->calculateState(); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * @param array $output |
||
241 | * |
||
242 | * @return void |
||
243 | */ |
||
244 | protected function processTestResults($output) |
||
245 | { |
||
246 | $availableModules = $this->getAvailableModules(); |
||
247 | $tests = $this->getTests(); |
||
248 | |||
249 | foreach ($availableModules as $category => $available) { |
||
250 | $categories = array_fill(0, count($tests), $category); |
||
251 | if (!isSet($output[$category])) { |
||
252 | $modules = array_map( |
||
253 | function($test, $category) { |
||
254 | return ($test->getCategory() === $category) ? $test->getModuleName() : false; |
||
255 | }, $tests, $categories |
||
256 | ); |
||
257 | $modules = array_diff($modules, array(false)); |
||
258 | $output[$category][] = array(State::OK, $category, implode(', ', $modules), "All required modules are loaded"); |
||
259 | } |
||
260 | $this->addMessages($output[$category], $category); |
||
261 | } |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param TestData $testData |
||
266 | * @param string $category |
||
267 | * @param array $dependencies |
||
268 | * @param array $output |
||
269 | * |
||
270 | * @return void |
||
271 | */ |
||
272 | private function testRequirement($testData, $category, $dependencies, &$output) |
||
297 | } |
||
298 |