Skip to content

Commit 014a282

Browse files
Lint exclusions, unify implementations
1 parent d177206 commit 014a282

7 files changed

+48
-25
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ lint:
110110
--exclude tests/PHPStan/Rules/Classes/data/enum-cannot-be-attribute.php \
111111
--exclude tests/PHPStan/Rules/Classes/data/class-attributes.php \
112112
--exclude tests/PHPStan/Rules/Classes/data/enum-attributes.php \
113+
--exclude tests/PHPStan/Rules/Cast/data/void-cast.php \
114+
--exclude tests/PHPStan/Rules/Properties/data/property-hook-attributes.php \
113115
src tests
114116

115117
install-paratest:

build/collision-detector.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"../tests/PHPStan/Rules/Properties/data/final-property-hooks.php",
1919
"../tests/PHPStan/Rules/Functions/data/function-call-statement-result-discarded.php",
2020
"../tests/PHPStan/Rules/Methods/data/method-call-statement-result-discarded.php",
21-
"../tests/PHPStan/Rules/Methods/data/static-method-call-statement-result-discarded.php"
21+
"../tests/PHPStan/Rules/Methods/data/static-method-call-statement-result-discarded.php",
22+
"../tests/PHPStan/Rules/Properties/data/property-hook-attributes.php"
2223
]
2324
}

src/Rules/Methods/CallToMethodStatementWithNoDiscardRule.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PHPStan\Rules\Methods;
44

55
use PhpParser\Node;
6-
use PhpParser\Node\Expr\MethodCall;
76
use PHPStan\Analyser\NullsafeOperatorHelper;
87
use PHPStan\Analyser\Scope;
98
use PHPStan\DependencyInjection\RegisteredRule;
@@ -16,7 +15,7 @@
1615
use function sprintf;
1716

1817
/**
19-
* @implements Rule<MethodCall>
18+
* @implements Rule<Node\Stmt\Expression>
2019
*/
2120
#[RegisteredRule(level: 4)]
2221
final class CallToMethodStatementWithNoDiscardRule implements Rule
@@ -28,24 +27,29 @@ public function __construct(private RuleLevelHelper $ruleLevelHelper)
2827

2928
public function getNodeType(): string
3029
{
31-
// We can ignore NullsafeMethodCall because a virtual MethodCall will
32-
// also be processed
33-
return MethodCall::class;
30+
return Node\Stmt\Expression::class;
3431
}
3532

3633
public function processNode(Node $node, Scope $scope): array
3734
{
38-
if (!$node->name instanceof Node\Identifier) {
35+
if (!$node->expr instanceof Node\Expr\MethodCall
36+
&& !$node->expr instanceof Node\Expr\NullsafeMethodCall
37+
) {
3938
return [];
4039
}
41-
if ($node->hasAttribute(VoidCastVisitor::ATTRIBUTE_NAME)) {
40+
41+
$funcCall = $node->expr;
42+
if (!$funcCall->name instanceof Node\Identifier) {
43+
return [];
44+
}
45+
if ($funcCall->hasAttribute(VoidCastVisitor::ATTRIBUTE_NAME)) {
4246
return [];
4347
}
44-
$methodName = $node->name->toString();
48+
$methodName = $funcCall->name->toString();
4549

4650
$typeResult = $this->ruleLevelHelper->findTypeToCheck(
4751
$scope,
48-
NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $node->var),
52+
NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $funcCall->var),
4953
'',
5054
static fn (Type $type): bool => $type->canCallMethods()->yes() && $type->hasMethod($methodName)->yes(),
5155
);

src/Rules/Methods/CallToStaticMethodStatementWithNoDiscardRule.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace PHPStan\Rules\Methods;
44

55
use PhpParser\Node;
6-
use PhpParser\Node\Expr\StaticCall;
76
use PHPStan\Analyser\NullsafeOperatorHelper;
87
use PHPStan\Analyser\Scope;
98
use PHPStan\DependencyInjection\RegisteredRule;
@@ -18,7 +17,7 @@
1817
use function sprintf;
1918

2019
/**
21-
* @implements Rule<StaticCall>
20+
* @implements Rule<Node\Stmt\Expression>
2221
*/
2322
#[RegisteredRule(level: 4)]
2423
final class CallToStaticMethodStatementWithNoDiscardRule implements Rule
@@ -33,21 +32,26 @@ public function __construct(
3332

3433
public function getNodeType(): string
3534
{
36-
return Node\Expr\StaticCall::class;
35+
return Node\Stmt\Expression::class;
3736
}
3837

3938
public function processNode(Node $node, Scope $scope): array
4039
{
41-
if (!$node->name instanceof Node\Identifier) {
40+
if (!$node->expr instanceof Node\Expr\StaticCall) {
4241
return [];
4342
}
44-
if ($node->hasAttribute(VoidCastVisitor::ATTRIBUTE_NAME)) {
43+
44+
$funcCall = $node->expr;
45+
if (!$funcCall->name instanceof Node\Identifier) {
46+
return [];
47+
}
48+
if ($funcCall->hasAttribute(VoidCastVisitor::ATTRIBUTE_NAME)) {
4549
return [];
4650
}
4751

48-
$methodName = $node->name->toString();
49-
if ($node->class instanceof Node\Name) {
50-
$className = $scope->resolveName($node->class);
52+
$methodName = $funcCall->name->toString();
53+
if ($funcCall->class instanceof Node\Name) {
54+
$className = $scope->resolveName($funcCall->class);
5155
if (!$this->reflectionProvider->hasClass($className)) {
5256
return [];
5357
}
@@ -56,7 +60,7 @@ public function processNode(Node $node, Scope $scope): array
5660
} else {
5761
$typeResult = $this->ruleLevelHelper->findTypeToCheck(
5862
$scope,
59-
NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $node->class),
63+
NullsafeOperatorHelper::getNullsafeShortcircuitedExprRespectingScope($scope, $funcCall->class),
6064
'',
6165
static fn (Type $type): bool => $type->canCallMethods()->yes() && $type->hasMethod($methodName)->yes(),
6266
);

tests/PHPStan/Rules/Functions/data/function-call-statement-result-discarded.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
namespace FunctionCallStatementResultDiscarded;
44

55
#[\NoDiscard]
6-
function withSideEffects(): int {
6+
function withSideEffects(): array {
77
echo __FUNCTION__ . "\n";
8-
return 1;
8+
return [1];
99
}
1010

1111
withSideEffects();
1212

1313
(void)withSideEffects();
14+
15+
foreach (withSideEffects() as $num) {
16+
var_dump($num);
17+
}

tests/PHPStan/Rules/Methods/data/method-call-statement-result-discarded.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
class ClassWithInstanceSideEffects {
66
#[\NoDiscard]
7-
public function instanceMethod(): int {
7+
public function instanceMethod(): array {
88
echo __METHOD__ . "\n";
9-
return 2;
9+
return [2];
1010
}
1111
}
1212

@@ -16,3 +16,7 @@ public function instanceMethod(): int {
1616

1717
(void)$o->instanceMethod();
1818
(void)$o?->instanceMethod();
19+
20+
foreach ($o->instanceMethod() as $num) {
21+
var_dump($num);
22+
}

tests/PHPStan/Rules/Methods/data/static-method-call-statement-result-discarded.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
class ClassWithStaticSideEffects {
66
#[\NoDiscard]
7-
public static function staticMethod(): int {
7+
public static function staticMethod(): array {
88
echo __METHOD__ . "\n";
9-
return 2;
9+
return [2];
1010
}
1111
}
1212

1313
ClassWithStaticSideEffects::staticMethod();
1414

1515
(void)ClassWithStaticSideEffects::staticMethod();
16+
17+
foreach (ClassWithStaticSideEffects::staticMethod() as $num) {
18+
var_dump($num);
19+
}

0 commit comments

Comments
 (0)