| Conditions | 18 |
| Paths | 254 |
| Total Lines | 112 |
| Code Lines | 75 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 63 | public function getCroppedThumbnail(File $file, int $maxX, int $maxY, bool $crop): ?IImage { |
||
| 64 | $maxSizeForImages = $this->config->getSystemValue('preview_max_filesize_image', 50); |
||
| 65 | |||
| 66 | $size = $file->getSize(); |
||
| 67 | |||
| 68 | if ($maxSizeForImages !== -1 && $size > ($maxSizeForImages * 1024 * 1024)) { |
||
| 69 | return null; |
||
| 70 | } |
||
| 71 | |||
| 72 | $imaginaryUrl = $this->config->getSystemValueString('preview_imaginary_url', 'invalid'); |
||
| 73 | if ($imaginaryUrl === 'invalid') { |
||
| 74 | $this->logger->error('Imaginary preview provider is enabled, but no url is configured. Please provide the url of your imaginary server to the \'preview_imaginary_url\' config variable.'); |
||
| 75 | return null; |
||
| 76 | } |
||
| 77 | $imaginaryUrl = rtrim($imaginaryUrl, '/'); |
||
| 78 | |||
| 79 | // Object store |
||
| 80 | $stream = $file->fopen('r'); |
||
| 81 | |||
| 82 | $httpClient = $this->service->newClient(); |
||
| 83 | |||
| 84 | $convert = false; |
||
| 85 | $autorotate = true; |
||
| 86 | |||
| 87 | switch ($file->getMimeType()) { |
||
| 88 | case 'image/heic': |
||
| 89 | // Autorotate seems to be broken for Heic so disable for that |
||
| 90 | $autorotate = false; |
||
| 91 | $mimeType = 'jpeg'; |
||
| 92 | break; |
||
| 93 | case 'image/gif': |
||
| 94 | case 'image/png': |
||
| 95 | $mimeType = 'png'; |
||
| 96 | break; |
||
| 97 | case 'image/svg+xml': |
||
| 98 | case 'application/pdf': |
||
| 99 | case 'application/illustrator': |
||
| 100 | $convert = true; |
||
| 101 | // Converted files do not need to be autorotated |
||
| 102 | $autorotate = false; |
||
| 103 | $mimeType = 'png'; |
||
| 104 | break; |
||
| 105 | default: |
||
| 106 | $mimeType = 'jpeg'; |
||
| 107 | } |
||
| 108 | |||
| 109 | $operations = []; |
||
| 110 | |||
| 111 | if ($convert) { |
||
| 112 | $operations[] = [ |
||
| 113 | 'operation' => 'convert', |
||
| 114 | 'params' => [ |
||
| 115 | 'type' => $mimeType, |
||
| 116 | ] |
||
| 117 | ]; |
||
| 118 | } elseif ($autorotate) { |
||
| 119 | $operations[] = [ |
||
| 120 | 'operation' => 'autorotate', |
||
| 121 | ]; |
||
| 122 | } |
||
| 123 | |||
| 124 | $quality = $this->config->getAppValue('preview', 'jpeg_quality', '80'); |
||
| 125 | |||
| 126 | $operations[] = [ |
||
| 127 | 'operation' => ($crop ? 'smartcrop' : 'fit'), |
||
| 128 | 'params' => [ |
||
| 129 | 'width' => $maxX, |
||
| 130 | 'height' => $maxY, |
||
| 131 | 'stripmeta' => 'true', |
||
| 132 | 'type' => $mimeType, |
||
| 133 | 'norotation' => 'true', |
||
| 134 | 'quality' => $quality, |
||
| 135 | ] |
||
| 136 | ]; |
||
| 137 | |||
| 138 | try { |
||
| 139 | $response = $httpClient->post( |
||
| 140 | $imaginaryUrl . '/pipeline', [ |
||
| 141 | 'query' => ['operations' => json_encode($operations)], |
||
| 142 | 'stream' => true, |
||
| 143 | 'content-type' => $file->getMimeType(), |
||
| 144 | 'body' => $stream, |
||
| 145 | 'nextcloud' => ['allow_local_address' => true], |
||
| 146 | ]); |
||
| 147 | } catch (\Exception $e) { |
||
| 148 | $this->logger->error('Imaginary preview generation failed: ' . $e->getMessage(), [ |
||
| 149 | 'exception' => $e, |
||
| 150 | ]); |
||
| 151 | return null; |
||
| 152 | } |
||
| 153 | |||
| 154 | if ($response->getStatusCode() !== 200) { |
||
| 155 | $this->logger->error('Imaginary preview generation failed: ' . json_decode($response->getBody())['message']); |
||
| 156 | return null; |
||
| 157 | } |
||
| 158 | |||
| 159 | // This is not optimal but previews are distorted if the wrong width and height values are |
||
| 160 | // used. Both dimension headers are only sent when passing the option "-return-size" to |
||
| 161 | // Imaginary. |
||
| 162 | if ($response->getHeader('Image-Width') && $response->getHeader('Image-Height')) { |
||
| 163 | $image = new StreamImage( |
||
| 164 | $response->getBody(), |
||
| 165 | $response->getHeader('Content-Type'), |
||
| 166 | (int)$response->getHeader('Image-Width'), |
||
| 167 | (int)$response->getHeader('Image-Height'), |
||
| 168 | ); |
||
| 169 | } else { |
||
| 170 | $image = new Image(); |
||
| 171 | $image->loadFromFileHandle($response->getBody()); |
||
| 172 | } |
||
| 173 | |||
| 174 | return $image->valid() ? $image : null; |
||
| 175 | } |
||
| 184 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: