From f539ccf199f5a68a2f5731207c369a1a0e9ccf52 Mon Sep 17 00:00:00 2001 From: Daniel Leech Date: Sat, 12 Oct 2024 14:45:02 +0100 Subject: [PATCH 1/2] Introduce rector --- composer.json | 3 ++- rector.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 rector.php diff --git a/composer.json b/composer.json index 6f5e3e8c..3d755a35 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,8 @@ }, "require-dev": { "phpunit/phpunit": "^8.5.15", - "phpstan/phpstan": "^1.8" + "phpstan/phpstan": "^1.8", + "rector/rector": "^1.2" }, "license": "MIT", "authors": [ diff --git a/rector.php b/rector.php new file mode 100644 index 00000000..a2bd7301 --- /dev/null +++ b/rector.php @@ -0,0 +1,17 @@ +withImportNames() + ->withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + ->withSkipPath('/tests/cases') + ->withRules([ + ExplicitNullableParamTypeRector::class, + ]); From 5c801b1fbcc6807e1a7a1ad118b4d5558531ae87 Mon Sep 17 00:00:00 2001 From: Daniel Leech Date: Sat, 12 Oct 2024 14:47:23 +0100 Subject: [PATCH 2/2] Fix 8.4 deprecations, bump library to 8.0 --- composer.json | 2 +- rector.php | 3 +-- src/Node.php | 8 ++++---- src/Parser.php | 2 +- src/Token.php | 2 +- tests/LexicalGrammarTest.php | 2 +- tests/ParserGrammarTest.php | 2 +- 7 files changed, 10 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 3d755a35..9b058be4 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Tolerant PHP-to-AST parser designed for IDE usage scenarios", "type": "library", "require": { - "php": ">=7.2" + "php": "^8.0" }, "require-dev": { "phpunit/phpunit": "^8.5.15", diff --git a/rector.php b/rector.php index a2bd7301..3f213dd6 100644 --- a/rector.php +++ b/rector.php @@ -6,12 +6,11 @@ use Rector\Php84\Rector\Param\ExplicitNullableParamTypeRector; return RectorConfig::configure() - ->withImportNames() ->withPaths([ __DIR__ . '/src', __DIR__ . '/tests', ]) - ->withSkipPath('/tests/cases') + ->withSkipPath('/tests/cases/*') ->withRules([ ExplicitNullableParamTypeRector::class, ]); diff --git a/src/Node.php b/src/Node.php index 0f1e2e64..5de35751 100644 --- a/src/Node.php +++ b/src/Node.php @@ -151,7 +151,7 @@ public function getRoot() : Node { * @param callable|null $shouldDescendIntoChildrenFn * @return \Generator|Node[]|Token[] */ - public function getDescendantNodesAndTokens(callable $shouldDescendIntoChildrenFn = null) { + public function getDescendantNodesAndTokens(?callable $shouldDescendIntoChildrenFn = null) { // TODO - write unit tests to prove invariants // (concatenating all descendant Tokens should produce document, concatenating all Nodes should produce document) foreach ($this->getChildNodesAndTokens() as $child) { @@ -176,7 +176,7 @@ public function getDescendantNodesAndTokens(callable $shouldDescendIntoChildrenF * @param callable|null $shouldDescendIntoChildrenFn * @return void */ - public function walkDescendantNodesAndTokens(callable $callback, callable $shouldDescendIntoChildrenFn = null) { + public function walkDescendantNodesAndTokens(callable $callback, ?callable $shouldDescendIntoChildrenFn = null) { // TODO - write unit tests to prove invariants // (concatenating all descendant Tokens should produce document, concatenating all Nodes should produce document) foreach (static::CHILD_NAMES as $name) { @@ -209,7 +209,7 @@ public function walkDescendantNodesAndTokens(callable $callback, callable $shoul * @param callable|null $shouldDescendIntoChildrenFn * @return \Generator|Node[] */ - public function getDescendantNodes(callable $shouldDescendIntoChildrenFn = null) { + public function getDescendantNodes(?callable $shouldDescendIntoChildrenFn = null) { foreach ($this->getChildNodes() as $child) { yield $child; if ($shouldDescendIntoChildrenFn === null || $shouldDescendIntoChildrenFn($child)) { @@ -223,7 +223,7 @@ public function getDescendantNodes(callable $shouldDescendIntoChildrenFn = null) * @param callable|null $shouldDescendIntoChildrenFn * @return \Generator|Token[] */ - public function getDescendantTokens(callable $shouldDescendIntoChildrenFn = null) { + public function getDescendantTokens(?callable $shouldDescendIntoChildrenFn = null) { foreach ($this->getChildNodesAndTokens() as $child) { if ($child instanceof Node) { if ($shouldDescendIntoChildrenFn == null || $shouldDescendIntoChildrenFn($child)) { diff --git a/src/Parser.php b/src/Parser.php index 4e55dcb6..fb215ede 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -175,7 +175,7 @@ protected function makeLexer(string $fileContents): TokenStreamProviderInterface * @param string $fileContents * @return SourceFileNode */ - public function parseSourceFile(string $fileContents, string $uri = null) : SourceFileNode { + public function parseSourceFile(string $fileContents, ?string $uri = null) : SourceFileNode { $this->lexer = $this->makeLexer($fileContents); $this->reset(); diff --git a/src/Token.php b/src/Token.php index 5fa1595d..dca04d29 100644 --- a/src/Token.php +++ b/src/Token.php @@ -42,7 +42,7 @@ public function getLeadingCommentsAndWhitespaceText(string $document) : string { * @param string|null $document * @return bool|null|string */ - public function getText(string $document = null) { + public function getText(?string $document = null) { if ($document === null) { return null; } diff --git a/tests/LexicalGrammarTest.php b/tests/LexicalGrammarTest.php index 88c4db5f..59ff69e4 100644 --- a/tests/LexicalGrammarTest.php +++ b/tests/LexicalGrammarTest.php @@ -17,7 +17,7 @@ class LexicalGrammarTest extends TestCase { private $expectedTokensFile; private $tokens; const FILE_PATTERN = __DIR__ . "/cases/lexical/*"; - public function run(TestResult $result = null) : TestResult { + public function run(?TestResult $result = null) : TestResult { if (!isset($GLOBALS["GIT_CHECKOUT_LEXER"])) { $GLOBALS["GIT_CHECKOUT_LEXER"] = true; exec("git -C " . dirname(self::FILE_PATTERN) . " checkout *.php.tokens"); diff --git a/tests/ParserGrammarTest.php b/tests/ParserGrammarTest.php index 25b9e773..3364c272 100644 --- a/tests/ParserGrammarTest.php +++ b/tests/ParserGrammarTest.php @@ -19,7 +19,7 @@ class ParserGrammarTest extends TestCase { private $expectedDiagnosticsFile; private $tokens; private $diagnostics; - public function run(TestResult $result = null) : TestResult { + public function run(?TestResult $result = null) : TestResult { if (!isset($GLOBALS["GIT_CHECKOUT_PARSER"])) { $GLOBALS["GIT_CHECKOUT_PARSER"] = true; exec("git -C " . dirname(self::FILE_PATTERN) . " checkout *.php.tree *.php.diag");