Skip to content

Commit 82ee000

Browse files
authored
Merge pull request #341 from jolicode/fix/bundle-extractor
fix(bundle): fix phpstan extractor not working with private
2 parents 7729037 + fd9b53c commit 82ee000

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/Symfony/Bundle/DependencyInjection/AutoMapperExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ public function load(array $configs, ContainerBuilder $container): void
7676
if ($config['map_private_properties']) {
7777
$container->getDefinition('automapper.property_info.reflection_extractor')
7878
->replaceArgument('$accessFlags', ReflectionExtractor::ALLOW_PUBLIC | ReflectionExtractor::ALLOW_PRIVATE | ReflectionExtractor::ALLOW_PROTECTED);
79+
80+
$container->getDefinition('automapper.property_info.phpstan_source_extractor')
81+
->replaceArgument('$allowPrivateAccess', true);
82+
83+
$container->getDefinition('automapper.property_info.phpstan_target_extractor')
84+
->replaceArgument('$allowPrivateAccess', true);
7985
}
8086

8187
$container->setParameter('automapper.map_private_properties', $config['map_private_properties']);

tests/Bundle/Resources/App/Entity/ClassWithPrivateProperty.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@
66

77
class ClassWithPrivateProperty
88
{
9+
/** @var Address[] */
10+
private array $addresses = [];
11+
912
public function __construct(
1013
private string $foo,
14+
array $addresses = [],
1115
) {
16+
$this->addresses = $addresses;
1217
}
1318

1419
private function getBar(): string

tests/Bundle/ServiceInstantiationTest.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use AutoMapper\Symfony\Bundle\CacheWarmup\CacheWarmer;
1313
use AutoMapper\Symfony\Bundle\DataCollector\MetadataCollector;
1414
use AutoMapper\Tests\Bundle\Resources\App\AppKernel;
15+
use AutoMapper\Tests\Bundle\Resources\App\Entity\Address;
1516
use AutoMapper\Tests\Bundle\Resources\App\Entity\AddressDTO;
1617
use AutoMapper\Tests\Bundle\Resources\App\Entity\ClassWithMapToContextAttribute;
1718
use AutoMapper\Tests\Bundle\Resources\App\Entity\ClassWithPrivateProperty;
@@ -156,7 +157,7 @@ public function testMapFromClassWithPrivateProperty(array $kernelOptions, array
156157
public static function mapFromClassWithPrivatePropertyProvider(): iterable
157158
{
158159
yield 'disallow private properties' => [[], []];
159-
yield 'allow private properties' => [['additionalConfigFile' => __DIR__ . '/Resources/config/with-private-properties.yml'], ['foo' => 'foo', 'bar' => 'bar']];
160+
yield 'allow private properties' => [['additionalConfigFile' => __DIR__ . '/Resources/config/with-private-properties.yml'], ['foo' => 'foo', 'bar' => 'bar', 'addresses' => []]];
160161
}
161162

162163
/**
@@ -324,4 +325,19 @@ public static function mapProvider(): iterable
324325
$b->relationNotMapped = $d;
325326
yield [$b, [$a]];
326327
}
328+
329+
public function testMapFromClassWithPrivatePropertyPhpstan(): void
330+
{
331+
static::bootKernel([
332+
'additionalConfigFile' => __DIR__ . '/Resources/config/with-private-properties.yml',
333+
]);
334+
$autoMapper = self::getContainer()->get(AutoMapperInterface::class);
335+
$address = new Address();
336+
$address->setCity('Toulon');
337+
338+
self::assertEquals(
339+
['foo' => 'foo', 'bar' => 'bar', 'addresses' => [['city' => 'Toulon']]],
340+
$autoMapper->map(new ClassWithPrivateProperty('foo', [$address]), 'array')
341+
);
342+
}
327343
}

0 commit comments

Comments
 (0)