| Conditions | 16 |
| Paths | 6 |
| Total Lines | 59 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 |
||
| 16 | public function run($request) |
||
| 17 | { |
||
| 18 | $this->outputToScreen('Expected URL parameters: ?parent=SiteTree&fieldname=MyImage&width=100&height=200', 'created'); |
||
| 19 | $parent = $request->getVar('parent'); |
||
| 20 | $fieldName = $request->getVar('fieldname'); |
||
| 21 | $width = $request->getVar('width'); |
||
| 22 | $height = $request->getVar('height'); |
||
| 23 | if ($height || $width) { |
||
| 24 | if (class_exists($parent)) { |
||
| 25 | $singleton = Injector::inst()->get($parent); |
||
| 26 | if ($singleton instanceof DataObject) { |
||
| 27 | if ($singleton->hasMethod($fieldName)) { |
||
| 28 | $objects = $parent::get()->where('"' . $fieldName . 'ID" <> 0 AND "' . $fieldName . 'ID" IS NOT NULL'); |
||
| 29 | for ($i = 0; $i < 100000; ++$i) { |
||
| 30 | $array = []; |
||
| 31 | $obj = $objects->limit(1, $i)->first(); |
||
| 32 | if (! $obj) { |
||
| 33 | break; |
||
| 34 | } |
||
| 35 | |||
| 36 | $image = $obj->{$fieldName}(); |
||
| 37 | if ($image && $image instanceof $image && $image->exists()) { |
||
| 38 | if ($width) { |
||
| 39 | $realWidth = $image->getWidth(); |
||
| 40 | if ($realWidth !== $width) { |
||
| 41 | $array[] = 'width is ' . round($width / $realWidth, 2) . '% of what it should be'; |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | if ($height) { |
||
| 46 | $realHeight = $image->getHeight(); |
||
| 47 | if ($realHeight !== $height) { |
||
| 48 | $array[] = 'height is ' . round($height / $realHeight, 2) . '% of what it should be'; |
||
| 49 | } |
||
| 50 | } |
||
| 51 | |||
| 52 | if (count($array) > 0) { |
||
| 53 | $this->outputToScreen('ERRORS WITH: ' . $obj->getTitle() . ' --- ' . implode('; ', $array), 'deleted'); |
||
| 54 | } else { |
||
| 55 | $this->outputToScreen('PERFECT PASS FOR: ' . $obj->getTitle()); |
||
| 56 | } |
||
| 57 | } else { |
||
| 58 | $this->outputToScreen('Skipping ' . $obj->getTitle() . ' as it does not have a valid image attached to it.'); |
||
| 59 | } |
||
| 60 | } |
||
| 61 | } else { |
||
| 62 | $this->outputToScreen('Please specify a valid field name like this fieldname=xxx, where xxx is the field name (e.g. Image).', 'deleted'); |
||
| 63 | } |
||
| 64 | } else { |
||
| 65 | $this->outputToScreen('Please specify a valid class name like this parent=xxx, where xxx is the class name that is a valid data object.', 'deleted'); |
||
| 66 | } |
||
| 67 | } else { |
||
| 68 | $this->outputToScreen('Please specify a valid class name like this parent=xxx, where xxx is the class name.', 'deleted'); |
||
| 69 | } |
||
| 70 | } else { |
||
| 71 | $this->outputToScreen('Please specify at least one of height or width.', 'deleted'); |
||
| 72 | } |
||
| 73 | |||
| 74 | echo '<h1>--- COMPLETED ---</h1>'; |
||
| 75 | } |
||
| 90 |