Skip to content

Commit ac79922

Browse files
authored
enable symplify/phpstan-rules, apply static fixes (#597)
* enable phpstan rules * bump rector * [enum] extract phpunit attribute to decouple class names from code * [cleanup] AnnotationWithValueToAttributeRector to avoid using local node property * remove node traverser stop from AssertFuncCallToPHPUnitAssertRector * tidy up AssertFuncCallToPHPUnitAssertRector * fixup! tidy up AssertFuncCallToPHPUnitAssertRector
1 parent e27f4fc commit ac79922

File tree

21 files changed

+327
-207
lines changed

21 files changed

+327
-207
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"rector/rector-src": "dev-main",
1818
"rector/swiss-knife": "^1.0",
1919
"rector/type-perfect": "^2.1",
20+
"symplify/phpstan-rules": "^14.9.3",
2021
"symplify/phpstan-extensions": "^12.0",
2122
"symplify/vendor-patches": "^11.5",
2223
"tomasvotruba/class-leak": "^1.2",

phpstan.neon

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
includes:
2+
- vendor/symplify/phpstan-rules/config/symplify-rules.neon
3+
- vendor/symplify/phpstan-rules/config/rector-rules.neon
4+
15
parameters:
26
level: 8
37

@@ -40,4 +44,14 @@ parameters:
4044
- '#Doing instanceof PHPStan\\Type\\.+ is error\-prone and deprecated#'
4145

4246
- identifier: instanceof.alwaysTrue
43-
- identifier: assign.propertyType
47+
48+
# false positive
49+
-
50+
identifier: assign.propertyType
51+
message: '#Property PhpParser\\Node\\Identifier\:\:\$name \(non\-empty\-string\) does not accept string#'
52+
path: rules/CodeQuality/Rector/ClassMethod/ReplaceTestFunctionPrefixWithAttributeRector.php
53+
54+
# handle next
55+
-
56+
identifier: symplify.forbiddenNode
57+
message: '#switch#'

rector.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
'*/Fixture/*',
2222
'*/Expected/*',
2323

24-
// object types
24+
// object types must be string as class might be missing + to keep downgrade safe
2525
StringClassNameToClassConstantRector::class => [
26-
__DIR__ . '/src/NodeAnalyzer/TestsNodeAnalyzer.php',
2726
__DIR__ . '/config',
28-
__DIR__ . '/src/NodeFinder/DataProviderClassMethodFinder.php',
27+
__DIR__ . '/src/Enum',
2928
],
3029
])
3130
->withPhpSets()

rules-tests/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector/Fixture/some_class.php.inc renamed to rules-tests/AnnotationsToAttributes/Rector/Class_/AnnotationWithValueToAttributeRector/Fixture/some_test.php.inc

File renamed without changes.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class AssertInsideStaticClosure extends TestCase
8+
{
9+
public function some($response)
10+
{
11+
static function () use ($response) {
12+
assert((bool) $response);
13+
};
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
22+
23+
use PHPUnit\Framework\TestCase;
24+
25+
final class AssertInsideStaticClosure extends TestCase
26+
{
27+
public function some($response)
28+
{
29+
static function () use ($response) {
30+
\PHPUnit\Framework\Assert::assertTrue((bool) $response);
31+
};
32+
}
33+
}
34+
35+
?>

rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/skip_in_static_method.php.inc

Lines changed: 0 additions & 18 deletions
This file was deleted.

rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/skip_inside_static_closure.php.inc

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
4+
5+
final class SkipOutsideTestCase
6+
{
7+
public function some($response)
8+
{
9+
assert((bool) $response);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class StaticAssertInStaticMethodTest extends TestCase
8+
{
9+
private static function getExceptionMessage(string $fqcn): string
10+
{
11+
$exception = new $fqcn();
12+
assert($exception instanceof \Exception);
13+
14+
return $exception->getMessage();
15+
}
16+
}
17+
18+
?>
19+
-----
20+
<?php
21+
22+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture;
23+
24+
use PHPUnit\Framework\TestCase;
25+
26+
final class StaticAssertInStaticMethodTest extends TestCase
27+
{
28+
private static function getExceptionMessage(string $fqcn): string
29+
{
30+
$exception = new $fqcn();
31+
\PHPUnit\Framework\Assert::assertInstanceOf(\Exception::class, $exception);
32+
33+
return $exception->getMessage();
34+
}
35+
}
36+
37+
?>

rules/AnnotationsToAttributes/NodeFactory/RequiresAttributeFactory.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node\AttributeGroup;
88
use Rector\PhpAttribute\NodeFactory\PhpAttributeGroupFactory;
9+
use Rector\PHPUnit\Enum\PHPUnitAttribute;
910

1011
final readonly class RequiresAttributeFactory
1112
{
@@ -23,7 +24,7 @@ public function create(string $annotationValue): ?AttributeGroup
2324

2425
switch ($type) {
2526
case 'PHP':
26-
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresPhp';
27+
$attributeClass = PHPUnitAttribute::REQUIRES_PHP;
2728

2829
// only version is used, we need to prefix with >=
2930
if (is_string($attributeValue) && is_numeric($attributeValue[0])) {
@@ -33,7 +34,7 @@ public function create(string $annotationValue): ?AttributeGroup
3334
$attributeValue = [$attributeValue];
3435
break;
3536
case 'PHPUnit':
36-
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresPhpunit';
37+
$attributeClass = PHPUnitAttribute::REQUIRES_PHPUNIT;
3738

3839
// only version is used, we need to prefix with >=
3940
if (is_string($attributeValue) && is_numeric($attributeValue[0])) {
@@ -43,30 +44,30 @@ public function create(string $annotationValue): ?AttributeGroup
4344
$attributeValue = [$attributeValue];
4445
break;
4546
case 'OS':
46-
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresOperatingSystem';
47+
$attributeClass = PHPUnitAttribute::REQUIRES_OS;
4748
$attributeValue = [$attributeValue];
4849
break;
4950
case 'OSFAMILY':
50-
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresOperatingSystemFamily';
51+
$attributeClass = PHPUnitAttribute::REQUIRES_OS_FAMILY;
5152
$attributeValue = [$attributeValue];
5253
break;
5354
case 'function':
5455
if (str_contains((string) $attributeValue, '::')) {
55-
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresMethod';
56+
$attributeClass = PHPUnitAttribute::REQUIRES_METHOD;
5657
$attributeValue = explode('::', (string) $attributeValue);
5758
$attributeValue[0] .= '::class';
5859
} else {
59-
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresFunction';
60+
$attributeClass = PHPUnitAttribute::REQUIRES_FUNCTION;
6061
$attributeValue = [$attributeValue];
6162
}
6263

6364
break;
6465
case 'extension':
65-
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresPhpExtension';
66+
$attributeClass = PHPUnitAttribute::REQUIRES_PHP_EXTENSION;
6667
$attributeValue = explode(' ', (string) $attributeValue, 2);
6768
break;
6869
case 'setting':
69-
$attributeClass = 'PHPUnit\Framework\Attributes\RequiresSetting';
70+
$attributeClass = PHPUnitAttribute::REQUIRES_SETTING;
7071
$attributeValue = explode(' ', (string) $attributeValue, 2);
7172
break;
7273
default:

0 commit comments

Comments
 (0)