add ContaintsStmts interface to mark stmt classes with nested stmts inside#1113
add ContaintsStmts interface to mark stmt classes with nested stmts inside#1113TomasVotruba wants to merge 3 commits intonikic:masterfrom
Conversation
a0325e3 to
013f2d2
Compare
|
@TomasVotruba just for note: we have custom node: |
|
Is this actually useful to you if it does not include ClassMethod? |
|
It's very useful, as instead of 18 nodes types we can check single one. Also, the |
samsonasik
left a comment
There was a problem hiding this comment.
TryCatch_ is also needs implemens Node\ContainsStmts, see
|
@TomasVotruba imo, public function getNodeTypes(): array
{
- return [StmtsAwareInterface::class];
+ return [Node\ContainsStmts::class, ClassMethod::class];
}for example, this required when we need to use it in required node, eg: downgrade rules which required code needs to be downgraded. we need also effort to update to include If the
interface ContainsStmts {}
// BC break patch, can be placed in our `bootstrap.php`
class_alias(ContainsStmts::class, \Rector\Contract\PhpParser\Node\StmtsAwareInterface::class);
class SomeNode implements ContainsStmts {}
$obj = new SomeNode();
// old should keep working
var_dump($obj instanceof \Rector\Contract\PhpParser\Node\StmtsAwareInterface);
// new
var_dump($obj instanceof ContainsStmts);
-final class FileWithoutNamespace extends Stmt implements StmtsAwareInterface
+final class FileWithoutNamespace extends Stmt implements Node\ContainsStmts
|
|
A way to include ClassMethod would be to give this a |
|
@nikic That sounds good. I think |
|
@TomasVotruba |
|
@samsonasik I agree. It was just example of such method :) |
|
@nikic I'm testing the interface and There is one issue with writing the modified $stmts = $if->getStmts();
foreach ($stmts as $key => $value) {
if (mt_rand(0, 1) {
// remove, add or shuffle nodes here
unset($stmts[$key]));
}
}
// then we want to update stmts array in the main node
$node->setStmts($stmts);Logical approach is to add /**
* @param Stmt[] $stmts
*/
public function setStmts(array $stmts): void
{
$this->stmts = $stmts;
}Thoughts? |
|
@TomasVotruba imo, For apply change, use direct That's why I suggest to add: /**
* @property Stmt[]|null $stmts
*/tag in the interface before #1113 (review) that will also make less noise on phpstan/psalm check, avoid property not exists notice. |
86a87b0 to
2a2622a
Compare
|
@samsonasik Rebased on latest The |
2a2622a to
c972ad3
Compare
c972ad3 to
0af497f
Compare
|
Maybe it makes sense to add new common base-classes (simular to CallLike), so you can at least reduce the number of "instanceof checks" required. E.g. Maybe something similar for the different "IF" types |
|
Looks good to me 👍 |
|
@TomasVotruba looking at declare(ticks=1) {
echo 'test';
}see https://3v4l.org/QmPYL#v8.0.26 . that's why it has the PHP-Parser/lib/PhpParser/Node/Stmt/Declare_.php Lines 8 to 12 in e481026 It likely less people use of the declare and its stmts syntax above, but that still possible, so I think we can add |
This PR is revision #836 to address only mentioned issue with the
ClassMethod.It's the only node, that can have
$stmts = nullvalue. Such method can beabstractor part ofInterface_, so there is nothing to iterate on. This revision exclude theClassMethodThis would solve run custom Rector tests on PHPUnit 12, remove lot of vendor patching in Rector
/vendor(ugly hack), make make writing PHPStan rules for all nodes that have$stmtseasier and streamline working with the$stmtsproperty in userland