Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility to dependency php parser v5 #1417

Merged
merged 2 commits into from
Jan 23, 2024
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
6 changes: 5 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ jobs:
composer-options: "--no-scripts --working-dir=tools/twigcs"

- name: "Install PHPUnit"
run: vendor/bin/simple-phpunit install
run: |
if [[ ${{ matrix.dependency_versions == 'lowest' }} ]]; then
echo "SYMFONY_PHPUNIT_REQUIRE=nikic/php-parser:^4.18" >> $GITHUB_ENV
fi
vendor/bin/simple-phpunit install

- name: "PHPUnit version"
run: vendor/bin/simple-phpunit --version
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"require": {
"php": ">=8.1",
"doctrine/inflector": "^2.0",
"nikic/php-parser": "^4.11",
"nikic/php-parser": "^4.18|^5.0",
"symfony/config": "^6.3|^7.0",
"symfony/console": "^6.3|^7.0",
"symfony/dependency-injection": "^6.3|^7.0",
Expand Down
3 changes: 2 additions & 1 deletion src/Security/SecurityControllerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\MakerBundle\Security;

use PhpParser\Builder\Param;
use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
Expand All @@ -32,7 +33,7 @@ public function addLoginMethod(ClassSourceManipulator $manipulator): void
$manipulator->addUseStatementIfNecessary(AuthenticationUtils::class);

$loginMethodBuilder->addParam(
(new \PhpParser\Builder\Param('authenticationUtils'))->setTypeHint('AuthenticationUtils')
(new Param('authenticationUtils'))->setType('AuthenticationUtils')
);

$manipulator->addMethodBody($loginMethodBuilder, <<<'CODE'
Expand Down
45 changes: 30 additions & 15 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\Parser;
use PhpParser\PhpVersion;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\Doctrine\BaseCollectionRelation;
use Symfony\Bundle\MakerBundle\Doctrine\BaseRelation;
Expand All @@ -48,7 +49,7 @@ final class ClassSourceManipulator
private const CONTEXT_CLASS_METHOD = 'class_method';
private const DEFAULT_VALUE_NONE = '__default_value_none';

private Parser\Php7 $parser;
private Parser $parser;
private Lexer\Emulative $lexer;
private PrettyPrinter $printer;
private ?ConsoleStyle $io = null;
Expand All @@ -64,14 +65,22 @@ public function __construct(
private bool $overwrite = false,
private bool $useAttributesForDoctrineMapping = true,
) {
$this->lexer = new Lexer\Emulative([
'usedAttributes' => [
'comments',
'startLine', 'endLine',
'startTokenPos', 'endTokenPos',
],
]);
$this->parser = new Parser\Php7($this->lexer);
/* @legacy Support for nikic/php-parser v4 */
if (class_exists(PhpVersion::class)) {
drieschel marked this conversation as resolved.
Show resolved Hide resolved
$version = PhpVersion::fromString(\PHP_VERSION);
$this->lexer = new Lexer\Emulative($version);
$this->parser = new Parser\Php8($this->lexer, $version);
} else {
$this->lexer = new Lexer\Emulative([
'usedAttributes' => [
'comments',
'startLine', 'endLine',
'startTokenPos', 'endTokenPos',
],
]);
$this->parser = new Parser\Php7($this->lexer);
}

$this->printer = new PrettyPrinter();

$this->setSourceCode($sourceCode);
Expand Down Expand Up @@ -327,7 +336,7 @@ public function createMethodBuilder(string $methodName, $returnType, bool $isRet
if (class_exists($returnType) || interface_exists($returnType)) {
$returnType = $this->addUseStatementIfNecessary($returnType);
}
$methodNodeBuilder->setReturnType($isReturnTypeNullable ? new Node\NullableType($returnType) : $returnType);
$methodNodeBuilder->setReturnType($isReturnTypeNullable ? new Node\NullableType(new Node\Identifier($returnType)) : $returnType);
}

if ($commentLines) {
Expand Down Expand Up @@ -414,7 +423,7 @@ private function addCustomGetter(string $propertyName, string $methodName, $retu
);

if (null !== $returnType) {
$getterNodeBuilder->setReturnType($isReturnTypeNullable ? new Node\NullableType($returnType) : $returnType);
$getterNodeBuilder->setReturnType($isReturnTypeNullable ? new Node\NullableType(new Node\Identifier($returnType)) : $returnType);
}

if ($commentLines) {
Expand All @@ -435,7 +444,7 @@ private function createSetterNodeBuilder(string $propertyName, $type, bool $isNu

$paramBuilder = new Builder\Param($propertyName);
if (null !== $type) {
$paramBuilder->setType($isNullable ? new Node\NullableType($type) : $type);
$paramBuilder->setType($isNullable ? new Node\NullableType(new Node\Identifier($type)) : $type);
}
$setterNodeBuilder->addParam($paramBuilder->getNode());

Expand Down Expand Up @@ -641,7 +650,7 @@ private function addCollectionRelation(BaseCollectionRelation $relation): void
$removerNodeBuilder = (new Builder\Method($relation->getRemoverMethodName()))->makePublic();

$paramBuilder = new Builder\Param($argName);
$paramBuilder->setTypeHint($typeHint);
$paramBuilder->setType($typeHint);
$removerNodeBuilder->addParam($paramBuilder->getNode());

// $this->avatars->removeElement($avatar)
Expand Down Expand Up @@ -841,7 +850,7 @@ public function buildAttributeNode(string $attributeClass, array $options, strin
$context = $this;
$nodeArguments = array_map(static function (string $option, mixed $value) use ($context) {
if (null === $value) {
return new Node\NullableType($option);
return new Node\NullableType(new Node\Identifier($option));
}

// Use the Doctrine Types constant
Expand Down Expand Up @@ -900,7 +909,13 @@ private function setSourceCode(string $sourceCode): void
{
$this->sourceCode = $sourceCode;
$this->oldStmts = $this->parser->parse($sourceCode);
$this->oldTokens = $this->lexer->getTokens();

/* @legacy Support for nikic/php-parser v4 */
if (\is_callable([$this->parser, 'getTokens'])) {
$this->oldTokens = $this->parser->getTokens();
} elseif (\is_callable([$this->lexer, 'getTokens'])) {
$this->oldTokens = $this->lexer->getTokens();
}

$traverser = new NodeTraverser();
$traverser->addVisitor(new NodeVisitor\CloningVisitor());
Expand Down
2 changes: 1 addition & 1 deletion src/Util/PrettyPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function setIndentLevel(int $level): void
* After
* public function getFoo(): string
*/
protected function pStmt_ClassMethod(Stmt\ClassMethod $node)
protected function pStmt_ClassMethod(Stmt\ClassMethod $node): string
{
$classMethod = parent::pStmt_ClassMethod($node);

Expand Down
2 changes: 1 addition & 1 deletion tests/Util/ClassSourceManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ public function testAddMethodWithBody(): void

$methodBuilder = $manipulator->createMethodBuilder('action', 'JsonResponse', false, ['@Route("/action", name="app_action")']);
$methodBuilder->addParam(
(new Param('param'))->setTypeHint('string')
(new Param('param'))->setType('string')
);
$manipulator->addMethodBody($methodBuilder,
<<<'CODE'
Expand Down
Loading