Fix CountOnNullRector for array typehint

This commit is contained in:
Tomas Votruba 2018-11-02 17:25:54 +01:00
parent 8af6da1362
commit 86140cf9ed
4 changed files with 84 additions and 8 deletions

View File

@ -0,0 +1,40 @@
<?php declare(strict_types=1);
namespace Rector\Php\Tests\Rector\FuncCall\CountOnNullRector\Wrong;
function a(array $a = [])
{
count($a);
}
/**
* @param array $b
*/
function b($b = [])
{
count($b);
}
/**
* @param array $c
*/
function c($c)
{
count($c);
}
/**
* @param mixed[] $d
*/
function d($d)
{
count($d);
}
/**
* @param string[] $e
*/
function e($e)
{
count($e);
}

View File

@ -25,6 +25,7 @@ final class CountOnNullRectorTest extends AbstractRectorTestCase
yield [__DIR__ . '/Wrong/wrong3.php.inc', __DIR__ . '/Correct/correct3.php.inc'];
yield [__DIR__ . '/Wrong/wrong4.php.inc', __DIR__ . '/Correct/correct4.php.inc'];
yield [__DIR__ . '/Wrong/wrong5.php.inc', __DIR__ . '/Correct/correct5.php.inc'];
yield [__DIR__ . '/Wrong/wrong6.php.inc', __DIR__ . '/Correct/correct6.php.inc'];
}
protected function provideConfig(): string

View File

@ -0,0 +1,40 @@
<?php declare(strict_types=1);
namespace Rector\Php\Tests\Rector\FuncCall\CountOnNullRector\Wrong;
function a(array $a = [])
{
count($a);
}
/**
* @param array $b
*/
function b($b = [])
{
count($b);
}
/**
* @param array $c
*/
function c($c)
{
count($c);
}
/**
* @param mixed[] $d
*/
function d($d)
{
count($d);
}
/**
* @param string[] $e
*/
function e($e)
{
count($e);
}

View File

@ -14,13 +14,12 @@ use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StringType;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\NodeTypeResolver\NodeTypeResolver;
use function Safe\class_implements;
/**
* This could be part of @see AbstractRector, but decopuling to trait
@ -100,16 +99,12 @@ trait TypeAnalyzerTrait
/** @var Scope $nodeScope */
$nodeScope = $node->getAttribute(Attribute::SCOPE);
$nodeType = $nodeScope->getType($node);
if ($nodeType instanceof ConstantArrayType) {
return true;
}
if ($nodeType instanceof ObjectType) {
$className = $nodeType->getClassName();
return in_array(Countable::class, class_implements($className), true);
return is_a($nodeType->getClassName(), Countable::class, true);
}
return false;
return $nodeType instanceof ArrayType;
}
/**