Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Node] add StmtsIterable interface to mark nodes that contain iterable stmts to improve hooking in node visitors #836

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Expr/Closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\Node\Expr;
use PhpParser\Node\FunctionLike;

class Closure extends Expr implements FunctionLike {
class Closure extends Expr implements FunctionLike, Node\StmtsIterable {
/** @var bool Whether the closure is static */
public $static;
/** @var bool Whether to return by reference */
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Case_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;

class Case_ extends Node\Stmt {
class Case_ extends Node\Stmt implements Node\StmtsIterable {
/** @var null|Node\Expr Condition (null for default) */
public $cond;
/** @var Node\Stmt[] Statements */
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Catch_.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr;

class Catch_ extends Node\Stmt {
class Catch_ extends Node\Stmt implements Node\StmtsIterable {
/** @var Node\Name[] Types of exceptions to catch */
public $types;
/** @var Expr\Variable|null Variable for exception */
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Do_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;

class Do_ extends Node\Stmt {
class Do_ extends Node\Stmt implements Node\StmtsIterable {
/** @var Node\Stmt[] Statements */
public $stmts;
/** @var Node\Expr Condition */
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/ElseIf_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;

class ElseIf_ extends Node\Stmt {
class ElseIf_ extends Node\Stmt implements Node\StmtsIterable {
/** @var Node\Expr Condition */
public $cond;
/** @var Node\Stmt[] Statements */
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Else_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;

class Else_ extends Node\Stmt {
class Else_ extends Node\Stmt implements Node\StmtsIterable {
/** @var Node\Stmt[] Statements */
public $stmts;

Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Finally_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;

class Finally_ extends Node\Stmt {
class Finally_ extends Node\Stmt implements Node\StmtsIterable {
/** @var Node\Stmt[] Statements */
public $stmts;

Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/For_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;

class For_ extends Node\Stmt {
class For_ extends Node\Stmt implements Node\StmtsIterable {
/** @var Node\Expr[] Init expressions */
public $init;
/** @var Node\Expr[] Loop conditions */
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Foreach_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;

class Foreach_ extends Node\Stmt {
class Foreach_ extends Node\Stmt implements Node\StmtsIterable {
/** @var Node\Expr Expression to iterate */
public $expr;
/** @var null|Node\Expr Variable to assign key to */
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Function_.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PhpParser\Node;
use PhpParser\Node\FunctionLike;

class Function_ extends Node\Stmt implements FunctionLike {
class Function_ extends Node\Stmt implements FunctionLike, Node\StmtsIterable {
/** @var bool Whether function returns by reference */
public $byRef;
/** @var Node\Identifier Name */
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/If_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;

class If_ extends Node\Stmt {
class If_ extends Node\Stmt implements Node\StmtsIterable {
/** @var Node\Expr Condition expression */
public $cond;
/** @var Node\Stmt[] Statements */
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/Namespace_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;

class Namespace_ extends Node\Stmt {
class Namespace_ extends Node\Stmt implements Node\StmtsIterable {
/* For use in the "kind" attribute */
public const KIND_SEMICOLON = 1;
public const KIND_BRACED = 2;
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/TryCatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;

class TryCatch extends Node\Stmt {
class TryCatch extends Node\Stmt implements Node\StmtsIterable {
/** @var Node\Stmt[] Statements */
public $stmts;
/** @var Catch_[] Catches */
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Node/Stmt/While_.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PhpParser\Node;

class While_ extends Node\Stmt {
class While_ extends Node\Stmt implements Node\StmtsIterable {
/** @var Node\Expr Condition */
public $cond;
/** @var Node\Stmt[] Statements */
Expand Down
14 changes: 14 additions & 0 deletions lib/PhpParser/Node/StmtsIterable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace PhpParser\Node;

use PhpParser\Node;

/**
* @property Stmt[] $stmts
TomasVotruba marked this conversation as resolved.
Show resolved Hide resolved
*/
interface StmtsIterable extends Node
{
}
35 changes: 35 additions & 0 deletions test/PhpParser/Node/StmtsIterableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace PhpParser\Node;

use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeFinder;
use PhpParser\ParserFactory;

final class StmtsIterableTest extends \PHPUnit\Framework\TestCase
{
public function test()
{
$parser = (new ParserFactory())->createForNewestSupportedVersion();
$nodes = $parser->parse(<<<'CODE_SAMPLE'
<?php

function clearItemList($items)
{
foreach ($items as $key => $value) {
$value = 100;
$value = 100;
}
}
CODE_SAMPLE
);

$nodeFinder = new NodeFinder();
$stmtsIterables = $nodeFinder->findInstanceOf($nodes, StmtsIterable::class);

$this->assertCount(2, $stmtsIterables);
// $this->assertInstanceOf(Function_::class, $stmtsIterables[0]);
// $this->assertInstanceOf(Foreach_::class, $stmtsIterables[1]);
}
}