Skip to content

Commit

Permalink
Modernize rules with RuleErrorBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 28, 2023
1 parent d8bdab0 commit d96b5a4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\NodeAbstract;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function count;

/**
Expand Down Expand Up @@ -40,13 +41,13 @@ public function processNode(Node $node, Scope $scope): array

if ($expectedArgumentValue->name->toLowerString() === 'true') {
return [
'You should use assertTrue() instead of assertSame() when expecting "true"',
RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')->build(),
];
}

if ($expectedArgumentValue->name->toLowerString() === 'false') {
return [
'You should use assertFalse() instead of assertSame() when expecting "false"',
RuleErrorBuilder::message('You should use assertFalse() instead of assertSame() when expecting "false"')->build(),
];
}

Expand Down
3 changes: 2 additions & 1 deletion src/Rules/PHPUnit/AssertSameNullExpectedRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\NodeAbstract;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function count;

/**
Expand Down Expand Up @@ -40,7 +41,7 @@ public function processNode(Node $node, Scope $scope): array

if ($expectedArgumentValue->name->toLowerString() === 'null') {
return [
'You should use assertNull() instead of assertSame(null, $actual).',
RuleErrorBuilder::message('You should use assertNull() instead of assertSame(null, $actual).')->build(),
];
}

Expand Down
5 changes: 3 additions & 2 deletions src/Rules/PHPUnit/AssertSameWithCountRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\NodeAbstract;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\ObjectType;
use function count;

Expand Down Expand Up @@ -42,7 +43,7 @@ public function processNode(Node $node, Scope $scope): array
&& $right->name->toLowerString() === 'count'
) {
return [
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).')->build(),
];
}

Expand All @@ -56,7 +57,7 @@ public function processNode(Node $node, Scope $scope): array

if ((new ObjectType(Countable::class))->isSuperTypeOf($type)->yes()) {
return [
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).',
RuleErrorBuilder::message('You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, $variable->count()).')->build(),
];
}
}
Expand Down
1 change: 0 additions & 1 deletion src/Rules/PHPUnit/ClassMethodCoversExistsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$errors = [];
$classPhpDoc = $classReflection->getResolvedPhpDoc();
[$classCovers, $classCoversDefaultClasses] = $this->coversHelper->getCoverAnnotations($classPhpDoc);

Expand Down
12 changes: 5 additions & 7 deletions src/Rules/PHPUnit/MockMethodCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPUnit\Framework\MockObject\Builder\InvocationMocker;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\MockObject\Stub;
Expand All @@ -28,9 +29,6 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
/** @var Node\Expr\MethodCall $node */
$node = $node;

if (!$node->name instanceof Node\Identifier || $node->name->name !== 'method') {
return [];
}
Expand Down Expand Up @@ -63,11 +61,11 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$errors[] = sprintf(
$errors[] = RuleErrorBuilder::message(sprintf(
'Trying to mock an undefined method %s() on class %s.',
$method,
implode('&', $mockClasses)
);
))->build();
continue;
}

Expand All @@ -81,11 +79,11 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

$errors[] = sprintf(
$errors[] = RuleErrorBuilder::message(sprintf(
'Trying to mock an undefined method %s() on class %s.',
$method,
implode('|', $classNames)
);
))->build();
}

return $errors;
Expand Down

0 comments on commit d96b5a4

Please sign in to comment.