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

Define parameters as a structure #222

Merged
merged 3 commits into from
Nov 30, 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 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',
];
}

}