Skip to content

Commit b0b0f75

Browse files
phpstan-botstaabmclaude
authored
Declare mutually exclusive constant groups for json_encode, json_decode, filter_var, filter_input, openlog and ZipArchive::open flags (#6571)
Co-authored-by: staabm <120441+staabm@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Markus Staab <maggus.staab@googlemail.com>
1 parent 6c99272 commit b0b0f75

5 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎resources/constantToFunctionParameterMap.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@
6666
'JSON_INVALID_UTF8_IGNORE',
6767
'JSON_INVALID_UTF8_SUBSTITUTE',
6868
],
69+
'exclusiveGroups' => [
70+
['JSON_INVALID_UTF8_IGNORE', 'JSON_INVALID_UTF8_SUBSTITUTE'],
71+
['JSON_PARTIAL_OUTPUT_ON_ERROR', 'JSON_THROW_ON_ERROR'],
72+
],
6973
],
7074
],
7175

@@ -79,6 +83,9 @@
7983
'JSON_INVALID_UTF8_IGNORE',
8084
'JSON_INVALID_UTF8_SUBSTITUTE',
8185
],
86+
'exclusiveGroups' => [
87+
['JSON_INVALID_UTF8_IGNORE', 'JSON_INVALID_UTF8_SUBSTITUTE'],
88+
],
8289
],
8390
],
8491

@@ -659,6 +666,9 @@
659666
'FILTER_FLAG_EMAIL_UNICODE',
660667
'FILTER_THROW_ON_FAILURE',
661668
],
669+
'exclusiveGroups' => [
670+
['FILTER_REQUIRE_SCALAR', 'FILTER_REQUIRE_ARRAY'],
671+
],
662672
],
663673
],
664674

@@ -733,6 +743,9 @@
733743
'FILTER_FLAG_EMAIL_UNICODE',
734744
'FILTER_THROW_ON_FAILURE',
735745
],
746+
'exclusiveGroups' => [
747+
['FILTER_REQUIRE_SCALAR', 'FILTER_REQUIRE_ARRAY'],
748+
],
736749
],
737750
],
738751

@@ -1149,6 +1162,9 @@
11491162
'LOG_PERROR',
11501163
'LOG_PID',
11511164
],
1165+
'exclusiveGroups' => [
1166+
['LOG_NDELAY', 'LOG_ODELAY'],
1167+
],
11521168
],
11531169
'facility' => [
11541170
'type' => 'single',
@@ -2103,6 +2119,9 @@
21032119
'ZipArchive::OVERWRITE',
21042120
'ZipArchive::RDONLY',
21052121
],
2122+
'exclusiveGroups' => [
2123+
['ZipArchive::RDONLY', 'ZipArchive::OVERWRITE'],
2124+
],
21062125
],
21072126
],
21082127

‎tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3004,6 +3004,37 @@ public function testBug12850(): void
30043004
]);
30053005
}
30063006

3007+
#[RequiresPhp('>= 8.0.0')]
3008+
public function testBug15306(): void
3009+
{
3010+
$this->analyse([__DIR__ . '/data/bug-15306.php'], [
3011+
[
3012+
'Constants JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE cannot be combined for parameter #4 $flags of function json_decode.',
3013+
5,
3014+
],
3015+
[
3016+
'Constants JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE cannot be combined for parameter #2 $flags of function json_encode.',
3017+
9,
3018+
],
3019+
[
3020+
'Constants JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_THROW_ON_ERROR cannot be combined for parameter #2 $flags of function json_encode.',
3021+
10,
3022+
],
3023+
[
3024+
'Constants FILTER_REQUIRE_SCALAR, FILTER_REQUIRE_ARRAY cannot be combined for parameter #3 $options of function filter_var.',
3025+
14,
3026+
],
3027+
[
3028+
'Constants FILTER_REQUIRE_SCALAR, FILTER_REQUIRE_ARRAY cannot be combined for parameter #4 $options of function filter_input.',
3029+
16,
3030+
],
3031+
[
3032+
'Constants LOG_NDELAY, LOG_ODELAY cannot be combined for parameter #2 $flags of function openlog.',
3033+
19,
3034+
],
3035+
]);
3036+
}
3037+
30073038
public function testBug4608(): void
30083039
{
30093040
$paramName = PHP_VERSION_ID >= 80000 ? 'callback' : 'function';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug15306;
4+
5+
json_decode('{}', true, 512, JSON_INVALID_UTF8_IGNORE | JSON_INVALID_UTF8_SUBSTITUTE);
6+
json_decode('{}', true, 512, JSON_INVALID_UTF8_IGNORE | JSON_THROW_ON_ERROR);
7+
json_decode('{}', true, 512, JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR);
8+
9+
json_encode([], JSON_INVALID_UTF8_IGNORE | JSON_INVALID_UTF8_SUBSTITUTE);
10+
json_encode([], JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_THROW_ON_ERROR);
11+
json_encode([], JSON_INVALID_UTF8_SUBSTITUTE | JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);
12+
json_encode([], JSON_INVALID_UTF8_SUBSTITUTE | JSON_PARTIAL_OUTPUT_ON_ERROR);
13+
14+
filter_var('foo', FILTER_VALIDATE_INT, FILTER_REQUIRE_SCALAR | FILTER_REQUIRE_ARRAY);
15+
filter_var('foo', FILTER_VALIDATE_INT, FILTER_REQUIRE_ARRAY | FILTER_NULL_ON_FAILURE);
16+
filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, FILTER_REQUIRE_SCALAR | FILTER_REQUIRE_ARRAY);
17+
filter_input(INPUT_GET, 'foo', FILTER_VALIDATE_INT, FILTER_FORCE_ARRAY | FILTER_NULL_ON_FAILURE);
18+
19+
openlog('foo', LOG_NDELAY | LOG_ODELAY, LOG_USER);
20+
openlog('foo', LOG_NDELAY | LOG_PID, LOG_USER);

‎tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4361,6 +4361,20 @@ public function testBug14808(): void
43614361
$this->analyse([__DIR__ . '/data/bug-14808.php'], []);
43624362
}
43634363

4364+
#[RequiresPhp('>= 8.0.0')]
4365+
public function testBug15306(): void
4366+
{
4367+
$this->checkThisOnly = false;
4368+
$this->checkNullables = true;
4369+
$this->checkUnionTypes = true;
4370+
$this->analyse([__DIR__ . '/data/bug-15306.php'], [
4371+
[
4372+
'Constants ZipArchive::RDONLY, ZipArchive::OVERWRITE cannot be combined for parameter #2 $flags of method ZipArchive::open().',
4373+
7,
4374+
],
4375+
]);
4376+
}
4377+
43644378
public function testBug14893(): void
43654379
{
43664380
$this->checkThisOnly = false;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php // lint >= 8.0
2+
3+
namespace Bug15306Methods;
4+
5+
function (\ZipArchive $zip): void {
6+
// ZipArchive::RDONLY is defined only in PHP8+
7+
$zip->open('foo.zip', \ZipArchive::RDONLY | \ZipArchive::OVERWRITE);
8+
$zip->open('foo.zip', \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
9+
$zip->open('foo.zip', \ZipArchive::RDONLY | \ZipArchive::CHECKCONS);
10+
};

0 commit comments

Comments
 (0)