Skip to content

Commit

Permalink
Compability with PhpParser v5
Browse files Browse the repository at this point in the history
  • Loading branch information
Korbeil committed Jan 11, 2024
1 parent d02c3b6 commit fb74b76
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 66 deletions.
29 changes: 25 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
- name: checkout
uses: actions/checkout@master
- name: setup
uses: shivammathur/setup-php@2.9.0
uses: shivammathur/setup-php@2.28.0
with:
php-version: 8.2
coverage: none
Expand All @@ -28,7 +28,7 @@ jobs:
path: ~/.composer/cache/
key: composer-${{ github.sha }}
- name: setup
uses: shivammathur/setup-php@2.9.0
uses: shivammathur/setup-php@2.28.0
with:
php-version: 8.3
coverage: none
Expand Down Expand Up @@ -58,7 +58,7 @@ jobs:
path: ~/.composer/cache/
key: composer-${{ github.sha }}
- name: setup
uses: shivammathur/setup-php@2.9.0
uses: shivammathur/setup-php@2.28.0
with:
php-version: '${{ matrix.php-versions }}'
coverage: none
Expand All @@ -67,6 +67,27 @@ jobs:
run: composer update --prefer-stable
- name: tests
run: vendor/bin/phpunit
tests-lowest:
runs-on: ubuntu-latest
name: tests-lowest
steps:
- name: checkout
uses: actions/checkout@master
- name: cache-composer
uses: actions/cache@v2
with:
path: ~/.composer/cache/
key: composer-${{ github.sha }}
- name: setup
uses: shivammathur/[email protected]
with:
php-version: '8.2'
coverage: none
extensions: mbstring, fileinfo, json, intl, dom
- name: composer install
run: composer update --prefer-stable --prefer-lowest
- name: tests
run: vendor/bin/phpunit
check-version:
runs-on: ubuntu-latest
steps:
Expand All @@ -78,7 +99,7 @@ jobs:
path: ~/.composer/cache/
key: composer-${{ github.sha }}
- name: setup
uses: shivammathur/setup-php@2.9.0
uses: shivammathur/setup-php@2.28.0
with:
php-version: 8.3
coverage: none
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- [GH#27](https://github.com/jolicode/automapper/pull/27) Use PhpStanExtractor instead of PhpDocExtractor
- [GH#31](https://github.com/jolicode/automapper/pull/31) Compability with PhpParser v5

## [8.1.0] - 2023-12-14
### Added
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": "^8.2",
"doctrine/inflector": "^1.4 || ^2.0",
"nikic/php-parser": "^4.0",
"nikic/php-parser": "^4.18 || ^5.0",
"symfony/property-info": "^5.4 || ^6.0 || ^7.0",
"symfony/serializer": "^5.4 || ^6.0 || ^7.0"
},
Expand All @@ -27,7 +27,7 @@
"moneyphp/money": "^3.0",
"phpdocumentor/type-resolver": "^1.7",
"phpstan/phpdoc-parser": "^1.24",
"phpunit/phpunit": "^8.0",
"phpunit/phpunit": "^9.0",
"symfony/filesystem": "^5.4 || ^6.0 || ^7.0",
"symfony/http-kernel": "^5.4 || ^6.0 || ^7.0",
"symfony/uid": "^5.4 || ^6.0 || ^7.0"
Expand Down
53 changes: 25 additions & 28 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
failOnRisky="true"
failOnWarning="true"
>
<php>
<ini name="error_reporting" value="-1" />
</php>


<testsuites>
<testsuite name="AutoMapper Unit Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
<coverage>
<include>
<directory>./</directory>
</include>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
</php>
<testsuites>
<testsuite name="AutoMapper Unit Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
2 changes: 1 addition & 1 deletion src/AutoMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public static function create(
$loader = new EvalLoader(
new Generator(
new ClassMethodToCallbackExtractor(),
(new ParserFactory())->create(ParserFactory::PREFER_PHP7),
(new ParserFactory())->createForHostVersion(),
new ClassDiscriminatorFromClassMetadata($classMetadataFactory),
$allowReadOnlyTargetToPopulate
)
Expand Down
18 changes: 14 additions & 4 deletions src/Extractor/ClassMethodToCallbackExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt;
use PhpParser\Parser;
Expand All @@ -30,7 +31,7 @@

public function __construct(?Parser $parser = null)
{
$this->parser = $parser ?? (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$this->parser = $parser ?? (new ParserFactory())->createForHostVersion();
}

/**
Expand Down Expand Up @@ -66,15 +67,24 @@ public function extract(string $class, string $method, array $inputParameters):
$closureParameters = [];
foreach ($classMethod->getParams() as $parameter) {
if ($parameter->var instanceof Expr\Variable && $parameter->type instanceof Identifier) {
$closureParameters[] = new Param(new Expr\Variable($parameter->var->name), type: $parameter->type->name);
$closureParameters[] = new Param(new Expr\Variable($parameter->var->name), type: new Name($parameter->type->name));
}
}

/** @var Stmt[] $statements */
$statements = $classMethod->stmts;

/** @var string|Identifier|Name|null $returnType */
$returnType = $classMethod->returnType;
if (\is_string($returnType)) {
$returnType = new Name($returnType);
}

return new Expr\FuncCall(
new Expr\Closure([
'stmts' => $classMethod->stmts,
'stmts' => $statements,
'params' => $closureParameters,
'returnType' => $classMethod->returnType,
'returnType' => $returnType,
]),
$inputParameters,
);
Expand Down
30 changes: 17 additions & 13 deletions src/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
use AutoMapper\Transformer\AssignedByReferenceTransformerInterface;
use AutoMapper\Transformer\DependentTransformerInterface;
use AutoMapper\Transformer\TransformerInterface;
use PhpParser\Modifiers;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem as NewArrayItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayItem as OldArrayItem;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Scalar;
Expand All @@ -40,7 +43,7 @@ public function __construct(
private ?ClassDiscriminatorResolverInterface $classDiscriminator = null,
private bool $allowReadOnlyTargetToPopulate = false,
) {
$this->parser = $parser ?? (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$this->parser = $parser ?? (new ParserFactory())->createForHostVersion();
}

/**
Expand All @@ -60,6 +63,7 @@ public function generate(MapperGeneratorMetadataInterface $mapperGeneratorMetada
$constructStatements = [];
$addedDependencies = [];
$canHaveCircularDependency = $mapperGeneratorMetadata->canHaveCircularReference() && 'array' !== $mapperGeneratorMetadata->getSource();
$arrayItemClass = class_exists(NewArrayItem::class) ? NewArrayItem::class : OldArrayItem::class;

/**
* First statement is to check if the source is null, if so, return null.
Expand Down Expand Up @@ -352,8 +356,8 @@ public function generate(MapperGeneratorMetadataInterface $mapperGeneratorMetada
new Expr\ArrayDimFetch($contextVariable, new Scalar\String_(MapperContext::GROUPS)),
new Expr\Array_()
)),
new Arg(new Expr\Array_(array_map(function (string $group) {
return new Expr\ArrayItem(new Scalar\String_($group));
new Arg(new Expr\Array_(array_map(function (string $group) use ($arrayItemClass) {

Check failure on line 359 in src/Generator/Generator.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $items of class PhpParser\Node\Expr\Array_ constructor expects array<PhpParser\Node\ArrayItem>, array<PhpParser\Node\ArrayItem|PhpParser\Node\Expr\ArrayItem> given.

Check failure on line 359 in src/Generator/Generator.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $items of class PhpParser\Node\Expr\Array_ constructor expects array<PhpParser\Node\ArrayItem>, array<PhpParser\Node\ArrayItem|PhpParser\Node\Expr\ArrayItem> given.
return new $arrayItemClass(new Scalar\String_($group));
}, $propertyMapping->sourceGroups))),
])
);
Expand All @@ -378,8 +382,8 @@ public function generate(MapperGeneratorMetadataInterface $mapperGeneratorMetada
new Expr\ArrayDimFetch($contextVariable, new Scalar\String_(MapperContext::GROUPS)),
new Expr\Array_()
)),
new Arg(new Expr\Array_(array_map(function (string $group) {
return new Expr\ArrayItem(new Scalar\String_($group));
new Arg(new Expr\Array_(array_map(function (string $group) use ($arrayItemClass) {

Check failure on line 385 in src/Generator/Generator.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $items of class PhpParser\Node\Expr\Array_ constructor expects array<PhpParser\Node\ArrayItem>, array<PhpParser\Node\ArrayItem|PhpParser\Node\Expr\ArrayItem> given.

Check failure on line 385 in src/Generator/Generator.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $items of class PhpParser\Node\Expr\Array_ constructor expects array<PhpParser\Node\ArrayItem>, array<PhpParser\Node\ArrayItem|PhpParser\Node\Expr\ArrayItem> given.
return new $arrayItemClass(new Scalar\String_($group));
}, $propertyMapping->targetGroups))),
])
);
Expand All @@ -396,7 +400,7 @@ public function generate(MapperGeneratorMetadataInterface $mapperGeneratorMetada
new Expr\ArrayDimFetch($contextVariable, new Scalar\String_(MapperContext::DEPTH)),
new Expr\ConstFetch(new Name('0'))
),
new Scalar\LNumber($propertyMapping->maxDepth)
new Scalar\Int_($propertyMapping->maxDepth)
);
}

Expand Down Expand Up @@ -468,14 +472,14 @@ public function generate(MapperGeneratorMetadataInterface $mapperGeneratorMetada
* }
*/
$mapMethod = new Stmt\ClassMethod('map', [
'flags' => Stmt\Class_::MODIFIER_PUBLIC,
'flags' => class_exists(Modifiers::class) ? Modifiers::PUBLIC : Stmt\Class_::MODIFIER_PUBLIC,
'params' => [
new Param(new Expr\Variable($sourceInput->name)),
new Param(new Expr\Variable('context'), new Expr\Array_(), 'array'),
new Param(new Expr\Variable('context'), new Expr\Array_(), new Name('array')),
],
'byRef' => true,
'stmts' => $statements,
'returnType' => \PHP_VERSION_ID >= 80000 ? 'mixed' : null,
'returnType' => \PHP_VERSION_ID >= 80000 ? new Name('mixed') : null,
]);

/*
Expand All @@ -490,7 +494,7 @@ public function generate(MapperGeneratorMetadataInterface $mapperGeneratorMetada
* }
*/
$constructMethod = new Stmt\ClassMethod('__construct', [
'flags' => Stmt\Class_::MODIFIER_PUBLIC,
'flags' => class_exists(Modifiers::class) ? Modifiers::PUBLIC : Stmt\Class_::MODIFIER_PUBLIC,
'stmts' => $constructStatements,
]);

Expand All @@ -509,11 +513,11 @@ public function generate(MapperGeneratorMetadataInterface $mapperGeneratorMetada
* }
*/
$classStmts[] = new Stmt\ClassMethod('injectMappers', [
'flags' => Stmt\Class_::MODIFIER_PUBLIC,
'flags' => class_exists(Modifiers::class) ? Modifiers::PUBLIC : Stmt\Class_::MODIFIER_PUBLIC,
'params' => [
new Param(new Expr\Variable('autoMapperRegistry'), null, new Name\FullyQualified(AutoMapperRegistryInterface::class)),
],
'returnType' => 'void',
'returnType' => new Name('void'),
'stmts' => $injectMapperStatements,
]);
}
Expand All @@ -526,7 +530,7 @@ public function generate(MapperGeneratorMetadataInterface $mapperGeneratorMetada
* }
*/
return new Stmt\Class_($mapperGeneratorMetadata->getMapperClassName(), [
'flags' => Stmt\Class_::MODIFIER_FINAL,
'flags' => class_exists(Modifiers::class) ? Modifiers::FINAL : Stmt\Class_::MODIFIER_FINAL,
'extends' => new Name\FullyQualified(GeneratedMapper::class),
'stmts' => $classStmts,
]);
Expand Down
6 changes: 5 additions & 1 deletion src/Transformer/BuiltinTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use AutoMapper\Extractor\PropertyMapping;
use AutoMapper\Generator\UniqueVariableScope;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem as NewArrayItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayItem as OldArrayItem;
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Name;
use Symfony\Component\PropertyInfo\Type;
Expand Down Expand Up @@ -100,7 +102,9 @@ public function transform(Expr $input, Expr $target, PropertyMapping $propertyMa

private function toArray(Expr $input): Expr
{
return new Expr\Array_([new Expr\ArrayItem($input)]);
$arrayItemClass = class_exists(NewArrayItem::class) ? NewArrayItem::class : OldArrayItem::class;

return new Expr\Array_([new $arrayItemClass($input)]);

Check failure on line 107 in src/Transformer/BuiltinTransformer.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $items of class PhpParser\Node\Expr\Array_ constructor expects array<PhpParser\Node\ArrayItem>, array<int, PhpParser\Node\ArrayItem|PhpParser\Node\Expr\ArrayItem> given.

Check failure on line 107 in src/Transformer/BuiltinTransformer.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $items of class PhpParser\Node\Expr\Array_ constructor expects array<PhpParser\Node\ArrayItem>, array<int, PhpParser\Node\ArrayItem|PhpParser\Node\Expr\ArrayItem> given.
}

private function fromIteratorToArray(Expr $input): Expr
Expand Down
10 changes: 8 additions & 2 deletions tests/AutoMapperBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
use AutoMapper\Generator\Generator;
use AutoMapper\Loader\ClassLoaderInterface;
use AutoMapper\Loader\FileLoader;
use Doctrine\Common\Annotations\AnnotationReader;
use PhpParser\ParserFactory;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;

/**
Expand All @@ -34,11 +36,15 @@ protected function buildAutoMapper(bool $allowReadOnlyTargetToPopulate = false,
{
$fs = new Filesystem();
$fs->remove(__DIR__ . '/cache/');
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
if (class_exists(AttributeLoader::class)) {
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
} else {
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
}

$this->loader = new FileLoader(new Generator(
new ClassMethodToCallbackExtractor(),
(new ParserFactory())->create(ParserFactory::PREFER_PHP7),
(new ParserFactory())->createForHostVersion(),
new ClassDiscriminatorFromClassMetadata($classMetadataFactory),
$allowReadOnlyTargetToPopulate
), __DIR__ . '/cache');
Expand Down
Loading

0 comments on commit fb74b76

Please sign in to comment.