Skip to content

Commit

Permalink
Check filter_input* type param type
Browse files Browse the repository at this point in the history
  • Loading branch information
herndlm authored and ondrejmirtes committed Apr 13, 2023
1 parent 84ee0cd commit 706ba08
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
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);
}

}

0 comments on commit 706ba08

Please sign in to comment.