Conditions | 11 |
Paths | 56 |
Total Lines | 45 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
105 | public static function renderStatic( |
||
106 | array $arguments, |
||
107 | \Closure $renderChildrenClosure, |
||
108 | RenderingContextInterface $renderingContext |
||
109 | ) { |
||
110 | $src = $arguments['src']; |
||
111 | $image = $arguments['image']; |
||
112 | $treatIdAsReference = $arguments['treatIdAsReference']; |
||
113 | $crop = $arguments['crop']; |
||
114 | //$absolute = $arguments['absolute']; |
||
115 | |||
116 | if (is_null($src) && is_null($image) || !is_null($src) && !is_null($image)) { |
||
117 | throw new Exception('You must either specify a string src or a File object.', 1382284105); |
||
118 | } |
||
119 | |||
120 | try { |
||
121 | $imageService = self::getImageService(); |
||
122 | $image = $imageService->getImage($src, $image, $treatIdAsReference); |
||
123 | |||
124 | if ($crop === null) { |
||
125 | $crop = $image instanceof FileReference ? $image->getProperty('crop') : null; |
||
126 | } |
||
127 | |||
128 | $processingInstructions = [ |
||
129 | 'width' => $arguments['width'], |
||
130 | 'height' => $arguments['height'], |
||
131 | 'minWidth' => $arguments['minWidth'], |
||
132 | 'minHeight' => $arguments['minHeight'], |
||
133 | 'maxWidth' => $arguments['maxWidth'], |
||
134 | 'maxHeight' => $arguments['maxHeight'], |
||
135 | 'crop' => $crop, |
||
136 | ]; |
||
137 | $processedImage = $imageService->applyProcessingInstructions($image, $processingInstructions); |
||
138 | return $imageService->getImageUri($processedImage); |
||
139 | } catch (ResourceDoesNotExistException $e) { |
||
140 | // thrown if file does not exist |
||
141 | } catch (\UnexpectedValueException $e) { |
||
142 | // thrown if a file has been replaced with a folder |
||
143 | } catch (\RuntimeException $e) { |
||
144 | // RuntimeException thrown if a file is outside of a storage |
||
145 | } catch (\InvalidArgumentException $e) { |
||
146 | // thrown if file storage does not exist |
||
147 | } |
||
148 | return ''; |
||
149 | } |
||
150 | |||
163 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: