Skip to content

Commit

Permalink
Always end the message with a full stop, unless it ends with ? or ! a…
Browse files Browse the repository at this point in the history
…lready
  • Loading branch information
spaze committed Nov 26, 2023
1 parent 626c4be commit b8b2faf
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Calls/ShellExecCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}

Expand Down
12 changes: 12 additions & 0 deletions src/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
11 changes: 8 additions & 3 deletions src/RuleErrors/DisallowedAttributeRuleErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}


Expand All @@ -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()) {
Expand Down
11 changes: 8 additions & 3 deletions src/RuleErrors/DisallowedCallsRuleErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}


Expand All @@ -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()) {
Expand Down
11 changes: 8 additions & 3 deletions src/RuleErrors/DisallowedConstantRuleErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}


Expand All @@ -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());
Expand Down
11 changes: 8 additions & 3 deletions src/RuleErrors/DisallowedNamespaceRuleErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}


Expand All @@ -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()) {
Expand Down
11 changes: 8 additions & 3 deletions src/RuleErrors/DisallowedVariableRuleErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@
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
{

/** @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;
}


Expand All @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion src/Usages/ClassConstantUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit b8b2faf

Please sign in to comment.