Skip to content

Commit

Permalink
Migrate to the nikic/php-parser: ^5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lisachenko committed Feb 25, 2024
1 parent f45b796 commit 9de6414
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 115 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
},
"require": {
"php": ">=8.2",
"nikic/php-parser": "^4.0"
"nikic/php-parser": "^5.0"
},
"require-dev": {
"phpunit/phpunit": "^10.5.8",
"tracy/tracy": "^2.10",
"rector/rector": "^1.0"
"rector/rector": "^1.0",
"rector/rector-php-parser": "^0.14.0"
},
"extra": {
"branch-alias": {
Expand Down
2 changes: 2 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
use Rector\Php71\Rector\ClassConst\PublicConstantVisibilityRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\PhpParser\Set\PhpParserSetList;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;

Expand All @@ -27,4 +28,5 @@
->withSets([
PHPUnitSetList::ANNOTATIONS_TO_ATTRIBUTES,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
PhpParserSetList::PHP_PARSER_50
]);
3 changes: 1 addition & 2 deletions src/NodeVisitor/GeneratorDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
namespace Go\ParserReflection\NodeVisitor;

use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;

/**
Expand All @@ -30,7 +29,7 @@ public function enterNode(Node $node)
{
// There may be internal generators in closures, we do not need to look at them
if ($node instanceof Node\Expr\Closure) {
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
return self::DONT_TRAVERSE_CHILDREN;
}

if ($node instanceof Node\Expr\Yield_ || $node instanceof Node\Expr\YieldFrom) {
Expand Down
3 changes: 1 addition & 2 deletions src/NodeVisitor/StaticVariablesCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

use Go\ParserReflection\ValueResolver\NodeExpressionResolver;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;

/**
Expand Down Expand Up @@ -48,7 +47,7 @@ public function enterNode(Node $node)
{
// There may be internal closures, we do not need to look at them
if ($node instanceof Node\Expr\Closure) {
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
return self::DONT_TRAVERSE_CHILDREN;
}

if ($node instanceof Node\Stmt\Static_) {
Expand Down
8 changes: 4 additions & 4 deletions src/ReflectionAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
namespace Go\ParserReflection;

use Go\ParserReflection\ValueResolver\NodeExpressionResolver;
use ReflectionAttribute as BaseReflectionAttribute;
use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\PropertyItem;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use ReflectionAttribute as BaseReflectionAttribute;

/**
* ref original usage https://3v4l.org/duaQI
Expand All @@ -38,11 +38,11 @@ public function __construct(

public function getNode(): Node\Attribute
{
/** @var Class_|ClassMethod|PropertyProperty|ClassConst|Function_|Param $node */
/** @var Class_|ClassMethod|PropertyItem|ClassConst|Function_|Param $node */
$node = $this->reflector->getNode();

// attrGroups only exists in Property Stmt
if ($node instanceof PropertyProperty) {
if ($node instanceof PropertyItem) {
$node = $this->reflector->getTypeNode();
}

Expand Down
27 changes: 2 additions & 25 deletions src/ReflectionEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,26 +56,11 @@ class ReflectionEngine
*/
protected static $traverser;

/**
* @var null|Lexer
*/
protected static $lexer;

private function __construct() {}

public static function init(LocatorInterface $locator): void
{
self::$lexer = new Lexer(['usedAttributes' => [
'comments',
'startLine',
'endLine',
'startTokenPos',
'endTokenPos',
'startFilePos',
'endFilePos'
]]);

self::$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7, self::$lexer);
self::$parser = (new ParserFactory())->createForHostVersion();

self::$traverser = $traverser = new NodeTraverser();
$traverser->addVisitor(new NameResolver());
Expand Down Expand Up @@ -191,7 +176,7 @@ public static function parseClassMethod(string $fullClassName, string $methodNam
/**
* Parses class property
*
* @return array Pair of [Property and PropertyProperty] nodes
* @return array Pair of [Property and PropertyItem] nodes
*/
public static function parseClassProperty(string $fullClassName, string $propertyName): array
{
Expand Down Expand Up @@ -284,12 +269,4 @@ public static function parseFileNamespace(string $fileName, string $namespaceNam

throw new ReflectionException("Namespace $namespaceName was not found in the file $fileName");
}

/**
* @return Lexer
*/
public static function getLexer(): ?Lexer
{
return self::$lexer;
}
}
2 changes: 1 addition & 1 deletion src/ReflectionFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public function isStrictMode(): bool

$declareStatement = reset($topLevelNode->declares);
$isStrictTypeKey = $declareStatement->key->toString() === 'strict_types';
$isScalarValue = $declareStatement->value instanceof Node\Scalar\LNumber;
$isScalarValue = $declareStatement->value instanceof Node\Scalar\Int_;
$isStrictMode = $isStrictTypeKey && $isScalarValue && $declareStatement->value->value === 1;

return $isStrictMode;
Expand Down
10 changes: 5 additions & 5 deletions src/ReflectionProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
use Go\ParserReflection\Traits\InitializationTrait;
use Go\ParserReflection\Traits\InternalPropertiesEmulationTrait;
use Go\ParserReflection\ValueResolver\NodeExpressionResolver;
use PhpParser\Node\PropertyItem;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use Reflection;
use ReflectionProperty as BaseReflectionProperty;

Expand All @@ -41,7 +41,7 @@ class ReflectionProperty extends BaseReflectionProperty
/**
* Concrete property node
*
* @var PropertyProperty
* @var PropertyItem
*/
private $propertyNode;

Expand All @@ -58,13 +58,13 @@ class ReflectionProperty extends BaseReflectionProperty
* @param string $className Name of the class with properties
* @param string $propertyName Name of the property to reflect
* @param ?Property $propertyType Property type definition node
* @param ?PropertyProperty $propertyNode Concrete property definition (value, name)
* @param ?PropertyItem $propertyNode Concrete property definition (value, name)
*/
public function __construct(
$className,
$propertyName,
Property $propertyType = null,
PropertyProperty $propertyNode = null
PropertyItem $propertyNode = null
) {
$this->className = ltrim($className, '\\');
if (!$propertyType || !$propertyNode) {
Expand All @@ -81,7 +81,7 @@ public function __construct(
/**
* Returns an AST-node for property
*
* @return PropertyProperty
* @return PropertyItem
*/
public function getNode()
{
Expand Down
Loading

0 comments on commit 9de6414

Please sign in to comment.