Complex classes like Base_Site_Model 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 Base_Site_Model, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); defined('BASEPATH') OR exit('No direct script access allowed'); |
||
28 | abstract class Base_Site_Model extends CI_Model { |
||
29 | public $site = ''; |
||
30 | public $titleFormat = '//'; |
||
31 | public $chapterFormat = '//'; |
||
32 | |||
33 | /** |
||
34 | * 0: No custom updater. |
||
35 | * 1: Uses following page. |
||
36 | * 2: Uses latest releases page. |
||
37 | */ |
||
38 | public $customType = 0; |
||
39 | |||
40 | 16 | public function __construct() { |
|
47 | |||
48 | /** |
||
49 | * Generates URL to the title page of the requested series. |
||
50 | * |
||
51 | * NOTE: In some cases, we are required to store more data in the title_string than is needed to generate the URL. (Namely as the title_string is our unique identifier for that series) |
||
52 | * When storing additional data, we use ':--:' as a delimiter to separate the data. Make sure to handle this as needed. |
||
53 | * |
||
54 | * Example: |
||
55 | * return "http://mangafox.me/manga/{$title_url}/"; |
||
56 | * |
||
57 | * Example (with extra data): |
||
58 | * $title_parts = explode(':--:', title_url); |
||
59 | * return "https://bato.to/comic/_/comics/-r".$title_parts[0]; |
||
60 | * |
||
61 | * @param string $title_url |
||
62 | * @return string |
||
63 | */ |
||
64 | abstract public function getFullTitleURL(string $title_url) : string; |
||
65 | |||
66 | /** |
||
67 | * Generates chapter data from given $title_url and $chapter. |
||
68 | * |
||
69 | * Chapter must be in a (v[0-9]+/)?c[0-9]+(\..+)? format. |
||
70 | * |
||
71 | * NOTE: In some cases, we are required to store the chapter number, and the segment required to generate the chapter URL separately. |
||
72 | * Much like when generating the title URL, we use ':--:' as a delimiter to separate the data. Make sure to handle this as needed. |
||
73 | * |
||
74 | * Example: |
||
75 | * return [ |
||
76 | * 'url' => $this->getFullTitleURL($title_url).'/'.$chapter, |
||
77 | * 'number' => "c{$chapter}" |
||
78 | * ]; |
||
79 | * |
||
80 | * @param string $title_url |
||
81 | * @param string $chapter |
||
82 | * @return array [url, number] |
||
83 | */ |
||
84 | abstract public function getChapterData(string $title_url, string $chapter) : array; |
||
85 | |||
86 | /** |
||
87 | * Used to get the latest chapter of given $title_url. |
||
88 | * |
||
89 | * This <should> utilize both get_content and parseTitleDataDOM functions when possible, as these can both reduce a lot of the code required to set this up. |
||
90 | * |
||
91 | * $titleData params must be set accordingly: |
||
92 | * * `title` should always be used with html_entity_decode. |
||
93 | * * `latest_chapter` must match $this->chapterFormat. |
||
94 | * * `last_updated` should always be in date("Y-m-d H:i:s") format. |
||
95 | * * `followed` should never be set within via getTitleData, with the exception of via a array_merge with doCustomFollow. |
||
96 | * |
||
97 | * $firstGet is set to true when the series is first added to the DB, and is used to follow the series on given site (if possible). |
||
98 | * |
||
99 | * @param string $title_url |
||
100 | * @param bool $firstGet |
||
101 | * @return array|null [title,latest_chapter,last_updated,followed?] |
||
102 | */ |
||
103 | abstract public function getTitleData(string $title_url, bool $firstGet = FALSE) : ?array; |
||
104 | |||
105 | /** |
||
106 | * Validates given $title_url against titleFormat. |
||
107 | * |
||
108 | * Failure to match against titleFormat will stop the series from being added to the DB. |
||
109 | * |
||
110 | * @param string $title_url |
||
111 | * @return bool |
||
112 | */ |
||
113 | 2 | final public function isValidTitleURL(string $title_url) : bool { |
|
118 | |||
119 | /** |
||
120 | * Validates given $chapter against chapterFormat. |
||
121 | * |
||
122 | * Failure to match against chapterFormat will stop the chapter being updated. |
||
123 | * |
||
124 | * @param string $chapter |
||
125 | * @return bool |
||
126 | */ |
||
127 | 2 | final public function isValidChapter(string $chapter) : bool { |
|
132 | |||
133 | /** |
||
134 | * Used by getTitleData (& similar functions) to get the requested page data. |
||
135 | * |
||
136 | * @param string $url |
||
137 | * @param string $cookie_string |
||
138 | * @param string $cookiejar_path |
||
139 | * @param bool $follow_redirect |
||
140 | * @param bool $isPost |
||
141 | * @param array $postFields |
||
142 | * |
||
143 | * @return array|bool |
||
144 | */ |
||
145 | final protected function get_content(string $url, string $cookie_string = "", string $cookiejar_path = "", bool $follow_redirect = FALSE, bool $isPost = FALSE, array $postFields = []) { |
||
146 | $ch = curl_init(); |
||
147 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
||
148 | curl_setopt($ch, CURLOPT_ENCODING , "gzip"); |
||
149 | //curl_setopt($ch, CURLOPT_VERBOSE, 1); |
||
150 | curl_setopt($ch, CURLOPT_HEADER, 1); |
||
151 | |||
152 | if($follow_redirect) curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); |
||
153 | |||
154 | if(!empty($cookie_string)) curl_setopt($ch, CURLOPT_COOKIE, $cookie_string); |
||
155 | if(!empty($cookiejar_path)) curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiejar_path); |
||
156 | |||
157 | //Some sites check the useragent for stuff, use a pre-defined user-agent to avoid stuff. |
||
158 | curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2824.0 Safari/537.36'); |
||
159 | |||
160 | //NOTE: This is required for SSL URLs for now. Without it we tend to get error code 60. |
||
161 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //FIXME: This isn't safe, but it allows us to grab SSL URLs |
||
162 | |||
163 | curl_setopt($ch, CURLOPT_URL, $url); |
||
164 | |||
165 | if($isPost) { |
||
166 | curl_setopt($ch,CURLOPT_POST, count($postFields)); |
||
167 | curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($postFields)); |
||
168 | } |
||
169 | |||
170 | $response = curl_exec($ch); |
||
171 | if($response === FALSE) { |
||
172 | log_message('error', "curl failed with error: ".curl_errno($ch)." | ".curl_error($ch)); |
||
173 | //FIXME: We don't always account for FALSE return |
||
174 | return FALSE; |
||
175 | } |
||
176 | |||
177 | $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
178 | $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
||
179 | $headers = http_parse_headers(substr($response, 0, $header_size)); |
||
180 | $body = substr($response, $header_size); |
||
181 | curl_close($ch); |
||
182 | |||
183 | return [ |
||
184 | 'headers' => $headers, |
||
185 | 'status_code' => $status_code, |
||
186 | 'body' => $body |
||
187 | ]; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Used by getTitleData to get the title, latest_chapter & last_updated data from the data returned by get_content. |
||
192 | * |
||
193 | * parseTitleDataDOM checks if the data returned by get_content is valid via a few simple checks. |
||
194 | * * If the request was actually successful, had a valid status code & data wasn't empty. We also do an additional check on an optional $failure_string param, which will throw a failure if it's matched. |
||
195 | * |
||
196 | * Data is cleaned by cleanTitleDataDOM prior to being passed to DOMDocument. |
||
197 | * |
||
198 | * All $node_* params must be XPath to the requested node, and must only return 1 result. Anything else will throw a failure. |
||
199 | * |
||
200 | * @param array $content |
||
201 | * @param string $title_url |
||
202 | * @param string $node_title_string |
||
203 | * @param string $node_row_string |
||
204 | * @param string $node_latest_string |
||
205 | * @param string $node_chapter_string |
||
206 | * @param string $failure_string |
||
207 | * @return DOMElement[]|false [nodes_title,nodes_chapter,nodes_latest] |
||
208 | */ |
||
209 | final protected function parseTitleDataDOM( |
||
264 | |||
265 | /** |
||
266 | * Used by parseTitleDataDOM to clean the data prior to passing it to DOMDocument & DOMXPath. |
||
267 | * This is mostly done as an (assumed) speed improvement due to the reduced amount of DOM to parse, or simply just making it easier to parse with XPath. |
||
268 | * |
||
269 | * @param string $data |
||
270 | * @return string |
||
271 | */ |
||
272 | public function cleanTitleDataDOM(string $data) : string { |
||
275 | |||
276 | /** |
||
277 | * Used to follow a series on given site if supported. |
||
278 | * |
||
279 | * This is called by getTitleData if $firstGet is true (which occurs when the series is first being added to the DB). |
||
280 | * |
||
281 | * Most of the actual following is done by handleCustomFollow. |
||
282 | * |
||
283 | * @param string $data |
||
284 | * @param array $extra |
||
285 | * @return array |
||
286 | */ |
||
287 | final public function doCustomFollow(string $data = "", array $extra = []) : array { |
||
314 | |||
315 | /** |
||
316 | * Used by doCustomFollow to handle following series on sites. |
||
317 | * |
||
318 | * Uses get_content to get data. |
||
319 | * |
||
320 | * $callback must return ($content, $id, closure $successCallback = NULL). |
||
321 | * * $content is simply just the get_content data. |
||
322 | * * $id is the dbID. This should be passed by the $extra arr. |
||
323 | * * $successCallback is an optional success check to make sure the series was properly followed. |
||
324 | * |
||
325 | * @param callable $callback |
||
326 | * @param string $data |
||
327 | * @param array $extra |
||
328 | */ |
||
329 | public function handleCustomFollow(callable $callback, string $data = "", array $extra = []) {} |
||
330 | |||
331 | /** |
||
332 | * Used to check the sites following page for new updates (if supported). |
||
333 | * This should work much like getTitleData, but instead checks the following page. |
||
334 | * |
||
335 | * This must return an array containing arrays of each of the chapters data. |
||
336 | */ |
||
337 | public function doCustomUpdate() {} |
||
338 | |||
339 | /** |
||
340 | * Used by the custom updater to check if a chapter looks newer than the current one. |
||
341 | * |
||
342 | * This calls doCustomCheckCompare which handles the majority of the checking. |
||
343 | * NOTE: Depending on the site, you may need to call getChapterData to get the chapter number to be used with this. |
||
344 | * |
||
345 | * @param string $oldChapter |
||
346 | * @param string $newChapter |
||
347 | */ |
||
348 | public function doCustomCheck(string $oldChapter, string $newChapter) {} |
||
349 | |||
350 | /** |
||
351 | * Used by doCustomCheck to check if a chapter looks newer than the current one. |
||
352 | * Chapter must be in a (v[0-9]+/)?c[0-9]+(\..+)? format. |
||
353 | * |
||
354 | * To avoid issues with the occasional off case, this will only ever return true if we are 100% sure that the new chapter is newer than the old one. |
||
355 | * |
||
356 | * @param array $oldChapterSegments |
||
357 | * @param array $newChapterSegments |
||
358 | * @return bool |
||
359 | */ |
||
360 | 12 | final public function doCustomCheckCompare(array $oldChapterSegments, array $newChapterSegments) : bool { |
|
410 | } |
||
411 | |||
520 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.