Skip to content

Commit

Permalink
Define parameters as a structure (#222)
Browse files Browse the repository at this point in the history
Close #192
  • Loading branch information
spaze committed Nov 30, 2023
2 parents e6ee58c + 0d4d19d commit f940bc8
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 107 deletions.
2 changes: 1 addition & 1 deletion disallowed-loose-calls.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ parameters:
function: 'htmlspecialchars()'
message: 'set the $flags parameter to `ENT_QUOTES` to also convert single quotes to entities to prevent some HTML injection bugs'
allowParamFlagsAnywhere:
2: ::ENT_QUOTES
2: ::constant(ENT_QUOTES)
237 changes: 175 additions & 62 deletions extension.neon

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/DisallowedConstantFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(Normalizer $normalizer)


/**
* @param array<array{class?:string, constant?:string, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, errorIdentifier?:string, errorTip?:string}> $config
* @param array<array{class?:string, constant?:string|list<string>, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, errorIdentifier?:string, errorTip?:string}> $config
* @return list<DisallowedConstant>
* @throws ShouldNotHappenException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/DisallowedNamespaceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(Normalizer $normalizer)


/**
* @param array<array{namespace?:string, class?:string, exclude?:string|list<string>, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, errorIdentifier?:string, errorTip?:string}> $config
* @param array<array{namespace?:string|list<string>, class?:string|list<string>, exclude?:string|list<string>, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, errorIdentifier?:string, errorTip?:string}> $config
* @return list<DisallowedNamespace>
*/
public function createFromConfig(array $config): array
Expand Down
2 changes: 1 addition & 1 deletion src/DisallowedSuperglobalFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DisallowedSuperglobalFactory implements DisallowedVariableFactory


/**
* @param array<array{superglobal?:string, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, errorIdentifier?:string, errorTip?:string}> $config
* @param array<array{superglobal?:string|list<string>, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, errorIdentifier?:string, errorTip?:string}> $config
* @return list<DisallowedVariable>
* @throws ShouldNotHappenException
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Usages/ClassConstantUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ClassConstantUsages implements Rule
* @param DisallowedConstantFactory $disallowedConstantFactory
* @param TypeResolver $typeResolver
* @param Formatter $formatter
* @param array<array{class?:string, constant?:string, message?:string, allowIn?:list<string>}> $disallowedConstants
* @param array<array{class?:string, constant?:string|list<string>, message?:string, allowIn?:list<string>}> $disallowedConstants
* @throws ShouldNotHappenException
*/
public function __construct(
Expand Down
2 changes: 1 addition & 1 deletion src/Usages/NamespaceUsages.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class NamespaceUsages implements Rule
* @param DisallowedNamespaceRuleErrors $disallowedNamespaceRuleErrors
* @param DisallowedNamespaceFactory $disallowNamespaceFactory
* @param Normalizer $normalizer
* @param array<array{namespace:string, message?:string, allowIn?:list<string>}> $forbiddenNamespaces
* @param array<array{namespace?:string|list<string>, class?:string|list<string>, exclude?:string|list<string>, message?:string, allowIn?:list<string>, allowExceptIn?:list<string>, disallowIn?:list<string>, errorIdentifier?:string, errorTip?:string}> $forbiddenNamespaces
*/
public function __construct(
DisallowedNamespaceRuleErrors $disallowedNamespaceRuleErrors,
Expand Down
49 changes: 10 additions & 39 deletions tests/Configs/LooseConfigFunctionCallsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,16 @@

namespace Spaze\PHPStan\Rules\Disallowed\Configs;

use Nette\Neon\Neon;
use PHPStan\File\FileHelper;
use PHPStan\Rules\Rule;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\RuleTestCase;
use Spaze\PHPStan\Rules\Disallowed\Allowed\Allowed;
use Spaze\PHPStan\Rules\Disallowed\Allowed\AllowedPath;
use Spaze\PHPStan\Rules\Disallowed\Calls\FunctionCalls;
use Spaze\PHPStan\Rules\Disallowed\DisallowedCallFactory;
use Spaze\PHPStan\Rules\Disallowed\File\FilePath;
use Spaze\PHPStan\Rules\Disallowed\Formatter\Formatter;
use Spaze\PHPStan\Rules\Disallowed\Identifier\Identifier;
use Spaze\PHPStan\Rules\Disallowed\Normalizer\Normalizer;
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedCallsRuleErrors;

class LooseConfigFunctionCallsTest extends RuleTestCase
{

/**
* @throws ShouldNotHappenException
*/
protected function getRule(): Rule
{
// Load the configuration from this file
$config = Neon::decode(file_get_contents(__DIR__ . '/../../disallowed-loose-calls.neon'));
// emulate how the real config loader expands constants that are used in the config file above (e.g. ::ENT_QUOTES)
foreach ($config['parameters']['disallowedFunctionCalls'] as &$call) {
foreach (['allowParamsAnywhere', 'allowParamFlagsAnywhere'] as $key) {
if (!isset($call[$key])) {
continue;
}
foreach ($call[$key] as &$param) {
if (is_string($param) && preg_match('/^::([A-Z0-9_]+)$/', $param, $matches)) {
$param = constant($matches[1]);
}
}
}
}
$normalizer = new Normalizer();
$formatter = new Formatter($normalizer);
$filePath = new FilePath(new FileHelper(__DIR__));
$allowed = new Allowed($formatter, $normalizer, new AllowedPath($filePath));
return new FunctionCalls(
new DisallowedCallsRuleErrors($allowed, new Identifier(), $filePath, $formatter),
new DisallowedCallFactory($formatter, $normalizer, $allowed),
$this->createReflectionProvider(),
$config['parameters']['disallowedFunctionCalls']
);
return self::getContainer()->getByType(FunctionCalls::class);
}


Expand All @@ -67,4 +29,13 @@ public function testRule(): void
]);
}


public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/../../extension.neon',
__DIR__ . '/../../disallowed-loose-calls.neon',
];
}

}

0 comments on commit f940bc8

Please sign in to comment.