Skip to content

Commit 10efc47

Browse files
committed
Test ImpossibleCheckType rules
1 parent edd1acc commit 10efc47

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\BeberleiAssert;
4+
5+
use PHPStan\Rules\Comparison\ImpossibleCheckTypeMethodCallRule;
6+
use PHPStan\Rules\Rule;
7+
8+
class ImpossibleCheckTypeMethodCallRuleTest extends \PHPStan\Testing\RuleTestCase
9+
{
10+
11+
protected function getRule(): Rule
12+
{
13+
return new ImpossibleCheckTypeMethodCallRule(true);
14+
}
15+
16+
/**
17+
* @return \PHPStan\Type\MethodTypeSpecifyingExtension[]
18+
*/
19+
protected function getMethodTypeSpecifyingExtensions(): array
20+
{
21+
return [
22+
new AssertionChainTypeSpecifyingExtension(),
23+
];
24+
}
25+
26+
/**
27+
* @return \PHPStan\Type\DynamicMethodReturnTypeExtension[]
28+
*/
29+
public function getDynamicMethodReturnTypeExtensions(): array
30+
{
31+
return [
32+
new AssertionChainDynamicReturnTypeExtension(),
33+
];
34+
}
35+
36+
/**
37+
* @return \PHPStan\Type\DynamicStaticMethodReturnTypeExtension[]
38+
*/
39+
public function getDynamicStaticMethodReturnTypeExtensions(): array
40+
{
41+
return [
42+
new AssertThatDynamicMethodReturnTypeExtension(),
43+
];
44+
}
45+
46+
public function testExtension(): void
47+
{
48+
self::markTestIncomplete('Does not work yet - needs to rework ImpossibleCheckType rules, dynamic return type extension now has priority when resolving return type.');
49+
$this->analyse([__DIR__ . '/data/impossible-check.php'], []);
50+
}
51+
52+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\BeberleiAssert;
4+
5+
use PHPStan\Rules\Comparison\ImpossibleCheckTypeStaticMethodCallRule;
6+
use PHPStan\Rules\Rule;
7+
8+
class ImpossibleCheckTypeStaticMethodCallRuleTest extends \PHPStan\Testing\RuleTestCase
9+
{
10+
11+
protected function getRule(): Rule
12+
{
13+
return new ImpossibleCheckTypeStaticMethodCallRule(true);
14+
}
15+
16+
/**
17+
* @return \PHPStan\Type\StaticMethodTypeSpecifyingExtension[]
18+
*/
19+
protected function getStaticMethodTypeSpecifyingExtensions(): array
20+
{
21+
return [
22+
new AssertTypeSpecifyingExtension(),
23+
];
24+
}
25+
26+
public function testExtension(): void
27+
{
28+
$this->analyse([__DIR__ . '/data/impossible-static-check.php'], [
29+
[
30+
'Call to static method Assert\Assertion::string() with string will always evaluate to true.',
31+
12,
32+
],
33+
[
34+
'Call to static method Assert\Assertion::allString() with array<string> will always evaluate to true.',
35+
14,
36+
],
37+
]);
38+
}
39+
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace ImpossibleCheck;
4+
5+
use Assert\Assert;
6+
7+
class Bar
8+
{
9+
10+
public function doBar(string $s)
11+
{
12+
Assert::that($s)->string();
13+
14+
/** @var string[] $strings */
15+
$strings = doFoo();
16+
Assert::that($strings)->all()->string();
17+
}
18+
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace ImpossibleCheck;
4+
5+
use Assert\Assertion;
6+
7+
class Foo
8+
{
9+
10+
public function doFoo(string $a, array $b)
11+
{
12+
Assertion::string($a);
13+
Assertion::allString($b);
14+
Assertion::allString($b);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)