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