Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the initial namespace separator from class names in one place #187

Merged
merged 1 commit into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DisallowedAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(
?string $errorIdentifier,
?string $errorTip
) {
$this->attribute = ltrim($attribute, '\\');
$this->attribute = $attribute;
$this->message = $message;
$this->allowedConfig = $allowedConfig;
$this->errorIdentifier = $errorIdentifier;
Expand Down
9 changes: 7 additions & 2 deletions src/DisallowedAttributeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@
namespace Spaze\PHPStan\Rules\Disallowed;

use Spaze\PHPStan\Rules\Disallowed\Exceptions\UnsupportedParamTypeInConfigException;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;

class DisallowedAttributeFactory
{

/** @var Allowed */
private $allowed;

/** @var Normalizer */
private $normalizer;

public function __construct(Allowed $allowed)

public function __construct(Allowed $allowed, Normalizer $normalizer)
{
$this->allowed = $allowed;
$this->normalizer = $normalizer;
}


Expand All @@ -31,7 +36,7 @@ public function createFromConfig(array $config): array
$attributes = $disallowed['attribute'];
foreach ((array)$attributes as $attribute) {
$disallowedAttribute = new DisallowedAttribute(
$attribute,
$this->normalizer->normalizeNamespace($attribute),
$disallowed['message'] ?? null,
$this->allowed->getConfig($disallowed),
$disallowed['errorIdentifier'] ?? null,
Expand Down
2 changes: 1 addition & 1 deletion src/DisallowedConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(
?string $errorIdentifier,
?string $errorTip
) {
$this->constant = ltrim($constant, '\\');
$this->constant = $constant;
$this->message = $message;
$this->allowIn = $allowIn;
$this->allowExceptIn = $allowExceptIn;
Expand Down
13 changes: 12 additions & 1 deletion src/DisallowedConstantFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
namespace Spaze\PHPStan\Rules\Disallowed;

use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;

class DisallowedConstantFactory
{

/** @var Normalizer */
private $normalizer;


public function __construct(Normalizer $normalizer)
{
$this->normalizer = $normalizer;
}


/**
* @param array<array{class?:string, constant?:string, message?:string, allowIn?:string[], allowExceptIn?:string[], disallowIn?:string[], errorIdentifier?:string, errorTip?:string}> $config
* @return DisallowedConstant[]
Expand All @@ -25,7 +36,7 @@ public function createFromConfig(array $config): array
foreach ((array)$constants as $constant) {
$class = $disallowed['class'] ?? null;
$disallowedConstant = new DisallowedConstant(
$class ? "{$class}::{$constant}" : $constant,
$this->normalizer->normalizeNamespace($class ? "{$class}::{$constant}" : $constant),
$disallowed['message'] ?? null,
$disallowed['allowIn'] ?? [],
$disallowed['allowExceptIn'] ?? $disallowed['disallowIn'] ?? [],
Expand Down
2 changes: 1 addition & 1 deletion src/DisallowedNamespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct(
?string $errorIdentifier,
?string $errorTip
) {
$this->namespace = ltrim($namespace, '\\');
$this->namespace = $namespace;
$this->message = $message;
$this->allowIn = $allowIn;
$this->allowExceptIn = $allowExceptIn;
Expand Down
13 changes: 12 additions & 1 deletion src/DisallowedNamespaceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
namespace Spaze\PHPStan\Rules\Disallowed;

use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;

class DisallowedNamespaceFactory
{

/** @var Normalizer */
private $normalizer;


public function __construct(Normalizer $normalizer)
{
$this->normalizer = $normalizer;
}


/**
* @param array<array{namespace?:string, class?:string, message?:string, allowIn?:string[], allowExceptIn?:string[], disallowIn?:string[], errorIdentifier?:string, errorTip?:string}> $config
* @return DisallowedNamespace[]
Expand All @@ -23,7 +34,7 @@ public function createFromConfig(array $config): array
}
foreach ((array)$namespaces as $namespace) {
$disallowedNamespace = new DisallowedNamespace(
$namespace,
$this->normalizer->normalizeNamespace($namespace),
$disallowed['message'] ?? null,
$disallowed['allowIn'] ?? [],
$disallowed['allowExceptIn'] ?? $disallowed['disallowIn'] ?? [],
Expand Down
20 changes: 19 additions & 1 deletion src/Formatter/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@
namespace Spaze\PHPStan\Rules\Disallowed\Formatter;

use PHPStan\Reflection\MethodReflection;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;

class Formatter
{

/** @var Normalizer */
private $normalizer;


public function __construct(Normalizer $normalizer)
{
$this->normalizer = $normalizer;
}


public function getFullyQualified(string $class, MethodReflection $method): string
{
return sprintf('%s::%s', $class, $method->getName());
Expand All @@ -20,7 +31,14 @@ public function getFullyQualified(string $class, MethodReflection $method): stri
*/
public function formatIdentifier(array $identifiers): string
{
return count($identifiers) === 1 ? $identifiers[0] : '{' . implode(',', $identifiers) . '}';
if (count($identifiers) === 1) {
return $this->normalizer->normalizeNamespace($identifiers[0]);
} else {
array_walk($identifiers, function (string &$identifier): void {
$identifier = $this->normalizer->normalizeNamespace($identifier);
});
return '{' . implode(',', $identifiers) . '}';
}
}

}
8 changes: 7 additions & 1 deletion src/Normalizer/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ class Normalizer
public function normalizeCall(string $call): string
{
$call = substr($call, -2) === '()' ? substr($call, 0, -2) : $call;
return ltrim($call, '\\');
return $this->normalizeNamespace($call);
}


public function normalizeNamespace(string $namespace): string
{
return ltrim($namespace, '\\');
}

}
2 changes: 1 addition & 1 deletion src/RuleErrors/DisallowedConstantRuleErrors.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function get(string $constant, Scope $scope, ?string $displayName, array
$errorBuilder = RuleErrorBuilder::message(sprintf(
'Using %s%s is forbidden, %s',
$disallowedConstant->getConstant(),
$displayName && ltrim($displayName, '\\') !== $disallowedConstant->getConstant() ? ' (as ' . $displayName . ')' : '',
$displayName && $displayName !== $disallowedConstant->getConstant() ? ' (as ' . $displayName . ')' : '',
$disallowedConstant->getMessage()
));
if ($disallowedConstant->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 @@ -95,7 +95,7 @@ public function processNode(Node $node, Scope $scope): array
if ($usedOnType->getConstantStrings()) {
$classNames = array_map(
function (ConstantStringType $constantString): string {
return ltrim($constantString->getValue(), '\\');
return $constantString->getValue();
},
$usedOnType->getConstantStrings()
);
Expand Down
16 changes: 13 additions & 3 deletions src/Usages/NamespaceUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PHPStan\Rules\RuleError;
use Spaze\PHPStan\Rules\Disallowed\DisallowedNamespace;
use Spaze\PHPStan\Rules\Disallowed\DisallowedNamespaceFactory;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedNamespaceRuleErrors;

/**
Expand All @@ -34,16 +35,25 @@ class NamespaceUsages implements Rule
/** @var DisallowedNamespace[] */
private $disallowedNamespace;

/** @var Normalizer */
private $normalizer;


/**
* @param DisallowedNamespaceRuleErrors $disallowedNamespaceRuleErrors
* @param DisallowedNamespaceFactory $disallowNamespaceFactory
* @param Normalizer $normalizer
* @param array<array{namespace:string, message?:string, allowIn?:string[]}> $forbiddenNamespaces
*/
public function __construct(DisallowedNamespaceRuleErrors $disallowedNamespaceRuleErrors, DisallowedNamespaceFactory $disallowNamespaceFactory, array $forbiddenNamespaces)
{
public function __construct(
DisallowedNamespaceRuleErrors $disallowedNamespaceRuleErrors,
DisallowedNamespaceFactory $disallowNamespaceFactory,
Normalizer $normalizer,
array $forbiddenNamespaces
) {
$this->disallowedNamespaceRuleErrors = $disallowedNamespaceRuleErrors;
$this->disallowedNamespace = $disallowNamespaceFactory->createFromConfig($forbiddenNamespaces);
$this->normalizer = $normalizer;
}


Expand Down Expand Up @@ -108,7 +118,7 @@ public function processNode(Node $node, Scope $scope): array
foreach ($namespaces as $namespace) {
$errors = array_merge(
$errors,
$this->disallowedNamespaceRuleErrors->getDisallowedMessage(ltrim($namespace, '\\'), $description ?? 'Namespace', $scope, $this->disallowedNamespace)
$this->disallowedNamespaceRuleErrors->getDisallowedMessage($this->normalizer->normalizeNamespace($namespace), $description ?? 'Namespace', $scope, $this->disallowedNamespace)
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/EchoCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class EchoCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new EchoCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/EmptyCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class EmptyCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new EmptyCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/EvalCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class EvalCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new EvalCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/ExitDieCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class ExitDieCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new ExitDieCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/FunctionCallsAllowInFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class FunctionCallsAllowInFunctionsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new FunctionCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/FunctionCallsAllowInMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class FunctionCallsAllowInMethodsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new FunctionCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/FunctionCallsInMultipleNamespacesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class FunctionCallsInMultipleNamespacesTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new FunctionCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/FunctionCallsNamedParamsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class FunctionCallsNamedParamsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new FunctionCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/FunctionCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class FunctionCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new FunctionCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/FunctionCallsUnsupportedParamConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public function testUnsupportedArrayInParamConfig(): void
{
$this->expectException(ShouldNotHappenException::class);
$this->expectExceptionMessage('{foo(),bar()}: Parameter #2 $definitelyNotScalar has an unsupported type array specified in configuration');
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
new FunctionCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/MethodCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class MethodCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new MethodCalls(
new DisallowedMethodRuleErrors(
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/NewCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class NewCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new NewCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/PrintCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class PrintCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new PrintCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/ShellExecCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class ShellExecCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new ShellExecCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
2 changes: 1 addition & 1 deletion tests/Calls/StaticCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class StaticCallsTest extends RuleTestCase
*/
protected function getRule(): Rule
{
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new StaticCalls(
new DisallowedMethodRuleErrors(
Expand Down
2 changes: 1 addition & 1 deletion tests/Configs/DangerousConfigEvalCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ protected function getRule(): Rule
{
// Load the configuration from this file
$config = Neon::decode(file_get_contents(__DIR__ . '/../../disallowed-dangerous-calls.neon'));
$formatter = new Formatter();
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$allowed = new Allowed($formatter, $normalizer, new AllowedPath(new FileHelper(__DIR__)));
return new EvalCalls(
new DisallowedCallsRuleErrors($allowed),
Expand Down
Loading