Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeDeclaration] Adds AddClosureParamTypeFromArgRector #6258

Merged
merged 13 commits into from
Aug 27, 2024

Conversation

TomasVotruba
Copy link
Member

@TomasVotruba TomasVotruba commented Aug 27, 2024

Rebase and merge of original PR #6198 by @peterfox 🙏

Closes #6198

@TomasVotruba TomasVotruba changed the title feature/argument type closure [TypeDeclaration] Adds AddClosureParamTypeFromArgRector Aug 27, 2024
Comment on lines 78 to 82
$type = match (true) {
$node instanceof MethodCall => $node->var,
$node instanceof StaticCall => $node->class,
default => null,
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this way we loose type of $node, that's why the if (! ($node->name ?? null) instanceof Identifier) { was needed.

Instead, the instanceof should be used to always improve knowledge about varaible types.

Comment on lines 143 to 152
} else {
$args = array_filter($callLike->getArgs(), static function (Arg $arg) use (
$addParamTypeForFunctionLikeWithinCallLikeArgFromArgDeclaration
): bool {
if ($arg->name === null) {
return false;
}

return $arg->name->name === $addParamTypeForFunctionLikeWithinCallLikeArgFromArgDeclaration->getCallLikePosition();
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the string named args feature, as those are skipped now anyway. I'd limit this rule to work only with standard args in future as well, as area of use case is pretty narrow.

@@ -69,11 +67,12 @@ public function getNodeTypes(): array
}

/**
* @param CallLike $node
* @param MethodCall|StaticCall $node
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always better to have exact same types as in getNodeTypes(). The CallLike is to generic and it gives the refactor() method types that are never passed.

private function processFunctionLike(
CallLike $callLike,
private function processCallLike(
MethodCall|StaticCall $callLike,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Result of well defined types in refactor() docblock 👍

Comment on lines 173 to 175
$argType = $this->nodeTypeResolver->getType($arg->value);
if (! $argType instanceof ConstantStringType) {
return;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPStan can help us with class name detection 👍

Comment on lines 21 to 19
SomeClass::someCall(SomeClass::class, function (\SomeNamespace\SomeClass $object) {
SimpleContainer::someCall(\Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgClassStringRector\Source\SomeType::class, function (\Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgClassStringRector\Source\SomeType $object) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By defalut, non-existing class are seen as strings. By both PHPStan and Rector.

That's why it's better to use existing class in tests, so we know it's an object and not a dummy string.

/**
* @see \Rector\Tests\TypeDeclaration\Rector\FunctionLike\AddClosureParamTypeFromArgRector\AddClosureParamTypeFromArgRectorTest
*/
final class AddClosureParamTypeFromArgClassStringRector extends AbstractRector implements ConfigurableRectorInterface
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can separate string and object type now, so both rule now do the same. I've merged them together.

@TomasVotruba TomasVotruba force-pushed the feature/argument-type-closure branch from e35731d to 3b5c524 Compare August 27, 2024 10:21
@TomasVotruba TomasVotruba force-pushed the feature/argument-type-closure branch from 3b5c524 to a934e09 Compare August 27, 2024 10:22
@TomasVotruba TomasVotruba merged commit dc3850f into main Aug 27, 2024
36 checks passed
@TomasVotruba TomasVotruba deleted the feature/argument-type-closure branch August 27, 2024 10:27
Comment on lines +51 to +57
// stmts aware type
RenameParamToMatchTypeRector::class => [
__DIR__ . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php',
],
AddMethodCallBasedStrictParamTypeRector::class => [
__DIR__ . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php',
],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed, I tested latest main branch without this skip and it is working ok, I will create PR to remove it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see #6262, tested with rm -rf vendor && composer update && bin/rector, nothing change on rules directory

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had some local glitches with PHPStan cache.
Patches get stuck sometimes:)

Thanks for restoring 👌

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may need to force remove it

php -r 'shell_exec("rm -rf " . sys_get_temp_dir() . "/rector_cached_files");';
php -r 'shell_exec("rm -rf " . sys_get_temp_dir() . "/cache/PHPStan");';
php -r 'shell_exec("rm -rf " . sys_get_temp_dir() . "/cache/nette.configurator");';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants