From 1272f88ae2aad57ab9362ba180455c437301fc27 Mon Sep 17 00:00:00 2001 From: Steve Bauman Date: Tue, 4 Jun 2024 07:38:00 -0400 Subject: [PATCH] Add missing return types (#3622) --- .../Actions/PerformConversionAction.php | 2 +- src/Conversions/Commands/RegenerateCommand.php | 2 +- src/Conversions/ConversionCollection.php | 2 +- src/InteractsWithMedia.php | 8 +++----- src/MediaCollections/Commands/CleanCommand.php | 2 +- src/MediaCollections/Commands/ClearCommand.php | 2 +- .../Exceptions/FunctionalityNotAvailable.php | 2 +- src/MediaCollections/Exceptions/InvalidUrl.php | 2 +- src/MediaCollections/File.php | 2 +- src/MediaCollections/FileAdder.php | 10 +++++----- src/MediaCollections/Filesystem.php | 2 +- src/MediaCollections/HtmlableMedia.php | 2 +- src/MediaCollections/MediaCollection.php | 4 ++-- .../Models/Collections/MediaCollection.php | 2 +- src/MediaCollections/Models/Concerns/HasUuid.php | 2 +- src/MediaCollections/Models/Media.php | 16 +++++++++------- .../Models/Observers/MediaObserver.php | 8 ++++---- src/MediaLibraryServiceProvider.php | 4 ++-- .../RegisteredResponsiveImages.php | 2 +- src/ResponsiveImages/ResponsiveImage.php | 4 ++-- tests/TestCase.php | 6 +++--- 21 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/Conversions/Actions/PerformConversionAction.php b/src/Conversions/Actions/PerformConversionAction.php index d134aef66..ebe5078ef 100644 --- a/src/Conversions/Actions/PerformConversionAction.php +++ b/src/Conversions/Actions/PerformConversionAction.php @@ -16,7 +16,7 @@ public function execute( Conversion $conversion, Media $media, string $copiedOriginalFile - ) { + ): void { $imageGenerator = ImageGeneratorFactory::forMedia($media); $copiedOriginalFile = $imageGenerator->convert($copiedOriginalFile, $conversion); diff --git a/src/Conversions/Commands/RegenerateCommand.php b/src/Conversions/Commands/RegenerateCommand.php index 34f8abaaf..9c5626a11 100644 --- a/src/Conversions/Commands/RegenerateCommand.php +++ b/src/Conversions/Commands/RegenerateCommand.php @@ -32,7 +32,7 @@ class RegenerateCommand extends Command protected array $errorMessages = []; - public function handle(MediaRepository $mediaRepository, FileManipulator $fileManipulator) + public function handle(MediaRepository $mediaRepository, FileManipulator $fileManipulator): void { $this->mediaRepository = $mediaRepository; diff --git a/src/Conversions/ConversionCollection.php b/src/Conversions/ConversionCollection.php index bd8de7f87..db795bffa 100644 --- a/src/Conversions/ConversionCollection.php +++ b/src/Conversions/ConversionCollection.php @@ -92,7 +92,7 @@ public function getConversions(string $collectionName = ''): self return $this->filter(fn (Conversion $conversion) => $conversion->shouldBePerformedOn($collectionName)); } - protected function addManipulationToConversion(Manipulations $manipulations, string $conversionName) + protected function addManipulationToConversion(Manipulations $manipulations, string $conversionName): void { /** @var Conversion|null $conversion */ $conversion = $this->first(function (Conversion $conversion) use ($conversionName) { diff --git a/src/InteractsWithMedia.php b/src/InteractsWithMedia.php index 8e998a869..de9307d01 100644 --- a/src/InteractsWithMedia.php +++ b/src/InteractsWithMedia.php @@ -39,7 +39,7 @@ trait InteractsWithMedia protected array $unAttachedMediaLibraryItems = []; - public static function bootInteractsWithMedia() + public static function bootInteractsWithMedia(): void { static::deleting(function (HasMedia $model) { if ($model->shouldDeletePreservingMedia()) { @@ -181,7 +181,6 @@ public function addMediaFromString(string $text): FileAdder /** * Add a base64 encoded file to the media library. * - * * @throws \Spatie\MediaLibrary\MediaCollections\Exceptions\FileCannotBeAdded * @throws InvalidBase64Data */ @@ -200,7 +199,7 @@ public function addMediaFromBase64(string $base64data, array|string ...$allowedM throw InvalidBase64Data::create(); } - // decoding and then reencoding should not change the data + // decoding and then re-encoding should not change the data if (base64_encode($binaryData) !== $base64data) { throw InvalidBase64Data::create(); } @@ -477,7 +476,6 @@ public function clearMediaCollectionExcept( * Delete the associated media with the given id. * You may also pass a media object. * - * * @throws \Spatie\MediaLibrary\MediaCollections\Exceptions\MediaCannotBeDeleted */ public function deleteMedia(int|string|Media $mediaId): void @@ -558,7 +556,7 @@ public function processUnattachedMedia(callable $callable): void $this->unAttachedMediaLibraryItems = []; } - protected function guardAgainstInvalidMimeType(string $file, ...$allowedMimeTypes) + protected function guardAgainstInvalidMimeType(string $file, ...$allowedMimeTypes): void { $allowedMimeTypes = Arr::flatten($allowedMimeTypes); diff --git a/src/MediaCollections/Commands/CleanCommand.php b/src/MediaCollections/Commands/CleanCommand.php index 3fc6724f1..79df4a583 100644 --- a/src/MediaCollections/Commands/CleanCommand.php +++ b/src/MediaCollections/Commands/CleanCommand.php @@ -43,7 +43,7 @@ public function handle( MediaRepository $mediaRepository, FileManipulator $fileManipulator, Factory $fileSystem, - ) { + ): void { $this->mediaRepository = $mediaRepository; $this->fileManipulator = $fileManipulator; $this->fileSystem = $fileSystem; diff --git a/src/MediaCollections/Commands/ClearCommand.php b/src/MediaCollections/Commands/ClearCommand.php index 111055e8f..eef895e23 100644 --- a/src/MediaCollections/Commands/ClearCommand.php +++ b/src/MediaCollections/Commands/ClearCommand.php @@ -19,7 +19,7 @@ class ClearCommand extends Command protected MediaRepository $mediaRepository; - public function handle(MediaRepository $mediaRepository) + public function handle(MediaRepository $mediaRepository): void { $this->mediaRepository = $mediaRepository; diff --git a/src/MediaCollections/Exceptions/FunctionalityNotAvailable.php b/src/MediaCollections/Exceptions/FunctionalityNotAvailable.php index 18bc71ed4..d3ae58a29 100644 --- a/src/MediaCollections/Exceptions/FunctionalityNotAvailable.php +++ b/src/MediaCollections/Exceptions/FunctionalityNotAvailable.php @@ -6,7 +6,7 @@ class FunctionalityNotAvailable extends Exception { - public static function mediaLibraryProRequired() + public static function mediaLibraryProRequired(): self { return new static('You need to have media library pro installed to make this work.'); } diff --git a/src/MediaCollections/Exceptions/InvalidUrl.php b/src/MediaCollections/Exceptions/InvalidUrl.php index 542e79882..bb8b98b75 100644 --- a/src/MediaCollections/Exceptions/InvalidUrl.php +++ b/src/MediaCollections/Exceptions/InvalidUrl.php @@ -6,7 +6,7 @@ class InvalidUrl extends Exception { - public static function doesNotStartWithProtocol(string $url) + public static function doesNotStartWithProtocol(string $url): self { return new static("Could not add `{$url}` because it does not start with either `http://` or `https://`"); } diff --git a/src/MediaCollections/File.php b/src/MediaCollections/File.php index 3f9abb49a..4616564c1 100644 --- a/src/MediaCollections/File.php +++ b/src/MediaCollections/File.php @@ -4,7 +4,7 @@ class File implements \Stringable { - public static function createFromMedia($media) + public static function createFromMedia($media): self { return new static($media->file_name, $media->size, $media->mime_type); } diff --git a/src/MediaCollections/FileAdder.php b/src/MediaCollections/FileAdder.php index ed441820d..a0e01e6ef 100644 --- a/src/MediaCollections/FileAdder.php +++ b/src/MediaCollections/FileAdder.php @@ -380,7 +380,7 @@ protected function determineConversionsDiskName(string $originalsDiskName, strin return $originalsDiskName; } - protected function ensureDiskExists(string $diskName) + protected function ensureDiskExists(string $diskName): void { if (is_null(config("filesystems.disks.{$diskName}"))) { throw DiskDoesNotExist::create($diskName); @@ -401,7 +401,7 @@ public function sanitizingFileName(callable $fileNameSanitizer): self return $this; } - protected function attachMedia(Media $media) + protected function attachMedia(Media $media): void { if (! $this->subject->exists) { $this->subject->prepareToAttachMedia($media, $this); @@ -420,7 +420,7 @@ protected function attachMedia(Media $media) $this->processMediaItem($this->subject, $media, $this); } - protected function processMediaItem(HasMedia $model, Media $media, self $fileAdder) + protected function processMediaItem(HasMedia $model, Media $media, self $fileAdder): void { $this->guardAgainstDisallowedFileAdditions($media); @@ -487,7 +487,7 @@ protected function getMediaCollection(string $collectionName): ?MediaCollection ->first(fn (MediaCollection $collection) => $collection->name === $collectionName); } - protected function guardAgainstDisallowedFileAdditions(Media $media) + protected function guardAgainstDisallowedFileAdditions(Media $media): void { $file = PendingFile::createFromMedia($media); @@ -504,7 +504,7 @@ protected function guardAgainstDisallowedFileAdditions(Media $media) } } - protected function checkGenerateResponsiveImages(Media $media) + protected function checkGenerateResponsiveImages(Media $media): void { $collection = optional($this->getMediaCollection($media->collection_name))->generateResponsiveImages; diff --git a/src/MediaCollections/Filesystem.php b/src/MediaCollections/Filesystem.php index 3b3d204ba..47327ebca 100644 --- a/src/MediaCollections/Filesystem.php +++ b/src/MediaCollections/Filesystem.php @@ -130,7 +130,7 @@ protected function streamFileToDisk($stream, string $destination, string $disk, ); } - public function copyToMediaLibrary(string $pathToFile, Media $media, ?string $type = null, ?string $targetFileName = null) + public function copyToMediaLibrary(string $pathToFile, Media $media, ?string $type = null, ?string $targetFileName = null): void { $destinationFileName = $targetFileName ?: pathinfo($pathToFile, PATHINFO_BASENAME); diff --git a/src/MediaCollections/HtmlableMedia.php b/src/MediaCollections/HtmlableMedia.php index 786363419..e84be8a17 100644 --- a/src/MediaCollections/HtmlableMedia.php +++ b/src/MediaCollections/HtmlableMedia.php @@ -51,7 +51,7 @@ public function lazy(): self return $this; } - public function toHtml() + public function toHtml(): string { $imageGenerator = ImageGeneratorFactory::forMedia($this->media) ?? new Image(); diff --git a/src/MediaCollections/MediaCollection.php b/src/MediaCollections/MediaCollection.php index 1ced2fec0..8f749155f 100644 --- a/src/MediaCollections/MediaCollection.php +++ b/src/MediaCollections/MediaCollection.php @@ -43,7 +43,7 @@ public function __construct( $this->acceptsFile = fn () => true; } - public static function create($name) + public static function create($name): self { return new static($name); } @@ -94,7 +94,7 @@ public function onlyKeepLatest(int $maximumNumberOfItemsInCollection): self return $this; } - public function registerMediaConversions(callable $mediaConversionRegistrations) + public function registerMediaConversions(callable $mediaConversionRegistrations): void { $this->mediaConversionRegistrations = $mediaConversionRegistrations; } diff --git a/src/MediaCollections/Models/Collections/MediaCollection.php b/src/MediaCollections/Models/Collections/MediaCollection.php index 85ab765ea..ec2c13212 100644 --- a/src/MediaCollections/Models/Collections/MediaCollection.php +++ b/src/MediaCollections/Models/Collections/MediaCollection.php @@ -37,7 +37,7 @@ public function totalSizeInBytes(): int return $this->sum('size'); } - public function toHtml() + public function toHtml(): string { return e(json_encode(old($this->formFieldName ?? $this->collectionName) ?? $this->map(function (Media $media) { return [ diff --git a/src/MediaCollections/Models/Concerns/HasUuid.php b/src/MediaCollections/Models/Concerns/HasUuid.php index 395a2b5d6..104662f1e 100644 --- a/src/MediaCollections/Models/Concerns/HasUuid.php +++ b/src/MediaCollections/Models/Concerns/HasUuid.php @@ -7,7 +7,7 @@ trait HasUuid { - public static function bootHasUuid() + public static function bootHasUuid(): void { static::creating(function (Model $model) { /** @var \Spatie\MediaLibrary\MediaCollections\Models\Media $model */ diff --git a/src/MediaCollections/Models/Media.php b/src/MediaCollections/Models/Media.php index 7aa0443fb..a34dd74f6 100644 --- a/src/MediaCollections/Models/Media.php +++ b/src/MediaCollections/Models/Media.php @@ -8,6 +8,7 @@ use Illuminate\Contracts\Support\Responsable; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Casts\Attribute; +use Illuminate\Database\Eloquent\Collection as EloquentCollection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphTo; @@ -32,6 +33,7 @@ use Spatie\MediaLibrary\Support\UrlGenerator\UrlGenerator; use Spatie\MediaLibrary\Support\UrlGenerator\UrlGeneratorFactory; use Spatie\MediaLibraryPro\Models\TemporaryUpload; +use Symfony\Component\HttpFoundation\StreamedResponse; /** * @property string $uuid @@ -80,7 +82,7 @@ class Media extends Model implements Attachable, Htmlable, Responsable protected int $streamChunkSize = (1024 * 1024); // default to 1MB chunks. - public function newCollection(array $models = []) + public function newCollection(array $models = []): MediaCollection { return new MediaCollection($models); } @@ -311,24 +313,24 @@ public function hasGeneratedConversion(string $conversionName): bool return Arr::get($generatedConversions, $conversionName, false); } - public function setStreamChunkSize(int $chunkSize) + public function setStreamChunkSize(int $chunkSize): self { $this->streamChunkSize = $chunkSize; return $this; } - public function toResponse($request) + public function toResponse($request): StreamedResponse { return $this->buildResponse($request, 'attachment'); } - public function toInlineResponse($request) + public function toInlineResponse($request): StreamedResponse { return $this->buildResponse($request, 'inline'); } - private function buildResponse($request, string $contentDispositionType) + private function buildResponse($request, string $contentDispositionType): StreamedResponse { $filename = str_replace('"', '\'', Str::ascii($this->getDownloadFilename())); @@ -433,7 +435,7 @@ public function stream() return $filesystem->getStream($this); } - public function toHtml() + public function toHtml(): string { return $this->img()->toHtml(); } @@ -457,7 +459,7 @@ public function temporaryUpload(): BelongsTo return $this->belongsTo(TemporaryUpload::class); } - public static function findWithTemporaryUploadInCurrentSession(array $uuids) + public static function findWithTemporaryUploadInCurrentSession(array $uuids): EloquentCollection { MediaLibraryPro::ensureInstalled(); diff --git a/src/MediaCollections/Models/Observers/MediaObserver.php b/src/MediaCollections/Models/Observers/MediaObserver.php index b3cd38440..2f4abe60a 100644 --- a/src/MediaCollections/Models/Observers/MediaObserver.php +++ b/src/MediaCollections/Models/Observers/MediaObserver.php @@ -8,7 +8,7 @@ class MediaObserver { - public function creating(Media $media) + public function creating(Media $media): void { if ($media->shouldSortWhenCreating()) { if (is_null($media->order_column)) { @@ -17,7 +17,7 @@ public function creating(Media $media) } } - public function updating(Media $media) + public function updating(Media $media): void { /** @var Filesystem $filesystem */ $filesystem = app(Filesystem::class); @@ -31,7 +31,7 @@ public function updating(Media $media) } } - public function updated(Media $media) + public function updated(Media $media): void { if (is_null($media->getOriginal('model_id'))) { return; @@ -52,7 +52,7 @@ public function updated(Media $media) } } - public function deleted(Media $media) + public function deleted(Media $media): void { if (method_exists($media, 'isForceDeleting') && ! $media->isForceDeleting()) { return; diff --git a/src/MediaLibraryServiceProvider.php b/src/MediaLibraryServiceProvider.php index 30842c1cb..aac9f1176 100644 --- a/src/MediaLibraryServiceProvider.php +++ b/src/MediaLibraryServiceProvider.php @@ -29,14 +29,14 @@ public function configurePackage(Package $package): void ]); } - public function packageBooted() + public function packageBooted(): void { $mediaClass = config('media-library.media_model', Media::class); $mediaClass::observe(new MediaObserver()); } - public function packageRegistered() + public function packageRegistered(): void { $this->app->bind(WidthCalculator::class, config('media-library.responsive_images.width_calculator')); $this->app->bind(TinyPlaceholderGenerator::class, config('media-library.responsive_images.tiny_placeholder_generator')); diff --git a/src/ResponsiveImages/RegisteredResponsiveImages.php b/src/ResponsiveImages/RegisteredResponsiveImages.php index 641cbefa2..cadb0c31c 100644 --- a/src/ResponsiveImages/RegisteredResponsiveImages.php +++ b/src/ResponsiveImages/RegisteredResponsiveImages.php @@ -51,7 +51,7 @@ public function getPlaceholderSvg(): ?string return $this->media->responsive_images[$this->generatedFor]['base64svg'] ?? null; } - public function delete() + public function delete(): void { $this->files->each->delete(); diff --git a/src/ResponsiveImages/ResponsiveImage.php b/src/ResponsiveImages/ResponsiveImage.php index bbbbb0ec5..ee05be7b5 100644 --- a/src/ResponsiveImages/ResponsiveImage.php +++ b/src/ResponsiveImages/ResponsiveImage.php @@ -9,7 +9,7 @@ class ResponsiveImage { - public static function register(Media $media, $fileName, $conversionName) + public static function register(Media $media, $fileName, $conversionName): void { $responsiveImages = $media->responsive_images; @@ -20,7 +20,7 @@ public static function register(Media $media, $fileName, $conversionName) $media->save(); } - public static function registerTinySvg(Media $media, string $base64Svg, string $conversionName) + public static function registerTinySvg(Media $media, string $base64Svg, string $conversionName): void { $responsiveImages = $media->responsive_images; diff --git a/tests/TestCase.php b/tests/TestCase.php index 13f4b1418..4285bfdeb 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -295,7 +295,7 @@ public function renderView($view, $parameters): string return trim((string) ($view)); } - protected function assertFileExistsInZip(string $zipPath, string $filename) + protected function assertFileExistsInZip(string $zipPath, string $filename): void { $this->assertTrue( $this->fileExistsInZip($zipPath, $filename), @@ -303,7 +303,7 @@ protected function assertFileExistsInZip(string $zipPath, string $filename) ); } - protected function assertFileExistsInZipRecognizeFolder(string $zipPath, string $filename) + protected function assertFileExistsInZipRecognizeFolder(string $zipPath, string $filename): void { $this->assertTrue( $this->fileExistsInZipRecognizeFolder($zipPath, $filename), @@ -311,7 +311,7 @@ protected function assertFileExistsInZipRecognizeFolder(string $zipPath, string ); } - protected function assertFileDoesntExistsInZip(string $zipPath, string $filename) + protected function assertFileDoesntExistsInZip(string $zipPath, string $filename): void { $this->assertFalse( $this->fileExistsInZip($zipPath, $filename),