Skip to content

Commit c32624c

Browse files
Trinary logic
1 parent 24db3f7 commit c32624c

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

src/Reflection/FunctionReflection.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,11 @@ public function isPure(): TrinaryLogic;
6262
*/
6363
public function getAttributes(): array;
6464

65+
/**
66+
* Has the #[\NoDiscard] attribute - on PHP 8.5+ if the function's return
67+
* value is unused at runtime a warning is emitted, phpstan will emit the
68+
* warning during analysis and on older PHP versions too
69+
*/
70+
public function hasNoDiscardAttribute(): TrinaryLogic;
71+
6572
}

src/Reflection/Php/PhpFunctionFromParserNodeReflection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,14 @@ public function getAttributes(): array
330330
return $this->attributes;
331331
}
332332

333+
public function hasNoDiscardAttribute(): TrinaryLogic
334+
{
335+
foreach ($this->attributes as $attrib) {
336+
if ($attrib->getName() === 'NoDiscard') {
337+
return TrinaryLogic::createYes();
338+
}
339+
}
340+
return TrinaryLogic::createNo();
341+
}
342+
333343
}

src/Reflection/Php/PhpFunctionReflection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,14 @@ public function getAttributes(): array
275275
return $this->attributes;
276276
}
277277

278+
public function hasNoDiscardAttribute(): TrinaryLogic
279+
{
280+
foreach ($this->attributes as $attrib) {
281+
if ($attrib->getName() === 'NoDiscard') {
282+
return TrinaryLogic::createYes();
283+
}
284+
}
285+
return TrinaryLogic::createNo();
286+
}
287+
278288
}

src/Rules/Functions/CallToFunctionStatementWithNoDiscardRule.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,7 @@ public function processNode(Node $node, Scope $scope): array
4343

4444
$function = $this->reflectionProvider->getFunction($funcCall->name, $scope);
4545

46-
$attributes = $function->getAttributes();
47-
$hasNoDiscard = false;
48-
foreach ($attributes as $attrib) {
49-
if ($attrib->getName() === 'NoDiscard') {
50-
$hasNoDiscard = true;
51-
break;
52-
}
53-
}
54-
if (!$hasNoDiscard) {
46+
if (!$function->hasNoDiscardAttribute()->yes()) {
5547
return [];
5648
}
5749

0 commit comments

Comments
 (0)