Skip to content

Commit 24a02ee

Browse files
Update packages to use PHPStan v2
1 parent 1be180b commit 24a02ee

10 files changed

Lines changed: 70 additions & 48 deletions

composer.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,20 @@
2323
},
2424
"require": {
2525
"php": "^8.2",
26-
"ekino/phpstan-banned-code": "^1.0",
26+
"ekino/phpstan-banned-code": "^3.0",
2727
"ergebnis/phpstan-rules": "^2.5",
2828
"moxio/php-codesniffer-sniffs": "^2.6",
2929
"phpcsstandards/phpcsextra": "^1.0",
30-
"phpstan/phpstan": "^1.11",
31-
"phpstan/phpstan-phpunit": "^1.0",
32-
"phpstan/phpstan-strict-rules": "^1.1",
33-
"phpstan/phpstan-webmozart-assert": "^1.2",
30+
"phpstan/phpstan": "^2.1",
31+
"phpstan/phpstan-phpunit": "^2.0",
32+
"phpstan/phpstan-strict-rules": "^2.0",
33+
"phpstan/phpstan-webmozart-assert": "^2.0",
3434
"phpunit/phpunit": "^9.6.16",
35-
"rector/rector": "^1.2",
36-
"roave/no-floaters": "^1.5",
37-
"shipmonk/phpstan-rules": "^3.2",
35+
"rector/rector": "^2.2",
36+
"roave/no-floaters": "^1.13",
37+
"shipmonk/phpstan-rules": "^4.2",
3838
"slevomat/coding-standard": "^8.6",
39-
"squizlabs/php_codesniffer": "^3",
40-
"thecodingmachine/phpstan-strict-rules": "^1.0"
39+
"squizlabs/php_codesniffer": "^3"
4140
},
4241
"config": {
4342
"allow-plugins": {

phpstan.extension.neon

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ includes:
77
- %currentWorkingDirectory%/vendor/phpstan/phpstan-phpunit/rules.neon
88
- %currentWorkingDirectory%/vendor/phpstan/phpstan-strict-rules/rules.neon
99
- %currentWorkingDirectory%/vendor/roave/no-floaters/rules.neon
10-
- %currentWorkingDirectory%/vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
10+
#- %currentWorkingDirectory%/vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
1111

1212
rules:
1313
- AssoConnect\PHPStanRules\Rules\DateTimeMustNotBeUsedRule
@@ -51,6 +51,8 @@ parameters:
5151
'>', '>=', '<', '<=', '<=>', # checked by AllowComparingOnlyComparableTypesRule
5252
'===', '!==', '??' # valid with null involved
5353
]
54+
banned_code:
55+
non_ignorable: false
5456
ignoreErrors:
5557
-
5658
# Prevents using @return mixed[] for all test providers

src/Rector/rules.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;
77
use Rector\CodingStyle\Rector\Closure\StaticClosureRector;
88
use Rector\Config\RectorConfig;
9-
use Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector;
109
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
1110

1211
return static function (RectorConfig $rectorConfig): void {
@@ -16,8 +15,6 @@
1615
StaticClosureRector::class,
1716
// Code Quality
1817
LogicalToBooleanRector::class,
19-
// Strict
20-
BooleanInBooleanNotRuleFixerRector::class,
2118
// Type Declaration
2219
ReturnTypeFromStrictTypedCallRector::class,
2320
]);

src/Rules/DateTimeMustNotBeUsedRule.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Expr\New_;
99
use PhpParser\Node\Name\FullyQualified;
1010
use PHPStan\Analyser\Scope;
11+
use PHPStan\Rules\IdentifierRuleError;
1112
use PHPStan\Rules\Rule;
1213
use PHPStan\Rules\RuleErrorBuilder;
1314

@@ -24,7 +25,10 @@ public function getNodeType(): string
2425
return New_::class;
2526
}
2627

27-
/** @param New_ $node */
28+
/**
29+
* @param New_ $node
30+
* @return list<IdentifierRuleError>
31+
*/
2832
public function processNode(Node $node, Scope $scope): array
2933
{
3034
if (!$node->class instanceof FullyQualified) {
@@ -34,7 +38,11 @@ public function processNode(Node $node, Scope $scope): array
3438
$classString = $node->class->toCodeString();
3539

3640
if ('\DateTime' === $classString) {
37-
return [RuleErrorBuilder::message(self::MESSAGE)->tip(self::TIP)->build()];
41+
return [
42+
RuleErrorBuilder::message(self::MESSAGE)->tip(self::TIP)
43+
->identifier('assoconnect.dateTimeMustNotBeUsed')
44+
->build(),
45+
];
3846
}
3947
return [];
4048
}

src/Rules/EnforceHttpsLinksRule.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace AssoConnect\PHPStanRules\Rules;
66

77
use PhpParser\Node;
8+
use PHPStan\Rules\IdentifierRuleError;
89
use PHPStan\Rules\Rule;
9-
use PHPStan\Rules\RuleError;
1010
use PHPStan\Rules\RuleErrorBuilder;
1111

1212
/**
@@ -17,11 +17,15 @@ abstract class EnforceHttpsLinksRule implements Rule
1717
{
1818
public const MESSAGE = 'The string contains an insecure link';
1919

20-
/** @return RuleError[] errors */
20+
/** @return list<IdentifierRuleError> errors */
2121
protected function checkStringValue(string $value): array
2222
{
2323
if (false !== strpos($value, 'http' . ':')) {
24-
return [RuleErrorBuilder::message(self::MESSAGE)->build()];
24+
return [
25+
RuleErrorBuilder::message(self::MESSAGE)
26+
->identifier('assoconnect.insecureLink')
27+
->build(),
28+
];
2529
}
2630

2731
return[];

src/Rules/ForbidIdenticalClassComparisonRule.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
1212
use PHPStan\Analyser\Scope;
1313
use PHPStan\Reflection\ReflectionProvider;
14+
use PHPStan\Rules\IdentifierRuleError;
1415
use PHPStan\Rules\Rule;
15-
use PHPStan\Rules\RuleError;
1616
use PHPStan\Rules\RuleErrorBuilder;
17-
use PHPStan\Type\ArrayType;
1817
use PHPStan\Type\BooleanType;
1918
use PHPStan\Type\CallableType;
20-
use PHPStan\Type\Constant\ConstantBooleanType;
2119
use PHPStan\Type\FloatType;
2220
use PHPStan\Type\IntegerType;
2321
use PHPStan\Type\MixedType;
@@ -83,7 +81,7 @@ public function getNodeType(): string
8381

8482
/**
8583
* @param BinaryOp $node
86-
* @return list<RuleError>
84+
* @return list<IdentifierRuleError>
8785
*/
8886
public function processNode(Node $node, Scope $scope): array
8987
{
@@ -92,7 +90,7 @@ public function processNode(Node $node, Scope $scope): array
9290
}
9391

9492
$nodeType = $scope->getType($node);
95-
if ($nodeType instanceof ConstantBooleanType) {
93+
if ($nodeType->isTrue()->yes() || $nodeType->isFalse()->yes()) {
9694
return []; // always-true or always-false, already reported by native PHPStan (like $a === $a)
9795
}
9896

@@ -109,7 +107,9 @@ public function processNode(Node $node, Scope $scope): array
109107
$node->getOperatorSigil(),
110108
$leftType->describe(VerbosityLevel::typeOnly()),
111109
$rightType->describe(VerbosityLevel::typeOnly()),
112-
))->build(),
110+
))
111+
->identifier('assoconnect.identicalClassComparison')
112+
->build(),
113113
];
114114
}
115115

@@ -130,12 +130,14 @@ private function isAccepted(Type $firstArm, Type $otherArm): bool
130130
}
131131

132132

133-
if ($firstArm instanceof ArrayType) {
133+
$arrays = $firstArm->getArrays();
134+
if ([] !== $arrays) {
135+
$firstArray = $arrays[0];
134136
// Empty array
135-
if ($firstArm->isIterableAtLeastOnce()->no()) {
137+
if ($firstArray->isIterableAtLeastOnce()->no()) {
136138
return true;
137139
}
138-
return $this->isAccepted($firstArm->getIterableValueType(), $otherArm);
140+
return $this->isAccepted($firstArray->getIterableValueType(), $otherArm);
139141
}
140142

141143
if ($this->typeIsEnumOrNull($firstArm) && $this->typeIsEnumOrNull($otherArm)) {

tests/Rules/EnforceHttpsLinksInPhpDocRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testTruePositivesAreDetected(): void
2121
{
2222
$this->analyse([__DIR__ . '/EnforceHttpsLinksRule.file.php'], array_fill(
2323
0,
24-
5,
24+
1,
2525
[
2626
EnforceHttpsLinksRule::MESSAGE,
2727
16,

tests/Rules/FooBar.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AssoConnect\PHPStanRules\Tests\Rules;
6+
7+
enum FooBar: string
8+
{
9+
case FOO = 'FOO';
10+
case BAR = 'BAR';
11+
}
12+
13+
function enum(): FooBar
14+
{
15+
return FooBar::BAR;
16+
}
17+
function enumNullable(): ?FooBar
18+
{
19+
return random_int(0, 2) < 1 ? FooBar::BAR : null;
20+
}

tests/Rules/ForbidIdenticalClassComparisonRule.file.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// phpcs:ignoreFile
33
declare(strict_types=1);
44

5+
use AssoConnect\PHPStanRules\Tests\Rules\FooBar;
6+
use function AssoConnect\PHPStanRules\Tests\Rules\enum;
7+
use function AssoConnect\PHPStanRules\Tests\Rules\enumNullable;
8+
59
(new \DateTime()) === (new \DateTime());
610
[(new \DateTime())] === [(new \DateTime())];
711

@@ -44,20 +48,6 @@
4448
$arrayOfStrings2 = ['b'];
4549
$arrayOfStrings1 === $arrayOfStrings2;
4650

47-
enum FooBar: string
48-
{
49-
case FOO = 'FOO';
50-
case BAR = 'BAR';
51-
}
52-
function enum(): FooBar
53-
{
54-
return FooBar::BAR;
55-
}
56-
function enumNullable(): ?FooBar
57-
{
58-
return FooBar::BAR;
59-
}
60-
6151

6252
FooBar::FOO === enum();
6353
FooBar::FOO === enumNullable();

tests/Rules/ForbidIdenticalClassComparisonRuleTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ public function testTruePositivesAreDetected(): void
2424
$this->analyse([__DIR__ . '/ForbidIdenticalClassComparisonRule.file.php'], [
2525
[
2626
'Using === with DateTime and DateTime is denied',
27-
5,
27+
9,
2828
],
2929
[
3030
'Using === with array<int, DateTime> and array<int, DateTime> is denied',
31-
6,
31+
10,
3232
],
3333
[
3434
'Using === with string|false and string|false is denied',
35-
12,
35+
16,
3636
],
3737
[
3838
'Using === with DateTime|string and DateTime|string is denied',
39-
18,
39+
22,
4040
],
4141
[
4242
'Using === with int|string and int|string is denied',
43-
24,
43+
28,
4444
],
4545
]);
4646
}

0 commit comments

Comments
 (0)