Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add InvalidNullCoalesce as level 4 rule #70

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/config.level4.neon
Expand Up @@ -8,6 +8,7 @@ rules:
- PHPStan\Rules\Comparison\BooleanOrConstantConditionRule
- PHPStan\Rules\Comparison\ElseIfConstantConditionRule
- PHPStan\Rules\Comparison\IfConstantConditionRule
- PHPStan\Rules\Comparison\InvalidNullCoalesce
- PHPStan\Rules\Comparison\TernaryOperatorConstantConditionRule
- PHPStan\Rules\Comparison\NumberComparisonOperatorsConstantConditionRule
- PHPStan\Rules\Comparison\UnreachableIfBranchesRule
Expand Down
49 changes: 49 additions & 0 deletions src/Rules/Comparison/InvalidNullCoalesce.php
@@ -0,0 +1,49 @@
<?php declare(strict_types=1);

namespace PHPStan\Rules\Comparison;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\NullType;
use PHPStan\Type\VerbosityLevel;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\BinaryOp\Coalesce>
*/
class InvalidNullCoalesce implements Rule
{

/**
* @phpstan-return class-string<\PhpParser\Node\Expr\BinaryOp\Coalesce>
* @return string
*/
public function getNodeType(): string
{
return Node\Expr\BinaryOp\Coalesce::class;
}

/**
* @phpstan-param \PhpParser\Node\Expr\BinaryOp\Coalesce $node
* @param \PhpParser\Node $node
* @param \PHPStan\Analyser\Scope $scope
* @return RuleError[] errors
*/
public function processNode(Node $node, Scope $scope): array
{
$type = $scope->getType($node->left);
$onlyNull = $type instanceof NullType;
if($onlyNull || $type->accepts(new NullType(), $scope->isDeclareStrictTypes())->no()) {
return [
RuleErrorBuilder::message(sprintf(
'Null coalesce on type %s is always %s.',
$type->describe(VerbosityLevel::value()),
$onlyNull ? 'true' : 'false'
))->line($node->left->getLine())->build(),
];
}
return [];
}
}
52 changes: 52 additions & 0 deletions tests/PHPStan/Rules/Comparison/InvalidNullCoalesceTest.php
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);

namespace PHPStan\Rules\Comparison;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends \PHPStan\Testing\RuleTestCase<InvalidNullCoalesce>
*/
class InvalidNullCoalesceTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new InvalidNullCoalesce();
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/invalid-null-coalesce.php'], [
[
'Null coalesce on type array is always false.',
10,
],
[
'Null coalesce on type int is always false.',
11,
],
[
'Null coalesce on type bool is always false.',
12,
],
[
'Null coalesce on type string is always false.',
16,
],
[
'Null coalesce on type int|string is always false.',
27,
],
[
'Null coalesce on type int|false is always false.',
36,
],
[
'Null coalesce on type null is always true.',
38,
],
]);
}
}
40 changes: 40 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/invalid-null-coalesce.php
@@ -0,0 +1,40 @@
<?php

function (
$mixed,
array $array,
int $integer,
?string $nullableString
) {
$mixed ?? 10;
$array ?? 10;
$integer ?? 10;
(function (): bool {
return true;
})() ?? false;
$nullableString ?? true;
(string) $mixed ?? 10;
};

/**
* @param int|string $union
* @param int|stdClass|null $nullableUnion
*/
function foo(
$union,
$nullableUnion
) {
$union ?? 10;
$nullableUnion ?? 10;
}

function(
string $one,
string $two,
?string $nullableString
) {
strpos($one, $two) ?? 'foo';
if (!is_string($nullableString)) {
$nullableString ?? 10;
}
};