Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Psalm 5.0 #127

Merged
merged 6 commits into from Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 8 additions & 5 deletions composer.json
Expand Up @@ -9,16 +9,12 @@
"email": "github@muglug.com"
}
],
"config": {
"optimize-autoloader": true,
"sort-packages": true
},
"require": {
"php": "^7.1 || ^8.0",
"ext-simplexml": "*",
"composer/semver": "^1.4 || ^2.0 || ^3.0",
"composer/package-versions-deprecated": "^1.10",
"vimeo/psalm": "dev-master || dev-4.x || ^4.5 || ^5@beta"
"vimeo/psalm": "dev-master || dev-4.x || ^4.7.1 || ^5@beta || ^5.0"
},
"conflict": {
"phpunit/phpunit": "<7.5"
Expand Down Expand Up @@ -56,5 +52,12 @@
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"test": "codecept run -v"
},
"config": {
"optimize-autoloader": true,
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
}
}
}
2 changes: 1 addition & 1 deletion psalm.xml.dist
Expand Up @@ -11,7 +11,7 @@

<issueHandlers>
<LessSpecificReturnType errorLevel="info" />
<UnnecessaryVarAnnotation errorLevel="suppress" />
<UnnecessaryVarAnnotation errorLevel="suppress" />
</issueHandlers>

<plugins>
Expand Down
14 changes: 13 additions & 1 deletion src/Hooks/TestCaseHandler.php
Expand Up @@ -169,10 +169,15 @@ public static function afterStatementAnalysis(AfterClassLikeAnalysisEvent $event

foreach ($specials['dataProvider'] as $line => $provider) {
try {
// for older Psalm versions
/**
* @psalm-suppress InvalidClone
* @var CodeLocation
*/
$provider_docblock_location = clone $method_storage->location;
/** @psalm-suppress UnusedMethodCall */
$provider_docblock_location->setCommentLine($line);
} catch (Error $e) {
/** @var CodeLocation */
$provider_docblock_location = $method_storage->location->setCommentLine($line);
}

Expand Down Expand Up @@ -338,11 +343,18 @@ static function (
$provider_docblock_location
): void {
if ($is_optional) {
/** @psalm-suppress RedundantCondition */
if (method_exists($param_type, 'setPossiblyUndefined')) {
/** @var Union */
$param_type = $param_type->setPossiblyUndefined(true);
} else {
// for older Psalm versions
/**
* @psalm-suppress InvalidClone
* @var Union
*/
$param_type = clone $param_type;
/** @psalm-suppress InaccessibleProperty */
$param_type->possibly_undefined = true;
}
}
Expand Down
28 changes: 25 additions & 3 deletions tests/acceptance/Prophecy.feature
Expand Up @@ -74,7 +74,7 @@ Feature: Prophecy
When I run Psalm
Then I see no errors

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

Scenario: Argument::that() only accepts callable with boolean return type [Psalm 5]
Given I have the following code
"""
class MyTestCase extends TestCase
{
/** @return void */
public function testSomething() {
$_argument = Argument::that(function (): string {
return 'hello';
});
}
}
"""
And I have Psalm newer than "4.99" (because of "changed issue type")
When I run Psalm
Then I see these errors
| Type | Message |
| InvalidArgument | /Argument 1 of Prophecy\\Argument::that expects callable\(mixed...\):bool, (but )?(pure-)?Closure\(\):(string\(hello\)\|"hello"\|'hello') provided/ |
And I see no other errors


Scenario: prophesize() provided by ProphecyTrait is generic
Given I have the following code
"""
Expand Down
50 changes: 41 additions & 9 deletions tests/acceptance/TestCase.feature
Expand Up @@ -15,6 +15,13 @@ Feature: TestCase
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>
<issueHandlers>
<DeprecatedMethod>
<errorLevel type="suppress">
<referencedMethod name="PhpUnit\Framework\TestCase::prophesize"/>
</errorLevel>
</DeprecatedMethod>
</issueHandlers>
</psalm>
"""
And I have the following code preamble
Expand All @@ -38,8 +45,8 @@ Feature: TestCase
"""
When I run Psalm
Then I see these errors
| Type | Message |
| InvalidArgument | Argument 1 of NS\MyTestCase::expectException expects class-string<Throwable>, NS\MyTestCase::class provided |
| Type | Message |
| InvalidArgument | /Argument 1 of NS\\MyTestCase::expectException expects class-string<Throwable>, (but )?NS\\MyTestCase::class provided/ |
And I see no other errors

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

Scenario: Invalid dataset array is reported
Expand Down Expand Up @@ -469,8 +476,8 @@ Feature: TestCase
"""
When I run Psalm
Then I see these errors
| Type | Message |
| 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\}>\)/ |
| Type | Message |
| 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\})>\)/ |
And I see no other errors

Scenario: Referenced providers are not marked as unused
Expand Down Expand Up @@ -605,7 +612,7 @@ Feature: TestCase
When I run Psalm
Then I see no errors

Scenario: Provider omitting offsets is fine when test method has defaults for those params (specified as constants)
Scenario: Provider omitting offsets is fine when test method has defaults for those params (specified as constants) [Psalm 4]
Given I have the following code
"""
class MyTestCase extends TestCase
Expand All @@ -625,6 +632,31 @@ Feature: TestCase
}
}
"""
And I have Psalm older than "5.0" (because of "sealed shapes")
When I run Psalm
Then I see no errors

Scenario: Provider omitting offsets is fine when test method has defaults for those params (specified as constants) [Psalm 5]
Given I have the following code
"""
class MyTestCase extends TestCase
{
/** @var string */
const S = "s";
/** @return iterable<string,list{int,...}> */
public function provide() {
yield "data set name" => rand(0,1) ? [1] : [1, "ss"];
}
/**
* @return void
* @dataProvider provide
*/
public function testSomething(int $int, string $_str = self::S) {
$this->assertEquals(1, $int);
}
}
"""
And I have Psalm newer than "4.99" (because of "sealed shapes")
When I run Psalm
Then I see no errors

Expand Down Expand Up @@ -960,8 +992,8 @@ Feature: TestCase
"""
When I run Psalm
Then I see these errors
| Type | Message |
| 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\}>\)/ |
| Type | Message |
| 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\})>\)/ |
And I see no other errors

Scenario: Providers generating incompatible datasets for variadic tests are reported
Expand Down