Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ImageExtractor 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 ImageExtractor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class ImageExtractor extends AbstractModule implements ModuleInterface { |
||
22 | use ArticleMutatorTrait; |
||
23 | |||
24 | /** @var string[] */ |
||
25 | private $badFileNames = [ |
||
26 | '\.html', '\.gif', '\.ico', 'button', 'twitter\.jpg', 'facebook\.jpg', |
||
27 | 'ap_buy_photo', 'digg\.jpg', 'digg\.png', 'delicious\.png', |
||
28 | 'facebook\.png', 'reddit\.jpg', 'doubleclick', 'diggthis', |
||
29 | 'diggThis', 'adserver', '\/ads\/', 'ec\.atdmt\.com', 'mediaplex\.com', |
||
30 | 'adsatt', 'view\.atdmt', |
||
31 | ]; |
||
32 | |||
33 | /** @var string[] */ |
||
34 | private static $KNOWN_IMG_DOM_NAMES = [ |
||
35 | 'yn-story-related-media', |
||
36 | 'cnn_strylccimg300cntr', |
||
37 | 'big_photo', |
||
38 | 'ap-smallphoto-a' |
||
39 | ]; |
||
40 | |||
41 | /** @var int */ |
||
42 | private static $MAX_PARENT_DEPTH = 2; |
||
43 | |||
44 | /** @var string[] */ |
||
45 | private static $CUSTOM_SITE_MAPPING = []; |
||
46 | |||
47 | /** |
||
48 | * @param Article $article |
||
49 | */ |
||
50 | public function run(Article $article) { |
||
62 | |||
63 | /** |
||
64 | * @return Image|null |
||
65 | */ |
||
66 | private function getBestImage() { |
||
89 | |||
90 | /** |
||
91 | * Prefer Twitter images (as they tend to have the right size for us), then Open Graph images |
||
92 | * (which seem to be smaller), and finally linked images. |
||
93 | * |
||
94 | * @return Image|null |
||
95 | */ |
||
96 | private function checkForMetaTag() { |
||
117 | |||
118 | /** |
||
119 | * although slow the best way to determine the best image is to download them and check the actual dimensions of the image when on disk |
||
120 | * so we'll go through a phased approach... |
||
121 | * 1. get a list of ALL images from the parent node |
||
122 | * 2. filter out any bad image names that we know of (gifs, ads, etc..) |
||
123 | * 3. do a head request on each file to make sure it meets our bare requirements |
||
124 | * 4. any images left over let's do a full GET request, download em to disk and check their dimensions |
||
125 | * 5. Score images based on different factors like height/width and possibly things like color density |
||
126 | * |
||
127 | * @param Element $node |
||
128 | * @param int $parentDepthLevel |
||
129 | * @param int $siblingDepthLevel |
||
130 | * |
||
131 | * @return Image|null |
||
132 | */ |
||
133 | private function checkForLargeImages(Element $node, $parentDepthLevel, $siblingDepthLevel) { |
||
134 | $goodLocalImages = $this->getImageCandidates($node); |
||
135 | |||
136 | $scoredLocalImages = $this->scoreLocalImages($goodLocalImages, $parentDepthLevel); |
||
137 | |||
138 | ksort($scoredLocalImages); |
||
139 | |||
140 | if (!empty($scoredLocalImages)) { |
||
141 | foreach ($scoredLocalImages as $imageScore => $scoredLocalImage) { |
||
142 | $mainImage = new Image(); |
||
143 | $mainImage->setImageSrc($scoredLocalImage->getImgSrc()); |
||
144 | $mainImage->setImageExtractionType('bigimage'); |
||
145 | $mainImage->setConfidenceScore(100 / count($scoredLocalImages)); |
||
146 | $mainImage->setImageScore($imageScore); |
||
147 | $mainImage->setBytes($scoredLocalImage->getBytes()); |
||
148 | $mainImage->setHeight($scoredLocalImage->getHeight()); |
||
149 | $mainImage->setWidth($scoredLocalImage->getWidth()); |
||
150 | |||
151 | return $mainImage; |
||
152 | } |
||
153 | } else { |
||
154 | $depthObj = $this->getDepthLevel($node, $parentDepthLevel, $siblingDepthLevel); |
||
155 | |||
156 | if ($depthObj && NULL !== $depthObj->node) { |
||
157 | return $this->checkForLargeImages($depthObj->node, $depthObj->parentDepth, $depthObj->siblingDepth); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | return null; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param Element $node |
||
166 | * @param int $parentDepth |
||
167 | * @param int $siblingDepth |
||
168 | * |
||
169 | * @return object|null |
||
170 | */ |
||
171 | private function getDepthLevel(Element $node, $parentDepth, $siblingDepth) { |
||
172 | if (is_null($node) || !($node->parent() instanceof Element)) { |
||
173 | return null; |
||
174 | } |
||
175 | |||
176 | if ($parentDepth > self::$MAX_PARENT_DEPTH) { |
||
177 | return null; |
||
178 | } |
||
179 | |||
180 | // Find previous sibling element node |
||
181 | $siblingNode = $node->preceding(function($node) { |
||
182 | return $node instanceof Element; |
||
183 | }); |
||
184 | |||
185 | if (is_null($siblingNode)) { |
||
186 | return (object)[ |
||
187 | 'node' => $node->parent(), |
||
188 | 'parentDepth' => $parentDepth + 1, |
||
189 | 'siblingDepth' => 0, |
||
190 | ]; |
||
191 | } |
||
192 | |||
193 | return (object)[ |
||
194 | 'node' => $siblingNode, |
||
195 | 'parentDepth' => $parentDepth, |
||
196 | 'siblingDepth' => $siblingDepth + 1, |
||
197 | ]; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Set image score and on locally downloaded images |
||
202 | * |
||
203 | * we're going to score the images in the order in which they appear so images higher up will have more importance, |
||
204 | * we'll count the area of the 1st image as a score of 1 and then calculate how much larger or small each image after it is |
||
205 | * we'll also make sure to try and weed out banner type ad blocks that have big widths and small heights or vice versa |
||
206 | * so if the image is 3rd found in the dom it's sequence score would be 1 / 3 = .33 * diff in area from the first image |
||
207 | * |
||
208 | * @param LocallyStoredImage[] $locallyStoredImages |
||
209 | * @param int $depthLevel |
||
210 | * |
||
211 | * @return LocallyStoredImage[] |
||
212 | */ |
||
213 | private function scoreLocalImages($locallyStoredImages, $depthLevel) { |
||
|
|||
214 | $results = []; |
||
215 | $i = 1; |
||
216 | $initialArea = 0; |
||
217 | |||
218 | // Limit to the first 30 images |
||
219 | $locallyStoredImages = array_slice($locallyStoredImages, 0, 30); |
||
220 | |||
221 | foreach ($locallyStoredImages as $locallyStoredImage) { |
||
222 | $sequenceScore = 1 / $i; |
||
223 | $area = $locallyStoredImage->getWidth() * $locallyStoredImage->getHeight(); |
||
224 | |||
225 | if ($initialArea == 0) { |
||
226 | $initialArea = $area * 1.48; |
||
227 | $totalScore = 1; |
||
228 | } else { |
||
229 | $areaDifference = $area * $initialArea; |
||
230 | $totalScore = $sequenceScore * $areaDifference; |
||
231 | } |
||
232 | |||
233 | $i++; |
||
234 | |||
235 | $results[$totalScore] = $locallyStoredImage; |
||
236 | } |
||
237 | |||
238 | return $results; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @param LocallyStoredImage $locallyStoredImage |
||
243 | * @param int $depthLevel |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | private function isWorthyImage($locallyStoredImage, $depthLevel) { |
||
258 | |||
259 | /** |
||
260 | * @return Image[] |
||
261 | */ |
||
262 | private function getAllImages() { |
||
288 | |||
289 | /** |
||
290 | * returns true if we think this is kind of a bannery dimension |
||
291 | * like 600 / 100 = 6 may be a fishy dimension for a good image |
||
292 | * |
||
293 | * @param int $width |
||
294 | * @param int $height |
||
295 | */ |
||
296 | private function isBannerDimensions($width, $height) { |
||
317 | |||
318 | /** |
||
319 | * takes a list of image elements and filters out the ones with bad names |
||
320 | * |
||
321 | * @param \DOMWrap\NodeList $images |
||
322 | * |
||
323 | * @return Element[] |
||
324 | */ |
||
325 | private function filterBadNames(NodeList $images) { |
||
338 | |||
339 | /** |
||
340 | * will check the image src against a list of bad image files we know of like buttons, etc... |
||
341 | * |
||
342 | * @param Element $imageNode |
||
343 | * |
||
344 | * @return bool |
||
345 | */ |
||
346 | private function isOkImageFileName(Element $imageNode) { |
||
361 | |||
362 | /** |
||
363 | * @param Element $node |
||
364 | * |
||
365 | * @return LocallyStoredImage[] |
||
366 | */ |
||
367 | private function getImageCandidates(Element $node) { |
||
368 | $images = $node->find('img'); |
||
369 | $filteredImages = $this->filterBadNames($images); |
||
370 | $goodImages = $this->findImagesThatPassByteSizeTest($filteredImages); |
||
371 | |||
372 | return $goodImages; |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * loop through all the images and find the ones that have the best bytes to even make them a candidate |
||
377 | * |
||
378 | * @param Element[] $images |
||
379 | * |
||
380 | * @return LocallyStoredImage[] |
||
381 | */ |
||
382 | private function findImagesThatPassByteSizeTest($images) { |
||
383 | $i = 0; /** @todo Re-factor how the LocallyStoredImage => Image relation works ? Note: PHP 5.6.x adds a 3rd argument to array_filter() to pass the key as well as value. */ |
||
384 | |||
385 | // Limit to the first 30 images |
||
386 | $images = array_slice($images, 0, 30); |
||
387 | |||
388 | // Generate a complete URL for each image |
||
389 | $imageUrls = array_map(function($image) { |
||
390 | return $this->buildImagePath($image->attr('src')); |
||
391 | }, $images); |
||
392 | |||
393 | $localImages = $this->getLocallyStoredImages($imageUrls, true); |
||
394 | |||
395 | $results = array_filter($localImages, function($localImage) use($images, $i) { |
||
396 | $image = $images[$i++]; |
||
397 | |||
398 | $bytes = $localImage->getBytes(); |
||
399 | |||
400 | if ($bytes < $this->config()->get('image_min_bytes') && $bytes != 0 || $bytes > $this->config()->get('image_max_bytes')) { |
||
401 | $image->remove(); |
||
402 | |||
403 | return false; |
||
404 | } |
||
405 | |||
406 | return true; |
||
407 | }); |
||
408 | |||
409 | return $results; |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * checks to see if we were able to find feature image tags on this page |
||
414 | * |
||
415 | * @return Image|null |
||
416 | */ |
||
417 | private function checkForLinkTag() { |
||
420 | |||
421 | /** |
||
422 | * checks to see if we were able to find open graph tags on this page |
||
423 | * |
||
424 | * @return Image|null |
||
425 | */ |
||
426 | private function checkForOpenGraphTag() { |
||
429 | |||
430 | /** |
||
431 | * checks to see if we were able to find twitter tags on this page |
||
432 | * |
||
433 | * @return Image|null |
||
434 | */ |
||
435 | private function checkForTwitterTag() { |
||
438 | |||
439 | /** |
||
440 | * @param string $selector |
||
441 | * @param string $attr |
||
442 | * @param string $type |
||
443 | * |
||
444 | * @return Image|null |
||
445 | */ |
||
446 | private function checkForTag($selector, $attr, $type) { |
||
479 | |||
480 | /** |
||
481 | * @param Image $mainImage |
||
482 | * |
||
483 | * @return Image|null |
||
484 | */ |
||
485 | private function ensureMinimumImageSize(Image $mainImage) { |
||
493 | |||
494 | /** |
||
495 | * @param string $imageSrc |
||
496 | * @param bool $returnAll |
||
497 | * |
||
498 | * @return LocallyStoredImage|null |
||
499 | */ |
||
500 | private function getLocallyStoredImage($imageSrc, $returnAll = false) { |
||
505 | |||
506 | /** |
||
507 | * @param string[] $imageSrcs |
||
508 | * @param bool $returnAll |
||
509 | * |
||
510 | * @return LocallyStoredImage[] |
||
511 | */ |
||
512 | private function getLocallyStoredImages($imageSrcs, $returnAll = false) { |
||
515 | |||
516 | /** |
||
517 | * @return string |
||
518 | */ |
||
519 | private function getCleanDomain() { |
||
522 | |||
523 | /** |
||
524 | * In here we check for known image contains from sites we've checked out like yahoo, techcrunch, etc... that have |
||
525 | * known places to look for good images. |
||
526 | * |
||
527 | * @todo enable this to use a series of settings files so people can define what the image ids/classes are on specific sites |
||
528 | * |
||
529 | * @return Image|null |
||
530 | */ |
||
531 | private function checkForKnownElements() { |
||
587 | |||
588 | /** |
||
589 | * This method will take an image path and build out the absolute path to that image |
||
590 | * using the initial url we crawled so we can find a link to the image if they use relative urls like ../myimage.jpg |
||
591 | * |
||
592 | * @param string $imageSrc |
||
593 | * |
||
594 | * @return string |
||
595 | */ |
||
596 | private function buildImagePath($imageSrc) { |
||
623 | |||
624 | /** |
||
625 | * @param string[] |
||
626 | */ |
||
627 | private function customSiteMapping() { |
||
642 | |||
643 | } |
||
644 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.