-
-
Notifications
You must be signed in to change notification settings - Fork 687
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
is_a() usage in the codebase #5906
Comments
@ondrejmirtes should |
|
For example this piece of code: /**
* @see https://doc.nette.org/en/3.0/components#toc-components-with-dependencies
*/
public function isComponentFactoryInterface(Interface_ $interface): bool
{
foreach ($interface->getMethods() as $classMethod) {
$returnType = $this->returnTypeInferer->inferFunctionLike($classMethod);
if (! $returnType instanceof TypeWithClassName) {
return false;
}
$className = $this->nodeTypeResolver->getFullyQualifiedClassName($returnType);
if (is_a($className, 'Nette\Application\UI\Control', true)) {
return true;
}
if (is_a($className, 'Nette\Application\UI\Form', true)) {
return true;
}
}
return false;
} Should be refactored like that: /**
* @see https://doc.nette.org/en/3.0/components#toc-components-with-dependencies
*/
public function isComponentFactoryInterface(Interface_ $interface): bool
{
foreach ($interface->getMethods() as $classMethod) {
$returnType = $this->returnTypeInferer->inferFunctionLike($classMethod);
$controlOrForm = new UnionType([
new ObjectType('Nette\Application\UI\Control'),
new ObjectType('Nette\Application\UI\Form'),
]);
return $controlOrForm->isSuperTypeOf($returnType)->yes();
}
return false;
} |
Instead of |
Instead of: foreach ($parametersAcceptor->getParameters() as $position => $parameterReflection) {
$parameterType = $parameterReflection->getType();
if (! $parameterType instanceof TypeWithClassName) {
continue;
}
if (! is_a($parameterType->getClassName(), Throwable::class, true)) {
continue;
}
return $position;
} You should do: foreach ($parametersAcceptor->getParameters() as $position => $parameterReflection) {
$parameterType = $parameterReflection->getType();
if (!(new ObjectType(\Throwable::class))->isSuperTypeOf($parameterType)->yes()) {
continue;
}
return $position;
} |
@ondrejmirtes Thank you 👍 |
@ondrejmirtes Thanks for real examples. I though the I tried to remove all those func calls in #5665, but missed a few. We should have a PHPStan rule for that, so we can avoid missing it in the code-reviews. The place when we found out in bug reports is too late. |
You should also be careful with more runtime concepts like class_exists and also use ReflectionProvider for that. The best way to be sure that Rector fully works with static reflection would probably be to figure out how to run the whole test suite a second time, but with a disabled autoloader so that the static reflection source locators will need to pick up all the sources. |
Re-opening to keep track of this, untill PHPStan rule checks are added and we're sure there are no leftovers. |
rectorphp/rector-src@0621d67 [Php80] Skip mixed doc with description and valid doc on MixedTypeRector (#5906)
Hi, I see many instances of
is_a()
usages in the codebase. It's not compatible with static reflection as it needs the classes loaded in runtime. It's fine when you ask about stuff that's already loaded (like PhpParser nodes, PHPStan classes, phpdoc-parser classes), but it shouldn't be used for analysed code.The text was updated successfully, but these errors were encountered: