Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make the null-coalesce side-effect test data PHP version independent
The data file declared `null` return types natively, which is only a type
on PHP 8.2+. With a lower `phpVersion` (the tests run on PHP 7.4 too) the
same declaration resolves to an object type named `null`, so the right
side isn't null and the pure-function case wasn't reported.

Declare the null return types in PHPDoc instead so every case exercises
what it claims on all analysed PHP versions.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
  • Loading branch information
2 people authored and staabm committed Sep 12, 2026
commit 5d68bc9b30a736983becb125aa4dc8ecad56036c
12 changes: 6 additions & 6 deletions tests/PHPStan/Rules/Variables/NullCoalesceRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,27 +515,27 @@ public function testUnnecessaryNullCoalesceSideEffects(): void
$this->analyse([__DIR__ . '/data/unnecessary-null-coalesce-side-effects.php'], [
[
'Coalesce operator ??= is unnecessary because the left side is always set and the right side is null.',
104,
111,
],
[
'Coalesce operator ?? is unnecessary because the left side is always set and the right side is null.',
111,
118,
],
[
'Coalesce operator ?? is unnecessary because the left side is always set and the right side is null.',
116,
123,
],
[
'Coalesce operator ?? is unnecessary because the left side is always set and the right side is null.',
121,
128,
],
[
'Coalesce operator ?? is unnecessary because the left side is always set and the right side is null.',
128,
135,
],
[
'Coalesce operator ?? is unnecessary because the left side is always set and the right side is null.',
133,
140,
],
]);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php // lint >= 8.2
<?php

declare(strict_types = 1);

Expand All @@ -8,7 +8,8 @@

const NULL_CONSTANT = null;

function returnsNull(bool $value): null
/** @return null */
function returnsNull(bool $value)
{
if ($value === false) {
throw new LogicException('nope');
Expand All @@ -17,8 +18,11 @@ function returnsNull(bool $value): null
return null;
}

/** @phpstan-pure */
function pureReturnsNull(): null
/**
* @phpstan-pure
* @return null
*/
function pureReturnsNull()
{
return null;
}
Expand All @@ -34,17 +38,20 @@ class Foo
/** @var null */
public $alwaysNull = null;

public function returnsNull(): null
/** @return null */
public function returnsNull()
{
return null;
}

public static function staticReturnsNull(): null
/** @return null */
public static function staticReturnsNull()
{
return null;
}

public function __invoke(): null
/** @return null */
public function __invoke()
{
return null;
}
Expand Down Expand Up @@ -73,7 +80,7 @@ function invokeOnRightSide(Foo $foo): ?string

function closureCallOnRightSide(Foo $foo): ?string
{
$closure = static function (): null {
$closure = static function () {
echo 'side effect';

return null;
Expand Down