Skip to content

Commit e4ab309

Browse files
authored
Merge pull request #127 from ddeboer/psalm-5
2 parents 057c1cd + df68e66 commit e4ab309

File tree

5 files changed

+88
-19
lines changed

5 files changed

+88
-19
lines changed

composer.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@
99
"email": "[email protected]"
1010
}
1111
],
12-
"config": {
13-
"optimize-autoloader": true,
14-
"sort-packages": true
15-
},
1612
"require": {
1713
"php": "^7.1 || ^8.0",
1814
"ext-simplexml": "*",
1915
"composer/semver": "^1.4 || ^2.0 || ^3.0",
2016
"composer/package-versions-deprecated": "^1.10",
21-
"vimeo/psalm": "dev-master || dev-4.x || ^4.5 || ^5@beta"
17+
"vimeo/psalm": "dev-master || dev-4.x || ^4.7.1 || ^5@beta || ^5.0"
2218
},
2319
"conflict": {
2420
"phpunit/phpunit": "<7.5"
@@ -56,5 +52,12 @@
5652
"cs-check": "phpcs",
5753
"cs-fix": "phpcbf",
5854
"test": "codecept run -v"
55+
},
56+
"config": {
57+
"optimize-autoloader": true,
58+
"sort-packages": true,
59+
"allow-plugins": {
60+
"composer/package-versions-deprecated": true
61+
}
5962
}
6063
}

psalm.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<issueHandlers>
1313
<LessSpecificReturnType errorLevel="info" />
14-
<UnnecessaryVarAnnotation errorLevel="suppress" />
14+
<UnnecessaryVarAnnotation errorLevel="suppress" />
1515
</issueHandlers>
1616

1717
<plugins>

src/Hooks/TestCaseHandler.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,15 @@ public static function afterStatementAnalysis(AfterClassLikeAnalysisEvent $event
169169

170170
foreach ($specials['dataProvider'] as $line => $provider) {
171171
try {
172+
// for older Psalm versions
173+
/**
174+
* @psalm-suppress InvalidClone
175+
* @var CodeLocation
176+
*/
172177
$provider_docblock_location = clone $method_storage->location;
178+
/** @psalm-suppress UnusedMethodCall */
173179
$provider_docblock_location->setCommentLine($line);
174180
} catch (Error $e) {
175-
/** @var CodeLocation */
176181
$provider_docblock_location = $method_storage->location->setCommentLine($line);
177182
}
178183

@@ -338,11 +343,18 @@ static function (
338343
$provider_docblock_location
339344
): void {
340345
if ($is_optional) {
346+
/** @psalm-suppress RedundantCondition */
341347
if (method_exists($param_type, 'setPossiblyUndefined')) {
342348
/** @var Union */
343349
$param_type = $param_type->setPossiblyUndefined(true);
344350
} else {
351+
// for older Psalm versions
352+
/**
353+
* @psalm-suppress InvalidClone
354+
* @var Union
355+
*/
345356
$param_type = clone $param_type;
357+
/** @psalm-suppress InaccessibleProperty */
346358
$param_type->possibly_undefined = true;
347359
}
348360
}

tests/acceptance/Prophecy.feature

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Feature: Prophecy
7474
When I run Psalm
7575
Then I see no errors
7676

77-
Scenario: Argument::that() only accepts callable with boolean return type
77+
Scenario: Argument::that() only accepts callable with boolean return type [Psalm 4]
7878
Given I have the following code
7979
"""
8080
class MyTestCase extends TestCase
@@ -87,12 +87,34 @@ Feature: Prophecy
8787
}
8888
}
8989
"""
90+
And I have Psalm older than "5.0" (because of "changed issue type")
9091
When I run Psalm
9192
Then I see these errors
92-
| Type | Message |
93-
| InvalidScalarArgument | /Argument 1 of Prophecy\\Argument::that expects callable\(mixed...\):bool, (pure-)?Closure\(\):(string\(hello\)\|"hello") provided/ |
93+
| Type | Message |
94+
| InvalidScalarArgument | /Argument 1 of Prophecy\\Argument::that expects callable\(mixed...\):bool, (but )?(pure-)?Closure\(\):(string\(hello\)\|"hello"\|'hello') provided/ |
9495
And I see no other errors
9596

97+
Scenario: Argument::that() only accepts callable with boolean return type [Psalm 5]
98+
Given I have the following code
99+
"""
100+
class MyTestCase extends TestCase
101+
{
102+
/** @return void */
103+
public function testSomething() {
104+
$_argument = Argument::that(function (): string {
105+
return 'hello';
106+
});
107+
}
108+
}
109+
"""
110+
And I have Psalm newer than "4.99" (because of "changed issue type")
111+
When I run Psalm
112+
Then I see these errors
113+
| Type | Message |
114+
| InvalidArgument | /Argument 1 of Prophecy\\Argument::that expects callable\(mixed...\):bool, (but )?(pure-)?Closure\(\):(string\(hello\)\|"hello"\|'hello') provided/ |
115+
And I see no other errors
116+
117+
96118
Scenario: prophesize() provided by ProphecyTrait is generic
97119
Given I have the following code
98120
"""

tests/acceptance/TestCase.feature

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ Feature: TestCase
1515
<plugins>
1616
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
1717
</plugins>
18+
<issueHandlers>
19+
<DeprecatedMethod>
20+
<errorLevel type="suppress">
21+
<referencedMethod name="PhpUnit\Framework\TestCase::prophesize"/>
22+
</errorLevel>
23+
</DeprecatedMethod>
24+
</issueHandlers>
1825
</psalm>
1926
"""
2027
And I have the following code preamble
@@ -38,8 +45,8 @@ Feature: TestCase
3845
"""
3946
When I run Psalm
4047
Then I see these errors
41-
| Type | Message |
42-
| InvalidArgument | Argument 1 of NS\MyTestCase::expectException expects class-string<Throwable>, NS\MyTestCase::class provided |
48+
| Type | Message |
49+
| InvalidArgument | /Argument 1 of NS\\MyTestCase::expectException expects class-string<Throwable>, (but )?NS\\MyTestCase::class provided/ |
4350
And I see no other errors
4451

4552
Scenario: TestCase::expectException() accepts throwables
@@ -421,8 +428,8 @@ Feature: TestCase
421428
"""
422429
When I run Psalm
423430
Then I see these errors
424-
| Type | Message |
425-
| InvalidArgument | /Argument 1 of NS\\MyTestCase::testSomething expects int, string provided by NS\\MyTestCase::provide\(\):\(iterable<string, array\{(0: )?string\}>\)/ |
431+
| Type | Message |
432+
| InvalidArgument | /Argument 1 of NS\\MyTestCase::testSomething expects int, string provided by NS\\MyTestCase::provide\(\):\(iterable<string, (array\{(0: )?string\}\|list\{string\})>\)/ |
426433
And I see no other errors
427434

428435
Scenario: Invalid dataset array is reported
@@ -469,8 +476,8 @@ Feature: TestCase
469476
"""
470477
When I run Psalm
471478
Then I see these errors
472-
| Type | Message |
473-
| TooFewArguments | /Too few arguments for NS\\MyTestCase::testSomething - expecting at least 2, but saw 1 provided by NS\\MyTestCase::provide\(\):\(iterable<string, array\{(0: )?int\}>\)/ |
479+
| Type | Message |
480+
| TooFewArguments | /Too few arguments for NS\\MyTestCase::testSomething - expecting at least 2, but saw 1 provided by NS\\MyTestCase::provide\(\):\(iterable<string, (array\{(0: )?int\}\|list\{int\})>\)/ |
474481
And I see no other errors
475482

476483
Scenario: Referenced providers are not marked as unused
@@ -605,7 +612,7 @@ Feature: TestCase
605612
When I run Psalm
606613
Then I see no errors
607614

608-
Scenario: Provider omitting offsets is fine when test method has defaults for those params (specified as constants)
615+
Scenario: Provider omitting offsets is fine when test method has defaults for those params (specified as constants) [Psalm 4]
609616
Given I have the following code
610617
"""
611618
class MyTestCase extends TestCase
@@ -625,6 +632,31 @@ Feature: TestCase
625632
}
626633
}
627634
"""
635+
And I have Psalm older than "5.0" (because of "sealed shapes")
636+
When I run Psalm
637+
Then I see no errors
638+
639+
Scenario: Provider omitting offsets is fine when test method has defaults for those params (specified as constants) [Psalm 5]
640+
Given I have the following code
641+
"""
642+
class MyTestCase extends TestCase
643+
{
644+
/** @var string */
645+
const S = "s";
646+
/** @return iterable<string,list{int,...}> */
647+
public function provide() {
648+
yield "data set name" => rand(0,1) ? [1] : [1, "ss"];
649+
}
650+
/**
651+
* @return void
652+
* @dataProvider provide
653+
*/
654+
public function testSomething(int $int, string $_str = self::S) {
655+
$this->assertEquals(1, $int);
656+
}
657+
}
658+
"""
659+
And I have Psalm newer than "4.99" (because of "sealed shapes")
628660
When I run Psalm
629661
Then I see no errors
630662

@@ -960,8 +992,8 @@ Feature: TestCase
960992
"""
961993
When I run Psalm
962994
Then I see these errors
963-
| Type | Message |
964-
| TooFewArguments | /Too few arguments for NS\\MyTestCase::testSomething - expecting at least 2, but saw 1 provided by NS\\MyTestCase::provide\(\):\(iterable<string, array\{(0: )?int\}>\)/ |
995+
| Type | Message |
996+
| TooFewArguments | /Too few arguments for NS\\MyTestCase::testSomething - expecting at least 2, but saw 1 provided by NS\\MyTestCase::provide\(\):\(iterable<string, (array\{(0: )?int\}\|list\{int\})>\)/ |
965997
And I see no other errors
966998

967999
Scenario: Providers generating incompatible datasets for variadic tests are reported

0 commit comments

Comments
 (0)