Skip to content

Commit

Permalink
BRAIN: Symfony 7 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
lernhart committed Jan 19, 2024
1 parent 74aba5f commit 6bb193a
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 20 deletions.
4 changes: 4 additions & 0 deletions config/services/braintree.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@

<tag name="shop">controller.argument_value_resolver</tag>
</service>

<service id="Swag\Braintree\Framework\Serializer\EntityNormalizer">
<argument type="service" id="serializer.normalizer.object"/>
</service>
</services>
</container>
5 changes: 0 additions & 5 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,16 @@ rules:
- Symplify\PHPStanRules\Rules\NoIssetOnObjectRule

# explicit naming
- Symplify\PHPStanRules\Rules\NoParentMethodCallOnNoOverrideProcessRule
- Symplify\PHPStanRules\Rules\ForbiddenMultipleClassLikeInOneFileRule

- Symplify\PHPStanRules\Rules\Complexity\ForbiddenArrayMethodCallRule

# complexity rules
- Symplify\PHPStanRules\Rules\Explicit\NoMixedPropertyFetcherRule
- Symplify\PHPStanRules\Rules\Explicit\NoMixedMethodCallerRule
- Symplify\PHPStanRules\Rules\PHPUnit\NoRightPHPUnitAssertScalarRule
- Symplify\PHPStanRules\Rules\NoDynamicNameRule

# naming rules
- Symplify\PHPStanRules\Rules\NoVoidGetterMethodRule
- Symplify\PHPStanRules\Rules\UppercaseConstantRule
- Symplify\PHPStanRules\Rules\CheckClassNamespaceFollowPsr4Rule

# test rules
- Symplify\PHPStanRules\Rules\NoConstructorInTestRule
9 changes: 8 additions & 1 deletion src/Framework/Serializer/BraintreeNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function normalize(mixed $object, string $format = null, array $context =
return \array_map(fn (Instance $braintreeObject) => $braintreeObject->toArray(), $object);
}

public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool

Check failure on line 23 in src/Framework/Serializer/BraintreeNormalizer.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Swag\Braintree\Framework\Serializer\BraintreeNormalizer::supportsNormalization() has parameter $context with no value type specified in iterable type array.
{
if (!\is_array($data) || \count($data) === 0) {
return false;
Expand All @@ -34,4 +34,11 @@ public function supportsNormalization(mixed $data, string $format = null): bool

return true;
}

public function getSupportedTypes(?string $format): array

Check failure on line 38 in src/Framework/Serializer/BraintreeNormalizer.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Swag\Braintree\Framework\Serializer\BraintreeNormalizer::getSupportedTypes() return type has no value type specified in iterable type array.
{
return [
Instance::class => true,
];
}
}
12 changes: 10 additions & 2 deletions src/Framework/Serializer/CollectionNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function normalize(mixed $object, string $format = null, array $context =
})->toArray();
}

public function supportsNormalization(mixed $data, string $format = null): bool
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool

Check failure on line 33 in src/Framework/Serializer/CollectionNormalizer.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Swag\Braintree\Framework\Serializer\CollectionNormalizer::supportsNormalization() has parameter $context with no value type specified in iterable type array.
{
return $data instanceof Collection && $data->forAll(function ($_, $item) {
return $item instanceof EntityInterface || $item instanceof AbstractShop;
Expand All @@ -47,8 +47,16 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
return new ArrayCollection($data);
}

public function supportsDenormalization(mixed $data, string $type, string $format = null): bool
public function supportsDenormalization(mixed $data, string $type, string $format = null, array $context = []): bool

Check failure on line 50 in src/Framework/Serializer/CollectionNormalizer.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Swag\Braintree\Framework\Serializer\CollectionNormalizer::supportsDenormalization() has parameter $context with no value type specified in iterable type array.
{
return \is_subclass_of($type, Collection::class, true) && \is_array($data);
}

public function getSupportedTypes(?string $format): array

Check failure on line 55 in src/Framework/Serializer/CollectionNormalizer.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Swag\Braintree\Framework\Serializer\CollectionNormalizer::getSupportedTypes() return type has no value type specified in iterable type array.
{
return [
AbstractShop::class => true,
Collection::class => true,
];
}
}
15 changes: 11 additions & 4 deletions src/Framework/Serializer/EntityNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Symfony\Component\DependencyInjection\Attribute\AutoconfigureTag;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

#[AutoconfigureTag(name: 'serializer.normalizer')]
class EntityNormalizer implements NormalizerInterface
Expand All @@ -17,15 +16,15 @@ class EntityNormalizer implements NormalizerInterface
private const REGEX = '/.*(Swag.*)/';

public function __construct(
private readonly ObjectNormalizer $normalizer
private readonly NormalizerInterface $normalizer
) {
}

/**
* @param EntityInterface|AbstractShop $object
* @param array<string, mixed> $context
*/
public function normalize(mixed $object, string $format = null, array $context = []): mixed
public function normalize(mixed $object, string $format = null, array $context = []): string|array|\ArrayObject|bool|float|int|null
{
if (\array_key_exists(self::ORIGINAL_DATA, $context) && $context[self::ORIGINAL_DATA]) {
$objectClass = $this->normalizeSwagNamespace($object::class);
Expand All @@ -43,7 +42,7 @@ public function normalize(mixed $object, string $format = null, array $context =
return $this->normalizer->normalize($object, $format, $context);
}

public function supportsNormalization(mixed $data, string $format = null): mixed
public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool

Check failure on line 45 in src/Framework/Serializer/EntityNormalizer.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Swag\Braintree\Framework\Serializer\EntityNormalizer::supportsNormalization() has parameter $context with no value type specified in iterable type array.
{
return $data instanceof EntityInterface || $data instanceof AbstractShop;
}
Expand All @@ -61,4 +60,12 @@ public function normalizeSwagNamespace(string $class): string

return $matches[1];
}

public function getSupportedTypes(?string $format): array

Check failure on line 64 in src/Framework/Serializer/EntityNormalizer.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Swag\Braintree\Framework\Serializer\EntityNormalizer::getSupportedTypes() return type has no value type specified in iterable type array.
{
return [
AbstractShop::class => true,
EntityInterface::class => true,
];
}
}
4 changes: 2 additions & 2 deletions src/Tests/Serializer/TestSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Swag\Braintree\Framework\Serializer\EntityNormalizer;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\ProblemNormalizer;
Expand All @@ -21,7 +21,7 @@ class TestSerializer
{
public static function create(): Serializer
{
$objectNormalizer = new ObjectNormalizer(new ClassMetadataFactory(new AnnotationLoader()));
$objectNormalizer = new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()));

return new Serializer([
new UidNormalizer(),
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/Framework/Serializer/BraintreeNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function testSupportsNormalization(): void
static::assertFalse($this->normalizer->supportsNormalization([$instance, $entity]));
static::assertFalse($this->normalizer->supportsNormalization(null));
}

public function testSupportedTypes(): void
{
static::assertSame(
[Instance::class => true],
$this->normalizer->getSupportedTypes(null)
);
}
}

class TestInstance extends Instance
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/Framework/Serializer/CollectionNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,15 @@ public function testSupportsDenormalization(): void
static::assertFalse($this->normalizer->supportsDenormalization([], 'null'));
static::assertFalse($this->normalizer->supportsDenormalization([], 'stdClass'));
}

public function testSupportedTypes(): void
{
static::assertSame(
[
AbstractShop::class => true,
Collection::class => true,
],
$this->normalizer->getSupportedTypes(null)
);
}
}
25 changes: 19 additions & 6 deletions tests/unit/Framework/Serializer/EntityNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Swag\Braintree\Tests\Entity;
use Swag\Braintree\Tests\IdsCollection;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

#[CoversClass(EntityNormalizer::class)]
Expand All @@ -22,11 +24,11 @@ class EntityNormalizerTest extends TestCase

protected function setUp(): void
{
$objectNormalizer = $this->createMock(ObjectNormalizer::class);
$objectNormalizer = $this->createMock(NormalizerInterface::class);
$objectNormalizer
->expects(static::any())
->method('normalize')
->willReturnCallback(fn (mixed $object) => $object);
->willReturnCallback(fn (mixed $object) => [$object]);

$this->normalizer = new EntityNormalizer($objectNormalizer);
$this->ids = new IdsCollection();
Expand All @@ -44,8 +46,8 @@ public function testNormalizeWithEntity(): void
$entity = new Entity();
$entity->setId($this->ids->getUuid('entity'));

static::assertSame($entity, $this->normalizer->normalize($entity, null, [EntityNormalizer::ORIGINAL_DATA => $entity]));
static::assertSame($entity, $this->normalizer->normalize($entity));
static::assertSame([$entity], $this->normalizer->normalize($entity, null, [EntityNormalizer::ORIGINAL_DATA => $entity]));
static::assertSame([$entity], $this->normalizer->normalize($entity));

static::assertSame($this->ids->get('entity'), $this->normalizer->normalize($entity, null, [EntityNormalizer::ORIGINAL_DATA => new ShopEntity('', '', '')]));
}
Expand All @@ -54,8 +56,8 @@ public function testNormalizeWithShop(): void
{
$shop = new ShopEntity($this->ids->get('shop'), '', '');

static::assertSame($shop, $this->normalizer->normalize($shop, null, [EntityNormalizer::ORIGINAL_DATA => $shop]));
static::assertSame($shop, $this->normalizer->normalize($shop));
static::assertSame([$shop], $this->normalizer->normalize($shop, null, [EntityNormalizer::ORIGINAL_DATA => $shop]));
static::assertSame([$shop], $this->normalizer->normalize($shop));

static::assertSame($this->ids->get('shop'), $this->normalizer->normalize($shop, null, [EntityNormalizer::ORIGINAL_DATA => new Entity()]));
}
Expand All @@ -82,4 +84,15 @@ public function testNormalizeNamespace(): void
{
static::assertSame('Swag\Entity\Braintree', $this->normalizer->normalizeSwagNamespace('Proxies\__CG__\Swag\Entity\Braintree'));
}

public function testSupportedTypes(): void
{
static::assertSame(
[
AbstractShop::class => true,
EntityInterface::class => true,
],
$this->normalizer->getSupportedTypes(null)
);
}
}

0 comments on commit 6bb193a

Please sign in to comment.