From eef1826750754ad9e7cca0ae156fb23a1b4c265f Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 23 Dec 2023 19:36:12 +0000 Subject: [PATCH] Fix StrictArrayParamDimFetchRector for string access --- .../return_self_different_namespace.php | 18 +++++++++++++++ .../Fixture/twice_same_type.php | 22 +++++++++++++++++++ .../skip_array_possible_string.php.inc | 17 ++++++++++++++ .../StrictArrayParamDimFetchRector.php | 11 +++++++++- 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Fixture/return_self_different_namespace.php create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Fixture/twice_same_type.php create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_array_possible_string.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Fixture/return_self_different_namespace.php b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Fixture/return_self_different_namespace.php new file mode 100644 index 00000000000..a6d3e9ce0b8 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Fixture/return_self_different_namespace.php @@ -0,0 +1,18 @@ + diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Fixture/twice_same_type.php b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Fixture/twice_same_type.php new file mode 100644 index 00000000000..677194110ec --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictTypedCallRector/Fixture/twice_same_type.php @@ -0,0 +1,22 @@ +getNumber(100); + } + + return $this->getNumber(10); + } + + private function getNumber(int $value): int + { + return $value; + } +} + +?> diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_array_possible_string.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_array_possible_string.php.inc new file mode 100644 index 00000000000..28e7fbe6f37 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/StrictArrayParamDimFetchRector/Fixture/skip_array_possible_string.php.inc @@ -0,0 +1,17 @@ +parentClassMethodTypeOverrideGuard->hasParentClassMethod($node)) { return null; } @@ -136,6 +135,10 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function return null; } + if (! $node->dim instanceof Expr) { + return null; + } + if (! $node->var instanceof Variable) { return null; } @@ -150,6 +153,12 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function return null; } + // skip integer in possibly string type as string can be accessed via int + $dimType = $this->getType($node->dim); + if ($dimType->isInteger()->yes() && $variableType->isString()->maybe()) { + return null; + } + $isParamAccessedArrayDimFetch = true; return null; });