Skip to content

Commit 5f60d4e

Browse files
Majkl578ondrejmirtes
authored andcommitted
Ban short ternary operator
1 parent f7f300f commit 5f60d4e

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Types in `switch` condition and `case` value must match. PHP compares them loosely by default and that can lead to unexpected results.
2020
* Check that statically declared methods are called statically.
2121
* Disallow `empty()` - it's a very loose comparison (see [manual](https://php.net/empty)), it's recommended to use more strict one.
22+
* Disallow short ternary operator (`?:`) - implies weak comparison, it's recommended to use null coalesce operator (`??`) or ternary operator with strict condition.
2223
* Disallow variable variables (`$$foo`, `$this->$method()` etc.)
2324
* Disallow overwriting variables with foreach key and value variables
2425
* Always true `instanceof`, type-checking `is_*` functions and strict comparisons `===`/`!==`. These checks can be turned off by setting `checkAlwaysTrueInstanceof`/`checkAlwaysTrueCheckTypeFunctionCall`/`checkAlwaysTrueStrictComparison` to false.

rules.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rules:
2121
- PHPStan\Rules\Classes\RequireParentConstructCallRule
2222
- PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule
2323
- PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule
24+
- PHPStan\Rules\DisallowedConstructs\DisallowedShortTernaryRule
2425
- PHPStan\Rules\ForeachLoop\OverwriteVariablesWithForeachRule
2526
- PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule
2627
- PHPStan\Rules\Operators\OperandInArithmeticPostDecrementRule
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\DisallowedConstructs;
4+
5+
use PhpParser\Node;
6+
use PHPStan\Analyser\Scope;
7+
8+
class DisallowedShortTernaryRule implements \PHPStan\Rules\Rule
9+
{
10+
11+
public function getNodeType(): string
12+
{
13+
return \PhpParser\Node\Expr\Ternary::class;
14+
}
15+
16+
/**
17+
* @param \PhpParser\Node\Expr\Ternary $node
18+
* @param \PHPStan\Analyser\Scope $scope
19+
* @return string[]
20+
*/
21+
public function processNode(Node $node, Scope $scope): array
22+
{
23+
if ($node->if !== null) {
24+
return [];
25+
}
26+
27+
return [
28+
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
29+
];
30+
}
31+
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\DisallowedConstructs;
4+
5+
use PHPStan\Rules\Rule;
6+
7+
class DisallowedShortTernaryRuleTest extends \PHPStan\Testing\RuleTestCase
8+
{
9+
10+
protected function getRule(): Rule
11+
{
12+
return new DisallowedShortTernaryRule();
13+
}
14+
15+
public function testRule(): void
16+
{
17+
$this->analyse([__DIR__ . '/data/short-ternary.php'], [
18+
[
19+
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
20+
3,
21+
],
22+
[
23+
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
24+
4,
25+
],
26+
]);
27+
}
28+
29+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
$foo = 123 ?: 456;
4+
$bar = 123 ? : 456;
5+
$baz = 123 ? 456 : 789;

0 commit comments

Comments
 (0)