Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cdaa291

Browse files
committed
feat: treat void as null for null coalescing operator
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
1 parent e95f537 commit cdaa291

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

‎src/Rules/Variables/NullCoalesceRule.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PHPStan\Rules\Rule;
99
use PHPStan\Type\NullType;
1010
use PHPStan\Type\Type;
11+
use PHPStan\Type\VoidType;
1112

1213
/**
1314
* @implements Rule<Node\Expr>
@@ -27,12 +28,13 @@ public function getNodeType(): string
2728
public function processNode(Node $node, Scope $scope): array
2829
{
2930
$typeMessageCallback = static function (Type $type): ?string {
31+
$isVoid = (new VoidType())->isSuperTypeOf($type);
3032
$isNull = (new NullType())->isSuperTypeOf($type);
31-
if ($isNull->maybe()) {
33+
if ($isNull->maybe() || $isVoid->maybe()) {
3234
return null;
3335
}
3436

35-
if ($isNull->yes()) {
37+
if ($isNull->yes() || $isVoid->yes()) {
3638
return 'is always null';
3739
}
3840

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,4 +368,17 @@ public function testBug8084(): void
368368
$this->analyse([__DIR__ . '/data/bug-8084.php'], []);
369369
}
370370

371+
public function testVoid(): void
372+
{
373+
$this->treatPhpDocTypesAsCertain = true;
374+
$this->strictUnnecessaryNullsafePropertyFetch = true;
375+
376+
$this->analyse([__DIR__ . '/data/null-coalesce-void.php'], [
377+
[
378+
'Expression on left side of ?? is always null.',
379+
22,
380+
],
381+
]);
382+
}
383+
371384
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php // lint >= 8.0
2+
3+
namespace CoalesceRuleVoid;
4+
5+
/** @return array|void */
6+
function get_post_custom_keys($maybe = false)
7+
{
8+
if ($maybe) {
9+
return;
10+
}
11+
12+
return [];
13+
}
14+
15+
function foo_bar(): void
16+
{
17+
return;
18+
}
19+
20+
$test1 = get_post_custom_keys(true) ?? 'foo';
21+
22+
$test2 = foo_bar() ?? 'bar';

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /