swiss-knife icon indicating copy to clipboard operation
swiss-knife copied to clipboard

[finalize-classes] Finalized a class that was extended

Open gnutix opened this issue 1 year ago • 2 comments

In a folder, I have two files :

  1. BlobNormalizer.php (non final)
  2. FileBlobNormalizer.php (final, extends BlobNormalizer)

Running vendor/bin/swiss-knife finalize-classes [folder] adds final to BlobNormalizer. Here are the files :

<?php

declare(strict_types=1);

namespace Gammadia\Storage\Symfony\Normalizer;

use Gammadia\Storage\Blob\Blob;
use Gammadia\Storage\Blob\Utils\BlobAdapter;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Webmozart\Assert\Assert;

class BlobNormalizer implements DenormalizerInterface
{
    public function __construct(
        private BlobAdapter $blobAdapter,
    ) {
    }

    /**
     * @param class-string $type
     * @param array<mixed> $context
     */
    public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): Blob
    {
        Assert::isInstanceOf($data, UploadedFile::class);

        return $this->blobAdapter->fromUploadedFile($data);
    }

    /**
     * @param class-string $type
     * @param array<mixed> $context
     */
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
    {
        return Blob::class === $type;
    }

    /**
     * @return array<class-string|'*'|'object'|string, bool|null>
     */
    public function getSupportedTypes(?string $format): array
    {
        return [Blob::class => false];
    }
}

and

<?php

declare(strict_types=1);

namespace Gammadia\Storage\Symfony\Normalizer;

use Gammadia\Storage\Blob\Blob;
use Gammadia\Storage\Blob\Decorator\FileBlob;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;

final class FileBlobNormalizer extends BlobNormalizer
{
    /**
     * @param class-string $type
     * @param array<mixed> $context
     */
    public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): Blob
    {
        $blob = parent::denormalize($data, $type, $format, $context);

        if (!$blob instanceof FileBlob) {
            throw new NotNormalizableValueException('The blob does not have a filename.');
        }

        return $blob;
    }

    /**
     * @param class-string $type
     * @param array<mixed> $context
     */
    public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
    {
        return FileBlob::class === $type;
    }
}

gnutix avatar Mar 19 '24 06:03 gnutix

I tried adding unit tests in #22, but they do not highlight this issue.. :(

gnutix avatar Apr 04 '24 08:04 gnutix

This would be hard to resolve with a unit test. Could you share the smallest possible reproducible repository? Ideally with failing Github Action, so we have the exact CLI command.

I'll check it to see if there is some glitch :slightly_smiling_face:

TomasVotruba avatar May 10 '24 05:05 TomasVotruba