Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 2 additions & 8 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
use Doctrine\ORM\Mapping\OneToOne;
use PhpParser\Builder;
use PhpParser\BuilderHelpers;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\Parser;
use PhpParser\PhpVersion;
use PhpParser\ParserFactory;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\Doctrine\BaseCollectionRelation;
use Symfony\Bundle\MakerBundle\Doctrine\BaseRelation;
Expand All @@ -51,7 +50,6 @@ final class ClassSourceManipulator
private const DEFAULT_VALUE_NONE = '__default_value_none';

private Parser $parser;
private Lexer\Emulative $lexer;
private PrettyPrinter $printer;
private ?ConsoleStyle $io = null;

Expand All @@ -66,11 +64,7 @@ public function __construct(
private bool $overwrite = false,
private bool $useAttributesForDoctrineMapping = true,
) {
$this->lexer = new Lexer\Emulative(
PhpVersion::fromString('8.1'),
);
$this->parser = new Parser\Php7($this->lexer);

$this->parser = (new ParserFactory())->createForHostVersion();
$this->printer = new PrettyPrinter();

$this->setSourceCode($sourceCode);
Expand Down
27 changes: 27 additions & 0 deletions tests/Util/ClassSourceManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -870,4 +870,31 @@ public function testAddConstructorInClassContainsConstructor()
CODE
);
}

/**
* @requires PHP >= 8.4
*/
public function testParsingPhp84PropertyHooks(): void
{
$source = file_get_contents(__DIR__.'/fixtures/source/User_property_hooks.php');

// This should not throw a PhpParser\Error
$manipulator = new ClassSourceManipulator($source);

// Verify we can still work with the class
$this->assertStringContainsString('class User', $manipulator->getSourceCode());
}

/**
* @requires PHP >= 8.4
*/
public function testAddPropertyToClassWithPropertyHooks(): void
{
$source = file_get_contents(__DIR__.'/fixtures/source/User_property_hooks.php');

$manipulator = new ClassSourceManipulator($source);
$manipulator->addProperty(name: 'newProp', propertyType: '?string');

$this->assertStringContainsString('private ?string $newProp', $manipulator->getSourceCode());
}
}
15 changes: 15 additions & 0 deletions tests/Util/fixtures/source/User_property_hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Entity;

class User
{
private ?int $id = null;

private(set) ?string $name = null;

public ?array $extra {
get => $this->extra ?? [];
set => $value;
}
}
Loading