From 7d54f263884cb3d690d7a9d1a9063172112cbb8f Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 10 May 2024 14:28:38 -0400 Subject: [PATCH 1/5] wip --- composer.json | 2 +- src/Endpoints/PHP/Extends_.php | 5 +---- src/Endpoints/PHP/Implements_.php | 4 +--- src/Endpoints/PHP/Namespace_.php | 7 ++----- src/Endpoints/PHP/UseTrait.php | 1 - src/Endpoints/PHP/Use_.php | 2 +- src/PHPFile.php | 2 -- src/Support/PSR2PrettyPrinter.php | 8 ++++---- src/Traits/HasIO.php | 21 ++++++--------------- 9 files changed, 16 insertions(+), 36 deletions(-) diff --git a/composer.json b/composer.json index 0c0dee5..9cf6609 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ } ], "require": { - "nikic/php-parser": "^4.11" + "nikic/php-parser": "^5.0" }, "require-dev": { "laravel/laravel": "^6.0 || ^7.0 || ^8.0 || ^9.0", diff --git a/src/Endpoints/PHP/Extends_.php b/src/Endpoints/PHP/Extends_.php index 0b68b42..1fe4d60 100644 --- a/src/Endpoints/PHP/Extends_.php +++ b/src/Endpoints/PHP/Extends_.php @@ -30,10 +30,7 @@ protected function get() return $this->file->astQuery() ->class() ->extends - ->remember('formatted_extends', function ($node) { - $parts = $node->parts ?? null; - return $parts ? join('\\', $parts) : null; - }) + ->remember('formatted_extends', fn ($node) => $node->name) ->recall('formatted_extends') ->first(); } diff --git a/src/Endpoints/PHP/Implements_.php b/src/Endpoints/PHP/Implements_.php index f0e5288..ff55de0 100644 --- a/src/Endpoints/PHP/Implements_.php +++ b/src/Endpoints/PHP/Implements_.php @@ -37,9 +37,7 @@ protected function get() ->class() ->implements ->get() - ->map(function ($name) { - return implode('\\', $name->parts); - })->toArray(); + ->map(fn ($node) => $node->name)->toArray(); } protected function set($newImplements) diff --git a/src/Endpoints/PHP/Namespace_.php b/src/Endpoints/PHP/Namespace_.php index c6e1caa..bbf36d5 100644 --- a/src/Endpoints/PHP/Namespace_.php +++ b/src/Endpoints/PHP/Namespace_.php @@ -37,10 +37,7 @@ protected function get() { return $this->file->astQuery() ->namespace() - ->remember('formatted_namespace', function ($node) { - $parts = $node->name->parts ?? null; - return $parts ? join('\\', $parts) : null; - }) + ->remember('formatted_namespace', fn ($node) => $node->name) ->recall('formatted_namespace') ->first(); } @@ -53,7 +50,7 @@ protected function set(string $newNamespace) if ($namespace) { // Modifying existing namespace - $namespace->name->parts = explode("\\", $newNamespace); + $namespace->name->name = $newNamespace; } else { // Add a namespace $ast = $this->file->ast(); diff --git a/src/Endpoints/PHP/UseTrait.php b/src/Endpoints/PHP/UseTrait.php index 86dd9a8..2d0f36d 100644 --- a/src/Endpoints/PHP/UseTrait.php +++ b/src/Endpoints/PHP/UseTrait.php @@ -31,7 +31,6 @@ protected function get() ->class() ->traitUse() ->name() - ->parts ->get() ->toArray(); diff --git a/src/Endpoints/PHP/Use_.php b/src/Endpoints/PHP/Use_.php index 7da9474..c79cf2f 100644 --- a/src/Endpoints/PHP/Use_.php +++ b/src/Endpoints/PHP/Use_.php @@ -44,7 +44,7 @@ protected function get() ->uses ->get() ->map(function ($useStatement) { - $base = join('\\', $useStatement->name->parts); + $base = $useStatement->name; return $base . ($useStatement->alias ? ' as ' . $useStatement->alias : ''); })->toArray(); } diff --git a/src/PHPFile.php b/src/PHPFile.php index e1b8c40..ff23c76 100644 --- a/src/PHPFile.php +++ b/src/PHPFile.php @@ -50,8 +50,6 @@ class PHPFile protected $tokens; - protected $lexer; - protected $directives = []; public function __construct( diff --git a/src/Support/PSR2PrettyPrinter.php b/src/Support/PSR2PrettyPrinter.php index 84c5c4b..4adbe23 100644 --- a/src/Support/PSR2PrettyPrinter.php +++ b/src/Support/PSR2PrettyPrinter.php @@ -18,13 +18,13 @@ public function __construct($options = []) } // Fix empty line before class definition - protected function pStmt_Class(Class_ $node) + protected function pStmt_Class(Class_ $node): string { return $this->pClassCommon($node, ' ' . $node->name); } // Fix empty line before class definition - protected function pStmt_ClassMethod(ClassMethod $node) + protected function pStmt_ClassMethod(ClassMethod $node): string { return $this->pAttrGroups($node->attrGroups) . $this->pModifiers($node->flags) @@ -36,7 +36,7 @@ protected function pStmt_ClassMethod(ClassMethod $node) : ';'); } - protected function pExpr_Array(Array_ $node) + protected function pExpr_Array(Array_ $node): string { $stmts = $this->pCommaSeparatedMultiline($node->items, true); $lineBreaked = $stmts ? $stmts . $this->nl : $stmts; @@ -48,7 +48,7 @@ protected function pExpr_Array(Array_ $node) * * @param [type] $nodes */ - protected function pClassCommon(Class_ $node, $afterClassToken) + protected function pClassCommon(Class_ $node, $afterClassToken): string { return $this->pModifiers($node->flags) . 'class' . $afterClassToken diff --git a/src/Traits/HasIO.php b/src/Traits/HasIO.php index c8a54b4..5ada80f 100644 --- a/src/Traits/HasIO.php +++ b/src/Traits/HasIO.php @@ -7,6 +7,7 @@ use PhpParser\Error as PHPParserError; use PhpParser\NodeTraverser; use PhpParser\NodeVisitor\CloningVisitor; +use PhpParser\ParserFactory; trait HasIO { @@ -88,23 +89,13 @@ public function preview() public function parse() { - $this->lexer = new \PhpParser\Lexer\Emulative([ - 'usedAttributes' => [ - 'comments', - 'startLine', 'endLine', - 'startTokenPos', 'endTokenPos', - ], - ]); - - $parser = new \PhpParser\Parser\Php7($this->lexer); - - //$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); - $traverser = new NodeTraverser(); - $traverser->addVisitor(new CloningVisitor()); - + $parser = (new ParserFactory)->createForNewestSupportedVersion(); + + $traverser = new NodeTraverser(new CloningVisitor()); + try { $this->originalAst = $parser->parse($this->contents()); - $this->tokens = $this->lexer->getTokens(); + $this->tokens = $parser->getTokens(); } catch (PHPParserError $error) { // rethrow with extra information throw new FileParseError($this->input->absolutePath(), $error); From 8a0d3dfe3fce5b6a5fc58f4ea26ceceb79cd1519 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 10 May 2024 14:29:31 -0400 Subject: [PATCH 2/5] bump From 749678f9a5cbeaaa8362f113893c0be00ca3e679 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Fri, 10 May 2024 14:59:39 -0400 Subject: [PATCH 3/5] wip --- src/Support/AST/ShallowNodeFinder.php | 3 ++- tests/Unit/Support/AST/ASTQueryBuilderTest.php | 4 ++-- tests/Unit/Support/AST/PrettyPrintingTest.php | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Support/AST/ShallowNodeFinder.php b/src/Support/AST/ShallowNodeFinder.php index bd3f5e6..ae9af43 100644 --- a/src/Support/AST/ShallowNodeFinder.php +++ b/src/Support/AST/ShallowNodeFinder.php @@ -2,6 +2,7 @@ namespace Archetype\Support\AST; +use PhpParser\Node; use PhpParser\NodeFinder; class ShallowNodeFinder extends NodeFinder @@ -27,7 +28,7 @@ public function findInstanceOf($node, string $class) : array })->filter()->flatten()->toArray(); } - public function findFirstInstanceOf($node, string $class) + public function findFirstInstanceOf($node, string $class): ?Node { return collect($this->findInstanceOf($node, $class))->first(); } diff --git a/tests/Unit/Support/AST/ASTQueryBuilderTest.php b/tests/Unit/Support/AST/ASTQueryBuilderTest.php index 8813fe9..5687269 100644 --- a/tests/Unit/Support/AST/ASTQueryBuilderTest.php +++ b/tests/Unit/Support/AST/ASTQueryBuilderTest.php @@ -99,7 +99,7 @@ PHPFile::fromString('class Cool extends Ice {}') ->astQuery() ->class() - ->where(fn($query) => $query->where('extends->parts', ['Ice'])->get()) + ->where(fn($query) => $query->where('extends->name', 'Ice')->get()) ->assertMatchCount(1); }); @@ -107,7 +107,7 @@ PHPFile::fromString('class Cool extends Ice {}') ->astQuery() ->class() - ->where(fn($query) => $query->where('extends->parts', ['Ice'])) + ->where(fn($query) => $query->where('extends->name', 'Ice')) ->assertMatchCount(1); }); diff --git a/tests/Unit/Support/AST/PrettyPrintingTest.php b/tests/Unit/Support/AST/PrettyPrintingTest.php index 16b8246..a115535 100644 --- a/tests/Unit/Support/AST/PrettyPrintingTest.php +++ b/tests/Unit/Support/AST/PrettyPrintingTest.php @@ -26,7 +26,7 @@ public function sleeping() CODE; it('two line breaks separate methods', function() { - $parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); + $parser = (new ParserFactory)->createForNewestSupportedVersion(); $prettyPrinter = new PSR2PrettyPrinter; $stmts = $parser->parse(CODE); From 30ce61957bcbff6fac3b032ba37c87ad2f0bf16c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20J=C3=BCrisoo?= Date: Sat, 11 May 2024 09:51:08 +0200 Subject: [PATCH 4/5] Install composer if on macOS --- .github/workflows/tests.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c2af63f..73dd951 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -44,6 +44,10 @@ jobs: - name: Checkout code uses: actions/checkout@v2 + - name: Install Composer (macOS) + if: startsWith(runner.os, 'macOS') + run: curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer + - name: Determine composer cache directory id: determine-composer-cache-directory run: 'echo "::set-output name=directory::$(composer config cache-dir)"' From d1320bd6969dcf9072c7d3c504bc711685ce89dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20J=C3=BCrisoo?= Date: Sat, 11 May 2024 09:56:39 +0200 Subject: [PATCH 5/5] Do setup-php@v2 first, add composer in tools. --- .github/workflows/tests.yml | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 73dd951..d3d7758 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -44,9 +44,14 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - - name: Install Composer (macOS) - if: startsWith(runner.os, 'macOS') - run: curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd + coverage: none + ini-values: "memory_limit=-1" + tools: composer - name: Determine composer cache directory id: determine-composer-cache-directory @@ -59,14 +64,6 @@ jobs: key: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-composer-${{ hashFiles('**/composer.lock') }} restore-keys: dependencies-os-${{ matrix.os }}-php-${{ matrix.php-version }}-laravel-${{ matrix.laravel-version }}-composer- - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-version }} - extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd - coverage: none - ini-values: "memory_limit=-1" - - name: Setup problem matchers for PHP run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"