Skip to content

Commit

Permalink
bumb php parser and related refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
drieschel committed Jan 8, 2024
1 parent f63b0c0 commit 47ff1fc
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 20 deletions.
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": "^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
25 changes: 9 additions & 16 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\ParserFactory;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\Doctrine\BaseCollectionRelation;
use Symfony\Bundle\MakerBundle\Doctrine\BaseRelation;
Expand All @@ -48,8 +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 Lexer\Emulative $lexer;
private Parser\Php8 $parser;
private PrettyPrinter $printer;
private ?ConsoleStyle $io = null;

Expand All @@ -64,14 +64,7 @@ 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);
$this->parser = (new ParserFactory())->createForNewestSupportedVersion();
$this->printer = new PrettyPrinter();

$this->setSourceCode($sourceCode);
Expand Down Expand Up @@ -327,7 +320,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 +407,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 +428,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 +634,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 +834,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 +893,7 @@ private function setSourceCode(string $sourceCode): void
{
$this->sourceCode = $sourceCode;
$this->oldStmts = $this->parser->parse($sourceCode);
$this->oldTokens = $this->lexer->getTokens();
$this->oldTokens = $this->parser->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

0 comments on commit 47ff1fc

Please sign in to comment.