Skip to content

Commit

Permalink
PHPParser class and property traversing is managed by PHPParserClassM…
Browse files Browse the repository at this point in the history
…ap 🚀 :explosion:
  • Loading branch information
ajthinking committed Apr 5, 2020
1 parent 862c5ec commit 463a168
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 34 deletions.
101 changes: 101 additions & 0 deletions sketches/notes2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
LaravelFile::load('app/User.php')->astQuery()->arrayDimFetch();
LaravelFile::load('app/User.php')->astQuery()->arrayItem();
LaravelFile::load('app/User.php')->astQuery()->array();
LaravelFile::load('app/User.php')->astQuery()->arrowFunction();
LaravelFile::load('app/User.php')->astQuery()->assign();
LaravelFile::load('app/User.php')->astQuery()->assignOp();
LaravelFile::load('app/User.php')->astQuery()->assignOp();
LaravelFile::load('app/User.php')->astQuery()->assignRef();
LaravelFile::load('app/User.php')->astQuery()->binaryOp();
LaravelFile::load('app/User.php')->astQuery()->binaryOp();
LaravelFile::load('app/User.php')->astQuery()->bitwiseNot();
LaravelFile::load('app/User.php')->astQuery()->booleanNot();
LaravelFile::load('app/User.php')->astQuery()->cast();
LaravelFile::load('app/User.php')->astQuery()->cast();
LaravelFile::load('app/User.php')->astQuery()->classConstFetch();
LaravelFile::load('app/User.php')->astQuery()->clone();
LaravelFile::load('app/User.php')->astQuery()->closure();
LaravelFile::load('app/User.php')->astQuery()->closureUse();
LaravelFile::load('app/User.php')->astQuery()->constFetch();
LaravelFile::load('app/User.php')->astQuery()->empty();
LaravelFile::load('app/User.php')->astQuery()->error();
LaravelFile::load('app/User.php')->astQuery()->errorSuppress();
LaravelFile::load('app/User.php')->astQuery()->eval();
LaravelFile::load('app/User.php')->astQuery()->exit();
LaravelFile::load('app/User.php')->astQuery()->funcCall();
LaravelFile::load('app/User.php')->astQuery()->include();
LaravelFile::load('app/User.php')->astQuery()->instanceof();
LaravelFile::load('app/User.php')->astQuery()->isset();
LaravelFile::load('app/User.php')->astQuery()->list();
LaravelFile::load('app/User.php')->astQuery()->methodCall();
LaravelFile::load('app/User.php')->astQuery()->new();
LaravelFile::load('app/User.php')->astQuery()->postDec();
LaravelFile::load('app/User.php')->astQuery()->postInc();
LaravelFile::load('app/User.php')->astQuery()->preDec();
LaravelFile::load('app/User.php')->astQuery()->preInc();
LaravelFile::load('app/User.php')->astQuery()->print();
LaravelFile::load('app/User.php')->astQuery()->propertyFetch();
LaravelFile::load('app/User.php')->astQuery()->shellExec();
LaravelFile::load('app/User.php')->astQuery()->staticCall();
LaravelFile::load('app/User.php')->astQuery()->staticPropertyFetch();
LaravelFile::load('app/User.php')->astQuery()->ternary();
LaravelFile::load('app/User.php')->astQuery()->unaryMinus();
LaravelFile::load('app/User.php')->astQuery()->unaryPlus();
LaravelFile::load('app/User.php')->astQuery()->variable();
LaravelFile::load('app/User.php')->astQuery()->yieldFrom();
LaravelFile::load('app/User.php')->astQuery()->yield();
LaravelFile::load('app/User.php')->astQuery()->fullyQualified();
LaravelFile::load('app/User.php')->astQuery()->relative();
LaravelFile::load('app/User.php')->astQuery()->dNumber();
LaravelFile::load('app/User.php')->astQuery()->encapsed();
LaravelFile::load('app/User.php')->astQuery()->encapsedStringPart();
LaravelFile::load('app/User.php')->astQuery()->lNumber();
LaravelFile::load('app/User.php')->astQuery()->magicConst();
LaravelFile::load('app/User.php')->astQuery()->magicConst();
LaravelFile::load('app/User.php')->astQuery()->string();
LaravelFile::load('app/User.php')->astQuery()->break();
LaravelFile::load('app/User.php')->astQuery()->case();
LaravelFile::load('app/User.php')->astQuery()->catch();
LaravelFile::load('app/User.php')->astQuery()->classConst();
LaravelFile::load('app/User.php')->astQuery()->classLike();
LaravelFile::load('app/User.php')->astQuery()->classMethod();
LaravelFile::load('app/User.php')->astQuery()->class();
LaravelFile::load('app/User.php')->astQuery()->const();
LaravelFile::load('app/User.php')->astQuery()->continue();
LaravelFile::load('app/User.php')->astQuery()->declareDeclare();
LaravelFile::load('app/User.php')->astQuery()->declare();
LaravelFile::load('app/User.php')->astQuery()->do();
LaravelFile::load('app/User.php')->astQuery()->echo();
LaravelFile::load('app/User.php')->astQuery()->elseIf();
LaravelFile::load('app/User.php')->astQuery()->else();
LaravelFile::load('app/User.php')->astQuery()->expression();
LaravelFile::load('app/User.php')->astQuery()->finally();
LaravelFile::load('app/User.php')->astQuery()->for();
LaravelFile::load('app/User.php')->astQuery()->foreach();
LaravelFile::load('app/User.php')->astQuery()->function();
LaravelFile::load('app/User.php')->astQuery()->global();
LaravelFile::load('app/User.php')->astQuery()->goto();
LaravelFile::load('app/User.php')->astQuery()->groupUse();
LaravelFile::load('app/User.php')->astQuery()->haltCompiler();
LaravelFile::load('app/User.php')->astQuery()->if();
LaravelFile::load('app/User.php')->astQuery()->inlineHTML();
LaravelFile::load('app/User.php')->astQuery()->interface();
LaravelFile::load('app/User.php')->astQuery()->label();
LaravelFile::load('app/User.php')->astQuery()->namespace();
LaravelFile::load('app/User.php')->astQuery()->nop();
LaravelFile::load('app/User.php')->astQuery()->property();
LaravelFile::load('app/User.php')->astQuery()->propertyProperty();
LaravelFile::load('app/User.php')->astQuery()->return();
LaravelFile::load('app/User.php')->astQuery()->staticVar();
LaravelFile::load('app/User.php')->astQuery()->static();
LaravelFile::load('app/User.php')->astQuery()->switch();
LaravelFile::load('app/User.php')->astQuery()->throw();
LaravelFile::load('app/User.php')->astQuery()->traitUse();
LaravelFile::load('app/User.php')->astQuery()->traitUseAdaptation();
LaravelFile::load('app/User.php')->astQuery()->traitUseAdaptation();
LaravelFile::load('app/User.php')->astQuery()->trait();
LaravelFile::load('app/User.php')->astQuery()->tryCatch();
LaravelFile::load('app/User.php')->astQuery()->unset();
LaravelFile::load('app/User.php')->astQuery()->useUse();
LaravelFile::load('app/User.php')->astQuery()->use();
LaravelFile::load('app/User.php')->astQuery()->while();
38 changes: 15 additions & 23 deletions src/Support/AST/ASTQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PhpParser\NodeFinder;
use PHPFileManipulator\Support\AST\ShallowNodeFinder;
use PHPFileManipulator\Traits\HasOperators;
use PHPFileManipulator\Traits\HasClassMap;
use PHPFileManipulator\Traits\PHPParserClassMap;
use PHPFileManipulator\Support\AST\Killable;
use PHPFileManipulator\Support\AST\RemovedNode;
use PHPFileManipulator\Support\AST\NodeReplacer;
Expand All @@ -22,7 +22,7 @@ class ASTQueryBuilder
{
use HasOperators;

use HasClassMap;
use PHPParserClassMap;

public $allowDeepQueries = true;

Expand All @@ -39,12 +39,20 @@ public function __construct($ast)

public function __call($method, $args)
{
// exists in classMap
// exists in classMap?
if($this->classMap($method)) return $this->traverse($this->classMap($method));

throw new Exception("Could not find a method $method in the ASTQueryBuilder!");
}

public function __get($property)
{
// exists in propertyMap?
if($this->propertyMap($property)) return $this->traverseInto($this->propertyMap($property));

throw new Exception("Could not find a property $property in the ASTQueryBuilder!");
}

public function traverse($expectedClass, $finderMethod = 'findInstanceOf')
{
$next = collect($this->tree[$this->currentDepth])->map(function($queryNode) use($expectedClass, $finderMethod) {
Expand Down Expand Up @@ -136,42 +144,26 @@ public function remember($key, $callback)
return $this;
}


/** OUTSIDE CONVENTION */
public function expression()
{
return $this->traverse(
$this->classMap('expression')
)->traverseInto('expr');
}

/** OUTSIDE CONVENTION */
public function named($string)
{
return $this->where('name->name', $string);
}

/** OUTSIDE CONVENTION */
public function arg($index)
{
return $this->traverseIntoArrayIndex('args', $index);
}

public function args()
{
return $this->traverseInto('args');
}

public function stmts()
{
return $this->traverseInto('stmts');
}

public function value()
{
return $this->traverseInto('value');
}

public function name()
{
return $this->traverseInto('name');
}

public function first()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace PHPFileManipulator\Traits;

trait HasClassMap
trait PHPParserClassMap
{
public function classMap($class = null)
{
Expand Down Expand Up @@ -132,7 +132,7 @@ public function propertyMap($property)
// 86 => "static",
// 91 => "trait",

// TEMPORARY FIX WITH NAMESPACING ___
// TEMPORARY FIX WITH NAMESPACING $1___
// CONSIDER USING A MAGIC GETTER TO ALLOW SAME NAME AS METHOD?
$map = [
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/ASTQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public function it_can_query_deep()
->staticCall()
->where('class', 'Schema')
->named('create')
->args()
->value()
->value()
->args
->value
->value
->get() // exit ASTQueryBuilder, get a Collection
->first();

Expand Down
12 changes: 6 additions & 6 deletions tests/samples/ast_data.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ function second() {
->remember('table_name', function ($node) {
return $node
->arg(0)
->value()
->value()
->value
->value
->get()
->first();
})
->arg(1)
->value()
->value
->stmts()
->methodCall()
->where('var->name', 'table')
Expand All @@ -55,9 +55,9 @@ function second() {
->get()
->first();
})
->args()
->value()
->value()
->args
->value
->value
->remember('column_name', function ($node) {
return $node->get();
})
Expand Down

0 comments on commit 463a168

Please sign in to comment.