Conditions | 11 |
Paths | 23 |
Total Lines | 35 |
Code Lines | 22 |
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 |
||
41 | public function load(ImageInterface $image, array $options = array()) |
||
42 | { |
||
43 | if (!isset($options[$this->dimensionKey]) && !isset($options[$this->ratioKey])) { |
||
44 | throw new \InvalidArgumentException("Missing $this->dimensionKey or $this->ratioKey option."); |
||
45 | } |
||
46 | |||
47 | $size = $image->getSize(); |
||
48 | $origWidth = $size->getWidth(); |
||
49 | $origHeight = $size->getHeight(); |
||
50 | |||
51 | if (isset($options[$this->ratioKey])) { |
||
52 | $ratio = $this->absoluteRatio ? $options[$this->ratioKey] : $this->calcAbsoluteRatio($options[$this->ratioKey]); |
||
53 | } elseif (isset($options[$this->dimensionKey])) { |
||
54 | $size = $options[$this->dimensionKey]; |
||
55 | $width = isset($size[0]) ? $size[0] : null; |
||
56 | $height = isset($size[1]) ? $size[1] : null; |
||
57 | |||
58 | $widthRatio = $width / $origWidth; |
||
59 | $heightRatio = $height / $origHeight; |
||
60 | |||
61 | if (null == $width || null == $height) { |
||
62 | $ratio = max($widthRatio, $heightRatio); |
||
63 | } else { |
||
64 | $ratio = min($widthRatio, $heightRatio); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | if ($this->isImageProcessable($ratio)) { |
||
69 | $filter = new Resize(new Box(round($origWidth * $ratio), round($origHeight * $ratio))); |
||
|
|||
70 | |||
71 | return $filter->apply($image); |
||
72 | } |
||
73 | |||
74 | return $image; |
||
75 | } |
||
76 | |||
87 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: