[Strict] Skip ArrayDimFetch on DisallowedEmptyRuleFixerRector (#2023)

* [ISSUE-7100] Added failing test cases for when accessing array key might lead to PHP Notice: Undefined Index

* [Strict] Skip ArrayDimFetch on DisallowedEmptyRuleFixerRector

Co-authored-by: Jiří Bok <jiri.bok@protonmail.com>
This commit is contained in:
Abdul Malik Ikhsan 2022-04-08 03:54:16 +07:00 committed by GitHub
parent ab8d35ab44
commit d17990345c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,19 @@
<?php
namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture;
class SkipArgArrayDimFetchFromPropertyFetch {
/**
* @var array<string>
*/
protected array $labels = [];
public function getLabel(string $value): string
{
if (empty($this->labels[$value])) {
throw new \InvalidArgumentException(sprintf('%s is missing label for value "%s"', static::class, $value));
}
return $this->labels[$value];
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace Rector\Tests\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector\Fixture;
class SkipArgArrayDimFetchFromStaticPropertyFetch {
/**
* @var array<string>
*/
protected static array $labels = [];
public static function getLabel(string $value): string
{
if (empty(static::$labels[$value])) {
throw new \InvalidArgumentException(sprintf('%s is missing label for value "%s"', static::class, $value));
}
return static::$labels[$value];
}
}

View File

@ -6,6 +6,7 @@ namespace Rector\Strict\Rector\Empty_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Empty_;
use PHPStan\Analyser\Scope;
@ -82,6 +83,10 @@ CODE_SAMPLE
return $this->refactorBooleanNot($node, $scope);
}
if ($node->expr instanceof ArrayDimFetch) {
return null;
}
return $this->refactorEmpty($node, $scope, $this->treatAsNonEmpty);
}