Skip to content

Commit

Permalink
Merge pull request #75 from ajthinking/add-insert-stmt-closure-with-b…
Browse files Browse the repository at this point in the history
…uilder

Allow passing a closure to insertStmt
  • Loading branch information
ajthinking authored May 25, 2022
2 parents 4d84592 + 490a6b6 commit 62abebd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Support/AST/ASTQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use PhpParser\BuilderFactory;
use PhpParser\ConstExprEvaluator;

class ASTQueryBuilder
Expand Down Expand Up @@ -351,6 +352,8 @@ public function insertStmts($newNodes): self

public function insertStmt($newNode): self
{
if($newNode instanceof Closure) $newNode = $newNode(new BuilderFactory);

$this->currentNodes()->each(function ($node) use ($newNode) {
$target = $node->result;

Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/Support/AST/ASTQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,16 @@
->render();

assertEquals($original, $after);
});

it('can insert stmt with a closure', function() {
PHPFile::make()->class(\App\Dummy::class)
->astQuery()
->class()
->insertStmt(function(BuilderFactory $builder) {
return $builder->property('someProperty')->getNode();
})
->commit()
->end()
->preview();
});

0 comments on commit 62abebd

Please sign in to comment.