Start writing nested expression fixer

This commit is contained in:
Daniil Gentili 2020-08-30 20:55:28 +02:00
parent e33da7edc8
commit 940c2bbab4
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 65 additions and 2 deletions

View File

@ -76,7 +76,7 @@ class IssetExpressionFixer extends Plugin
if (!$needsFixing) {
continue;
}
switch (\get_class($workVar)) {
switch ($class = \get_class($workVar)) {
case ArrayDimFetch::class:
case PropertyFetch::class:
$workVar->var = self::callPoly('returnMe', $workVar->var);
@ -97,6 +97,8 @@ class IssetExpressionFixer extends Plugin
new LNumber(0)
));
break;
default:
throw new \RuntimeException("Trying to fix unknown isset expression $class");
}
}
}

View File

@ -0,0 +1,61 @@
<?php
namespace Phabel\Plugin;
use Phabel\Plugin;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
class NestedExpressionFixer extends Plugin
{
public function enter(Expr $expr): void
{
/** @var array<string, array<class-string<Expr>, true>> */
$subNodes = $this->getConfig($class = \get_class($expr), false);
if (!$subNodes) {
return;
}
foreach ($subNodes as $key => $types) {
/** @var Expr $value */
$value = &$expr->{$key};
if (!isset($types[\get_class($value)])) {
continue;
}
switch ($class) {
case ArrayDimFetch::class:
case PropertyFetch::class:
case MethodCall::class:
case New_::class:
case Instanceof_::class:
$value = self::callPoly('returnMe', $value);
break;
case FuncCall::class:
$expr->var = self::callPoly('returnMe', $expr->var);
break;
}
}
}
/**
* Returns the data provided.
*
* @param mixed $data Data
*
* @return mixed
*
* @template T
*
* @psalm-param T $data data
*
* @psalm-return T
*/
public static function returnMe($data)
{
return $data;
}
}

View File

@ -264,7 +264,7 @@ foreach ($instanceArgTypes as $class => $argTypes) {
}
$keys = [];
foreach ($result['isset'] as $version) {
foreach ($result['main'] as $version) {
$keys = array_merge_recursive($keys, $version);
}
foreach ($keys as &$values) {