| Conditions | 6 |
| Paths | 3 |
| Total Lines | 59 |
| Code Lines | 33 |
| 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 |
||
| 60 | public function performConversions(ConversionCollection $conversions, Media $media, $onlyIfMissing = false) |
||
| 61 | { |
||
| 62 | if ($conversions->isEmpty()) { |
||
| 63 | return; |
||
| 64 | } |
||
| 65 | |||
| 66 | $imageGenerator = $this->determineImageGenerator($media); |
||
| 67 | |||
| 68 | if (! $imageGenerator) { |
||
| 69 | return; |
||
| 70 | } |
||
| 71 | |||
| 72 | $temporaryDirectory = TemporaryDirectory::create(); |
||
| 73 | |||
| 74 | $copiedOriginalFile = app(Filesystem::class)->copyFromMediaLibrary( |
||
| 75 | $media, |
||
| 76 | $temporaryDirectory->path(str_random(16).'.'.$media->extension) |
||
| 77 | ); |
||
| 78 | |||
| 79 | $conversions |
||
| 80 | ->reject(function (Conversion $conversion) use ($onlyIfMissing, $media) { |
||
| 81 | $relativePath = $media->getPath($conversion->getName()); |
||
| 82 | |||
| 83 | $rootPath = config('filesystems.disks.'.$media->disk.'.root'); |
||
| 84 | |||
| 85 | if ($rootPath) { |
||
| 86 | $relativePath = str_replace($rootPath, '', $relativePath); |
||
| 87 | } |
||
| 88 | |||
| 89 | return $onlyIfMissing && Storage::disk($media->disk)->exists($relativePath); |
||
| 90 | }) |
||
| 91 | ->each(function (Conversion $conversion) use ($media, $imageGenerator, $copiedOriginalFile) { |
||
| 92 | event(new ConversionWillStart($media, $conversion)); |
||
| 93 | |||
| 94 | $copiedOriginalFile = $imageGenerator->convert($copiedOriginalFile, $conversion); |
||
| 95 | |||
| 96 | $conversionResult = $this->performConversion($media, $conversion, $copiedOriginalFile); |
||
| 97 | |||
| 98 | $newFileName = pathinfo($media->file_name, PATHINFO_FILENAME). |
||
| 99 | '-'.$conversion->getName(). |
||
| 100 | '.'.$conversion->getResultExtension(pathinfo($copiedOriginalFile, PATHINFO_EXTENSION)); |
||
| 101 | |||
| 102 | $renamedFile = MediaLibraryFileHelper::renameInDirectory($conversionResult, $newFileName); |
||
| 103 | |||
| 104 | if ($conversion->shouldGenerateResponsiveImages()) { |
||
| 105 | app(ResponsiveImageGenerator::class)->generateResponsiveImagesForConversion( |
||
| 106 | $media, |
||
| 107 | $conversion, |
||
| 108 | $renamedFile |
||
| 109 | ); |
||
| 110 | } |
||
| 111 | |||
| 112 | app(Filesystem::class)->copyToMediaLibrary($renamedFile, $media, 'conversions'); |
||
| 113 | |||
| 114 | event(new ConversionHasBeenCompleted($media, $conversion)); |
||
| 115 | }); |
||
| 116 | |||
| 117 | $temporaryDirectory->delete(); |
||
| 118 | } |
||
| 119 | |||
| 168 |
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: