From b8b2faf7e7b4b5f4fa83169708df68ca2e950bfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20=C5=A0pa=C4=8Dek?= Date: Sun, 26 Nov 2023 17:54:54 +0100 Subject: [PATCH] Always end the message with a full stop, unless it ends with ? or ! already --- src/Calls/ShellExecCalls.php | 2 +- src/Formatter/Formatter.php | 12 ++++++++++++ src/RuleErrors/DisallowedAttributeRuleErrors.php | 11 ++++++++--- src/RuleErrors/DisallowedCallsRuleErrors.php | 11 ++++++++--- src/RuleErrors/DisallowedConstantRuleErrors.php | 11 ++++++++--- src/RuleErrors/DisallowedNamespaceRuleErrors.php | 11 ++++++++--- src/RuleErrors/DisallowedVariableRuleErrors.php | 11 ++++++++--- src/Usages/ClassConstantUsages.php | 2 +- 8 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/Calls/ShellExecCalls.php b/src/Calls/ShellExecCalls.php index dd83f53..fa498bc 100644 --- a/src/Calls/ShellExecCalls.php +++ b/src/Calls/ShellExecCalls.php @@ -69,7 +69,7 @@ public function processNode(Node $node, Scope $scope): array null, null, $this->disallowedCalls, - 'Using the backtick operator (`...`) is forbidden because shell_exec() is forbidden, %2$s%3$s' + 'Using the backtick operator (`...`) is forbidden because shell_exec() is forbidden%2$s%3$s' ); } diff --git a/src/Formatter/Formatter.php b/src/Formatter/Formatter.php index 7bcbd00..5896f4e 100644 --- a/src/Formatter/Formatter.php +++ b/src/Formatter/Formatter.php @@ -41,4 +41,16 @@ public function formatIdentifier(array $identifiers): string } } + + public function formatDisallowedMessage(?string $message): string + { + if (!$message) { + return '.'; + } + if ($message[-1] !== '?' && $message[-1] !== '!') { + $message = rtrim($message, '.') . '.'; + } + return ', ' . $message; + } + } diff --git a/src/RuleErrors/DisallowedAttributeRuleErrors.php b/src/RuleErrors/DisallowedAttributeRuleErrors.php index 9021c51..10b9b51 100644 --- a/src/RuleErrors/DisallowedAttributeRuleErrors.php +++ b/src/RuleErrors/DisallowedAttributeRuleErrors.php @@ -9,6 +9,7 @@ use PHPStan\Rules\RuleErrorBuilder; use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed; use Spaze\PHPStan\Rules\Disallowed\DisallowedAttribute; +use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; use Spaze\PHPStan\Rules\Disallowed\Identifier\Identifier; class DisallowedAttributeRuleErrors @@ -20,11 +21,15 @@ class DisallowedAttributeRuleErrors /** @var Identifier */ private $identifier; + /** @var Formatter */ + private $formatter; - public function __construct(Allowed $allowed, Identifier $identifier) + + public function __construct(Allowed $allowed, Identifier $identifier, Formatter $formatter) { $this->allowed = $allowed; $this->identifier = $identifier; + $this->formatter = $formatter; } @@ -46,9 +51,9 @@ public function get(Attribute $attribute, Scope $scope, array $disallowedAttribu } $errorBuilder = RuleErrorBuilder::message(sprintf( - 'Attribute %s is forbidden, %s%s', + 'Attribute %s is forbidden%s%s', $attributeName, - $disallowedAttribute->getMessage(), + $this->formatter->formatDisallowedMessage($disallowedAttribute->getMessage()), $disallowedAttribute->getAttribute() !== $attributeName ? " [{$attributeName} matches {$disallowedAttribute->getAttribute()}]" : '' )); if ($disallowedAttribute->getErrorIdentifier()) { diff --git a/src/RuleErrors/DisallowedCallsRuleErrors.php b/src/RuleErrors/DisallowedCallsRuleErrors.php index 4057cdd..2a6ec44 100644 --- a/src/RuleErrors/DisallowedCallsRuleErrors.php +++ b/src/RuleErrors/DisallowedCallsRuleErrors.php @@ -11,6 +11,7 @@ use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed; use Spaze\PHPStan\Rules\Disallowed\DisallowedCall; use Spaze\PHPStan\Rules\Disallowed\File\FilePath; +use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; use Spaze\PHPStan\Rules\Disallowed\Identifier\Identifier; class DisallowedCallsRuleErrors @@ -25,12 +26,16 @@ class DisallowedCallsRuleErrors /** @var FilePath */ private $filePath; + /** @var Formatter */ + private $formatter; - public function __construct(Allowed $allowed, Identifier $identifier, FilePath $filePath) + + public function __construct(Allowed $allowed, Identifier $identifier, FilePath $filePath, Formatter $formatter) { $this->allowed = $allowed; $this->identifier = $identifier; $this->filePath = $filePath; + $this->formatter = $formatter; } @@ -54,9 +59,9 @@ public function get(?CallLike $node, Scope $scope, string $name, ?string $displa && !$this->allowed->isAllowed($scope, isset($node) ? $node->getArgs() : null, $disallowedCall) ) { $errorBuilder = RuleErrorBuilder::message(sprintf( - $message ?? 'Calling %s is forbidden, %s%s', + $message ?? 'Calling %s is forbidden%s%s', ($displayName && $displayName !== $name) ? "{$name}() (as {$displayName}())" : "{$name}()", - $disallowedCall->getMessage(), + $this->formatter->formatDisallowedMessage($disallowedCall->getMessage()), $disallowedCall->getCall() !== $name ? " [{$name}() matches {$disallowedCall->getCall()}()]" : '' )); if ($disallowedCall->getErrorIdentifier()) { diff --git a/src/RuleErrors/DisallowedConstantRuleErrors.php b/src/RuleErrors/DisallowedConstantRuleErrors.php index e4c8b90..281d5a2 100644 --- a/src/RuleErrors/DisallowedConstantRuleErrors.php +++ b/src/RuleErrors/DisallowedConstantRuleErrors.php @@ -9,6 +9,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; use Spaze\PHPStan\Rules\Disallowed\DisallowedConstant; +use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; class DisallowedConstantRuleErrors { @@ -17,10 +18,14 @@ class DisallowedConstantRuleErrors /** @var AllowedPath */ private $allowedPath; + /** @var Formatter */ + private $formatter; - public function __construct(AllowedPath $allowedPath) + + public function __construct(AllowedPath $allowedPath, Formatter $formatter) { $this->allowedPath = $allowedPath; + $this->formatter = $formatter; } @@ -37,10 +42,10 @@ public function get(string $constant, Scope $scope, ?string $displayName, array foreach ($disallowedConstants as $disallowedConstant) { if ($disallowedConstant->getConstant() === $constant && !$this->allowedPath->isAllowedPath($scope, $disallowedConstant)) { $errorBuilder = RuleErrorBuilder::message(sprintf( - 'Using %s%s is forbidden, %s', + 'Using %s%s is forbidden%s', $disallowedConstant->getConstant(), $displayName && $displayName !== $disallowedConstant->getConstant() ? ' (as ' . $displayName . ')' : '', - $disallowedConstant->getMessage() + $this->formatter->formatDisallowedMessage($disallowedConstant->getMessage()) )); if ($disallowedConstant->getErrorIdentifier()) { $errorBuilder->identifier($disallowedConstant->getErrorIdentifier()); diff --git a/src/RuleErrors/DisallowedNamespaceRuleErrors.php b/src/RuleErrors/DisallowedNamespaceRuleErrors.php index 3b5a1dc..a756879 100644 --- a/src/RuleErrors/DisallowedNamespaceRuleErrors.php +++ b/src/RuleErrors/DisallowedNamespaceRuleErrors.php @@ -8,6 +8,7 @@ use PHPStan\Rules\RuleErrorBuilder; use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; use Spaze\PHPStan\Rules\Disallowed\DisallowedNamespace; +use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; use Spaze\PHPStan\Rules\Disallowed\Identifier\Identifier; class DisallowedNamespaceRuleErrors @@ -19,11 +20,15 @@ class DisallowedNamespaceRuleErrors /** @var Identifier */ private $identifier; + /** @var Formatter */ + private $formatter; - public function __construct(AllowedPath $allowedPath, Identifier $identifier) + + public function __construct(AllowedPath $allowedPath, Identifier $identifier, Formatter $formatter) { $this->allowedPath = $allowedPath; $this->identifier = $identifier; + $this->formatter = $formatter; } @@ -46,10 +51,10 @@ public function getDisallowedMessage(string $namespace, string $description, Sco } $errorBuilder = RuleErrorBuilder::message(sprintf( - '%s %s is forbidden, %s%s', + '%s %s is forbidden%s%s', $description, $namespace, - $disallowedNamespace->getMessage(), + $this->formatter->formatDisallowedMessage($disallowedNamespace->getMessage()), $disallowedNamespace->getNamespace() !== $namespace ? " [{$namespace} matches {$disallowedNamespace->getNamespace()}]" : '' )); if ($disallowedNamespace->getErrorIdentifier()) { diff --git a/src/RuleErrors/DisallowedVariableRuleErrors.php b/src/RuleErrors/DisallowedVariableRuleErrors.php index 5a900ef..8754ac5 100644 --- a/src/RuleErrors/DisallowedVariableRuleErrors.php +++ b/src/RuleErrors/DisallowedVariableRuleErrors.php @@ -9,6 +9,7 @@ use PHPStan\ShouldNotHappenException; use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath; use Spaze\PHPStan\Rules\Disallowed\DisallowedVariable; +use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter; class DisallowedVariableRuleErrors { @@ -16,10 +17,14 @@ class DisallowedVariableRuleErrors /** @var AllowedPath */ private $allowedPath; + /** @var Formatter */ + private $formatter; - public function __construct(AllowedPath $allowedPath) + + public function __construct(AllowedPath $allowedPath, Formatter $formatter) { $this->allowedPath = $allowedPath; + $this->formatter = $formatter; } @@ -35,9 +40,9 @@ public function get(string $variable, Scope $scope, array $disallowedVariables): foreach ($disallowedVariables as $disallowedVariable) { if ($disallowedVariable->getVariable() === $variable && !$this->allowedPath->isAllowedPath($scope, $disallowedVariable)) { $errorBuilder = RuleErrorBuilder::message(sprintf( - 'Using %s is forbidden, %s', + 'Using %s is forbidden%s', $disallowedVariable->getVariable(), - $disallowedVariable->getMessage() + $this->formatter->formatDisallowedMessage($disallowedVariable->getMessage()) )); if ($disallowedVariable->getErrorIdentifier()) { $errorBuilder->identifier($disallowedVariable->getErrorIdentifier()); diff --git a/src/Usages/ClassConstantUsages.php b/src/Usages/ClassConstantUsages.php index fb0ba0b..f8a2d58 100644 --- a/src/Usages/ClassConstantUsages.php +++ b/src/Usages/ClassConstantUsages.php @@ -105,7 +105,7 @@ function (ConstantStringType $constantString): string { } elseif ($type->hasConstant($constant)->no()) { return [ RuleErrorBuilder::message(sprintf( - 'Cannot access constant %s on %s', + 'Cannot access constant %s on %s.', $constant, $type->describe(VerbosityLevel::getRecommendedLevelByType($type)) ))->build(),