rectorphp / swiss-knife

Swiss knife in pocket of every upgrade architect!
https://getrector.com
MIT License
71 stars 8 forks source link

[finalize-classes] Finalized a class that was extended #16

Open gnutix opened 4 months ago

gnutix commented 4 months ago

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 commented 3 months ago

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

TomasVotruba commented 2 months ago

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: