Complex classes like Checker 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 Checker, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | * @author bootjp |
||
11 | */ |
||
12 | class Checker |
||
13 | { |
||
14 | protected $client; |
||
15 | |||
16 | protected $contentsSize = 500; |
||
17 | |||
18 | protected $doubleCheck = true; |
||
19 | |||
20 | protected $recursion = false; |
||
21 | |||
22 | protected $garbage = []; |
||
23 | |||
24 | protected $isContentsFetch = true; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * initialisation. |
||
29 | * @param array $args |
||
30 | */ |
||
31 | public function __construct(array $args) |
||
65 | |||
66 | /** |
||
67 | * Wrapper |
||
68 | * @param mixed $url [require] |
||
69 | * @return array |
||
70 | * @throws \ErrorException |
||
71 | * @throws \ReflectionException |
||
72 | */ |
||
73 | public function start($url) |
||
126 | |||
127 | /** |
||
128 | * Fetch Page Contents Links |
||
129 | * @param mixed $baseUrl |
||
130 | * @return array URlList |
||
131 | * @throws \ErrorException |
||
132 | */ |
||
133 | private function fetchByContents($baseUrl) |
||
172 | |||
173 | /** |
||
174 | * Error check by header |
||
175 | * @param \GuzzleHttp\Message\Response $metaData |
||
176 | * @return array |
||
177 | */ |
||
178 | private function hardCheckByHeader(\GuzzleHttp\Message\Response $metaData) |
||
215 | |||
216 | /** |
||
217 | * Soft404 check by contents Length |
||
218 | * @param \GuzzleHttp\Message\Response $metaData |
||
219 | * @return array |
||
220 | */ |
||
221 | public function softCheckByContents(\GuzzleHttp\Message\Response $metaData) |
||
244 | |||
245 | /** |
||
246 | * Soft404 Error check by words |
||
247 | * @param \GuzzleHttp\Message\Response $metaData |
||
248 | * @return array Result |
||
249 | */ |
||
250 | private function softCheckByContentsWords(\GuzzleHttp\Message\Response $metaData) |
||
266 | |||
267 | /** |
||
268 | * Return soft404 Page on Words. |
||
269 | * @param none |
||
270 | * @return array |
||
271 | */ |
||
272 | private static function getSoftErrorWords() |
||
276 | |||
277 | /** |
||
278 | * multidimensional array to single arry comvert. |
||
279 | * @param array $urlList |
||
280 | * @return array URLLIST |
||
281 | */ |
||
282 | private function urlFilter(array $urlList) |
||
283 | { |
||
284 | $result = []; |
||
285 | array_walk_recursive($urlList, function($v) use (&$result) { |
||
286 | $result[] = $v; |
||
287 | }); |
||
288 | |||
289 | return array_values(array_unique($result)); |
||
290 | } |
||
292 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.