Skip to content

Commit

Permalink
Format classnames and function names etc. in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
spaze committed Mar 20, 2023
1 parent 9006bb3 commit d598c89
Show file tree
Hide file tree
Showing 30 changed files with 123 additions and 33 deletions.
1 change: 1 addition & 0 deletions extension.neon
Expand Up @@ -85,6 +85,7 @@ services:
- Spaze\PHPStan\Rules\Disallowed\DisallowedConstantFactory
- Spaze\PHPStan\Rules\Disallowed\DisallowedNamespaceFactory
- Spaze\PHPStan\Rules\Disallowed\DisallowedSuperglobalFactory
- Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter
- Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedConstantRuleErrors
- Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedMethodRuleErrors
- Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedNamespaceRuleErrors
Expand Down
13 changes: 11 additions & 2 deletions src/DisallowedCallFactory.php
Expand Up @@ -16,6 +16,16 @@
class DisallowedCallFactory
{

/** @var IdentifierFormatter */
private $identifierFormatter;


public function __construct(IdentifierFormatter $identifierFormatter)
{
$this->identifierFormatter = $identifierFormatter;
}


/**
* @param array $config
* @phpstan-param ForbiddenCallsConfig $config
Expand Down Expand Up @@ -92,8 +102,7 @@ public function createFromConfig(array $config): array
$disallowedCalls[$disallowedCall->getKey()] = $disallowedCall;
}
} catch (UnsupportedParamTypeInConfigException $e) {
$message = count($calls) === 1 ? $calls[0] : '{' . implode(',', $calls) . '}';
throw new ShouldNotHappenException("{$message}: {$e->getMessage()}");
throw new ShouldNotHappenException(sprintf('%s: %s', $this->identifierFormatter->format($calls), $e->getMessage()));
}
}
return array_values($disallowedCalls);
Expand Down
18 changes: 18 additions & 0 deletions src/IdentifierFormatter.php
@@ -0,0 +1,18 @@
<?php
declare(strict_types = 1);

namespace Spaze\PHPStan\Rules\Disallowed;

class IdentifierFormatter
{

/**
* @param list<string> $identifiers
* @return string
*/
public function format(array $identifiers): string
{
return count($identifiers) === 1 ? $identifiers[0] : '{' . implode(',', $identifiers) . '}';
}

}
11 changes: 7 additions & 4 deletions src/RuleErrors/DisallowedMethodRuleErrors.php
Expand Up @@ -13,6 +13,7 @@
use PHPStan\Rules\RuleError;
use PHPStan\ShouldNotHappenException;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCall;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\Type\TypeResolver;

class DisallowedMethodRuleErrors
Expand All @@ -24,11 +25,15 @@ class DisallowedMethodRuleErrors
/** @var TypeResolver */
private $typeResolver;

/** @var IdentifierFormatter */
private $identifierFormatter;

public function __construct(DisallowedRuleErrors $disallowedRuleErrors, TypeResolver $typeResolver)

public function __construct(DisallowedRuleErrors $disallowedRuleErrors, TypeResolver $typeResolver, IdentifierFormatter $identifierFormatter)
{
$this->disallowedRuleErrors = $disallowedRuleErrors;
$this->typeResolver = $typeResolver;
$this->identifierFormatter = $identifierFormatter;
}


Expand All @@ -52,10 +57,8 @@ public function get($class, CallLike $node, Scope $scope, array $disallowedCalls
$classNames = $calledOnType->getObjectClassNames();
if (count($classNames) === 0) {
$calledAs = null;
} elseif (count($classNames) === 1) {
$calledAs = $this->disallowedRuleErrors->getFullyQualified($classNames[0], $method);
} else {
$calledAs = $this->disallowedRuleErrors->getFullyQualified('{' . implode(',', $classNames) . '}', $method);
$calledAs = $this->disallowedRuleErrors->getFullyQualified($this->identifierFormatter->format($classNames), $method);
}

foreach ($method->getDeclaringClass()->getTraits() as $trait) {
Expand Down
10 changes: 8 additions & 2 deletions src/Usages/ClassConstantUsages.php
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Type\VerbosityLevel;
use Spaze\PHPStan\Rules\Disallowed\DisallowedConstant;
use Spaze\PHPStan\Rules\Disallowed\DisallowedConstantFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedConstantRuleErrors;
use Spaze\PHPStan\Rules\Disallowed\Type\TypeResolver;

Expand All @@ -33,6 +34,9 @@ class ClassConstantUsages implements Rule
/** @var TypeResolver */
private $typeResolver;

/** @var IdentifierFormatter */
private $identifierFormatter;

/** @var DisallowedConstant[] */
private $disallowedConstants;

Expand All @@ -41,17 +45,20 @@ class ClassConstantUsages implements Rule
* @param DisallowedConstantRuleErrors $disallowedConstantRuleErrors
* @param DisallowedConstantFactory $disallowedConstantFactory
* @param TypeResolver $typeResolver
* @param IdentifierFormatter $identifierFormatter
* @param array<array{class?:string, constant?:string, message?:string, allowIn?:string[]}> $disallowedConstants
* @throws ShouldNotHappenException
*/
public function __construct(
DisallowedConstantRuleErrors $disallowedConstantRuleErrors,
DisallowedConstantFactory $disallowedConstantFactory,
TypeResolver $typeResolver,
IdentifierFormatter $identifierFormatter,
array $disallowedConstants
) {
$this->disallowedConstantRuleErrors = $disallowedConstantRuleErrors;
$this->typeResolver = $typeResolver;
$this->identifierFormatter = $identifierFormatter;
$this->disallowedConstants = $disallowedConstantFactory->createFromConfig($disallowedConstants);
}

Expand Down Expand Up @@ -115,8 +122,7 @@ function (ConstantStringType $constantString): string {
*/
private function getFullyQualified(array $classNames, string $constant): string
{
$className = count($classNames) === 1 ? $classNames[0] : '{' . implode(',', $classNames) . '}';
return $className . '::' . $constant;
return $this->identifierFormatter->format($classNames) . '::' . $constant;
}

}
3 changes: 2 additions & 1 deletion tests/Calls/EchoCallsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;

class EchoCallsTest extends RuleTestCase
Expand All @@ -21,7 +22,7 @@ protected function getRule(): Rule
{
return new EchoCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'function' => 'echo()',
Expand Down
3 changes: 2 additions & 1 deletion tests/Calls/EmptyCallsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;

class EmptyCallsTest extends RuleTestCase
Expand All @@ -21,7 +22,7 @@ protected function getRule(): Rule
{
return new EmptyCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'function' => 'empty()',
Expand Down
3 changes: 2 additions & 1 deletion tests/Calls/EvalCallsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;

class EvalCallsTest extends RuleTestCase
Expand All @@ -21,7 +22,7 @@ protected function getRule(): Rule
{
return new EvalCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'function' => 'eval()',
Expand Down
3 changes: 2 additions & 1 deletion tests/Calls/ExitDieCallsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;

class ExitDieCallsTest extends RuleTestCase
Expand All @@ -21,7 +22,7 @@ protected function getRule(): Rule
{
return new ExitDieCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'function' => [
Expand Down
3 changes: 2 additions & 1 deletion tests/Calls/FunctionCallsAllowInFunctionsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;

class FunctionCallsAllowInFunctionsTest extends RuleTestCase
Expand All @@ -21,7 +22,7 @@ protected function getRule(): Rule
{
return new FunctionCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'function' => 'md*()',
Expand Down
3 changes: 2 additions & 1 deletion tests/Calls/FunctionCallsAllowInMethodsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;

class FunctionCallsAllowInMethodsTest extends RuleTestCase
Expand All @@ -21,7 +22,7 @@ protected function getRule(): Rule
{
return new FunctionCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'function' => 'md5_file()',
Expand Down
3 changes: 2 additions & 1 deletion tests/Calls/FunctionCallsInMultipleNamespacesTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;

class FunctionCallsInMultipleNamespacesTest extends RuleTestCase
Expand All @@ -21,7 +22,7 @@ protected function getRule(): Rule
{
return new FunctionCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'function' => '__()',
Expand Down
3 changes: 2 additions & 1 deletion tests/Calls/FunctionCallsNamedParamsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;

/**
Expand All @@ -24,7 +25,7 @@ protected function getRule(): Rule
{
return new FunctionCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'function' => 'Foo\Bar\Waldo\foo()',
Expand Down
3 changes: 2 additions & 1 deletion tests/Calls/FunctionCallsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;
use Waldo\Quux\Blade;

Expand All @@ -22,7 +23,7 @@ protected function getRule(): Rule
{
return new FunctionCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'function' => '\var_dump()',
Expand Down
3 changes: 2 additions & 1 deletion tests/Calls/FunctionCallsUnsupportedParamConfigTest.php
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Testing\PHPStanTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;

class FunctionCallsUnsupportedParamConfigTest extends PHPStanTestCase
Expand All @@ -22,7 +23,7 @@ public function testUnsupportedArrayInParamConfig(): void
$this->expectExceptionMessage('{foo(),bar()}: Parameter #2 $definitelyNotScalar has an unsupported type array specified in configuration');
new FunctionCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'function' => [
Expand Down
5 changes: 3 additions & 2 deletions tests/Calls/MethodCallsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedMethodRuleErrors;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;
use Spaze\PHPStan\Rules\Disallowed\Type\TypeResolver;
Expand All @@ -22,8 +23,8 @@ class MethodCallsTest extends RuleTestCase
protected function getRule(): Rule
{
return new MethodCalls(
new DisallowedMethodRuleErrors(new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))), new TypeResolver()),
new DisallowedCallFactory(),
new DisallowedMethodRuleErrors(new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))), new TypeResolver(), new IdentifierFormatter()),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'method' => 'Waldo\Quux\Blade::run*()',
Expand Down
3 changes: 2 additions & 1 deletion tests/Calls/NewCallsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;

class NewCallsTest extends RuleTestCase
Expand All @@ -21,7 +22,7 @@ protected function getRule(): Rule
{
return new NewCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'method' => '\Constructor\ClassWithConstructor::__construct()',
Expand Down
3 changes: 2 additions & 1 deletion tests/Calls/PrintCallsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;

class PrintCallsTest extends RuleTestCase
Expand All @@ -21,7 +22,7 @@ protected function getRule(): Rule
{
return new PrintCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'function' => 'print()',
Expand Down
3 changes: 2 additions & 1 deletion tests/Calls/ShellExecCallsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;

class ShellExecCallsTest extends RuleTestCase
Expand All @@ -21,7 +22,7 @@ protected function getRule(): Rule
{
return new ShellExecCalls(
new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))),
new DisallowedCallFactory(),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'function' => 'shell_*()',
Expand Down
5 changes: 3 additions & 2 deletions tests/Calls/StaticCallsTest.php
Expand Up @@ -9,6 +9,7 @@
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\IdentifierFormatter;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedMethodRuleErrors;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedRuleErrors;
use Spaze\PHPStan\Rules\Disallowed\Type\TypeResolver;
Expand All @@ -22,8 +23,8 @@ class StaticCallsTest extends RuleTestCase
protected function getRule(): Rule
{
return new StaticCalls(
new DisallowedMethodRuleErrors(new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))), new TypeResolver()),
new DisallowedCallFactory(),
new DisallowedMethodRuleErrors(new DisallowedRuleErrors(new AllowedPath(new FileHelper(__DIR__))), new TypeResolver(), new IdentifierFormatter()),
new DisallowedCallFactory(new IdentifierFormatter()),
[
[
'method' => 'Fiction\Pulp\Royale::withCheese()',
Expand Down

0 comments on commit d598c89

Please sign in to comment.