Skip to content

Commit

Permalink
Remove "because reasons", because reasons (#221)
Browse files Browse the repository at this point in the history
Was funny back then but maybe it's not anymore. Also it doesn't really indicate that the message could be changed. Also, the message will always end with a `.`, unless it ends with `?` or `!` already. Sorry if it breaks things for you, should have done this much earlier.

Close #220
  • Loading branch information
spaze authored Nov 28, 2023
2 parents 1500f90 + b8b11f4 commit 4b952b1
Show file tree
Hide file tree
Showing 45 changed files with 344 additions and 276 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
4 changes: 2 additions & 2 deletions src/DisallowedAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public function getExcludes(): array
}


public function getMessage(): string
public function getMessage(): ?string
{
return $this->message ?? 'because reasons';
return $this->message;
}


Expand Down
4 changes: 2 additions & 2 deletions src/DisallowedCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public function getDefinedIn(): array
}


public function getMessage(): string
public function getMessage(): ?string
{
return $this->message ?? 'because reasons';
return $this->message;
}


Expand Down
4 changes: 2 additions & 2 deletions src/DisallowedConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public function getConstant(): string
}


public function getMessage(): string
public function getMessage(): ?string
{
return $this->message ?? 'because reasons';
return $this->message;
}


Expand Down
4 changes: 2 additions & 2 deletions src/DisallowedNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public function getExcludes(): array
}


public function getMessage(): string
public function getMessage(): ?string
{
return $this->message ?? 'because reasons';
return $this->message;
}


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
4 changes: 2 additions & 2 deletions tests/Calls/EchoCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function getRule(): Rule
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
return new EchoCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath),
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
[
[
Expand All @@ -49,7 +49,7 @@ public function testRule(): void
// Based on the configuration above, in this file:
$this->analyse([__DIR__ . '/../src/disallowed/functionCalls.php'], [
[
'Calling echo() is forbidden, because reasons',
'Calling echo() is forbidden.',
42,
],
]);
Expand Down
4 changes: 2 additions & 2 deletions tests/Calls/EmptyCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function getRule(): Rule
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
return new EmptyCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath),
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
[
[
Expand All @@ -49,7 +49,7 @@ public function testRule(): void
// Based on the configuration above, in this file:
$this->analyse([__DIR__ . '/../src/disallowed/functionCalls.php'], [
[
'Calling empty() is forbidden, because reasons',
'Calling empty() is forbidden.',
41,
],
]);
Expand Down
4 changes: 2 additions & 2 deletions tests/Calls/EvalCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function getRule(): Rule
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
return new EvalCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath),
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
[
[
Expand All @@ -49,7 +49,7 @@ public function testRule(): void
// Based on the configuration above, in this file:
$this->analyse([__DIR__ . '/../src/disallowed/functionCalls.php'], [
[
'Calling eval() is forbidden, because reasons',
'Calling eval() is forbidden.',
28,
],
]);
Expand Down
10 changes: 5 additions & 5 deletions tests/Calls/ExitDieCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function getRule(): Rule
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
return new ExitDieCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath),
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
[
[
Expand All @@ -52,19 +52,19 @@ public function testRule(): void
// Based on the configuration above, in this file:
$this->analyse([__DIR__ . '/../src/disallowed/functionCalls.php'], [
[
'Calling die() is forbidden, because reasons',
'Calling die() is forbidden.',
30,
],
[
'Calling die() is forbidden, because reasons',
'Calling die() is forbidden.',
33,
],
[
'Calling exit() is forbidden, because reasons',
'Calling exit() is forbidden.',
36,
],
[
'Calling exit() is forbidden, because reasons',
'Calling exit() is forbidden.',
39,
],
]);
Expand Down
4 changes: 2 additions & 2 deletions tests/Calls/FunctionCallsAllowInFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function getRule(): Rule
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
return new FunctionCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath),
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
$this->createReflectionProvider(),
[
Expand All @@ -56,7 +56,7 @@ public function testRule(): void
$this->analyse([__DIR__ . '/../libs/Functions.php'], [
[
// expect this error message:
'Calling sha1() is forbidden, because reasons [sha1() matches sha*()]',
'Calling sha1() is forbidden. [sha1() matches sha*()]',
// on this line:
15,
],
Expand Down
4 changes: 2 additions & 2 deletions tests/Calls/FunctionCallsAllowInMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected function getRule(): Rule
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
return new FunctionCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath),
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
$this->createReflectionProvider(),
[
Expand Down Expand Up @@ -65,7 +65,7 @@ public function testRule(): void
$this->analyse([__DIR__ . '/../libs/Royale.php'], [
[
// expect this error message:
'Calling sha1() is forbidden, because reasons',
'Calling sha1() is forbidden.',
// on this line:
11,
],
Expand Down
Loading

0 comments on commit 4b952b1

Please sign in to comment.