| Conditions | 36 |
| Paths | 9990 |
| Total Lines | 125 |
| Code Lines | 85 |
| 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 |
||
| 100 | public function PerfectCMSImageLink( |
||
| 101 | $name, |
||
| 102 | $backupObject = null, |
||
| 103 | $backupField = '', |
||
| 104 | $useRetina = null |
||
| 105 | ) { |
||
| 106 | if (isset($_GET['flush'])) { |
||
| 107 | if (! Config::inst()->get('Image', 'force_resample')) { |
||
| 108 | Config::inst()->update('Image', 'force_resample', true); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | $image = $this->owner; |
||
| 112 | if ($image && $image->exists()) { |
||
| 113 | //we are all good ... |
||
| 114 | } else { |
||
| 115 | if (!$backupObject) { |
||
| 116 | $backupObject = SiteConfig::current_site_config(); |
||
| 117 | } |
||
| 118 | if (!$backupField) { |
||
| 119 | $backupField = $name; |
||
| 120 | } |
||
| 121 | if ($backupObject->hasMethod($backupField)) { |
||
| 122 | $image = $backupObject->$backupField(); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | $perfectWidth = self::get_width($name, true); |
||
| 127 | $perfectHeight = self::get_height($name, true); |
||
| 128 | |||
| 129 | if ($image) { |
||
| 130 | if ($image instanceof Image) { |
||
| 131 | if ($image->exists()) { |
||
| 132 | //work out perfect with and height |
||
| 133 | if (!$useRetina) { |
||
| 134 | $useRetina = PerfectCMSImageDataExtension::use_retina($name); |
||
| 135 | } else { |
||
| 136 | $useRetina = $useRetina; |
||
| 137 | } |
||
| 138 | $crop = PerfectCMSImageDataExtension::crop($name); |
||
| 139 | $multiplier = 1; |
||
| 140 | if ($useRetina) { |
||
| 141 | $multiplier = 2; |
||
| 142 | } |
||
| 143 | $perfectWidth = $perfectWidth * $multiplier; |
||
| 144 | $perfectHeight = $perfectHeight * $multiplier; |
||
| 145 | |||
| 146 | //get current width and height |
||
| 147 | $myWidth = $image->getWidth(); |
||
| 148 | $myHeight = $image->getHeight(); |
||
| 149 | // $backEndString = Image::get_backend(); |
||
| 150 | // $backend = Injector::inst()->get($backEndString); |
||
| 151 | if ($perfectWidth && $perfectHeight) { |
||
| 152 | |||
| 153 | //if the height or the width are already perfect then we can not do anything about it. |
||
| 154 | if ($myWidth == $perfectWidth && $myHeight == $perfectHeight) { |
||
| 155 | $link = $image->Link(); |
||
| 156 | } elseif ($crop) { |
||
| 157 | $link = $image->Fill($perfectWidth, $perfectHeight)->Link(); |
||
| 158 | } elseif ($myWidth < $perfectWidth || $myHeight < $perfectHeight) { |
||
| 159 | $link = $image->Pad( |
||
| 160 | $perfectWidth, |
||
| 161 | $perfectHeight, |
||
| 162 | PerfectCMSImageDataExtension::get_padding_bg_colour($name) |
||
| 163 | )->Link(); |
||
| 164 | } else { |
||
| 165 | $link = $image->FitMax($perfectWidth, $perfectHeight)->Link(); |
||
| 166 | } |
||
| 167 | } elseif ($perfectWidth) { |
||
| 168 | if ($myWidth == $perfectWidth) { |
||
| 169 | $link = $image->Link(); |
||
| 170 | } elseif ($crop) { |
||
| 171 | $link = $image->Fill($perfectHeight, $myHeight)->Link(); |
||
| 172 | } else { |
||
| 173 | $link = $image->ScaleWidth($perfectWidth)->Link(); |
||
| 174 | } |
||
| 175 | } elseif ($perfectHeight) { |
||
| 176 | if ($myHeight == $perfectHeight) { |
||
| 177 | $link = $image->Link(); |
||
| 178 | } elseif ($crop) { |
||
| 179 | $link = $image->Fill($myWidth, $perfectHeight)->Link(); |
||
| 180 | } else { |
||
| 181 | $link = $image->ScaleHeight($perfectHeight)->Link(); |
||
| 182 | } |
||
| 183 | } else { |
||
| 184 | $link = $image->ScaleWidth($myWidth)->Link(); |
||
| 185 | } |
||
| 186 | $path_parts = pathinfo($link); |
||
| 187 | |||
| 188 | if (class_exists('HashPathExtension')) { |
||
| 189 | if ($curr = Controller::curr()) { |
||
| 190 | if ($curr->hasMethod('HashPath')) { |
||
| 191 | $link = $curr->HashPath($link, false); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | $imageClasses = Config::inst()->get('PerfectCMSImageDataExtension', 'perfect_cms_images_append_title_to_image_links_classes'); |
||
| 196 | if (in_array($image->ClassName, $imageClasses) && $image->Title) { |
||
| 197 | $link = $this->replaceLastInstance( |
||
| 198 | '.'.$path_parts['extension'], |
||
| 199 | '.pci/'.$image->Title.'.'.$path_parts['extension'], |
||
| 200 | $link |
||
| 201 | ); |
||
| 202 | } |
||
| 203 | |||
| 204 | return $link; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | // no image -> provide placeholder if in DEV MODE only!!! |
||
| 209 | if (Director::isDev()) { |
||
| 210 | if ($perfectWidth || $perfectHeight) { |
||
| 211 | if (!$perfectWidth) { |
||
| 212 | $perfectWidth = $perfectHeight; |
||
| 213 | } |
||
| 214 | if (!$perfectHeight) { |
||
| 215 | $perfectHeight = $perfectWidth; |
||
| 216 | } |
||
| 217 | $text = "$perfectWidth x $perfectHeight /2 = ".round($perfectWidth/2)." x ".round($perfectHeight/2).""; |
||
| 218 | |||
| 219 | return 'https://placehold.it/'.($perfectWidth).'x'.($perfectHeight).'?text='.urlencode($text); |
||
| 220 | } else { |
||
| 221 | return 'https://placehold.it/500x500?text='.urlencode('no size set'); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 396 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.