From bdb989c40c136fc998d173652d65151d9d71b6fc Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 21 Aug 2023 16:16:47 +0700 Subject: [PATCH 1/2] [TypeDeclaration] Add new array replaced with array on ReturnTypeFromStrictNewArrayRector --- .../Fixture/new_array_replace.php.inc | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/Fixture/new_array_replace.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/Fixture/new_array_replace.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/Fixture/new_array_replace.php.inc new file mode 100644 index 00000000000..ee56874214f --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/Fixture/new_array_replace.php.inc @@ -0,0 +1,53 @@ +getFilePaths('/' . $identifier . '_' . $size . '.jpg/'); + } else { + $imagePaths = $this->getFilePaths('/' . $identifier . '.jpg/'); + } + + return $imagePaths; + } + + private function getFilePaths(): array + { + return []; + } +} + +?> +----- +getFilePaths('/' . $identifier . '_' . $size . '.jpg/'); + } else { + $imagePaths = $this->getFilePaths('/' . $identifier . '.jpg/'); + } + + return $imagePaths; + } + + private function getFilePaths(): array + { + return []; + } +} + +?> From 33fa87c13c0f8d81bbc63021b1242a9bd1ee21b6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 21 Aug 2023 16:21:50 +0700 Subject: [PATCH 2/2] implemented :tada: --- .../Fixture/new_array_replace.php.inc | 3 +++ .../ClassMethod/ReturnTypeFromStrictNewArrayRector.php | 7 ++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/Fixture/new_array_replace.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/Fixture/new_array_replace.php.inc index ee56874214f..83f9f1df9d1 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/Fixture/new_array_replace.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector/Fixture/new_array_replace.php.inc @@ -31,6 +31,9 @@ namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNe final class NewArrayReplace { + /** + * @return mixed[] + */ public function getImagePaths($identifier = 'detail', $size = null): array { $imagePaths = []; diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php index e01dafe504e..8c4196e0082 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromStrictNewArrayRector.php @@ -192,7 +192,12 @@ private function isVariableOverriddenWithNonArray( continue; } - if (! $assign->expr instanceof Array_) { + if ($assign->expr instanceof Array_) { + continue; + } + + $nativeType = $this->nodeTypeResolver->getNativeType($assign->expr); + if (! $nativeType->isArray()->yes()) { return true; } }