diff --git a/packages/NodeTypeResolver/src/NodeTypeResolver.php b/packages/NodeTypeResolver/src/NodeTypeResolver.php index a6020dd65aa..cc25d26b2b0 100644 --- a/packages/NodeTypeResolver/src/NodeTypeResolver.php +++ b/packages/NodeTypeResolver/src/NodeTypeResolver.php @@ -270,6 +270,13 @@ final class NodeTypeResolver return true; } + if ($node instanceof PropertyFetch) { + // PHPStan false positive, when variable has type[] docblock, but default array is missing + if ($this->isPropertyFetchWithArrayDefault($node) === false) { + return false; + } + } + if ($nodeStaticType instanceof MixedType) { if ($nodeStaticType->isExplicitMixed()) { return false; diff --git a/packages/Php/src/Rector/FuncCall/CountOnNullRector.php b/packages/Php/src/Rector/FuncCall/CountOnNullRector.php index 8403f45a90e..21f6704ad8c 100644 --- a/packages/Php/src/Rector/FuncCall/CountOnNullRector.php +++ b/packages/Php/src/Rector/FuncCall/CountOnNullRector.php @@ -71,7 +71,7 @@ CODE_SAMPLE return null; } - if ($this->isNullType($countedNode)) { + if ($this->isNullableType($countedNode) || $this->isNullType($countedNode)) { $identicalNode = new Identical($countedNode, $this->createNull()); $ternaryNode = new Ternary($identicalNode, new LNumber(0), $node); } else { diff --git a/packages/Php/tests/Rector/FuncCall/CountOnNullRector/CountOnNullRectorTest.php b/packages/Php/tests/Rector/FuncCall/CountOnNullRector/CountOnNullRectorTest.php index 5c941de9f62..90075b8272c 100644 --- a/packages/Php/tests/Rector/FuncCall/CountOnNullRector/CountOnNullRectorTest.php +++ b/packages/Php/tests/Rector/FuncCall/CountOnNullRector/CountOnNullRectorTest.php @@ -20,7 +20,11 @@ final class CountOnNullRectorTest extends AbstractRectorTestCase __DIR__ . '/Fixture/preg_match_array.php.inc', __DIR__ . '/Fixture/local_property.php.inc', __DIR__ . '/Fixture/double_same_variable.php.inc', - __DIR__ . '/Fixture/array_merge.php.inc', + + __DIR__ . '/Fixture/property_with_doc.php.inc', + __DIR__ . '/Fixture/nullable_array.php.inc', + // skip + __DIR__ . '/Fixture/skip_array_merge.php.inc', ]); } diff --git a/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/local_property.php.inc b/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/local_property.php.inc index 28be486b68d..1062a7a1d46 100644 --- a/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/local_property.php.inc +++ b/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/local_property.php.inc @@ -9,7 +9,7 @@ final class HeaderControl /** * @var mixed[] */ - private $alsoTitles; + private $alsoTitles = []; /** * @var mixed @@ -42,7 +42,7 @@ final class HeaderControl /** * @var mixed[] */ - private $alsoTitles; + private $alsoTitles = []; /** * @var mixed diff --git a/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/nullable_array.php.inc b/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/nullable_array.php.inc new file mode 100644 index 00000000000..4f845f37c45 --- /dev/null +++ b/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/nullable_array.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/property_with_doc.php.inc b/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/property_with_doc.php.inc new file mode 100644 index 00000000000..79eb19de65c --- /dev/null +++ b/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/property_with_doc.php.inc @@ -0,0 +1,37 @@ +fail); + } +} + +?> +----- +fail) || $this->fail instanceof \Countable ? count($this->fail) : 0; + } +} + +?> diff --git a/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/array_merge.php.inc b/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/skip_array_merge.php.inc similarity index 95% rename from packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/array_merge.php.inc rename to packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/skip_array_merge.php.inc index d968c845a46..bd5e2d2a161 100644 --- a/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/array_merge.php.inc +++ b/packages/Php/tests/Rector/FuncCall/CountOnNullRector/Fixture/skip_array_merge.php.inc @@ -2,7 +2,7 @@ namespace Rector\Php\Tests\Rector\FuncCall\CountOnNullRector\Fixture; -class ArrayMerge +class SkipArrayMerge { public function run(string $string, array $regexes): ?array {