Skip to content

Commit

Permalink
Contextual help about PHP-Parser 4.13.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 31, 2021
1 parent 038c7a5 commit c836a42
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
11 changes: 10 additions & 1 deletion src/Rules/FoundTypeResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class FoundTypeResult
/** @var RuleError[] */
private array $unknownClassErrors;

private ?string $tip;

/**
* @param \PHPStan\Type\Type $type
* @param string[] $referencedClasses
Expand All @@ -24,12 +26,14 @@ class FoundTypeResult
public function __construct(
Type $type,
array $referencedClasses,
array $unknownClassErrors
array $unknownClassErrors,
?string $tip
)
{
$this->type = $type;
$this->referencedClasses = $referencedClasses;
$this->unknownClassErrors = $unknownClassErrors;
$this->tip = $tip;
}

public function getType(): Type
Expand All @@ -53,4 +57,9 @@ public function getUnknownClassErrors(): array
return $this->unknownClassErrors;
}

public function getTip(): ?string
{
return $this->tip;
}

}
15 changes: 10 additions & 5 deletions src/Rules/Properties/AccessPropertiesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,17 @@ static function (Type $type) use ($name): bool {
}
}

$ruleErrorBuilder = RuleErrorBuilder::message(sprintf(
'Access to an undefined property %s::$%s.',
$type->describe(VerbosityLevel::typeOnly()),
$name
));
if ($typeResult->getTip() !== null) {
$ruleErrorBuilder->tip($typeResult->getTip());
}

return [
RuleErrorBuilder::message(sprintf(
'Access to an undefined property %s::$%s.',
$type->describe(VerbosityLevel::typeOnly()),
$name
))->build(),
$ruleErrorBuilder->build(),
];
}

Expand Down
20 changes: 13 additions & 7 deletions src/Rules/RuleLevelHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Type\TypeTraverser;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;

class RuleLevelHelper
{
Expand Down Expand Up @@ -135,7 +136,7 @@ public function findTypeToCheck(
): FoundTypeResult
{
if ($this->checkThisOnly && !$this->isThis($var)) {
return new FoundTypeResult(new ErrorType(), [], []);
return new FoundTypeResult(new ErrorType(), [], [], null);
}
$type = $scope->getType($var);
if (!$this->checkNullables && !$type instanceof NullType) {
Expand All @@ -148,11 +149,11 @@ public function findTypeToCheck(
&& !$type instanceof TemplateMixedType
&& $type->isExplicitMixed()
) {
return new FoundTypeResult(new StrictMixedType(), [], []);
return new FoundTypeResult(new StrictMixedType(), [], [], null);
}

if ($type instanceof MixedType || $type instanceof NeverType) {
return new FoundTypeResult(new ErrorType(), [], []);
return new FoundTypeResult(new ErrorType(), [], [], null);
}
if ($type instanceof StaticType) {
$type = $type->getStaticObjectType();
Expand All @@ -178,12 +179,12 @@ public function findTypeToCheck(
}

if (count($errors) > 0 || $hasClassExistsClass) {
return new FoundTypeResult(new ErrorType(), [], $errors);
return new FoundTypeResult(new ErrorType(), [], $errors, null);
}

if (!$this->checkUnionTypes) {
if ($type instanceof ObjectWithoutClassType) {
return new FoundTypeResult(new ErrorType(), [], []);
return new FoundTypeResult(new ErrorType(), [], [], null);
}
if ($type instanceof UnionType) {
$newTypes = [];
Expand All @@ -196,12 +197,17 @@ public function findTypeToCheck(
}

if (count($newTypes) > 0) {
return new FoundTypeResult(TypeCombinator::union(...$newTypes), $directClassNames, []);
return new FoundTypeResult(TypeCombinator::union(...$newTypes), $directClassNames, [], null);
}
}
}

return new FoundTypeResult($type, $directClassNames, []);
$tip = null;
if (strpos($type->describe(VerbosityLevel::typeOnly()), 'PhpParser\\Node\\Arg|PhpParser\\Node\\VariadicPlaceholder') !== false && !$unionTypeCriteriaCallback($type)) {
$tip = 'Use <fg=cyan>->getArgs()</> instead of <fg=cyan>->args</>.';
}

return new FoundTypeResult($type, $directClassNames, [], $tip);
}

}

0 comments on commit c836a42

Please sign in to comment.