Skip to content

Commit 80e0e7b

Browse files
authored
Avoid warning about non-nullable class-property in traits. (#6100)
1 parent 03fb80d commit 80e0e7b

9 files changed

Lines changed: 332 additions & 32 deletions

File tree

‎src/Analyser/ExpressionResult.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public function getBeforeScope(): MutatingScope
5454
return $this->beforeScope;
5555
}
5656

57+
public function getExpr(): Expr
58+
{
59+
return $this->expr;
60+
}
61+
5762
public function hasYield(): bool
5863
{
5964
return $this->hasYield;

‎src/Rules/Variables/EmptyRule.php‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace PHPStan\Rules\Variables;
44

55
use PhpParser\Node;
6+
use PHPStan\Analyser\CollectedDataEmitter;
7+
use PHPStan\Analyser\NodeCallbackInvoker;
68
use PHPStan\Analyser\Scope;
79
use PHPStan\DependencyInjection\RegisteredRule;
810
use PHPStan\Node\EmptyExpressionNode;
11+
use PHPStan\Rules\Comparison\ConstantConditionInTraitHelper;
912
use PHPStan\Rules\IssetCheck;
1013
use PHPStan\Rules\Rule;
1114
use PHPStan\Type\Type;
@@ -17,7 +20,10 @@
1720
final class EmptyRule implements Rule
1821
{
1922

20-
public function __construct(private IssetCheck $issetCheck)
23+
public function __construct(
24+
private IssetCheck $issetCheck,
25+
private ConstantConditionInTraitHelper $constantConditionInTraitHelper,
26+
)
2127
{
2228
}
2329

@@ -26,9 +32,10 @@ public function getNodeType(): string
2632
return EmptyExpressionNode::class;
2733
}
2834

29-
public function processNode(Node $node, Scope $scope): array
35+
public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataEmitter $scope): array
3036
{
31-
$error = $this->issetCheck->check($node->getExprResult(), $scope, 'in empty()', 'empty', static function (Type $type): ?string {
37+
$exprResult = $node->getExprResult();
38+
$error = $this->issetCheck->check($exprResult, $scope, 'in empty()', 'empty', static function (Type $type): ?string {
3239
$isNull = $type->isNull();
3340
if ($isNull->maybe()) {
3441
return null;
@@ -61,6 +68,14 @@ public function processNode(Node $node, Scope $scope): array
6168
});
6269

6370
if ($error === null) {
71+
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $exprResult->getExpr());
72+
return [];
73+
}
74+
75+
if ($scope->isInTrait()) {
76+
// IssetCheck's message already distinguishes the possible outcomes,
77+
// so the contexts only need to be told apart by error/no error.
78+
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $exprResult->getExpr(), true, $error);
6479
return [];
6580
}
6681

‎src/Rules/Variables/IssetRule.php‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace PHPStan\Rules\Variables;
44

55
use PhpParser\Node;
6+
use PHPStan\Analyser\CollectedDataEmitter;
7+
use PHPStan\Analyser\NodeCallbackInvoker;
68
use PHPStan\Analyser\Scope;
79
use PHPStan\DependencyInjection\RegisteredRule;
810
use PHPStan\Node\IssetExpressionNode;
11+
use PHPStan\Rules\Comparison\ConstantConditionInTraitHelper;
912
use PHPStan\Rules\IssetCheck;
1013
use PHPStan\Rules\Rule;
1114
use PHPStan\Type\Type;
@@ -17,7 +20,10 @@
1720
final class IssetRule implements Rule
1821
{
1922

20-
public function __construct(private IssetCheck $issetCheck)
23+
public function __construct(
24+
private IssetCheck $issetCheck,
25+
private ConstantConditionInTraitHelper $constantConditionInTraitHelper,
26+
)
2127
{
2228
}
2329

@@ -26,7 +32,7 @@ public function getNodeType(): string
2632
return IssetExpressionNode::class;
2733
}
2834

29-
public function processNode(Node $node, Scope $scope): array
35+
public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataEmitter $scope): array
3036
{
3137
$messages = [];
3238
foreach ($node->getVarResults() as $varResult) {
@@ -43,8 +49,17 @@ public function processNode(Node $node, Scope $scope): array
4349
return 'is not nullable';
4450
});
4551
if ($error === null) {
52+
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $varResult->getExpr());
4653
continue;
4754
}
55+
56+
if ($scope->isInTrait()) {
57+
// IssetCheck's message already distinguishes the possible outcomes,
58+
// so the contexts only need to be told apart by error/no error.
59+
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $varResult->getExpr(), true, $error);
60+
continue;
61+
}
62+
4863
$messages[] = $error;
4964
}
5065

‎src/Rules/Variables/NullCoalesceRule.php‎

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

55
use PhpParser\Node;
6+
use PHPStan\Analyser\CollectedDataEmitter;
7+
use PHPStan\Analyser\NodeCallbackInvoker;
68
use PHPStan\Analyser\Scope;
79
use PHPStan\DependencyInjection\AutowiredParameter;
810
use PHPStan\DependencyInjection\RegisteredRule;
911
use PHPStan\Node\CoalesceExpressionNode;
12+
use PHPStan\Rules\Comparison\ConstantConditionInTraitHelper;
1013
use PHPStan\Rules\IdentifierRuleError;
1114
use PHPStan\Rules\IssetCheck;
1215
use PHPStan\Rules\Rule;
@@ -23,6 +26,7 @@ final class NullCoalesceRule implements Rule
2326

2427
public function __construct(
2528
private IssetCheck $issetCheck,
29+
private ConstantConditionInTraitHelper $constantConditionInTraitHelper,
2630
#[AutowiredParameter(ref: '%featureToggles.unnecessaryNullCoalesce%')]
2731
private bool $unnecessaryNullCoalesce,
2832
)
@@ -34,10 +38,11 @@ public function getNodeType(): string
3438
return CoalesceExpressionNode::class;
3539
}
3640

37-
public function processNode(Node $node, Scope $scope): array
41+
public function processNode(Node $node, Scope&NodeCallbackInvoker&CollectedDataEmitter $scope): array
3842
{
43+
$subjectResult = $node->getSubjectResult();
3944
$error = $this->issetCheck->check(
40-
$node->getSubjectResult(),
45+
$subjectResult,
4146
$scope,
4247
$node->getOperatorDescription(),
4348
'nullCoalesce',
@@ -53,18 +58,21 @@ static function (Type $type): ?string {
5358

5459
return 'is not nullable';
5560
},
56-
);
61+
) ?? $this->checkUnnecessaryNullCoalesce($node, $scope);
5762

58-
if ($error !== null) {
59-
return [$error];
63+
if ($error === null) {
64+
$this->constantConditionInTraitHelper->emitNoError(self::class, $scope, $subjectResult->getExpr());
65+
return [];
6066
}
6167

62-
$unnecessaryError = $this->checkUnnecessaryNullCoalesce($node, $scope);
63-
if ($unnecessaryError !== null) {
64-
return [$unnecessaryError];
68+
if ($scope->isInTrait()) {
69+
// The error messages already distinguish the possible outcomes,
70+
// so the contexts only need to be told apart by error/no error.
71+
$this->constantConditionInTraitHelper->emitError(self::class, $scope, $subjectResult->getExpr(), true, $error);
72+
return [];
6573
}
6674

67-
return [];
75+
return [$error];
6876
}
6977

7078
private function checkUnnecessaryNullCoalesce(CoalesceExpressionNode $node, Scope $scope): ?IdentifierRuleError

‎tests/PHPStan/Rules/Variables/EmptyRuleTest.php‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
namespace PHPStan\Rules\Variables;
44

5+
use PHPStan\Rules\Comparison\ConstantConditionInTraitHelper;
6+
use PHPStan\Rules\Comparison\ConstantConditionInTraitRule;
57
use PHPStan\Rules\IssetCheck;
68
use PHPStan\Rules\Properties\PropertyDescriptor;
79
use PHPStan\Rules\Rule;
10+
use PHPStan\Testing\CompositeRule;
811
use PHPStan\Testing\RuleTestCase;
912
use PHPUnit\Framework\Attributes\DataProvider;
1013
use PHPUnit\Framework\Attributes\RequiresPhp;
1114

1215
/**
13-
* @extends RuleTestCase<EmptyRule>
16+
* @extends RuleTestCase<CompositeRule>
1417
*/
1518
class EmptyRuleTest extends RuleTestCase
1619
{
@@ -19,11 +22,18 @@ class EmptyRuleTest extends RuleTestCase
1922

2023
protected function getRule(): Rule
2124
{
22-
return new EmptyRule(new IssetCheck(
23-
new PropertyDescriptor(),
24-
true,
25-
$this->treatPhpDocTypesAsCertain,
26-
));
25+
// @phpstan-ignore argument.type
26+
return new CompositeRule([
27+
new EmptyRule(
28+
new IssetCheck(
29+
new PropertyDescriptor(),
30+
true,
31+
$this->treatPhpDocTypesAsCertain,
32+
),
33+
self::getContainer()->getByType(ConstantConditionInTraitHelper::class),
34+
),
35+
new ConstantConditionInTraitRule(),
36+
]);
2737
}
2838

2939
protected function shouldTreatPhpDocTypesAsCertain(): bool
@@ -274,4 +284,24 @@ public function testPropertyInitializationCustomSerialization(): void
274284
]);
275285
}
276286

287+
public function testInTrait(): void
288+
{
289+
$this->treatPhpDocTypesAsCertain = true;
290+
291+
$this->analyse([__DIR__ . '/data/isset-in-trait.php'], [
292+
[
293+
'Property IssetInTrait\FirstNonNullableProperty::$k (int<1, max>) in empty() is not falsy.',
294+
84,
295+
],
296+
[
297+
'Property IssetInTrait\SecondNonNullableProperty::$k (int<1, max>) in empty() is not falsy.',
298+
84,
299+
],
300+
[
301+
'Variable $s in empty() always exists and is not falsy.',
302+
121,
303+
],
304+
]);
305+
}
306+
277307
}

‎tests/PHPStan/Rules/Variables/IssetRuleTest.php‎

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
namespace PHPStan\Rules\Variables;
44

5+
use PHPStan\Rules\Comparison\ConstantConditionInTraitHelper;
6+
use PHPStan\Rules\Comparison\ConstantConditionInTraitRule;
57
use PHPStan\Rules\IssetCheck;
68
use PHPStan\Rules\Properties\PropertyDescriptor;
79
use PHPStan\Rules\Rule;
10+
use PHPStan\Testing\CompositeRule;
811
use PHPStan\Testing\RuleTestCase;
912
use PHPUnit\Framework\Attributes\RequiresPhp;
1013

1114
/**
12-
* @extends RuleTestCase<IssetRule>
15+
* @extends RuleTestCase<CompositeRule>
1316
*/
1417
class IssetRuleTest extends RuleTestCase
1518
{
@@ -18,11 +21,18 @@ class IssetRuleTest extends RuleTestCase
1821

1922
protected function getRule(): Rule
2023
{
21-
return new IssetRule(new IssetCheck(
22-
new PropertyDescriptor(),
23-
true,
24-
$this->treatPhpDocTypesAsCertain,
25-
));
24+
// @phpstan-ignore argument.type
25+
return new CompositeRule([
26+
new IssetRule(
27+
new IssetCheck(
28+
new PropertyDescriptor(),
29+
true,
30+
$this->treatPhpDocTypesAsCertain,
31+
),
32+
self::getContainer()->getByType(ConstantConditionInTraitHelper::class),
33+
),
34+
new ConstantConditionInTraitRule(),
35+
]);
2636
}
2737

2838
protected function shouldTreatPhpDocTypesAsCertain(): bool
@@ -634,4 +644,31 @@ public function testNullCoalesceAssignRightSideScope(): void
634644
$this->analyse([__DIR__ . '/data/null-coalesce-assign-right-side-scope.php'], []);
635645
}
636646

647+
public function testInTrait(): void
648+
{
649+
$this->treatPhpDocTypesAsCertain = true;
650+
651+
$this->analyse([__DIR__ . '/data/isset-in-trait.php'], [
652+
[
653+
'Property IssetInTrait\FirstNonNullableProperty::$k (int<1, max>) in isset() is not nullable.',
654+
83,
655+
],
656+
[
657+
'Property IssetInTrait\SecondNonNullableProperty::$k (int<1, max>) in isset() is not nullable.',
658+
83,
659+
],
660+
[
661+
'Variable $s in isset() always exists and is not nullable.',
662+
120,
663+
],
664+
]);
665+
}
666+
667+
public function testBug14416(): void
668+
{
669+
$this->treatPhpDocTypesAsCertain = true;
670+
671+
$this->analyse([__DIR__ . '/data/bug-14416.php'], []);
672+
}
673+
637674
}

‎tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php‎

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,37 @@
22

33
namespace PHPStan\Rules\Variables;
44

5+
use PHPStan\Rules\Comparison\ConstantConditionInTraitHelper;
6+
use PHPStan\Rules\Comparison\ConstantConditionInTraitRule;
57
use PHPStan\Rules\IssetCheck;
68
use PHPStan\Rules\Properties\PropertyDescriptor;
79
use PHPStan\Rules\Rule;
10+
use PHPStan\Testing\CompositeRule;
811
use PHPStan\Testing\RuleTestCase;
912
use PHPUnit\Framework\Attributes\RequiresPhp;
1013
use const PHP_VERSION_ID;
1114

1215
/**
13-
* @extends RuleTestCase<NullCoalesceRule>
16+
* @extends RuleTestCase<CompositeRule>
1417
*/
1518
class NullCoalesceRuleTest extends RuleTestCase
1619
{
1720

1821
protected function getRule(): Rule
1922
{
20-
return new NullCoalesceRule(new IssetCheck(
21-
new PropertyDescriptor(),
22-
true,
23-
$this->shouldTreatPhpDocTypesAsCertain(),
24-
), true);
23+
// @phpstan-ignore argument.type
24+
return new CompositeRule([
25+
new NullCoalesceRule(
26+
new IssetCheck(
27+
new PropertyDescriptor(),
28+
true,
29+
$this->shouldTreatPhpDocTypesAsCertain(),
30+
),
31+
self::getContainer()->getByType(ConstantConditionInTraitHelper::class),
32+
true,
33+
),
34+
new ConstantConditionInTraitRule(),
35+
]);
2536
}
2637

2738
public function testCoalesceRule(): void
@@ -639,4 +650,22 @@ public function testBug15046(): void
639650
]);
640651
}
641652

653+
public function testInTrait(): void
654+
{
655+
$this->analyse([__DIR__ . '/data/isset-in-trait.php'], [
656+
[
657+
'Property IssetInTrait\FirstNonNullableProperty::$k (int<1, max>) on left side of ?? is not nullable.',
658+
82,
659+
],
660+
[
661+
'Property IssetInTrait\SecondNonNullableProperty::$k (int<1, max>) on left side of ?? is not nullable.',
662+
82,
663+
],
664+
[
665+
'Variable $s on left side of ?? always exists and is not nullable.',
666+
119,
667+
],
668+
]);
669+
}
670+
642671
}

0 commit comments

Comments
 (0)