Skip to content

Commit

Permalink
Show custom tip for unfound Drupal symbols
Browse files Browse the repository at this point in the history
- add optional $className argument to discoveringSymbolsTip() method
- if that name starts with 'Drupal/' show link to phpstan-drupal project
- pass the symbol name to all 22 calls to discoveringSymbolsTip()
  • Loading branch information
bkline committed Sep 4, 2023
1 parent 7711bdf commit f951720
Show file tree
Hide file tree
Showing 20 changed files with 27 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Rules/Classes/ClassConstantRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function processNode(Node $node, Scope $scope): array
return [
RuleErrorBuilder::message(sprintf('Class %s not found.', $className))
->identifier('class.notFound')
->discoveringSymbolsTip()
->discoveringSymbolsTip($className)
->build(),
];
}
Expand All @@ -106,7 +106,7 @@ public function processNode(Node $node, Scope $scope): array
sprintf('Access to constant %s on an unknown class %s.', $constantName, $className),
)
->identifier('class.notFound')
->discoveringSymbolsTip()
->discoveringSymbolsTip($className)
->build(),
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Classes/ExistingClassInClassExtendsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function processNode(Node $node, Scope $scope): array
))
->identifier('class.notFound')
->nonIgnorable()
->discoveringSymbolsTip()
->discoveringSymbolsTip($extendedClassName)
->build();
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Classes/ExistingClassInInstanceOfRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function processNode(Node $node, Scope $scope): array
RuleErrorBuilder::message(sprintf('Class %s not found.', $name))
->identifier('class.notFound')
->line($class->getLine())
->discoveringSymbolsTip()
->discoveringSymbolsTip($name)
->build(),
];
} elseif ($this->checkClassCaseSensitivity) {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Classes/ExistingClassInTraitUseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function processNode(Node $node, Scope $scope): array
$messages[] = RuleErrorBuilder::message(sprintf('%s uses unknown trait %s.', $currentName, $traitName))
->identifier('trait.notFound')
->nonIgnorable()
->discoveringSymbolsTip()
->discoveringSymbolsTip($traitName)
->build();
} else {
$reflection = $this->reflectionProvider->getClass($traitName);
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Classes/ExistingClassesInClassImplementsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function processNode(Node $node, Scope $scope): array
))
->identifier('interface.notFound')
->nonIgnorable()
->discoveringSymbolsTip()
->discoveringSymbolsTip($implementedClassName)
->build();
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Classes/ExistingClassesInEnumImplementsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function processNode(Node $node, Scope $scope): array
))
->identifier('interface.notFound')
->nonIgnorable()
->discoveringSymbolsTip()
->discoveringSymbolsTip($implementedClassName)
->build();
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function processNode(Node $node, Scope $scope): array
))
->identifier('interface.notFound')
->nonIgnorable()
->discoveringSymbolsTip()
->discoveringSymbolsTip($extendedInterfaceName)
->build();
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Classes/InstantiationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private function checkClassName(string $class, bool $isName, Node $node, Scope $
return [
RuleErrorBuilder::message(sprintf('Instantiated class %s not found.', $class))
->identifier('class.notFound')
->discoveringSymbolsTip()
->discoveringSymbolsTip($class)
->build(),
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Classes/MixinRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function processNode(Node $node, Scope $scope): array
if (!$this->reflectionProvider->hasClass($class)) {
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @mixin contains unknown class %s.', $class))
->identifier('class.notFound')
->discoveringSymbolsTip()
->discoveringSymbolsTip($class)
->build();
} elseif ($this->reflectionProvider->getClass($class)->isTrait()) {
$errors[] = RuleErrorBuilder::message(sprintf('PHPDoc tag @mixin contains invalid type %s.', $class))
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Constants/ConstantRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function processNode(Node $node, Scope $scope): array
(string) $node->name,
))
->identifier('constant.notFound')
->discoveringSymbolsTip()
->discoveringSymbolsTip((string) $node->name)
->build(),
];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Exceptions/CaughtExceptionExistenceRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function processNode(Node $node, Scope $scope): array
$errors[] = RuleErrorBuilder::message(sprintf('Caught class %s not found.', $className))
->line($class->getLine())
->identifier('class.notFound')
->discoveringSymbolsTip()
->discoveringSymbolsTip($className)
->build();
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Functions/CallToNonExistentFunctionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array
}

return [
RuleErrorBuilder::message(sprintf('Function %s not found.', (string) $node->name))->identifier('function.notFound')->discoveringSymbolsTip()->build(),
RuleErrorBuilder::message(sprintf('Function %s not found.', (string) $node->name))->identifier('function.notFound')->discoveringSymbolsTip((string) $node->name)->build(),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Methods/StaticMethodCallCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function check(
'Call to static method %s() on an unknown class %s.',
$methodName,
$className,
))->identifier('class.notFound')->discoveringSymbolsTip()->build(),
))->identifier('class.notFound')->discoveringSymbolsTip($className)->build(),
],
null,
];
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Namespaces/ExistingNamesInGroupUseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private function checkConstant(Node\Name $name): ?IdentifierRuleError
{
if (!$this->reflectionProvider->hasConstant($name, null)) {
return RuleErrorBuilder::message(sprintf('Used constant %s not found.', (string) $name))
->discoveringSymbolsTip()
->discoveringSymbolsTip((string) $name)
->line($name->getLine())
->identifier('constant.notFound')
->build();
Expand All @@ -86,7 +86,7 @@ private function checkFunction(Node\Name $name): ?IdentifierRuleError
{
if (!$this->reflectionProvider->hasFunction($name, null)) {
return RuleErrorBuilder::message(sprintf('Used function %s not found.', (string) $name))
->discoveringSymbolsTip()
->discoveringSymbolsTip((string) $name)
->line($name->getLine())
->identifier('function.notFound')
->build();
Expand Down
4 changes: 2 additions & 2 deletions src/Rules/Namespaces/ExistingNamesInUseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private function checkConstants(array $uses): array
$errors[] = RuleErrorBuilder::message(sprintf('Used constant %s not found.', (string) $use->name))
->line($use->name->getLine())
->identifier('constant.notFound')
->discoveringSymbolsTip()
->discoveringSymbolsTip((string) $use->name)
->build();
}

Expand All @@ -91,7 +91,7 @@ private function checkFunctions(array $uses): array
$errors[] = RuleErrorBuilder::message(sprintf('Used function %s not found.', (string) $use->name))
->line($use->name->getLine())
->identifier('function.notFound')
->discoveringSymbolsTip()
->discoveringSymbolsTip((string) $use->name)
->build();
} elseif ($this->checkFunctionNameCase) {
$functionReflection = $this->reflectionProvider->getFunction($use->name, null);
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/PhpDoc/InvalidPhpDocVarTagTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function processNode(Node $node, Scope $scope): array
$referencedClass,
))
->identifier('class.notFound')
->discoveringSymbolsTip()
->discoveringSymbolsTip($referencedClass)
->build();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Properties/AccessStaticPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private function processSingleProperty(Scope $scope, StaticPropertyFetch $node,
$name,
$class,
))
->discoveringSymbolsTip()
->discoveringSymbolsTip($class)
->identifier('class.notFound')
->build(),
];
Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Properties/ExistingClassesInPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function processNode(Node $node, Scope $scope): array
$propertyReflection->getDeclaringClass()->getDisplayName(),
$node->getName(),
$referencedClass,
))->identifier('class.notFound')->discoveringSymbolsTip()->build();
))->identifier('class.notFound')->discoveringSymbolsTip($referencedClass)->build();
}

if ($this->checkClassCaseSensitivity) {
Expand Down
6 changes: 5 additions & 1 deletion src/Rules/RuleErrorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,15 @@ public function addTip(string $tip): self
}

/**
* @param ?string $className
* @phpstan-this-out self<T&TipRuleError>
* @return self<T&TipRuleError>
*/
public function discoveringSymbolsTip(): self
public function discoveringSymbolsTip(?string $className = null): self
{
if (!empty($className) && substr($className, 0, 7) === 'Drupal/') {

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 188 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Construct empty() is not allowed. Use more strict comparison.
return $this->tip('Learn more at https://github.com/mglaman/phpstan-drupal');
}
return $this->tip('Learn more at https://phpstan.org/user-guide/discovering-symbols');

Check failure on line 191 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 191 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, windows-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 191 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Construct empty() is not allowed. Use more strict comparison.

Check failure on line 191 in src/Rules/RuleErrorBuilder.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, windows-latest)

Construct empty() is not allowed. Use more strict comparison.
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/RuleLevelHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public function findTypeToCheck(
$errors[] = RuleErrorBuilder::message(sprintf($unknownClassErrorPattern, $referencedClass))
->line($var->getLine())
->identifier('class.notFound')
->discoveringSymbolsTip()
->discoveringSymbolsTip($referencedClass)
->build();
}

Expand Down

0 comments on commit f951720

Please sign in to comment.