From be8b2ebb42e018e9bc203589fc454d011cc501e2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Sep 2023 06:04:12 +0700 Subject: [PATCH 01/11] [TypeDeclaration] Handle fallback from param on ReturnTypeFromReturnNewRector --- .../Fixture/fallback_from_param.php.inc | 43 +++++++++++++++++++ .../Fixture/fallback_from_param2.php.inc | 39 +++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param.php.inc create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param2.php.inc diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param.php.inc new file mode 100644 index 00000000000..ab4b0483d50 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param.php.inc @@ -0,0 +1,43 @@ + +----- + diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param2.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param2.php.inc new file mode 100644 index 00000000000..5437489b9f7 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param2.php.inc @@ -0,0 +1,39 @@ + +----- + From 7594001bc3bce8ad3f99c06dfbb6aa38bdf4becf Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Sep 2023 06:05:08 +0700 Subject: [PATCH 02/11] implemented :tada: --- .../Rector/ClassMethod/ReturnTypeFromReturnNewRector.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php index 7a2b90a1a14..e7e379a2566 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php @@ -201,6 +201,12 @@ private function resolveReturnNewType(array $returns): ?array $newTypes = []; foreach ($returns as $return) { if (! $return->expr instanceof New_) { + $returnType = $this->nodeTypeResolver->getNativeType($return->expr); + if ($returnType instanceof \PHPStan\Type\ObjectType) { + $newTypes[] = $returnType; + continue; + } + return null; } From 2a154c6dcbee1bde44246d01e4ff090a4e123e7b Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sun, 17 Sep 2023 23:07:01 +0000 Subject: [PATCH 03/11] [ci-review] Rector Rectify --- .../Rector/ClassMethod/ReturnTypeFromReturnNewRector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php index e7e379a2566..f66c11eba9d 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php @@ -202,7 +202,7 @@ private function resolveReturnNewType(array $returns): ?array foreach ($returns as $return) { if (! $return->expr instanceof New_) { $returnType = $this->nodeTypeResolver->getNativeType($return->expr); - if ($returnType instanceof \PHPStan\Type\ObjectType) { + if ($returnType instanceof ObjectType) { $newTypes[] = $returnType; continue; } From e69bc6e3bca01d62572135d04506826690d8351f Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Sep 2023 06:06:57 +0700 Subject: [PATCH 04/11] rename fixture --- ...k_from_param2.php.inc => fallback_from_param_self.php.inc} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/{fallback_from_param2.php.inc => fallback_from_param_self.php.inc} (91%) diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param2.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param_self.php.inc similarity index 91% rename from rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param2.php.inc rename to rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param_self.php.inc index 5437489b9f7..d6f3cf0f48a 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param2.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param_self.php.inc @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture; -final class FallbackFromParam2 +final class FallbackFromParamSelf { public function action(self $obj) { @@ -24,7 +24,7 @@ declare(strict_types=1); namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture; -final class FallbackFromParam2 +final class FallbackFromParamSelf { public function action(self $obj): \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture\FallbackFromParam2 { From f93af9d65f7401fefb51acc5c10caaa71f8bdf7b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Sep 2023 06:07:11 +0700 Subject: [PATCH 05/11] rename fixture --- .../Fixture/fallback_from_param_self.php.inc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param_self.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param_self.php.inc index d6f3cf0f48a..efebf7e45a3 100644 --- a/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param_self.php.inc +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector/Fixture/fallback_from_param_self.php.inc @@ -9,7 +9,7 @@ final class FallbackFromParamSelf public function action(self $obj) { if (rand(0, 1)) { - return new FallbackFromParam2(); + return new FallbackFromParamSelf(); } return $obj; @@ -26,10 +26,10 @@ namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNe final class FallbackFromParamSelf { - public function action(self $obj): \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture\FallbackFromParam2 + public function action(self $obj): \Rector\Tests\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnNewRector\Fixture\FallbackFromParamSelf { if (rand(0, 1)) { - return new FallbackFromParam2(); + return new FallbackFromParamSelf(); } return $obj; From ed8860f2a097da662b41c30f2e53c874e9094da2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Sep 2023 06:10:12 +0700 Subject: [PATCH 06/11] fix phpstan --- .../Rector/ClassMethod/ReturnTypeFromReturnNewRector.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php index f66c11eba9d..a7f16a499e5 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php @@ -5,6 +5,7 @@ namespace Rector\TypeDeclaration\Rector\ClassMethod; use PhpParser\Node; +use PhpParser\Node\Expr; use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Expr\New_; @@ -201,6 +202,10 @@ private function resolveReturnNewType(array $returns): ?array $newTypes = []; foreach ($returns as $return) { if (! $return->expr instanceof New_) { + if (! $return->expr instanceof Expr) { + return null; + } + $returnType = $this->nodeTypeResolver->getNativeType($return->expr); if ($returnType instanceof ObjectType) { $newTypes[] = $returnType; From 11c285c35d6c84adbb16394024ee19b87626c318 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Sep 2023 06:10:32 +0700 Subject: [PATCH 07/11] fix phpstan --- .../Rector/ClassMethod/ReturnTypeFromReturnNewRector.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php index a7f16a499e5..d73b9c9d01e 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/ReturnTypeFromReturnNewRector.php @@ -201,11 +201,11 @@ private function resolveReturnNewType(array $returns): ?array { $newTypes = []; foreach ($returns as $return) { - if (! $return->expr instanceof New_) { - if (! $return->expr instanceof Expr) { - return null; - } + if (! $return->expr instanceof Expr) { + return null; + } + if (! $return->expr instanceof New_) { $returnType = $this->nodeTypeResolver->getNativeType($return->expr); if ($returnType instanceof ObjectType) { $newTypes[] = $returnType; From 402d4abf762267bb03f5ea8b7de7ef0cbe1270b7 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Sep 2023 12:46:39 +0700 Subject: [PATCH 08/11] realpath() always string after file_exists() on bin/rector.php --- bin/rector.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/bin/rector.php b/bin/rector.php index d68ff9e851d..d09d40fc444 100755 --- a/bin/rector.php +++ b/bin/rector.php @@ -106,11 +106,8 @@ public function loadIfExistsAndNotLoadedYet(string $filePath): void return; } + /** @var string $realPath always string after file_exists() check */ $realPath = realpath($filePath); - if (! is_string($realPath)) { - return; - } - $this->alreadyLoadedAutoloadFiles[] = $realPath; require_once $filePath; From 3bb67a3baf1eabb4588d642ef8ffad0feab7852b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Sep 2023 12:59:18 +0700 Subject: [PATCH 09/11] Fix phpstan --- packages/Caching/Detector/ChangedFilesDetector.php | 1 + packages/Config/RectorConfig.php | 6 ++++-- packages/Skipper/FileSystem/FnMatchPathNormalizer.php | 1 + packages/Skipper/RealpathMatcher.php | 2 ++ src/Autoloading/BootstrapFilesIncluder.php | 1 + utils/Command/MissingInSetCommand.php | 4 +++- 6 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/Caching/Detector/ChangedFilesDetector.php b/packages/Caching/Detector/ChangedFilesDetector.php index a49557d1c97..691216ffb1b 100644 --- a/packages/Caching/Detector/ChangedFilesDetector.php +++ b/packages/Caching/Detector/ChangedFilesDetector.php @@ -85,6 +85,7 @@ public function setFirstResolvedConfigFileInfo(string $filePath): void private function resolvePath(string $filePath): string { + /** @var string|false $realPath */ $realPath = realpath($filePath); if ($realPath === false) { diff --git a/packages/Config/RectorConfig.php b/packages/Config/RectorConfig.php index 1233a4ce06a..c7d2f6c66bb 100644 --- a/packages/Config/RectorConfig.php +++ b/packages/Config/RectorConfig.php @@ -373,8 +373,10 @@ private function isRuleNoLongerExists(mixed $skipRule): bool is_string($skipRule) // not regex path && ! str_contains($skipRule, '*') - // not realpath - && realpath($skipRule) === false + // not directory + && ! is_dir($skipRule) + // not file + && ! is_file($skipRule) // a Rector end && str_ends_with($skipRule, 'Rector') // class not exists diff --git a/packages/Skipper/FileSystem/FnMatchPathNormalizer.php b/packages/Skipper/FileSystem/FnMatchPathNormalizer.php index 88d78d2a314..98f1737bc5f 100644 --- a/packages/Skipper/FileSystem/FnMatchPathNormalizer.php +++ b/packages/Skipper/FileSystem/FnMatchPathNormalizer.php @@ -16,6 +16,7 @@ public function normalizeForFnmatch(string $path): string } if (\str_contains($path, '..')) { + /** @var string|false $path */ $path = realpath($path); if ($path === false) { return ''; diff --git a/packages/Skipper/RealpathMatcher.php b/packages/Skipper/RealpathMatcher.php index 476a6f59d68..5f50e12b989 100644 --- a/packages/Skipper/RealpathMatcher.php +++ b/packages/Skipper/RealpathMatcher.php @@ -8,11 +8,13 @@ final class RealpathMatcher { public function match(string $matchingPath, string $filePath): bool { + /** @var string|false $realPathMatchingPath */ $realPathMatchingPath = realpath($matchingPath); if (! is_string($realPathMatchingPath)) { return false; } + /** @var string|false $realpathFilePath */ $realpathFilePath = realpath($filePath); if (! is_string($realpathFilePath)) { return false; diff --git a/src/Autoloading/BootstrapFilesIncluder.php b/src/Autoloading/BootstrapFilesIncluder.php index c81e973bcda..b908776a191 100644 --- a/src/Autoloading/BootstrapFilesIncluder.php +++ b/src/Autoloading/BootstrapFilesIncluder.php @@ -41,6 +41,7 @@ public function includeBootstrapFiles(): void private function requireRectorStubs(): void { + /** @var false|string $stubsRectorDirectory */ $stubsRectorDirectory = realpath(__DIR__ . '/../../stubs-rector'); if ($stubsRectorDirectory === false) { return; diff --git a/utils/Command/MissingInSetCommand.php b/utils/Command/MissingInSetCommand.php index b4d9ef34cbd..14b44aa9a49 100644 --- a/utils/Command/MissingInSetCommand.php +++ b/utils/Command/MissingInSetCommand.php @@ -141,7 +141,9 @@ static function (string $rectorClass): bool { $hasError = true; $this->symfonyStyle->title('We could not find there rules in configs'); - $setRealpath = (string) realpath($setFile); + /** @var string|false $setRealpath */ + $setRealpath = realpath($setFile); + $setRealpath = (string) $setRealpath; $relativeFilePath = Strings::after($setRealpath, getcwd() . '/'); $this->symfonyStyle->writeln(' * ' . $relativeFilePath); From 8fae3786555cc27787bbfd9a1dfe6f41068376f6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Sep 2023 13:05:32 +0700 Subject: [PATCH 10/11] cheap check early --- packages/Config/RectorConfig.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/Config/RectorConfig.php b/packages/Config/RectorConfig.php index c7d2f6c66bb..f27d1be36f8 100644 --- a/packages/Config/RectorConfig.php +++ b/packages/Config/RectorConfig.php @@ -373,12 +373,12 @@ private function isRuleNoLongerExists(mixed $skipRule): bool is_string($skipRule) // not regex path && ! str_contains($skipRule, '*') + // a Rector end + && str_ends_with($skipRule, 'Rector') // not directory && ! is_dir($skipRule) // not file && ! is_file($skipRule) - // a Rector end - && str_ends_with($skipRule, 'Rector') // class not exists && ! class_exists($skipRule); } From a5c966ea304873c71d14c3ba2e6459658ad86a76 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 18 Sep 2023 13:09:34 +0700 Subject: [PATCH 11/11] Trigger CI