Skip to content

Commit 3fd0dff

Browse files
committed
Move throw type test to dedicated test class
1 parent 322c15f commit 3fd0dff

4 files changed

Lines changed: 84 additions & 15 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\Exceptions;
4+
5+
use PHPStan\Rules\Rule;
6+
use PHPStan\ShouldNotHappenException;
7+
use PHPStan\Testing\RuleTestCase;
8+
9+
/**
10+
* @extends RuleTestCase<MissingCheckedExceptionInMethodThrowsRule>
11+
*/
12+
class DomDocumentCreateElementThrowTypeExtensionTest extends RuleTestCase
13+
{
14+
15+
protected function getRule(): Rule
16+
{
17+
return new MissingCheckedExceptionInMethodThrowsRule(
18+
new MissingCheckedExceptionInThrowsCheck(new DefaultExceptionTypeResolver(
19+
self::createReflectionProvider(),
20+
[],
21+
[ShouldNotHappenException::class],
22+
[],
23+
[],
24+
)),
25+
);
26+
}
27+
28+
public function testRule(): void
29+
{
30+
$this->analyse([__DIR__ . '/data/bug-13792.php'], [
31+
[
32+
'Method Bug13792\Foo::dynamicName() throws checked exception DOMException but it\'s missing from the PHPDoc @throws tag.',
33+
20,
34+
],
35+
[
36+
'Method Bug13792\Foo::invalidConstantName() throws checked exception DOMException but it\'s missing from the PHPDoc @throws tag.',
37+
25,
38+
],
39+
[
40+
'Method Bug13792\Foo::unions() throws checked exception DOMException but it\'s missing from the PHPDoc @throws tag.',
41+
36,
42+
],
43+
]);
44+
}
45+
46+
}

tests/PHPStan/Rules/Exceptions/MissingCheckedExceptionInMethodThrowsRuleTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ public function testRule(): void
6767
122,
6868
];
6969
}
70-
$errors[] = [
71-
'Method MissingExceptionMethodThrows\Foo::domCreateElementDoesThrow() throws checked exception DOMException but it\'s missing from the PHPDoc @throws tag.',
72-
133,
73-
];
7470
$this->analyse([__DIR__ . '/data/missing-exception-method-throws.php'], $errors);
7571
}
7672

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13792;
4+
5+
use DOMDocument;
6+
7+
class Foo
8+
{
9+
10+
public function validConstantNames(DOMDocument $doc): void
11+
{
12+
$doc->createElement('div');
13+
$doc->createElement('my-element');
14+
$doc->createElement('ns:tag');
15+
$doc->createElement('_private');
16+
}
17+
18+
public function dynamicName(DOMDocument $doc, string $name): void
19+
{
20+
$doc->createElement($name); // error
21+
}
22+
23+
public function invalidConstantName(DOMDocument $doc): void
24+
{
25+
$doc->createElement(''); // error
26+
}
27+
28+
/**
29+
* @param 'div'|'span' $validUnion
30+
* @param 'div'|'' $mixedUnion
31+
*/
32+
public function unions(DOMDocument $doc, string $validUnion, string $mixedUnion): void
33+
{
34+
$doc->createElement($validUnion);
35+
$doc->createElement($mixedUnion); // error
36+
}
37+
38+
}

tests/PHPStan/Rules/Exceptions/data/missing-exception-method-throws.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,4 @@ public function dateTimeModifyDoesThrows(\DateTime $dt, \DateTimeImmutable $dti,
122122
$dti->modify($m);
123123
}
124124

125-
public function domCreateElementDoesNotThrow(\DOMDocument $doc): void
126-
{
127-
$doc->createElement('div');
128-
$doc->createElement('my-element');
129-
}
130-
131-
public function domCreateElementDoesThrow(\DOMDocument $doc, string $name): void
132-
{
133-
$doc->createElement($name);
134-
}
135-
136125
}

0 commit comments

Comments
 (0)