| Conditions | 14 | 
| Paths | 770 | 
| Total Lines | 64 | 
| Code Lines | 35 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 3 | ||
| Bugs | 1 | Features | 1 | 
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 | ||
| 93 | public function apply(BinaryInterface $binary, array $config) | ||
| 94 |     { | ||
| 95 | $config = array_replace( | ||
| 96 | array( | ||
| 97 | 'filters' => array(), | ||
| 98 | 'quality' => 100, | ||
| 99 | 'animated' => false, | ||
| 100 | ), | ||
| 101 | $config | ||
| 102 | ); | ||
| 103 | |||
| 104 |         if ($binary instanceof FileBinaryInterface) { | ||
| 105 | $image = $this->imagine->open($binary->getPath()); | ||
| 106 |         } else { | ||
| 107 | $image = $this->imagine->load($binary->getContent()); | ||
| 108 | } | ||
| 109 | |||
| 110 |         foreach ($config['filters'] as $eachFilter => $eachOptions) { | ||
| 111 |             if (!isset($this->loaders[$eachFilter])) { | ||
| 112 | throw new \InvalidArgumentException(sprintf( | ||
| 113 | 'Could not find filter loader for "%s" filter type', $eachFilter | ||
| 114 | )); | ||
| 115 | } | ||
| 116 | |||
| 117 | $prevImage = $image; | ||
| 118 | $image = $this->loaders[$eachFilter]->load($image, $eachOptions); | ||
| 119 | |||
| 120 | // If the filter returns a different image object destruct the old one because imagick keeps consuming memory if we don't | ||
| 121 | // See https://github.com/liip/LiipImagineBundle/pull/682 | ||
| 122 |             if ($prevImage !== $image && method_exists($prevImage, '__destruct')) { | ||
| 123 | $prevImage->__destruct(); | ||
|  | |||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | $options = array( | ||
| 128 | 'quality' => $config['quality'], | ||
| 129 | ); | ||
| 130 | |||
| 131 |         if (isset($config['jpeg_quality'])) { | ||
| 132 | $options['jpeg_quality'] = $config['jpeg_quality']; | ||
| 133 | } | ||
| 134 |         if (isset($config['png_compression_level'])) { | ||
| 135 | $options['png_compression_level'] = $config['png_compression_level']; | ||
| 136 | } | ||
| 137 |         if (isset($config['png_compression_filter'])) { | ||
| 138 | $options['png_compression_filter'] = $config['png_compression_filter']; | ||
| 139 | } | ||
| 140 | |||
| 141 |         if ($binary->getFormat() === 'gif' && $config['animated']) { | ||
| 142 | $options['animated'] = $config['animated']; | ||
| 143 | } | ||
| 144 | |||
| 145 | $filteredFormat = isset($config['format']) ? $config['format'] : $binary->getFormat(); | ||
| 146 | $filteredContent = $image->get($filteredFormat, $options); | ||
| 147 | $filteredMimeType = $filteredFormat === $binary->getFormat() ? $binary->getMimeType() : $this->mimeTypeGuesser->guess($filteredContent); | ||
| 148 | |||
| 149 | // We are done with the image object so we can destruct the this because imagick keeps consuming memory if we don't | ||
| 150 | // See https://github.com/liip/LiipImagineBundle/pull/682 | ||
| 151 |         if (method_exists($image, '__destruct')) { | ||
| 152 | $image->__destruct(); | ||
| 153 | } | ||
| 154 | |||
| 155 | return $this->applyPostProcessors(new Binary($filteredContent, $filteredMimeType, $filteredFormat), $config); | ||
| 156 | } | ||
| 157 | |||
| 202 | 
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: