Skip to content

Commit

Permalink
Add error identifiers (#43)
Browse files Browse the repository at this point in the history
* Add error identifiers

closes #41

* Fix tests
  • Loading branch information
SanderMuller authored Sep 18, 2024
1 parent fcb0ea2 commit 14689aa
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor
composer.lock

.idea
.phpunit.cache
1 change: 1 addition & 0 deletions src/Rules/NarrowPrivateClassMethodParamTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ private function validateParam(Param $param, int $position, Expr $expr, Scope $s
$errorMessage = sprintf(self::ERROR_MESSAGE, $position + 1, $argTypeAsString);

return RuleErrorBuilder::message($errorMessage)
->identifier('typePerfect.narrowPrivateClassMethodParamType')
->line($param->getLine())
->build();
}
Expand Down
1 change: 1 addition & 0 deletions src/Rules/NarrowPublicClassMethodParamTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function processNode(Node $node, Scope $scope): array
}

$ruleErrors[] = RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $uniqueCollectedArgTypesString))
->identifier('typePerfect.narrowPublicClassMethodParamType')
->file($filePath)
->line($line)
->build();
Expand Down
11 changes: 9 additions & 2 deletions src/Rules/NarrowReturnObjectTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
Expand Down Expand Up @@ -47,7 +49,7 @@ public function getNodeType(): string

/**
* @param ClassMethod $node
* @return mixed[]
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand Down Expand Up @@ -94,7 +96,12 @@ public function processNode(Node $node, Scope $scope): array

/** @var TypeWithClassName $returnExprType */
$errorMessage = sprintf(self::ERROR_MESSAGE, $returnExprType->getClassName());
return [$errorMessage];

return [
RuleErrorBuilder::message($errorMessage)
->identifier('typePerfect.narrowReturnObjectType')
->build(),
];
}

private function shouldSkipReturnObjectType(ObjectType $objectType): bool
Expand Down
8 changes: 6 additions & 2 deletions src/Rules/NoArrayAccessOnObjectRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PhpParser\Node\Expr\ArrayDimFetch;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;

/**
Expand Down Expand Up @@ -36,7 +38,7 @@ public function getNodeType(): string

/**
* @param ArrayDimFetch $node
* @return string[]
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -50,7 +52,9 @@ public function processNode(Node $node, Scope $scope): array
}

return [
self::ERROR_MESSAGE,
RuleErrorBuilder::message(self::ERROR_MESSAGE)
->identifier('typePerfect.noArrayAccessOnObject')
->build(),
];
}

Expand Down
8 changes: 6 additions & 2 deletions src/Rules/NoEmptyOnObjectRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PhpParser\Node\Expr\Empty_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use Rector\TypePerfect\Guard\EmptyIssetGuard;

/**
Expand All @@ -32,7 +34,7 @@ public function getNodeType(): string

/**
* @param Empty_ $node
* @return string[]
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -41,7 +43,9 @@ public function processNode(Node $node, Scope $scope): array
}

return [
self::ERROR_MESSAGE,
RuleErrorBuilder::message(self::ERROR_MESSAGE)
->identifier('typePerfect.noEmptyOnObject')
->build(),
];
}
}
8 changes: 6 additions & 2 deletions src/Rules/NoIssetOnObjectRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PhpParser\Node\Expr\Isset_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use Rector\TypePerfect\Guard\EmptyIssetGuard;

/**
Expand All @@ -34,7 +36,7 @@ public function getNodeType(): string
/**
* @param Isset_ $node
*
* @return string[]
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -44,7 +46,9 @@ public function processNode(Node $node, Scope $scope): array
}

return [
self::ERROR_MESSAGE,
RuleErrorBuilder::message(self::ERROR_MESSAGE)
->identifier('typePerfect.noIssetOnObject')
->build(),
];
}

Expand Down
8 changes: 6 additions & 2 deletions src/Rules/NoMixedMethodCallerRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ErrorType;
use PHPStan\Type\MixedType;
use Rector\TypePerfect\Configuration;
Expand Down Expand Up @@ -40,7 +42,7 @@ public function getNodeType(): string

/**
* @param MethodCall $node
* @return mixed[]
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -66,7 +68,9 @@ public function processNode(Node $node, Scope $scope): array
$printedMethodCall = $this->printerStandard->prettyPrintExpr($node->var);

return [
sprintf(self::ERROR_MESSAGE, $printedMethodCall),
RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $printedMethodCall))
->identifier('typePerfect.noMixedMethodCaller')
->build(),
];
}

Expand Down
10 changes: 8 additions & 2 deletions src/Rules/NoMixedPropertyFetcherRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\MixedType;
use Rector\TypePerfect\Configuration;

Expand Down Expand Up @@ -39,7 +41,7 @@ public function getNodeType(): string

/**
* @param PropertyFetch $node
* @return mixed[]
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand All @@ -54,6 +56,10 @@ public function processNode(Node $node, Scope $scope): array

$printedVar = $this->standard->prettyPrintExpr($node->var);

return [sprintf(self::ERROR_MESSAGE, $printedVar)];
return [
RuleErrorBuilder::message(sprintf(self::ERROR_MESSAGE, $printedVar))
->identifier('typePerfect.noMixedPropertyFetcher')
->build(),
];
}
}
10 changes: 8 additions & 2 deletions src/Rules/NoParamTypeRemovalRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use Rector\TypePerfect\Reflection\MethodNodeAnalyser;
Expand Down Expand Up @@ -39,7 +41,7 @@ public function getNodeType(): string

/**
* @param ClassMethod $node
* @return string[]
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand Down Expand Up @@ -68,7 +70,11 @@ public function processNode(Node $node, Scope $scope): array
}

// removed param type!
return [self::ERROR_MESSAGE];
return [
RuleErrorBuilder::message(self::ERROR_MESSAGE)
->identifier('typePerfect.noParamTypeRemoval')
->build(),
];
}

return [];
Expand Down
8 changes: 6 additions & 2 deletions src/Rules/ReturnNullOverFalseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use PhpParser\NodeFinder;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
use Rector\TypePerfect\Configuration;
Expand Down Expand Up @@ -44,7 +46,7 @@ public function getNodeType(): string

/**
* @param ClassMethod $node
* @return string[]
* @return RuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
Expand Down Expand Up @@ -90,7 +92,9 @@ public function processNode(Node $node, Scope $scope): array

if (! $hasTrueType && $hasFalseType) {
return [
self::ERROR_MESSAGE,
RuleErrorBuilder::message(self::ERROR_MESSAGE)
->identifier('typePerfect.nullOverFalse')
->build(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ class SkipRecursive
/**
* @param SomeStaticCall|MethodCall $node
*/
public function processNode(Node $node)
public function processNode(Node $node): void
{
$this->run($node);
}

/**
* @param SomeStaticCall|MethodCall $node
*/
private function run(Node $node)
private function run(Node $node): void
{
if ($node->name instanceof MethodCall) {
$this->run($node->name);
Expand Down

0 comments on commit 14689aa

Please sign in to comment.