Skip to content

Commit

Permalink
Add phpstan and resolve for level 5
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil committed Oct 16, 2023
1 parent 24e0a8b commit 7828de9
Show file tree
Hide file tree
Showing 42 changed files with 157 additions and 117 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ jobs:
composer-options: '--working-dir=tools/php-cs-fixer'
- name: 'php-cs-fixer check'
run: 'tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --dry-run --diff'
phpstan:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@master
- name: setup
uses: shivammathur/[email protected]
with:
php-version: 8.2
coverage: none
- name: install phpstan
uses: ramsey/composer-install@v2
with:
composer-options: '--working-dir=tools/phpstan'
- name: 'php-cs-fixer check'
run: 'tools/phpstan/vendor/bin/phpstan analyse --configuration=tools/phpstan/phpstan.neon'
tests:
runs-on: ubuntu-latest
name: tests
Expand Down
8 changes: 0 additions & 8 deletions src/AutoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,6 @@ public function map(null|array|object $source, string|array|object $target, arra
$sourceType = 'array';
}

if (null === $sourceType) {
throw new NoMappingFoundException('Cannot map this value, source is neither an object or an array.');
}

if (\is_object($target)) {
$targetType = \get_class($target);
$context[MapperContext::TARGET_TO_POPULATE] = $target;
Expand All @@ -120,10 +116,6 @@ public function map(null|array|object $source, string|array|object $target, arra
$targetType = $target;
}

if (null === $targetType) {
throw new NoMappingFoundException('Cannot map this value, target is neither an object or an array.');
}

if ('array' === $sourceType && 'array' === $targetType) {
throw new NoMappingFoundException('Cannot map this value, both source and target are array.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Extractor/FromSourceMappingExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function getPropertiesMapping(MapperMetadataInterface $mapperMetadata): a
$this->getMaxDepth($mapperMetadata->getSource(), $property),
$this->isIgnoredProperty($mapperMetadata->getSource(), $property),
$this->isIgnoredProperty($mapperMetadata->getTarget(), $property),
PropertyReadInfo::VISIBILITY_PUBLIC === $this->readInfoExtractor->getReadInfo($mapperMetadata->getSource(), $property)?->getVisibility() ?? true,
PropertyReadInfo::VISIBILITY_PUBLIC === ($this->readInfoExtractor->getReadInfo($mapperMetadata->getSource(), $property)?->getVisibility() ?? true)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Extractor/FromTargetMappingExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
PropertyWriteInfoExtractorInterface $writeInfoExtractor,
TransformerFactoryInterface $transformerFactory,
ClassMetadataFactoryInterface $classMetadataFactory = null,
private ?AdvancedNameConverterInterface $nameConverter = null,
private readonly ?AdvancedNameConverterInterface $nameConverter = null,
) {
parent::__construct($propertyInfoExtractor, $readInfoExtractor, $writeInfoExtractor, $transformerFactory, $classMetadataFactory);
}
Expand Down Expand Up @@ -86,7 +86,7 @@ public function getPropertiesMapping(MapperMetadataInterface $mapperMetadata): a
$this->getMaxDepth($mapperMetadata->getTarget(), $property),
$this->isIgnoredProperty($mapperMetadata->getSource(), $property),
$this->isIgnoredProperty($mapperMetadata->getTarget(), $property),
PropertyReadInfo::VISIBILITY_PUBLIC === $this->readInfoExtractor->getReadInfo($mapperMetadata->getSource(), $property)?->getVisibility() ?? true,
PropertyReadInfo::VISIBILITY_PUBLIC === ($this->readInfoExtractor->getReadInfo($mapperMetadata->getSource(), $property)?->getVisibility() ?? true),
);
}

Expand Down
8 changes: 2 additions & 6 deletions src/Extractor/MappingExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ protected function getMaxDepth($class, $property): ?int
return null;
}

if (!$this->classMetadataFactory->getMetadataFor($class)) {
return null;
}

$serializerClassMetadata = $this->classMetadataFactory->getMetadataFor($class);
$maxDepth = null;

Expand All @@ -134,7 +130,7 @@ protected function getGroups($class, $property): ?array
return null;
}

if (null === $this->classMetadataFactory || !$this->classMetadataFactory->getMetadataFor($class)) {
if (null === $this->classMetadataFactory) {
return null;
}

Expand Down Expand Up @@ -167,7 +163,7 @@ protected function isIgnoredProperty($class, $property): bool
return false;
}

if (null === $this->classMetadataFactory || !$this->classMetadataFactory->getMetadataFor($class)) {
if (null === $this->classMetadataFactory) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Extractor/SourceTargetMappingExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getPropertiesMapping(MapperMetadataInterface $mapperMetadata): a
$maxDepth,
$this->isIgnoredProperty($mapperMetadata->getSource(), $property),
$this->isIgnoredProperty($mapperMetadata->getTarget(), $property),
PropertyReadInfo::VISIBILITY_PUBLIC === $this->readInfoExtractor->getReadInfo($mapperMetadata->getSource(), $property)?->getVisibility() ?? true,
PropertyReadInfo::VISIBILITY_PUBLIC === ($this->readInfoExtractor->getReadInfo($mapperMetadata->getSource(), $property)?->getVisibility() ?? true),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function generate(MapperGeneratorMetadataInterface $mapperGeneratorMetada
]),
new Scalar\String_($mapperGeneratorMetadata->getTarget())
)));
$statements[] = new Stmt\If_(new Expr\StaticCall(new Name\FullyQualified(MapperContext::class), new Name('shouldHandleCircularReference'), [
$statements[] = new Stmt\If_(new Expr\StaticCall(new Name\FullyQualified(MapperContext::class), 'shouldHandleCircularReference', [
new Arg($contextVariable),
new Arg($hashVariable),
new Arg(new Expr\PropertyFetch(new Expr\Variable('this'), 'circularReferenceLimit')),
Expand Down Expand Up @@ -339,7 +339,7 @@ public function generate(MapperGeneratorMetadataInterface $mapperGeneratorMetada
]);
}

return new Stmt\Class_(new Name($mapperGeneratorMetadata->getMapperClassName()), [
return new Stmt\Class_($mapperGeneratorMetadata->getMapperClassName(), [
'flags' => Stmt\Class_::MODIFIER_FINAL,
'extends' => new Name\FullyQualified(GeneratedMapper::class),
'stmts' => $classStmts,
Expand Down
3 changes: 2 additions & 1 deletion src/Normalizer/AutoMapperNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace AutoMapper\Normalizer;

use AutoMapper\AutoMapperInterface;
use AutoMapper\AutoMapperRegistryInterface;
use AutoMapper\MapperContext;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
Expand All @@ -30,7 +31,7 @@
];

public function __construct(
private AutoMapperInterface $autoMapper,
private AutoMapperInterface&AutoMapperRegistryInterface $autoMapper,
) {
}

Expand Down
2 changes: 1 addition & 1 deletion src/Transformer/BuiltinTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* @author Joel Wurtz <[email protected]>
*/
final class BuiltinTransformer implements TransformerInterface
final readonly class BuiltinTransformer implements TransformerInterface
{
private const CAST_MAPPING = [
Type::BUILTIN_TYPE_BOOL => [
Expand Down
2 changes: 1 addition & 1 deletion src/Transformer/ChainTransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
final class ChainTransformerFactory implements TransformerFactoryInterface
{
/** @var TransformerFactoryInterface[] */
/** @var array<TransformerFactoryInterface[]> */
private array $factories = [];

/** @var TransformerFactoryInterface[]|null */
Expand Down
6 changes: 3 additions & 3 deletions src/Transformer/DateTimeTransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ protected function createTransformerForSource(Type $targetType, MapperMetadataIn

protected function createTransformerForTarget(Type $sourceType, Type $targetType, MapperMetadataInterface $mapperMetadata): ?TransformerInterface
{
if (Type::BUILTIN_TYPE_STRING === $sourceType->getBuiltinType()) {
return new StringToDateTimeTransformer($this->getClassName($targetType), $mapperMetadata->getDateTimeFormat());
if (Type::BUILTIN_TYPE_STRING === $sourceType->getBuiltinType() && null !== ($targetClassName = $this->getClassName($targetType))) {
return new StringToDateTimeTransformer($targetClassName, $mapperMetadata->getDateTimeFormat());
}

return null;
Expand All @@ -79,7 +79,7 @@ private function isDateTimeType(Type $type): bool
return true;
}

private function getClassName(Type $type): ?string
private function getClassName(Type $type): string
{
if (\DateTimeInterface::class !== $type->getClassName()) {
return \DateTimeImmutable::class;
Expand Down
14 changes: 8 additions & 6 deletions src/Transformer/EnumTransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected function createTransformer(Type $sourceType, Type $targetType, MapperM
}

// target is enum, source isn't
if (!$this->isEnumType($sourceType) && $this->isEnumType($targetType, true)) {
if (!$this->isEnumType($sourceType) && $this->isEnumType($targetType, true) && null !== $targetType->getClassName()) {
return new TargetEnumTransformer($targetType->getClassName());
}

Expand All @@ -38,12 +38,14 @@ private function isEnumType(Type $type, bool $backed = false): bool
return false;
}

if (!is_subclass_of($type->getClassName(), \UnitEnum::class)) {
return false;
}
if (null !== $type->getClassName()) {
if (!is_subclass_of($type->getClassName(), \UnitEnum::class)) {
return false;
}

if ($backed && !is_subclass_of($type->getClassName(), \BackedEnum::class)) {
return false;
if ($backed && !is_subclass_of($type->getClassName(), \BackedEnum::class)) {
return false;
}
}

return true;
Expand Down
3 changes: 3 additions & 0 deletions src/Transformer/MultipleTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ final class MultipleTransformer implements TransformerInterface, DependentTransf
Type::BUILTIN_TYPE_ITERABLE => 'is_iterable',
];

/**
* @param array<array{transformer: TransformerInterface, type: Type}> $transformers
*/
public function __construct(
private readonly array $transformers,
) {
Expand Down
3 changes: 0 additions & 3 deletions src/Transformer/MultipleTransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public function __construct(
) {
}

/**
* {@inheritdoc}
*/
public function getTransformer(?array $sourceTypes, ?array $targetTypes, MapperMetadataInterface $mapperMetadata): ?TransformerInterface
{
if (null === $sourceTypes || \count($sourceTypes) <= 1) {
Expand Down
2 changes: 1 addition & 1 deletion src/Transformer/NullableTransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getTransformer(?array $sourceTypes, ?array $targetTypes, MapperM

$isTargetNullable = false;

foreach ($targetTypes as $targetType) {
foreach ($targetTypes ?? [] as $targetType) {
if ($targetType->isNullable()) {
$isTargetNullable = true;

Expand Down
12 changes: 11 additions & 1 deletion src/Transformer/ObjectTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @author Joel Wurtz <[email protected]>
*/
final class ObjectTransformer implements TransformerInterface, DependentTransformerInterface, AssignedByReferenceTransformerInterface
final readonly class ObjectTransformer implements TransformerInterface, DependentTransformerInterface, AssignedByReferenceTransformerInterface
{
public function __construct(
private Type $sourceType,
Expand Down Expand Up @@ -62,6 +62,11 @@ private function getSource(): string
$sourceTypeName = 'array';

if (Type::BUILTIN_TYPE_OBJECT === $this->sourceType->getBuiltinType()) {
/**
* Cannot be null since we check the source type is an Object.
*
* @var string $sourceTypeName
*/
$sourceTypeName = $this->sourceType->getClassName();
}

Expand All @@ -73,6 +78,11 @@ private function getTarget(): string
$targetTypeName = 'array';

if (Type::BUILTIN_TYPE_OBJECT === $this->targetType->getBuiltinType()) {
/**
* Cannot be null since we check the target type is an Object.
*
* @var string $targetTypeName
*/
$targetTypeName = $this->targetType->getClassName();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Transformer/ObjectTransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private function isObjectType(Type $type): bool
return false;
}

if (is_subclass_of($type->getClassName(), \UnitEnum::class)) {
if (null !== $type->getClassName() && is_subclass_of($type->getClassName(), \UnitEnum::class)) {
return false;
}

Expand Down
16 changes: 10 additions & 6 deletions src/Transformer/SymfonyUidTransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/
final class SymfonyUidTransformerFactory extends AbstractUniqueTypeTransformerFactory implements PrioritizedTransformerFactoryInterface
{
/** @var array<string, array{0: bool, 1: bool}> */
private array $reflectionCache = [];

protected function createTransformer(Type $sourceType, Type $targetType, MapperMetadataInterface $mapperMetadata): ?TransformerInterface
Expand All @@ -29,7 +30,7 @@ protected function createTransformer(Type $sourceType, Type $targetType, MapperM
return new SymfonyUidToStringTransformer($this->reflectionCache[$sourceType->getClassName()][1]);
}

if ($isTargetUid) {
if ($isTargetUid && null !== $targetType->getClassName()) {
return new StringToSymfonyUidTransformer($targetType->getClassName());
}

Expand All @@ -42,16 +43,19 @@ private function isUid(Type $type): bool
return false;
}

if (null === $type->getClassName()) {
/** @var class-string|null $typeClassName */
$typeClassName = $type->getClassName();

if (null === $typeClassName) {
return false;
}

if (!\array_key_exists($type->getClassName(), $this->reflectionCache)) {
$reflClass = new \ReflectionClass($type->getClassName());
$this->reflectionCache[$type->getClassName()] = [$reflClass->isSubclassOf(AbstractUid::class), $type->getClassName() === Ulid::class];
if (!\array_key_exists($typeClassName, $this->reflectionCache)) {
$reflClass = new \ReflectionClass($typeClassName);
$this->reflectionCache[$typeClassName] = [$reflClass->isSubclassOf(AbstractUid::class), $typeClassName === Ulid::class];
}

return $this->reflectionCache[$type->getClassName()][0];
return $this->reflectionCache[$typeClassName][0];
}

public function getPriority(): int
Expand Down
4 changes: 2 additions & 2 deletions src/Transformer/TransformerFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ interface TransformerFactoryInterface
/**
* Get transformer to use when mapping from an array of type to another array of type.
*
* @param Type[] $sourceTypes
* @param Type[] $targetTypes
* @param Type[]|null $sourceTypes
* @param Type[]|null $targetTypes
*/
public function getTransformer(?array $sourceTypes, ?array $targetTypes, MapperMetadataInterface $mapperMetadata): ?TransformerInterface;
}
4 changes: 0 additions & 4 deletions src/Transformer/UniqueTypeTransformerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ public function getTransformer(?array $sourceTypes, ?array $targetTypes, MapperM
}

foreach ($targetTypes as $targetType) {
if (null === $targetType) {
continue;
}

$transformer = $this->chainTransformerFactory->getTransformer($sourceTypes, [$targetType], $mapperMetadata);

if (null !== $transformer) {
Expand Down
7 changes: 2 additions & 5 deletions tests/AutoMapperBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,8 @@
*/
abstract class AutoMapperBaseTest extends TestCase
{
/** @var AutoMapper */
protected $autoMapper;

/** @var ClassLoaderInterface */
protected $loader;
protected AutoMapper $autoMapper;
protected ClassLoaderInterface $loader;

protected function setUp(): void
{
Expand Down
8 changes: 4 additions & 4 deletions tests/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ public function testSkipNullValues(): void
self::assertEquals('foobar', $entity->getName());
}

public function testAdderAndRemoverWithClass()
public function testAdderAndRemoverWithClass(): void
{
$this->buildAutoMapper(mapPrivatePropertiesAndMethod: true);

Expand All @@ -959,7 +959,7 @@ public function testAdderAndRemoverWithClass()
self::assertSame('dog', $petOwnerData->getPets()[1]->type);
}

public function testAdderAndRemoverWithInstance()
public function testAdderAndRemoverWithInstance(): void
{
$this->buildAutoMapper(mapPrivatePropertiesAndMethod: true);

Expand Down Expand Up @@ -989,7 +989,7 @@ public function testAdderAndRemoverWithInstance()
self::assertSame('dog', $petOwner->getPets()[2]->type);
}

public function testAdderAndRemoverWithNull()
public function testAdderAndRemoverWithNull(): void
{
$petOwner = [
'pets' => [
Expand All @@ -1004,7 +1004,7 @@ public function testAdderAndRemoverWithNull()
self::assertCount(0, $petOwnerData->getPets());
}

public function testIssueTargetToPopulate()
public function testIssueTargetToPopulate(): void
{
$source = new Fixtures\IssueTargetToPopulate\VatModel();
$source->setCountryCode('fr');
Expand Down
Loading

0 comments on commit 7828de9

Please sign in to comment.