Conditions | 8 |
Paths | 7 |
Total Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
35 | protected function inlineImage($excerpt) |
||
36 | { |
||
37 | $image = parent::inlineImage($excerpt); |
||
38 | |||
39 | if (!isset($image)) { |
||
40 | return null; |
||
41 | } |
||
42 | |||
43 | $query = parse_url($image['element']['attributes']['src'], PHP_URL_QUERY); |
||
44 | |||
45 | // nothing to do |
||
46 | if ($query === null) { |
||
47 | return $image; |
||
48 | } |
||
49 | // URL without query string |
||
50 | $image['element']['attributes']['src'] = strtok($image['element']['attributes']['src'], '?'); |
||
51 | |||
52 | // has resize value |
||
53 | parse_str($query, $result); |
||
54 | if (array_key_exists('resize', $result)) { |
||
55 | $resize = (int) $result['resize']; |
||
56 | $image['element']['attributes']['width'] = $resize; |
||
57 | // no config or no GD, can't process |
||
58 | if ($this->config === null || !extension_loaded('gd')) { |
||
59 | return $image; |
||
60 | } |
||
61 | // external image? return data URL |
||
62 | if (preg_match('~^(?:f|ht)tps?://~i', $image['element']['attributes']['src'])) { |
||
63 | try { |
||
64 | $img = Image::make($image['element']['attributes']['src']); |
||
65 | } catch (NotReadableException $e) { |
||
66 | throw new Exception(sprintf('Cannot get image "%s"', $image['element']['attributes']['src'])); |
||
67 | } |
||
68 | $imgPath = (string) $img->encode('data-url'); |
||
69 | $image['element']['attributes']['src'] = $imgPath; |
||
70 | |||
71 | return $image; |
||
72 | } |
||
73 | // save thumb file |
||
74 | $img = Image::make($this->config->getStaticPath().'/'.$image['element']['attributes']['src']); |
||
75 | $img->resize($resize, null, function ($constraint) { |
||
76 | $constraint->aspectRatio(); |
||
77 | $constraint->upsize(); |
||
78 | }); |
||
79 | $imgThumbPath = '/'.self::TMP_DIR.'/images/thumbs/'.$resize; |
||
80 | $imgPath = $imgThumbPath.$image['element']['attributes']['src']; |
||
81 | $imgSubdir = Util::getFS()->makePathRelative( |
||
82 | dirname($imgPath), |
||
83 | $imgThumbPath |
||
84 | ); |
||
85 | Util::getFS()->mkdir($this->config->getDestinationDir().$imgThumbPath.'/'.$imgSubdir); |
||
86 | $img->save($this->config->getDestinationDir().$imgPath); |
||
87 | $imgPath = '/images/thumbs/'.$resize.$image['element']['attributes']['src']; |
||
88 | $image['element']['attributes']['src'] = $imgPath; |
||
89 | } |
||
90 | |||
91 | return $image; |
||
92 | } |
||
93 | } |
||
94 |