From 91fae9caa8475ac89e39aaaaebc6ab2e19b20bde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Votruba?= Date: Mon, 28 May 2018 20:19:54 +0200 Subject: [PATCH] Upgrade to PHP-Parser 4 (#203) --- composer.json | 4 +- composer.lock | 45 ++++++++++--------- src/PhpParser/NodeTraverser.php | 2 +- .../Collection/UseStmtCollection.php | 2 +- .../NodeVisitor/WhitelistedClassAppender.php | 11 ++--- tests/PhpParser/FakeParser.php | 2 +- 6 files changed, 34 insertions(+), 32 deletions(-) diff --git a/composer.json b/composer.json index 51ef1a22..0d1aa123 100644 --- a/composer.json +++ b/composer.json @@ -19,9 +19,9 @@ "require": { "php": "^7.1", - "nikic/php-parser": "^3.0", + "nikic/php-parser": "^4.0", "ocramius/package-versions": "^1.1", - "roave/better-reflection": "^2.0", + "roave/better-reflection": "^3.0", "symfony/console": "^3.2 || ^4.0", "symfony/filesystem": "^3.2 || ^4.0", "symfony/finder": "^3.2 || ^4.0" diff --git a/composer.lock b/composer.lock index f3c485c2..844f973f 100644 --- a/composer.lock +++ b/composer.lock @@ -1,31 +1,31 @@ { "_readme": [ "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "cbdca31a397b067d2007e15f810c4d35", + "content-hash": "bb0e14006a8b00a8bd3dafd5777e8975", "packages": [ { "name": "nikic/php-parser", - "version": "v3.1.5", + "version": "v4.0.1", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "bb87e28e7d7b8d9a7fda231d37457c9210faf6ce" + "reference": "e4a54fa90a5cd8e8dd3fb4099942681731c5cdd3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/bb87e28e7d7b8d9a7fda231d37457c9210faf6ce", - "reference": "bb87e28e7d7b8d9a7fda231d37457c9210faf6ce", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/e4a54fa90a5cd8e8dd3fb4099942681731c5cdd3", + "reference": "e4a54fa90a5cd8e8dd3fb4099942681731c5cdd3", "shasum": "" }, "require": { "ext-tokenizer": "*", - "php": ">=5.5" + "php": ">=7.0" }, "require-dev": { - "phpunit/phpunit": "~4.0|~5.0" + "phpunit/phpunit": "^6.5 || ^7.0" }, "bin": [ "bin/php-parse" @@ -33,7 +33,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.0-dev" + "dev-master": "4.0-dev" } }, "autoload": { @@ -55,7 +55,7 @@ "parser", "php" ], - "time": "2018-02-28T20:30:58+00:00" + "time": "2018-03-25T17:35:16+00:00" }, { "name": "ocramius/package-versions", @@ -260,27 +260,27 @@ }, { "name": "roave/better-reflection", - "version": "2.0.2", + "version": "dev-master", "source": { "type": "git", "url": "https://github.com/Roave/BetterReflection.git", - "reference": "efc45b50cb52d5eeaacab15741376e981e28738b" + "reference": "f8b3fc6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Roave/BetterReflection/zipball/efc45b50cb52d5eeaacab15741376e981e28738b", - "reference": "efc45b50cb52d5eeaacab15741376e981e28738b", + "url": "https://api.github.com/repos/Roave/BetterReflection/zipball/f8b3fc6", + "reference": "f8b3fc6", "shasum": "" }, "require": { - "nikic/php-parser": "^3.1.1", + "nikic/php-parser": "^4.0.0", "php": ">=7.1.0,<7.3.0", "phpdocumentor/reflection-docblock": "^4.1.1", "phpdocumentor/type-resolver": "^0.4.0", "roave/signature": "^1.0" }, "require-dev": { - "phpunit/phpunit": "^6.3.0" + "phpunit/phpunit": "^7.0.0" }, "suggest": { "composer/composer": "Required to use the ComposerSourceLocator" @@ -296,11 +296,6 @@ "MIT" ], "authors": [ - { - "name": "Marco Pivetta", - "email": "ocramius@gmail.com", - "homepage": "http://ocramius.github.io/" - }, { "name": "James Titcumb", "email": "james@asgrim.com", @@ -315,10 +310,15 @@ "name": "Jaroslav HanslĂ­k", "email": "kukulich@kukulich.cz", "homepage": "https://github.com/kukulich" + }, + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com", + "homepage": "https://ocramius.github.io/" } ], "description": "Better Reflection - an improved code reflection API", - "time": "2018-02-05T08:08:57+00:00" + "time": "2018-05-08T21:55:53+00:00" }, { "name": "roave/signature", @@ -3617,6 +3617,7 @@ "aliases": [], "minimum-stability": "stable", "stability-flags": { + "roave/better-reflection": 20, "humbug/box": 20 }, "prefer-stable": false, diff --git a/src/PhpParser/NodeTraverser.php b/src/PhpParser/NodeTraverser.php index 0f5d21cc..70b685b7 100644 --- a/src/PhpParser/NodeTraverser.php +++ b/src/PhpParser/NodeTraverser.php @@ -41,7 +41,7 @@ public function __construct(string $prefix) /** * @inheritdoc */ - public function traverse(array $nodes) + public function traverse(array $nodes): array { $nodes = $this->wrapInNamespace($nodes); $nodes = $this->replaceGroupUseStatements($nodes); diff --git a/src/PhpParser/NodeVisitor/Collection/UseStmtCollection.php b/src/PhpParser/NodeVisitor/Collection/UseStmtCollection.php index 34190234..ae6fcec5 100644 --- a/src/PhpParser/NodeVisitor/Collection/UseStmtCollection.php +++ b/src/PhpParser/NodeVisitor/Collection/UseStmtCollection.php @@ -71,7 +71,7 @@ public function findStatementForNode(?Name $namespaceName, Name $node): ?Name foreach ($useStatements as $use_) { foreach ($use_->uses as $useStatement) { if ($useStatement instanceof UseUse) { - if ($name === strtolower($useStatement->alias)) { + if ($name === $useStatement->getAlias()->toLowerString()) { if ($parentNode instanceof FuncCall && 1 === count($node->parts)) { if (Use_::TYPE_FUNCTION === $use_->type) { return $useStatement->name; diff --git a/src/PhpParser/NodeVisitor/WhitelistedClassAppender.php b/src/PhpParser/NodeVisitor/WhitelistedClassAppender.php index d9eb01e5..84821c66 100644 --- a/src/PhpParser/NodeVisitor/WhitelistedClassAppender.php +++ b/src/PhpParser/NodeVisitor/WhitelistedClassAppender.php @@ -22,6 +22,7 @@ use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Class_; +use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Interface_; use PhpParser\Node\Stmt\Namespace_; use PhpParser\NodeVisitorAbstract; @@ -89,21 +90,21 @@ private function appendToNamespaceStmt(Namespace_ $namespace): Namespace_ } /** @var Class_ $stmt */ - $name = FullyQualified::concat($namespace->name, $stmt->name); + $name = FullyQualified::concat((string) $namespace->name, (string) $stmt->name); $originalName = $name->slice(1); if (false === in_array((string) $originalName, $this->whitelist, true)) { continue; } - $newStmts[] = new FuncCall( + $newStmts[] = new Expression(new FuncCall( new Name('class_alias'), [ new Arg( - new String_($name) + new String_((string) $name) ), new Arg( - new String_($originalName) + new String_((string) $originalName) ), new Arg( new ConstFetch( @@ -112,7 +113,7 @@ private function appendToNamespaceStmt(Namespace_ $namespace): Namespace_ ), ], ['whitelist_class_alias' => true] - ); + )); } $namespace->stmts = $newStmts; diff --git a/tests/PhpParser/FakeParser.php b/tests/PhpParser/FakeParser.php index 2dc7f579..1c5041f9 100644 --- a/tests/PhpParser/FakeParser.php +++ b/tests/PhpParser/FakeParser.php @@ -22,7 +22,7 @@ final class FakeParser implements Parser /** * @inheritdoc */ - public function parse($code, ErrorHandler $errorHandler = null) + public function parse(string $code, ErrorHandler $errorHandler = null) { throw new \LogicException(); }