Skip to content

Commit

Permalink
Merge pull request #85 from jasonvarga/php-parser-5
Browse files Browse the repository at this point in the history
Support for php-parser 5
  • Loading branch information
ajthinking authored May 11, 2024
2 parents 291f5d4 + d1320bd commit d2c5cd0
Show file tree
Hide file tree
Showing 13 changed files with 30 additions and 48 deletions.
17 changes: 9 additions & 8 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2

- 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
run: 'echo "::set-output name=directory::$(composer config cache-dir)"'
Expand All @@ -55,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"

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 1 addition & 4 deletions src/Endpoints/PHP/Extends_.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 1 addition & 3 deletions src/Endpoints/PHP/Implements_.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions src/Endpoints/PHP/Namespace_.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
Expand Down
1 change: 0 additions & 1 deletion src/Endpoints/PHP/UseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function get()
->class()
->traitUse()
->name()
->parts
->get()
->toArray();

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/PHP/Use_.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 0 additions & 2 deletions src/PHPFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class PHPFile

protected $tokens;

protected $lexer;

protected $directives = [];

public function __construct(
Expand Down
3 changes: 2 additions & 1 deletion src/Support/AST/ShallowNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Archetype\Support\AST;

use PhpParser\Node;
use PhpParser\NodeFinder;

class ShallowNodeFinder extends NodeFinder
Expand All @@ -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();
}
Expand Down
8 changes: 4 additions & 4 deletions src/Support/PSR2PrettyPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand All @@ -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
Expand Down
21 changes: 6 additions & 15 deletions src/Traits/HasIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Error as PHPParserError;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\ParserFactory;

trait HasIO
{
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Support/AST/ASTQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@
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);
});

test('unresolved where closures with matches are considered truthy', function() {
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);
});

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Support/AST/PrettyPrintingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d2c5cd0

Please sign in to comment.