-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix
filter_var()
narrowing with unknown options
- Loading branch information
Showing
5 changed files
with
65 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace FilterVar; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class FilterVar | ||
{ | ||
|
||
public function doFoo($mixed): void | ||
{ | ||
assertType('int|false', filter_var($mixed, FILTER_VALIDATE_INT)); | ||
assertType('int|null', filter_var($mixed, FILTER_VALIDATE_INT, ['flags' => FILTER_NULL_ON_FAILURE])); | ||
assertType('array<int|false>', filter_var($mixed, FILTER_VALIDATE_INT, ['flags' => FILTER_FORCE_ARRAY])); | ||
assertType('array<int|null>', filter_var($mixed, FILTER_VALIDATE_INT, ['flags' => FILTER_FORCE_ARRAY|FILTER_NULL_ON_FAILURE])); | ||
assertType('0|int<17, 19>', filter_var($mixed, FILTER_VALIDATE_INT, ['options' => ['default' => 0, 'min_range' => 17, 'max_range' => 19]])); | ||
} | ||
|
||
public function intToInt(int $int, array $options): void | ||
{ | ||
assertType('int', filter_var($int, FILTER_VALIDATE_INT)); | ||
assertType('int|false', filter_var($int, FILTER_VALIDATE_INT, $options)); | ||
} | ||
|
||
public function constants(): void | ||
{ | ||
assertType('array<false>', filter_var(false, FILTER_VALIDATE_BOOLEAN, FILTER_FORCE_ARRAY | FILTER_NULL_ON_FAILURE)); | ||
assertType('17', filter_var(17, FILTER_VALIDATE_INT)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php declare(strict_types = 1); // lint >= 7.4 | ||
|
||
namespace Bug8516; | ||
|
||
function validate($value, array $options = null): bool | ||
{ | ||
if (is_int($value)) { | ||
$options ??= ['options' => ['min_range' => 0]]; | ||
if (filter_var($value, FILTER_VALIDATE_INT, $options) === false) { | ||
return false; | ||
} | ||
// ... | ||
} | ||
if (is_string($value)) { | ||
// ... | ||
} | ||
return true; | ||
} |