Skip to content

Commit ea62732

Browse files
committed
Parameter type has to have the same type as parameters with described allowed constants to report passed constant as an error
1 parent 69e056c commit ea62732

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/Rules/FunctionCallParametersCheck.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,14 +108,13 @@ public function check(
108108

109109
$functionParametersMinCount = 0;
110110
$functionParametersMaxCount = 0;
111-
$hasAllowedConstants = false;
111+
$allowedConstantsTypes = [];
112112
foreach ($parametersAcceptor->getParameters() as $parameter) {
113113
if (
114114
$parameter instanceof ExtendedParameterReflection
115-
&& !$hasAllowedConstants
116115
&& $parameter->getAllowedConstants() !== null
117116
) {
118-
$hasAllowedConstants = true;
117+
$allowedConstantsTypes[] = $parameter->getType();
119118
}
120119
if (!$parameter->isOptional()) {
121120
$functionParametersMinCount++;
@@ -124,6 +123,11 @@ public function check(
124123
$functionParametersMaxCount++;
125124
}
126125

126+
$allowedConstantsType = null;
127+
if (count($allowedConstantsTypes) > 0) {
128+
$allowedConstantsType = TypeCombinator::union(...$allowedConstantsTypes);
129+
}
130+
127131
if ($parametersAcceptor->isVariadic()) {
128132
$functionParametersMaxCount = -1;
129133
}
@@ -469,7 +473,7 @@ public function check(
469473
->line($argumentLine)
470474
->build();
471475
}
472-
} elseif ($isBuiltin && $hasAllowedConstants) {
476+
} elseif ($isBuiltin && $allowedConstantsType !== null && $allowedConstantsType->isSuperTypeOf($parameterType)->yes()) {
473477
foreach ($constantReflections as $constantReflection) {
474478
if ($constantReflection->isBuiltin()->no()) {
475479
continue;

tests/PHPStan/Rules/Functions/data/constant-parameter-check.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,6 @@ public function hashPassword(string $password): string
102102
json_decode($json, true);
103103
json_decode($json, null);
104104
json_decode($json, false);
105+
106+
// PHP_OS passed to $subject of preg_match - should not report
107+
preg_match('/foo/', PHP_OS);

0 commit comments

Comments
 (0)