[Php71] Skip property with array doc filled by __construct on CountOnNullRector (#1208)

* [Php71] Skip property with array doc on CountOnNullRector

* rename fixture

* Fixed 🎉

* Trigger notification

* clean up

* rectify
This commit is contained in:
Abdul Malik Ikhsan 2021-11-11 05:14:01 +07:00 committed by GitHub
parent cd217264b7
commit 48a97d51c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 6 deletions

View File

@ -51,6 +51,7 @@ abstract class AbstractRectorTestCase extends AbstractTestCase implements Rector
if (file_exists(__DIR__ . '/../../../preload.php') && file_exists(__DIR__ . '/../../../vendor')) {
require_once __DIR__ . '/../../../preload.php';
}
if (\file_exists(__DIR__ . '/../../../vendor/scoper-autoload.php')) {
require_once __DIR__ . '/../../../vendor/scoper-autoload.php';
}

View File

@ -0,0 +1,19 @@
<?php
namespace Rector\Tests\Php71\Rector\FuncCall\CountOnNullRector\Fixture;
final class SkipPropertyArrayFilledByConstruct
{
/** @var array */
private $property;
public function __construct(array $property)
{
$this->property = $property;
}
public function run(): int
{
return count($this->property);
}
}

View File

@ -15,6 +15,7 @@ use PHPStan\Reflection\PropertyReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\TypeWithClassName;
use PHPStan\Type\UnionType;
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
@ -24,7 +25,8 @@ final class CountableAnalyzer
public function __construct(
private NodeTypeResolver $nodeTypeResolver,
private NodeNameResolver $nodeNameResolver,
private ReflectionProvider $reflectionProvider
private ReflectionProvider $reflectionProvider,
private PropertyFetchAnalyzer $propertyFetchAnalyzer
) {
}
@ -78,6 +80,10 @@ final class CountableAnalyzer
return false;
}
if ($this->propertyFetchAnalyzer->isFilledByConstructParam($expr)) {
return false;
}
$propertyDefaultValue = $propertiesDefaults[$propertyName];
return $propertyDefaultValue === null;
}

View File

@ -127,7 +127,7 @@ final class PropertyFetchAnalyzer
return $this->isLocalPropertyFetch($node->var);
}
public function isFilledByConstructParam(Property $property): bool
public function isFilledByConstructParam(Property|PropertyFetch|StaticPropertyFetch $property): bool
{
$class = $this->betterNodeFinder->findParentType($property, Class_::class);
if (! $class instanceof Class_) {
@ -150,10 +150,17 @@ final class PropertyFetchAnalyzer
}
/** @var string $propertyName */
$propertyName = $this->nodeNameResolver->getName($property->props[0]->name);
$kindPropertyFetch = $property->isStatic()
? StaticPropertyFetch::class
: PropertyFetch::class;
$propertyName = $property instanceof Property
? $this->nodeNameResolver->getName($property->props[0]->name)
: $this->nodeNameResolver->getName($property);
if ($property instanceof Property) {
$kindPropertyFetch = $property->isStatic()
? StaticPropertyFetch::class
: PropertyFetch::class;
} else {
$kindPropertyFetch = $property::class;
}
return $this->isParamFilledStmts($params, $stmts, $propertyName, $kindPropertyFetch);
}