diff --git a/src/Symfony/Bundle/DependencyInjection/AutoMapperExtension.php b/src/Symfony/Bundle/DependencyInjection/AutoMapperExtension.php index d76e3334..626366b4 100644 --- a/src/Symfony/Bundle/DependencyInjection/AutoMapperExtension.php +++ b/src/Symfony/Bundle/DependencyInjection/AutoMapperExtension.php @@ -76,6 +76,12 @@ public function load(array $configs, ContainerBuilder $container): void if ($config['map_private_properties']) { $container->getDefinition('automapper.property_info.reflection_extractor') ->replaceArgument('$accessFlags', ReflectionExtractor::ALLOW_PUBLIC | ReflectionExtractor::ALLOW_PRIVATE | ReflectionExtractor::ALLOW_PROTECTED); + + $container->getDefinition('automapper.property_info.phpstan_source_extractor') + ->replaceArgument('$allowPrivateAccess', true); + + $container->getDefinition('automapper.property_info.phpstan_target_extractor') + ->replaceArgument('$allowPrivateAccess', true); } $container->setParameter('automapper.map_private_properties', $config['map_private_properties']); diff --git a/tests/Bundle/Resources/App/Entity/ClassWithPrivateProperty.php b/tests/Bundle/Resources/App/Entity/ClassWithPrivateProperty.php index 119ad15d..a20df7be 100644 --- a/tests/Bundle/Resources/App/Entity/ClassWithPrivateProperty.php +++ b/tests/Bundle/Resources/App/Entity/ClassWithPrivateProperty.php @@ -6,9 +6,14 @@ class ClassWithPrivateProperty { + /** @var Address[] */ + private array $addresses = []; + public function __construct( private string $foo, + array $addresses = [], ) { + $this->addresses = $addresses; } private function getBar(): string diff --git a/tests/Bundle/ServiceInstantiationTest.php b/tests/Bundle/ServiceInstantiationTest.php index e2d0c8cf..d253aa8c 100644 --- a/tests/Bundle/ServiceInstantiationTest.php +++ b/tests/Bundle/ServiceInstantiationTest.php @@ -12,6 +12,7 @@ use AutoMapper\Symfony\Bundle\CacheWarmup\CacheWarmer; use AutoMapper\Symfony\Bundle\DataCollector\MetadataCollector; use AutoMapper\Tests\Bundle\Resources\App\AppKernel; +use AutoMapper\Tests\Bundle\Resources\App\Entity\Address; use AutoMapper\Tests\Bundle\Resources\App\Entity\AddressDTO; use AutoMapper\Tests\Bundle\Resources\App\Entity\ClassWithMapToContextAttribute; use AutoMapper\Tests\Bundle\Resources\App\Entity\ClassWithPrivateProperty; @@ -156,7 +157,7 @@ public function testMapFromClassWithPrivateProperty(array $kernelOptions, array public static function mapFromClassWithPrivatePropertyProvider(): iterable { yield 'disallow private properties' => [[], []]; - yield 'allow private properties' => [['additionalConfigFile' => __DIR__ . '/Resources/config/with-private-properties.yml'], ['foo' => 'foo', 'bar' => 'bar']]; + yield 'allow private properties' => [['additionalConfigFile' => __DIR__ . '/Resources/config/with-private-properties.yml'], ['foo' => 'foo', 'bar' => 'bar', 'addresses' => []]]; } /** @@ -324,4 +325,19 @@ public static function mapProvider(): iterable $b->relationNotMapped = $d; yield [$b, [$a]]; } + + public function testMapFromClassWithPrivatePropertyPhpstan(): void + { + static::bootKernel([ + 'additionalConfigFile' => __DIR__ . '/Resources/config/with-private-properties.yml', + ]); + $autoMapper = self::getContainer()->get(AutoMapperInterface::class); + $address = new Address(); + $address->setCity('Toulon'); + + self::assertEquals( + ['foo' => 'foo', 'bar' => 'bar', 'addresses' => [['city' => 'Toulon']]], + $autoMapper->map(new ClassWithPrivateProperty('foo', [$address]), 'array') + ); + } }