Skip to content

Commit e3cf399

Browse files
phpstan-botVincentLangletclaude
authored
Type DOMDocument load/save/validate string arguments as non-empty-string on PHP 8 (#6075)
Co-authored-by: VincentLanglet <9052536+VincentLanglet@users.noreply.github.com> Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 8a28718 commit e3cf399

4 files changed

Lines changed: 94 additions & 1 deletion

File tree

‎resources/functionMap_php80delta_bleedingEdge.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,29 @@
44
'new' => [
55
'array_rand' => ['int|string|array<int,int>|array<int,string>', 'input'=>'non-empty-array', 'num_req'=>'positive-int'],
66
'array_rand\'1' => ['int|string', 'input'=>'non-empty-array'],
7+
'DOMDocument::load' => ['bool', 'filename'=>'non-empty-string', 'options='=>'int'],
8+
'DOMDocument::loadHTML' => ['bool', 'source'=>'non-empty-string', 'options='=>'int'],
9+
'DOMDocument::loadHTMLFile' => ['bool', 'filename'=>'non-empty-string', 'options='=>'int'],
10+
'DOMDocument::loadXML' => ['bool', 'source'=>'non-empty-string', 'options='=>'int'],
11+
'DOMDocument::relaxNGValidate' => ['bool', 'filename'=>'non-empty-string'],
12+
'DOMDocument::relaxNGValidateSource' => ['bool', 'source'=>'non-empty-string'],
13+
'DOMDocument::save' => ['int|false', 'filename'=>'non-empty-string', 'options='=>'int'],
14+
'DOMDocument::saveHTMLFile' => ['int|false', 'filename'=>'non-empty-string'],
15+
'DOMDocument::schemaValidate' => ['bool', 'filename'=>'non-empty-string', 'flags='=>'int'],
16+
'DOMDocument::schemaValidateSource' => ['bool', 'source'=>'non-empty-string', 'flags='=>'int'],
717
],
818
'old' => [
919
'array_rand' => ['int|string|array<int,int>|array<int,string>', 'input'=>'array', 'num_req'=>'int'],
1020
'array_rand\'1' => ['int|string', 'input'=>'array'],
21+
'DOMDocument::load' => ['bool', 'filename'=>'string', 'options='=>'int'],
22+
'DOMDocument::loadHTML' => ['bool', 'source'=>'string', 'options='=>'int'],
23+
'DOMDocument::loadHTMLFile' => ['bool', 'filename'=>'string', 'options='=>'int'],
24+
'DOMDocument::loadXML' => ['bool', 'source'=>'string', 'options='=>'int'],
25+
'DOMDocument::relaxNGValidate' => ['bool', 'filename'=>'string'],
26+
'DOMDocument::relaxNGValidateSource' => ['bool', 'source'=>'string'],
27+
'DOMDocument::save' => ['int|false', 'filename'=>'string', 'options='=>'int'],
28+
'DOMDocument::saveHTMLFile' => ['int|false', 'filename'=>'string'],
29+
'DOMDocument::schemaValidate' => ['bool', 'filename'=>'string', 'flags='=>'int'],
30+
'DOMDocument::schemaValidateSource' => ['bool', 'source'=>'string', 'flags='=>'int'],
1131
],
1232
];

‎tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,9 @@ public function testFormatErrors(
152152
);
153153

154154
$xml = new DOMDocument();
155-
$xml->loadXML($this->getOutputContent());
155+
$outputContent = $this->getOutputContent();
156+
$this->assertNotSame('', $outputContent);
157+
$xml->loadXML($outputContent);
156158

157159
$this->assertTrue(
158160
$xml->schemaValidate(__DIR__ . '/junit-schema.xsd'),

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4267,4 +4267,54 @@ public function testBug14893(): void
42674267
]);
42684268
}
42694269

4270+
#[RequiresPhp('>= 8.0.0')]
4271+
public function testBug14979(): void
4272+
{
4273+
$this->checkThisOnly = false;
4274+
$this->checkNullables = true;
4275+
$this->checkUnionTypes = true;
4276+
$this->analyse([__DIR__ . '/data/bug-14979.php'], [
4277+
[
4278+
"Parameter #1 \$filename of method DOMDocument::load() expects non-empty-string, '' given.",
4279+
7,
4280+
],
4281+
[
4282+
"Parameter #1 \$source of method DOMDocument::loadXML() expects non-empty-string, '' given.",
4283+
8,
4284+
],
4285+
[
4286+
"Parameter #1 \$source of method DOMDocument::loadHTML() expects non-empty-string, '' given.",
4287+
9,
4288+
],
4289+
[
4290+
"Parameter #1 \$filename of method DOMDocument::loadHTMLFile() expects non-empty-string, '' given.",
4291+
10,
4292+
],
4293+
[
4294+
"Parameter #1 \$filename of method DOMDocument::save() expects non-empty-string, '' given.",
4295+
11,
4296+
],
4297+
[
4298+
"Parameter #1 \$filename of method DOMDocument::saveHTMLFile() expects non-empty-string, '' given.",
4299+
12,
4300+
],
4301+
[
4302+
"Parameter #1 \$filename of method DOMDocument::schemaValidate() expects non-empty-string, '' given.",
4303+
13,
4304+
],
4305+
[
4306+
"Parameter #1 \$source of method DOMDocument::schemaValidateSource() expects non-empty-string, '' given.",
4307+
14,
4308+
],
4309+
[
4310+
"Parameter #1 \$filename of method DOMDocument::relaxNGValidate() expects non-empty-string, '' given.",
4311+
15,
4312+
],
4313+
[
4314+
"Parameter #1 \$source of method DOMDocument::relaxNGValidateSource() expects non-empty-string, '' given.",
4315+
16,
4316+
],
4317+
]);
4318+
}
4319+
42704320
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14979;
4+
5+
function doFoo(\DOMDocument $doc): void
6+
{
7+
$doc->load('');
8+
$doc->loadXML('');
9+
$doc->loadHTML('');
10+
$doc->loadHTMLFile('');
11+
$doc->save('');
12+
$doc->saveHTMLFile('');
13+
$doc->schemaValidate('');
14+
$doc->schemaValidateSource('');
15+
$doc->relaxNGValidate('');
16+
$doc->relaxNGValidateSource('');
17+
18+
// valid, non-empty strings are accepted
19+
$doc->loadHTML('<html></html>');
20+
$doc->loadXML('<root/>');
21+
}

0 commit comments

Comments
 (0)