Skip to content

Commit f8ad453

Browse files
Add support for PHP 8.5 #[\NoDiscard]
Co-authored-by: Ondrej Mirtes <ondrej@mirtes.cz>
1 parent ee5ee65 commit f8ad453

File tree

65 files changed

+1089
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1089
-5
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ jobs:
135135
uses: "shivammathur/setup-php@v2"
136136
with:
137137
coverage: "none"
138-
php-version: "8.4"
138+
php-version: "8.5"
139139

140140
- name: "Install dependencies"
141141
run: "composer install --no-interaction --no-progress"

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ 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-nodiscard.php \
115+
--exclude tests/PHPStan/Rules/Functions/data/arrow-function-typehints-nodiscard.php \
116+
--exclude tests/PHPStan/Rules/Functions/data/closure-typehints-nodiscard.php \
117+
--exclude tests/PHPStan/Rules/Functions/data/typehints-nodiscard.php \
118+
--exclude tests/PHPStan/Rules/Methods/data/typehints-nodiscard.php \
113119
src tests
114120

115121
install-paratest:

build/collision-detector.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"../tests/PHPStan/Rules/Functions/data/define-bug-3349.php",
1616
"../tests/PHPStan/Levels/data/stubs/function.php",
1717
"../tests/PHPStan/Rules/Properties/data/abstract-final-property-hook-parse-error.php",
18-
"../tests/PHPStan/Rules/Properties/data/final-property-hooks.php"
18+
"../tests/PHPStan/Rules/Properties/data/final-property-hooks.php",
19+
"../tests/PHPStan/Rules/Cast/data/void-cast.php"
1920
]
2021
}

src/Analyser/MutatingScope.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,6 +1442,16 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
14421442
if (array_key_exists($cacheKey, $cachedTypes)) {
14431443
$cachedClosureData = $cachedTypes[$cacheKey];
14441444

1445+
$mustUseReturnValue = TrinaryLogic::createNo();
1446+
foreach ($node->attrGroups as $attrGroup) {
1447+
foreach ($attrGroup->attrs as $attr) {
1448+
if ($attr->name->toLowerString() === 'nodiscard') {
1449+
$mustUseReturnValue = TrinaryLogic::createYes();
1450+
break;
1451+
}
1452+
}
1453+
}
1454+
14451455
return new ClosureType(
14461456
$parameters,
14471457
$cachedClosureData['returnType'],
@@ -1454,6 +1464,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
14541464
invalidateExpressions: $cachedClosureData['invalidateExpressions'],
14551465
usedVariables: $cachedClosureData['usedVariables'],
14561466
acceptsNamedArguments: TrinaryLogic::createYes(),
1467+
mustUseReturnValue: $mustUseReturnValue,
14571468
);
14581469
}
14591470
if (self::$resolveClosureTypeDepth >= 2) {
@@ -1656,6 +1667,16 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
16561667
];
16571668
$node->setAttribute('phpstanCachedTypes', $cachedTypes);
16581669

1670+
$mustUseReturnValue = TrinaryLogic::createNo();
1671+
foreach ($node->attrGroups as $attrGroup) {
1672+
foreach ($attrGroup->attrs as $attr) {
1673+
if ($attr->name->toLowerString() === 'nodiscard') {
1674+
$mustUseReturnValue = TrinaryLogic::createYes();
1675+
break;
1676+
}
1677+
}
1678+
}
1679+
16591680
return new ClosureType(
16601681
$parameters,
16611682
$returnType,
@@ -1668,6 +1689,7 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
16681689
invalidateExpressions: $invalidateExpressions,
16691690
usedVariables: $usedVariables,
16701691
acceptsNamedArguments: TrinaryLogic::createYes(),
1692+
mustUseReturnValue: $mustUseReturnValue,
16711693
);
16721694
} elseif ($node instanceof New_) {
16731695
if ($node->class instanceof Name) {
@@ -2716,10 +2738,12 @@ private function createFirstClassCallable(
27162738
$throwPoints = [];
27172739
$impurePoints = [];
27182740
$acceptsNamedArguments = TrinaryLogic::createYes();
2741+
$mustUseReturnValue = TrinaryLogic::createMaybe();
27192742
if ($variant instanceof CallableParametersAcceptor) {
27202743
$throwPoints = $variant->getThrowPoints();
27212744
$impurePoints = $variant->getImpurePoints();
27222745
$acceptsNamedArguments = $variant->acceptsNamedArguments();
2746+
$mustUseReturnValue = $variant->mustUseReturnValue();
27232747
} elseif ($function !== null) {
27242748
$returnTypeForThrow = $variant->getReturnType();
27252749
$throwType = $function->getThrowType();
@@ -2745,6 +2769,7 @@ private function createFirstClassCallable(
27452769
}
27462770

27472771
$acceptsNamedArguments = $function->acceptsNamedArguments();
2772+
$mustUseReturnValue = $function->mustUseReturnValue();
27482773
}
27492774

27502775
$parameters = $variant->getParameters();
@@ -2759,6 +2784,7 @@ private function createFirstClassCallable(
27592784
$throwPoints,
27602785
$impurePoints,
27612786
acceptsNamedArguments: $acceptsNamedArguments,
2787+
mustUseReturnValue: $mustUseReturnValue,
27622788
);
27632789
}
27642790

src/PhpDoc/TypeNodeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ function (CallableTypeParameterNode $parameterNode) use ($nameScope, &$isVariadi
10321032
),
10331033
]);
10341034
} elseif ($mainType instanceof ClosureType) {
1035-
$closure = new ClosureType($parameters, $returnType, $isVariadic, $templateTypeMap, templateTags: $templateTags, impurePoints: $mainType->getImpurePoints(), invalidateExpressions: $mainType->getInvalidateExpressions(), usedVariables: $mainType->getUsedVariables(), acceptsNamedArguments: $mainType->acceptsNamedArguments());
1035+
$closure = new ClosureType($parameters, $returnType, $isVariadic, $templateTypeMap, templateTags: $templateTags, impurePoints: $mainType->getImpurePoints(), invalidateExpressions: $mainType->getInvalidateExpressions(), usedVariables: $mainType->getUsedVariables(), acceptsNamedArguments: $mainType->acceptsNamedArguments(), mustUseReturnValue: $mainType->mustUseReturnValue());
10361036
if ($closure->isPure()->yes() && $returnType->isVoid()->yes()) {
10371037
return new ErrorType();
10381038
}

src/Reflection/Annotations/AnnotationMethodReflection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,9 @@ public function getAttributes(): array
183183
return [];
184184
}
185185

186+
public function mustUseReturnValue(): TrinaryLogic
187+
{
188+
return TrinaryLogic::createNo();
189+
}
190+
186191
}

src/Reflection/Callables/CallableParametersAcceptor.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,11 @@ public function getInvalidateExpressions(): array;
3636
*/
3737
public function getUsedVariables(): array;
3838

39+
/**
40+
* Has the #[\NoDiscard] attribute - on PHP 8.5+ if the function's return
41+
* value is unused at runtime a warning is emitted, PHPStan will emit the
42+
* warning during analysis and on older PHP versions too
43+
*/
44+
public function mustUseReturnValue(): TrinaryLogic;
45+
3946
}

src/Reflection/Callables/FunctionCallableVariant.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,9 @@ public function acceptsNamedArguments(): TrinaryLogic
168168
return $this->function->acceptsNamedArguments();
169169
}
170170

171+
public function mustUseReturnValue(): TrinaryLogic
172+
{
173+
return $this->function->mustUseReturnValue();
174+
}
175+
171176
}

src/Reflection/Dummy/ChangedTypeMethodReflection.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,9 @@ public function getAttributes(): array
172172
return $this->reflection->getAttributes();
173173
}
174174

175+
public function mustUseReturnValue(): TrinaryLogic
176+
{
177+
return $this->reflection->mustUseReturnValue();
178+
}
179+
175180
}

src/Reflection/Dummy/DummyConstructorReflection.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,10 @@ public function getAttributes(): array
157157
return [];
158158
}
159159

160+
public function mustUseReturnValue(): TrinaryLogic
161+
{
162+
// Align with the getAttributes() returning empty
163+
return TrinaryLogic::createNo();
164+
}
165+
160166
}

0 commit comments

Comments
 (0)