Skip to content

Commit

Permalink
Merge pull request #17 from swissspidy/fix/use-rule-error-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
swissspidy committed Jun 5, 2024
2 parents 3608dca + 0909bdf commit 86d67c9
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 32 deletions.
12 changes: 8 additions & 4 deletions src/Rules/NoPrivate/CallToPrivateFunctionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Broker\FunctionNotFoundException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\FileTypeMapper;
use function sprintf;

Expand Down Expand Up @@ -66,10 +67,13 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

return [sprintf(
'Call to private/internal function %s().',
$function->getName()
)];
return [
RuleErrorBuilder::message(
sprintf('Call to private/internal function %s().', $function->getName())
)
->identifier('no.private.function')
->build()
];
}

}
17 changes: 12 additions & 5 deletions src/Rules/NoPrivate/CallToPrivateMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Reflection\MissingMethodFromReflectionException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\FileTypeMapper;
use function sprintf;

Expand Down Expand Up @@ -81,11 +82,17 @@ public function processNode(Node $node, Scope $scope): array
continue;
}

return [sprintf(
'Call to private/internal method %s() of class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
)];
return [
RuleErrorBuilder::message(
sprintf(
'Call to private/internal method %s() of class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
)
)
->identifier('no.private.method')
->build()
];
} catch (ClassNotFoundException $e) {
// Other rules will notify if the class is not found
} catch (MissingMethodFromReflectionException $e) {
Expand Down
29 changes: 19 additions & 10 deletions src/Rules/NoPrivate/CallToPrivateStaticMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Reflection\MissingMethodFromReflectionException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use PHPStan\Type\FileTypeMapper;
Expand Down Expand Up @@ -90,11 +91,15 @@ static function (Type $type) use ($methodName): bool {

$resolvedPhpDoc = $class->getResolvedPhpDoc();
if ($resolvedPhpDoc && PrivateAnnotationHelper::isPrivate($resolvedPhpDoc)) {
$errors[] = sprintf(
'Call to method %s() of private/internal class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
);
$errors[] = RuleErrorBuilder::message(
sprintf(
'Call to method %s() of private/internal class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
)
)
->identifier('no.private.method')
->build();
continue;
}

Expand Down Expand Up @@ -124,11 +129,15 @@ static function (Type $type) use ($methodName): bool {
continue;
}

$errors[] = sprintf(
'Call to private/internal method %s() of class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
);
$errors[] = RuleErrorBuilder::message(
sprintf(
'Call to private/internal method %s() of class %s.',
$methodReflection->getName(),
$methodReflection->getDeclaringClass()->getName()
)
)
->identifier('no.private.method')
->build();
}

return $errors;
Expand Down
27 changes: 18 additions & 9 deletions src/Rules/NoPrivate/InheritanceOfPrivateClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Broker\ClassNotFoundException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;

/**
Expand Down Expand Up @@ -57,16 +58,24 @@ public function processNode(Node $node, Scope $scope): array
}

if (!$class->isAnonymous()) {
$errors[] = sprintf(
'Class %s extends private/internal class %s.',
$className,
$parentClassName
);
$errors[] = RuleErrorBuilder::message(
sprintf(
'Class %s extends private/internal class %s.',
$className,
$parentClassName
)
)
->identifier('no.private.class')
->build();
} else {
$errors[] = sprintf(
'Anonymous class extends private/internal class %s.',
$parentClassName
);
$errors[] = RuleErrorBuilder::message(
sprintf(
'Anonymous class extends private/internal class %s.',
$parentClassName
)
)
->identifier('no.private.class')
->build();
}
} catch (ClassNotFoundException $e) {
// Other rules will notify if the interface is not found
Expand Down
13 changes: 9 additions & 4 deletions src/Rules/NoPrivate/InstantiationOfPrivateClassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Broker\ClassNotFoundException;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Type\ErrorType;
use function sprintf;
Expand Down Expand Up @@ -80,10 +81,14 @@ static function (): bool {
continue;
}

$errors[] = sprintf(
'Instantiation of private/internal class %s.',
$referencedClass
);
$errors[] = RuleErrorBuilder::message(
sprintf(
'Instantiation of private/internal class %s.',
$referencedClass
)
)
->identifier('no.private.class')
->build();
}

return $errors;
Expand Down

0 comments on commit 86d67c9

Please sign in to comment.