Skip to content

Conversation

zonuexe
Copy link
Contributor

@zonuexe zonuexe commented Oct 3, 2024

@zonuexe zonuexe force-pushed the fix/bug-11780 branch 2 times, most recently from a6a9831 to 3d82da8 Compare October 3, 2024 11:18
@ondrejmirtes
Copy link
Member

Better fit would be to add it to AssertRuleHelper. We need to check for:

  • Nonexistent classes and traits, like here:
    if (!$this->reflectionProvider->hasClass($class)) {
    $errors[] = RuleErrorBuilder::message(sprintf($parameterMessage, $param->var->name, $class))
    ->line($param->type->getStartLine())
    ->identifier('class.notFound')
    ->build();
    continue;
    }
    $classReflection = $this->reflectionProvider->getClass($class);
    if ($classReflection->isTrait()) {
    $errors[] = RuleErrorBuilder::message(sprintf($parameterMessage, $param->var->name, $class))
    ->line($param->type->getStartLine())
    ->identifier('parameter.trait')
    ->build();
    continue;
    }
    $errors = array_merge(
    $errors,
    $this->classCheck->checkClassNames([
    new ClassNameNodePair($class, $param->type),
    ], $this->checkClassCaseSensitivity),
    );
  • Unresolvable types, like here:
    } elseif (
    $this->unresolvableTypeHelper->containsUnresolvableType($phpDocParamType)
    ) {
    $errors[] = RuleErrorBuilder::message(sprintf(
    'PHPDoc tag %s for parameter $%s contains unresolvable type.',
    $tagName,
    $parameterName,
    ))->identifier('parameter.unresolvableType')->build();
  • Missing types, like here:
    foreach ($this->missingTypehintCheck->getIterableTypesWithMissingValueTypehint($type) as $iterableType) {
    $iterableTypeDescription = $iterableType->describe(VerbosityLevel::typeOnly());
    $errors[] = RuleErrorBuilder::message(sprintf(
    '%s %s has PHPDoc tag @mixin with no value type specified in iterable type %s.',
    $classReflection->getClassTypeDescription(),
    $classReflection->getDisplayName(),
    $iterableTypeDescription,
    ))
    ->tip(MissingTypehintCheck::MISSING_ITERABLE_VALUE_TYPE_TIP)
    ->identifier('missingType.iterableValue')
    ->build();
    }
    foreach ($this->missingTypehintCheck->getNonGenericObjectTypesWithGenericClass($type) as [$innerName, $genericTypeNames]) {
    $errors[] = RuleErrorBuilder::message(sprintf(
    'PHPDoc tag @mixin contains generic %s but does not specify its types: %s',
    $innerName,
    implode(', ', $genericTypeNames),
    ))
    ->identifier('missingType.generics')
    ->build();
    }
    foreach ($this->missingTypehintCheck->getCallablesWithMissingSignature($type) as $callableType) {
    $errors[] = RuleErrorBuilder::message(sprintf(
    '%s %s has PHPDoc tag @mixin with no signature specified for %s.',
    $classReflection->getClassTypeDescription(),
    $classReflection->getDisplayName(),
    $callableType->describe(VerbosityLevel::typeOnly()),
    ))->identifier('missingType.callable')->build();
    }
  • Generics, like here:
    $errors = array_merge($errors, $this->genericObjectTypeCheck->check(
    $type,
    'PHPDoc tag @mixin contains generic type %s but %s %s is not generic.',
    'Generic type %s in PHPDoc tag @mixin does not specify all template types of %s %s: %s',
    'Generic type %s in PHPDoc tag @mixin specifies %d template types, but %s %s supports only %d: %s',
    'Type %s in generic type %s in PHPDoc tag @mixin is not subtype of template type %s of %s %s.',
    'Call-site variance of %s in generic type %s in PHPDoc tag @mixin is in conflict with %s template type %s of %s %s.',
    'Call-site variance of %s in generic type %s in PHPDoc tag @mixin is redundant, template type %s of %s %s has the same variance.',
    ));

@zonuexe zonuexe changed the title Add assert tags support to IncompatiblePhpDocTypeRule Add error reporting for unresolvable types in @phpstan-assert tags Oct 3, 2024
@zonuexe zonuexe marked this pull request as draft October 3, 2024 12:40
@zonuexe zonuexe marked this pull request as ready for review October 3, 2024 12:41
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

continue;
}

if ($assert->getType() instanceof ErrorType) {
Copy link
Member

Choose a reason for hiding this comment

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

Call UnresolvableTypeHelper instead please. You should follow the code samples I sent you :)

Copy link
Member

Choose a reason for hiding this comment

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

Also please add all the other things that should be checked too.

@zonuexe zonuexe marked this pull request as draft October 3, 2024 15:07
@ondrejmirtes
Copy link
Member

I'm sorry, I'm in a bit of a hurry, finishing this myself.

@ondrejmirtes
Copy link
Member

Superseded by ca0a7e9

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.

3 participants