Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion tests/Bundle/ServiceInstantiationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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' => []]];
}

/**
Expand Down Expand Up @@ -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')
);
}
}
Loading