From d17990345ced9d96a33665c03e5a7d8d0a4cf5db Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 8 Apr 2022 03:54:16 +0700 Subject: [PATCH] [Strict] Skip ArrayDimFetch on DisallowedEmptyRuleFixerRector (#2023) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [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 --- ...rray_dim_fetch_from_property_fetch.php.inc | 19 +++++++++++++++++++ ...m_fetch_from_static_property_fetch.php.inc | 19 +++++++++++++++++++ .../Empty_/DisallowedEmptyRuleFixerRector.php | 5 +++++ 3 files changed, 43 insertions(+) create mode 100644 rules-tests/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector/Fixture/skip_arg_array_dim_fetch_from_property_fetch.php.inc create mode 100644 rules-tests/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector/Fixture/skip_arg_array_dim_fetch_from_static_property_fetch.php.inc diff --git a/rules-tests/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector/Fixture/skip_arg_array_dim_fetch_from_property_fetch.php.inc b/rules-tests/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector/Fixture/skip_arg_array_dim_fetch_from_property_fetch.php.inc new file mode 100644 index 00000000000..6cce8cd69e9 --- /dev/null +++ b/rules-tests/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector/Fixture/skip_arg_array_dim_fetch_from_property_fetch.php.inc @@ -0,0 +1,19 @@ + + */ + 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]; + } +} diff --git a/rules-tests/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector/Fixture/skip_arg_array_dim_fetch_from_static_property_fetch.php.inc b/rules-tests/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector/Fixture/skip_arg_array_dim_fetch_from_static_property_fetch.php.inc new file mode 100644 index 00000000000..855de842375 --- /dev/null +++ b/rules-tests/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector/Fixture/skip_arg_array_dim_fetch_from_static_property_fetch.php.inc @@ -0,0 +1,19 @@ + + */ + 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]; + } +} diff --git a/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php b/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php index 0e7cd70116d..558cd6fe664 100644 --- a/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php +++ b/rules/Strict/Rector/Empty_/DisallowedEmptyRuleFixerRector.php @@ -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); }