| Conditions | 30 | 
| Paths | 2556 | 
| Total Lines | 93 | 
| Code Lines | 66 | 
| 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 | ||
| 61 | public function PerfectCMSImageLink( | ||
| 62 | $name, | ||
| 63 | $backupObject = null, | ||
| 64 | $backupField = '', | ||
| 65 | $isRetina = true | ||
| 66 |     ) { | ||
| 67 |         if (! Config::inst()->get('Image', 'force_resample')) { | ||
| 68 |             Config::inst()->update('Image', 'force_resample', true); | ||
| 69 | } | ||
| 70 | $image = $this->owner; | ||
| 71 |         if ($image && $image->exists()) { | ||
| 72 | //we are all good ... | ||
| 73 |         } else { | ||
| 74 |             if (!$backupObject) { | ||
| 75 | $backupObject = SiteConfig::current_site_config(); | ||
| 76 | } | ||
| 77 |             if (!$backupField) { | ||
| 78 | $backupField = $name; | ||
| 79 | } | ||
| 80 |             if ($backupObject->hasMethod($backupField)) { | ||
| 81 | $image = $backupObject->$backupField(); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | $perfectWidth = (intval(self::get_width($name)) - 0); | ||
| 86 | $perfectHeight = (intval(self::get_height($name)) - 0); | ||
| 87 |         if ($isRetina) { | ||
| 88 | $perfectWidth = $perfectWidth * 2; | ||
| 89 | $perfectHeight = $perfectHeight * 2; | ||
| 90 | } | ||
| 91 |         if ($image) { | ||
| 92 |             if ($image instanceof Image) { | ||
| 93 |                 if ($image->exists()) { | ||
| 94 | //get preferred width and height | ||
| 95 | $myWidth = $image->getWidth(); | ||
| 96 | $myHeight = $image->getHeight(); | ||
| 97 | $backEndString = Image::get_backend(); | ||
| 98 | $backend = Injector::inst()->get($backEndString); | ||
| 99 |                     if ($perfectWidth && $perfectHeight) { | ||
| 100 |                         if ($myWidth == $perfectWidth || $myHeight ==  $perfectHeight) { | ||
| 101 | $link = $image->ScaleWidth($myWidth)->Link(); | ||
| 102 |                         } elseif ($myWidth < $perfectWidth || $myHeight < $perfectHeight) { | ||
| 103 | $link = $image->Pad( | ||
| 104 | $perfectWidth, | ||
| 105 | $perfectHeight, | ||
| 106 |                                 Config::inst()->get('PerfectCMSImageDataExtension', 'perfect_cms_images_background_padding_color') | ||
| 107 | )->Link(); | ||
| 108 |                         } elseif ($myWidth > $perfectWidth || $myHeight > $perfectHeight) { | ||
| 109 | $link = $image->FitMax($perfectWidth, $perfectHeight)->Link(); | ||
| 110 | } | ||
| 111 |                     } elseif ($perfectWidth) { | ||
| 112 | $link = $image->ScaleWidth($perfectWidth)->Link(); | ||
| 113 |                     } elseif ($perfectHeight) { | ||
| 114 | $link = $image->ScaleHeight($perfectHeight)->Link(); | ||
| 115 |                     } else { | ||
| 116 | $link = $image->ScaleWidth($myWidth)->Link(); | ||
| 117 | } | ||
| 118 | $path_parts = pathinfo($link); | ||
| 119 | |||
| 120 |                     if (class_exists('HashPathExtension')) { | ||
| 121 |                         if ($curr = Controller::curr()) { | ||
| 122 |                             if ($curr->hasMethod('HashPath')) { | ||
| 123 | $link = $curr->HashPath($link, false); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 |                     $imageClasses = Config::inst()->get('PerfectCMSImageDataExtension', 'perfect_cms_images_append_title_to_image_links_classes'); | ||
| 128 |                     if(in_array($image->ClassName, $imageClasses) && $image->Title){ | ||
| 129 | $link = $this->replaceLastInstance( | ||
| 130 | '.'.$path_parts['extension'], | ||
| 131 | '.pci/'.$image->Title.'.'.$path_parts['extension'], | ||
| 132 | $link | ||
| 133 | ); | ||
| 134 | } | ||
| 135 | return $link; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | // no image -> provide placeholder | ||
| 140 |         if ($perfectWidth || $perfectHeight) { | ||
| 141 |             if (!$perfectWidth) { | ||
| 142 | $perfectWidth = $perfectHeight; | ||
| 143 | } | ||
| 144 |             if (!$perfectHeight) { | ||
| 145 | $perfectHeight = $perfectWidth; | ||
| 146 | } | ||
| 147 | $text = "$perfectWidth x $perfectHeight /2 = ".round($perfectWidth/2)." x ".round($perfectHeight/2).""; | ||
| 148 | |||
| 149 | return 'https://placehold.it/'.($perfectWidth).'x'.($perfectHeight).'?text='.urlencode($text); | ||
| 150 |         } else { | ||
| 151 |             return 'https://placehold.it/500x500?text='.urlencode('no size set'); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 262 | 
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.