Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector\Fixture;

final class SkipUninitializedTypedArrayProperty
{
private array $items;

public function setItems(array $items): void
{
$this->items = $items;
}

public function isEmpty(): bool
{
return empty($this->items);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector\Fixture;

final class TypedArrayPropertyAssignedInConstructor
{
private array $items;

public function __construct(array $items)
{
$this->items = $items;
}

public function isEmpty(): bool
{
return empty($this->items);
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Empty_\SimplifyEmptyCheckOnEmptyArrayRector\Fixture;

final class TypedArrayPropertyAssignedInConstructor
{
private array $items;

public function __construct(array $items)
{
$this->items = $items;
}

public function isEmpty(): bool
{
return $this->items === [];
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\NodeAnalyzer\ExprAnalyzer;
Expand All @@ -27,6 +29,7 @@
use Rector\Reflection\ReflectionResolver;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
use Rector\TypeDeclaration\TypeInferer\PropertyTypeInferer\AllAssignNodePropertyTypeInferer;

/**
Expand All @@ -39,7 +42,8 @@ public function __construct(
private readonly ReflectionResolver $reflectionResolver,
private readonly AstResolver $astResolver,
private readonly AllAssignNodePropertyTypeInferer $allAssignNodePropertyTypeInferer,
private readonly ReservedKeywordAnalyzer $reservedKeywordAnalyzer
private readonly ReservedKeywordAnalyzer $reservedKeywordAnalyzer,
private readonly ConstructorAssignDetector $constructorAssignDetector
) {
}

Expand Down Expand Up @@ -128,8 +132,12 @@ private function isAllowedExpr(Variable|PropertyFetch|StaticPropertyFetch $expr,
$nativeType = $phpPropertyReflection->getNativeType();

if (! $nativeType instanceof MixedType) {
return $nativeType->isArray()
->yes();
if (! $nativeType->isArray()->yes()) {
return false;
}

// reading an uninitialized typed property throws, while empty() returns true
return $this->isInitializedProperty($phpPropertyReflection, $propertyName);
}

$property = $this->astResolver->resolvePropertyFromPropertyReflection($phpPropertyReflection);
Expand All @@ -152,4 +160,29 @@ private function isAllowedExpr(Variable|PropertyFetch|StaticPropertyFetch $expr,
return $type->isArray()
->yes();
}

private function isInitializedProperty(PhpPropertyReflection $phpPropertyReflection, string $propertyName): bool
{
$propertyNode = $this->astResolver->resolvePropertyFromPropertyReflection($phpPropertyReflection);

// promoted property is assigned on construct
if ($propertyNode instanceof Param) {
return true;
}

if (! $propertyNode instanceof Property) {
return false;
}

if ($propertyNode->props[0]->default !== null) {
return true;
}

$classLike = $this->astResolver->resolveClassFromClassReflection($phpPropertyReflection->getDeclaringClass());
if ($classLike === null) {
return false;
}

return $this->constructorAssignDetector->isPropertyAssigned($classLike, $propertyName);
}
}
Loading