Skip to content

Check filter_input* type param type #2271

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

Merged
merged 1 commit into from
Apr 13, 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
11 changes: 11 additions & 0 deletions resources/functionMap_php80delta_bleedingEdge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php // phpcs:ignoreFile

return [
'new' => [
'filter_input' => ['mixed', 'type'=>'INPUT_GET|INPUT_POST|INPUT_COOKIE|INPUT_SERVER|INPUT_ENV', 'variable_name'=>'string', 'filter='=>'int', 'options='=>'array|int'],
'filter_input_array' => ['array|false|null', 'type'=>'INPUT_GET|INPUT_POST|INPUT_COOKIE|INPUT_SERVER|INPUT_ENV', 'definition='=>'int|array', 'add_empty='=>'bool'],
],
'old' => [

]
];
9 changes: 9 additions & 0 deletions src/Reflection/SignatureMap/FunctionSignatureMapProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,15 @@ public function getSignatureMap(): array
}

$signatureMap = $this->computeSignatureMap($signatureMap, $stricterFunctionMap);

if ($this->phpVersion->getVersionId() >= 80000) {
$php80StricterFunctionMapDelta = require __DIR__ . '/../../../resources/functionMap_php80delta_bleedingEdge.php';
if (!is_array($php80StricterFunctionMapDelta)) {
throw new ShouldNotHappenException('Signature map could not be loaded.');
}

$signatureMap = $this->computeSignatureMap($signatureMap, $php80StricterFunctionMapDelta);
}
}

if ($this->phpVersion->getVersionId() >= 70400) {
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1301,4 +1301,32 @@ public function testBug7239(): void
]);
}

public function testFilterInputType(): void
{
$errors = [
[
'Parameter #1 $type of function filter_input expects 0|1|2|4|5, -1 given.',
16,
],
[
'Parameter #1 $type of function filter_input expects 0|1|2|4|5, int given.',
17,
],
[
'Parameter #1 $type of function filter_input_array expects 0|1|2|4|5, -1 given.',
28,
],
[
'Parameter #1 $type of function filter_input_array expects 0|1|2|4|5, int given.',
29,
],
];

if (PHP_VERSION_ID < 80000) {
$errors = [];
}

$this->analyse([__DIR__ . '/data/filter-input-type.php'], $errors);
}

}
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Functions/data/filter-input-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace FilterInputType;

class Foo
{

public function doFoo(int $type): void
{
filter_input(INPUT_GET, 'foo');
filter_input(INPUT_POST, 'foo');
filter_input(INPUT_COOKIE, 'foo');
filter_input(INPUT_SERVER, 'foo');
filter_input(INPUT_ENV, 'foo');

filter_input(-1, 'foo');
filter_input($type, 'foo');
}

public function doBar(int $type): void
{
filter_input_array(INPUT_GET);
filter_input_array(INPUT_POST);
filter_input_array(INPUT_COOKIE);
filter_input_array(INPUT_SERVER);
filter_input_array(INPUT_ENV);

filter_input_array(-1);
filter_input_array($type);
}

}