From a6ded82254d3160ed68b9d700a219e962966a8b8 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Mon, 5 Dec 2022 22:33:04 +0100 Subject: [PATCH 1/2] TASK: Use custom param type inferer --- .../InitializeArgumentsClassMethodFactory.php | 6 +- .../FunctionLikeDocParamTypeInferer.php | 75 +++++++++++++++++++ 2 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 src/TypeInferer/ParamTypeInferer/FunctionLikeDocParamTypeInferer.php diff --git a/src/NodeFactory/InitializeArgumentsClassMethodFactory.php b/src/NodeFactory/InitializeArgumentsClassMethodFactory.php index bd4ce840d..52dfe9561 100644 --- a/src/NodeFactory/InitializeArgumentsClassMethodFactory.php +++ b/src/NodeFactory/InitializeArgumentsClassMethodFactory.php @@ -41,7 +41,7 @@ use Rector\PHPStanStaticTypeMapper\Enum\TypeKind; use Rector\StaticTypeMapper\StaticTypeMapper; use Rector\StaticTypeMapper\ValueObject\Type\ShortenedObjectType; -use Rector\TypeDeclaration\TypeInferer\ParamTypeInferer; +use Ssch\TYPO3Rector\TypeInferer\ParamTypeInferer\FunctionLikeDocParamTypeInferer; final class InitializeArgumentsClassMethodFactory { @@ -73,7 +73,7 @@ final class InitializeArgumentsClassMethodFactory /** * @readonly */ - private ParamTypeInferer $paramTypeInferer; + private FunctionLikeDocParamTypeInferer $paramTypeInferer; /** * @readonly @@ -99,7 +99,7 @@ public function __construct( NodeFactory $nodeFactory, NodeNameResolver $nodeNameResolver, StaticTypeMapper $staticTypeMapper, - ParamTypeInferer $paramTypeInferer, + FunctionLikeDocParamTypeInferer $paramTypeInferer, PhpDocInfoFactory $phpDocInfoFactory, ReflectionProvider $reflectionProvider, ValueResolver $valueResolver, diff --git a/src/TypeInferer/ParamTypeInferer/FunctionLikeDocParamTypeInferer.php b/src/TypeInferer/ParamTypeInferer/FunctionLikeDocParamTypeInferer.php new file mode 100644 index 000000000..ba897d130 --- /dev/null +++ b/src/TypeInferer/ParamTypeInferer/FunctionLikeDocParamTypeInferer.php @@ -0,0 +1,75 @@ +nodeNameResolver = $nodeNameResolver; + $this->phpDocInfoFactory = $phpDocInfoFactory; + $this->betterNodeFinder = $betterNodeFinder; + } + + public function inferParam(Param $param): Type + { + $functionLike = $this->resolveScopeNode($param); + if (! $functionLike instanceof FunctionLike) { + return new MixedType(); + } + $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike); + $paramTypesByName = $phpDocInfo->getParamTypesByName(); + if ([] === $paramTypesByName) { + return new MixedType(); + } + return $this->matchParamNodeFromDoc($paramTypesByName, $param); + } + + /** + * @return ClassMethod|Function_|null + */ + private function resolveScopeNode(Param $param): ?Node + { + return $this->betterNodeFinder->findParentByTypes($param, [ClassMethod::class, Function_::class]); + } + + /** + * @param Type[] $paramWithTypes + */ + private function matchParamNodeFromDoc(array $paramWithTypes, Param $param): Type + { + $paramNodeName = '$' . $this->nodeNameResolver->getName($param->var); + return $paramWithTypes[$paramNodeName] ?? new MixedType(); + } +} From 6ace92e63145b4c21fbc50427a7117bc57bb6b65 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Mon, 5 Dec 2022 22:37:33 +0100 Subject: [PATCH 2/2] TASK: Update to Rector 0.15.1 --- composer.json | 4 ++-- rector.php | 1 - .../Fixture/fixture.php.inc | 6 ------ .../Fixture/not_working_unit_test_fixture.php.inc | 10 ++-------- .../Nimut/TestingFramework/config/configured_rule.php | 2 +- .../ContentObjectRendererFileResourceRectorTest.php | 1 - .../config/configured_rule.php | 2 +- 7 files changed, 6 insertions(+), 20 deletions(-) diff --git a/composer.json b/composer.json index 13b644189..77da157a8 100644 --- a/composer.json +++ b/composer.json @@ -27,8 +27,8 @@ "helmich/typo3-typoscript-parser": "^2.4.1", "nette/utils": "^3.0", "nikic/php-parser": "^4.14", - "phpstan/phpstan": "1.9.2", - "rector/rector": "0.14.8", + "phpstan/phpstan": "^1.7.12", + "rector/rector": "0.15.1", "symfony/console": "^4.0 || ^5.0 || ^6.0", "symfony/polyfill-php80": "^1.26", "symfony/polyfill-php81": "^1.26", diff --git a/rector.php b/rector.php index 622da2cac..684a72c76 100644 --- a/rector.php +++ b/rector.php @@ -52,6 +52,5 @@ '*/Fixture/*', ]); - $rectorConfig->rule(TypedPropertyRector::class); $rectorConfig->rule(ClassPropertyAssignToConstructorPromotionRector::class); }; diff --git a/tests/Rector/Extensions/solr/v9/ApacheSolrDocumentToSolariumDocumentRector/Fixture/fixture.php.inc b/tests/Rector/Extensions/solr/v9/ApacheSolrDocumentToSolariumDocumentRector/Fixture/fixture.php.inc index 7c60910bc..91867fddc 100644 --- a/tests/Rector/Extensions/solr/v9/ApacheSolrDocumentToSolariumDocumentRector/Fixture/fixture.php.inc +++ b/tests/Rector/Extensions/solr/v9/ApacheSolrDocumentToSolariumDocumentRector/Fixture/fixture.php.inc @@ -16,9 +16,6 @@ class MySolrIndexer $document->setMultiValue('key', 'foo', true); } - /** - * @param Apache_Solr_Document[] $documents - */ public function multiple(array $documents) { /** @var Apache_Solr_Document $document */ @@ -48,9 +45,6 @@ class MySolrIndexer $document->addField('key', 'foo', true); } - /** - * @param Document[] $documents - */ public function multiple(array $documents) { /** @var Apache_Solr_Document $document */ diff --git a/tests/Rector/Nimut/TestingFramework/Fixture/not_working_unit_test_fixture.php.inc b/tests/Rector/Nimut/TestingFramework/Fixture/not_working_unit_test_fixture.php.inc index cad9be0da..8622cdafe 100644 --- a/tests/Rector/Nimut/TestingFramework/Fixture/not_working_unit_test_fixture.php.inc +++ b/tests/Rector/Nimut/TestingFramework/Fixture/not_working_unit_test_fixture.php.inc @@ -8,10 +8,7 @@ use Nimut\TestingFramework\TestCase\UnitTestCase; class MyUnitTest extends UnitTestCase { - /** - * @var AccessibleMockObjectInterface - */ - protected $fooBarBaz; + protected AccessibleMockObjectInterface $fooBarBaz; public function testFooBar() { @@ -35,10 +32,7 @@ use TYPO3\TestingFramework\Core\Exception; class MyUnitTest extends UnitTestCase { - /** - * @var AccessibleObjectInterface - */ - protected $fooBarBaz; + protected AccessibleObjectInterface $fooBarBaz; public function testFooBar() { diff --git a/tests/Rector/Nimut/TestingFramework/config/configured_rule.php b/tests/Rector/Nimut/TestingFramework/config/configured_rule.php index 4b4cb5a15..94a7a31f9 100644 --- a/tests/Rector/Nimut/TestingFramework/config/configured_rule.php +++ b/tests/Rector/Nimut/TestingFramework/config/configured_rule.php @@ -6,6 +6,6 @@ use Ssch\TYPO3Rector\Set\Extension\NimutTestingFrameworkSetList; return static function (RectorConfig $rectorConfig): void { - $rectorConfig->disableImportNames(); + $rectorConfig->importNames(false); $rectorConfig->sets([NimutTestingFrameworkSetList::NIMUT_TESTING_FRAMEWORK_TO_TYPO3_TESTING_FRAMEWORK]); }; diff --git a/tests/Rector/v8/v5/ContentObjectRendererFileResourceRector/ContentObjectRendererFileResourceRectorTest.php b/tests/Rector/v8/v5/ContentObjectRendererFileResourceRector/ContentObjectRendererFileResourceRectorTest.php index 00e372532..1d48d930a 100644 --- a/tests/Rector/v8/v5/ContentObjectRendererFileResourceRector/ContentObjectRendererFileResourceRectorTest.php +++ b/tests/Rector/v8/v5/ContentObjectRendererFileResourceRector/ContentObjectRendererFileResourceRectorTest.php @@ -16,7 +16,6 @@ final class ContentObjectRendererFileResourceRectorTest extends AbstractRectorTe public function test(string $filePath): void { $this->markTestIncomplete('The comparison is false positive wrongly.'); - #$this->doTestFile($filePath); } /** diff --git a/tests/Rector/v9/v5/RefactorTypeInternalTypeFileAndFileReferenceToFalRector/config/configured_rule.php b/tests/Rector/v9/v5/RefactorTypeInternalTypeFileAndFileReferenceToFalRector/config/configured_rule.php index a5b81c12a..c3dc89108 100644 --- a/tests/Rector/v9/v5/RefactorTypeInternalTypeFileAndFileReferenceToFalRector/config/configured_rule.php +++ b/tests/Rector/v9/v5/RefactorTypeInternalTypeFileAndFileReferenceToFalRector/config/configured_rule.php @@ -7,6 +7,6 @@ return static function (RectorConfig $rectorConfig): void { $rectorConfig->import(__DIR__ . '/../../../../../../config/config_test.php'); - $rectorConfig->disableImportNames(); + $rectorConfig->importNames(false); $rectorConfig->rule(RefactorTypeInternalTypeFileAndFileReferenceToFalRector::class); };