Total Complexity | 43 |
Total Lines | 293 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 1 | Features | 1 |
Complex classes like Af_Readability 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 Af_Readability, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Af_Readability extends Plugin { |
||
6 | |||
7 | /* @var PluginHost $host */ |
||
8 | private $host; |
||
9 | |||
10 | public function about() { |
||
11 | return array(1.0, |
||
12 | "Try to inline article content using Readability", |
||
13 | "fox"); |
||
14 | } |
||
15 | |||
16 | public function flags() { |
||
17 | return array("needs_curl" => true); |
||
18 | } |
||
19 | |||
20 | public function save() { |
||
26 | } |
||
27 | |||
28 | public function init($host) |
||
47 | } |
||
48 | |||
49 | public function hook_prefs_tab($args) { |
||
120 | } |
||
121 | |||
122 | public function hook_prefs_edit_feed($feed_id) { |
||
123 | print "<header>".__("Readability")."</header>"; |
||
124 | print "<section>"; |
||
125 | |||
126 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
||
127 | if (!is_array($enabled_feeds)) { |
||
128 | $enabled_feeds = array(); |
||
129 | } |
||
130 | |||
131 | $key = array_search($feed_id, $enabled_feeds); |
||
132 | $checked = $key !== false ? "checked" : ""; |
||
133 | |||
134 | print "<fieldset>"; |
||
135 | |||
136 | print "<label class='checkbox'><input dojoType='dijit.form.CheckBox' type='checkbox' id='af_readability_enabled' |
||
137 | name='af_readability_enabled' $checked> ".__('Inline article content')."</label>"; |
||
138 | |||
139 | print "</fieldset>"; |
||
140 | |||
141 | print "</section>"; |
||
142 | } |
||
143 | |||
144 | public function hook_prefs_save_feed($feed_id) { |
||
145 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
||
146 | if (!is_array($enabled_feeds)) { |
||
147 | $enabled_feeds = array(); |
||
148 | } |
||
149 | |||
150 | $enable = checkbox_to_sql_bool($_POST["af_readability_enabled"]); |
||
151 | $key = array_search($feed_id, $enabled_feeds); |
||
152 | |||
153 | if ($enable) { |
||
154 | if ($key === false) { |
||
155 | array_push($enabled_feeds, $feed_id); |
||
156 | } |
||
157 | } else { |
||
158 | if ($key !== false) { |
||
159 | unset($enabled_feeds[$key]); |
||
160 | } |
||
161 | } |
||
162 | |||
163 | $this->host->set($this, "enabled_feeds", $enabled_feeds); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
||
168 | */ |
||
169 | public function hook_article_filter_action($article, $action) { |
||
170 | return $this->process_article($article); |
||
171 | } |
||
172 | |||
173 | public function extract_content($url) { |
||
174 | |||
175 | global $fetch_effective_url; |
||
176 | |||
177 | $tmp = fetch_file_contents([ |
||
178 | "url" => $url, |
||
179 | "http_accept" => "text/*", |
||
180 | "type" => "text/html"]); |
||
181 | |||
182 | if ($tmp && mb_strlen($tmp) < 1024 * 500) { |
||
183 | $tmpdoc = new DOMDocument("1.0", "UTF-8"); |
||
184 | |||
185 | if (!@$tmpdoc->loadHTML($tmp)) { |
||
186 | return false; |
||
187 | } |
||
188 | |||
189 | // this is the worst hack yet :( |
||
190 | if (strtolower($tmpdoc->encoding) != 'utf-8') { |
||
191 | $tmp = preg_replace("/<meta.*?charset.*?\/?>/i", "", $tmp); |
||
192 | if (empty($tmpdoc->encoding)) { |
||
193 | $tmp = mb_convert_encoding($tmp, 'utf-8'); |
||
194 | } else { |
||
195 | $tmp = mb_convert_encoding($tmp, 'utf-8', $tmpdoc->encoding); |
||
196 | } |
||
197 | } |
||
198 | |||
199 | try { |
||
200 | $r = new Readability(new Configuration()); |
||
201 | |||
202 | if ($r->parse($tmp)) { |
||
203 | |||
204 | $tmpxpath = new DOMXPath($r->getDOMDOcument()); |
||
205 | $entries = $tmpxpath->query('(//a[@href]|//img[@src])'); |
||
206 | |||
207 | foreach ($entries as $entry) { |
||
208 | if ($entry->hasAttribute("href")) { |
||
209 | $entry->setAttribute("href", |
||
210 | rewrite_relative_url($fetch_effective_url, $entry->getAttribute("href"))); |
||
211 | |||
212 | } |
||
213 | |||
214 | if ($entry->hasAttribute("src")) { |
||
215 | $entry->setAttribute("src", |
||
216 | rewrite_relative_url($fetch_effective_url, $entry->getAttribute("src"))); |
||
217 | |||
218 | } |
||
219 | } |
||
220 | |||
221 | return $r->getContent(); |
||
222 | } |
||
223 | |||
224 | } catch (Exception $e) { |
||
225 | return false; |
||
226 | } |
||
227 | } |
||
228 | |||
229 | return false; |
||
230 | } |
||
231 | |||
232 | public function process_article($article) { |
||
233 | |||
234 | $extracted_content = $this->extract_content($article["link"]); |
||
235 | |||
236 | # let's see if there's anything of value in there |
||
237 | $content_test = trim(strip_tags(sanitize($extracted_content))); |
||
238 | |||
239 | if ($content_test) { |
||
240 | $article["content"] = $extracted_content; |
||
241 | } |
||
242 | |||
243 | return $article; |
||
244 | } |
||
245 | |||
246 | public function hook_article_filter($article) { |
||
247 | |||
248 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); |
||
249 | if (!is_array($enabled_feeds)) { |
||
250 | return $article; |
||
251 | } |
||
252 | |||
253 | $key = array_search($article["feed"]["id"], $enabled_feeds); |
||
254 | if ($key === false) { |
||
255 | return $article; |
||
256 | } |
||
257 | |||
258 | return $this->process_article($article); |
||
259 | |||
260 | } |
||
261 | |||
262 | public function hook_get_full_text($link) |
||
263 | { |
||
264 | $enable_share_anything = $this->host->get($this, "enable_share_anything"); |
||
265 | |||
266 | if ($enable_share_anything) { |
||
267 | $extracted_content = $this->extract_content($link); |
||
268 | |||
269 | # let's see if there's anything of value in there |
||
270 | $content_test = trim(strip_tags(sanitize($extracted_content))); |
||
271 | |||
272 | if ($content_test) { |
||
273 | return $extracted_content; |
||
274 | } |
||
275 | } |
||
276 | |||
277 | return false; |
||
278 | } |
||
279 | |||
280 | public function api_version() { |
||
282 | } |
||
283 | |||
284 | private function filter_unknown_feeds($enabled_feeds) { |
||
298 | } |
||
299 | |||
300 | } |
||
301 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.