Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;

/**
* @implements Rule<Ternary>
Expand All @@ -25,11 +29,29 @@
return [];
}

if ($scope->getType($node->cond)->isSuperTypeOf($this->getNonBooleanFalseyType())->no()) {
return [];
}

return [
RuleErrorBuilder::message('Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.')
->identifier('ternary.shortNotAllowed')
->build(),
];
}

private function getNonBooleanFalseyType(): Type
{
static $falseyWithoutFalse;

if ($falseyWithoutFalse === null) {
$falseyWithoutFalse = TypeCombinator::remove(
StaticTypeFactory::falsey(),

Check failure on line 49 in src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, lowest)

Calling PHPStan\Type\StaticTypeFactory::falsey() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 49 in src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, lowest)

Calling PHPStan\Type\StaticTypeFactory::falsey() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 49 in src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, highest)

Calling PHPStan\Type\StaticTypeFactory::falsey() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 49 in src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, lowest)

Calling PHPStan\Type\StaticTypeFactory::falsey() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 49 in src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, highest)

Calling PHPStan\Type\StaticTypeFactory::falsey() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 49 in src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, highest)

Calling PHPStan\Type\StaticTypeFactory::falsey() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 49 in src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, lowest)

Calling PHPStan\Type\StaticTypeFactory::falsey() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 49 in src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, highest)

Calling PHPStan\Type\StaticTypeFactory::falsey() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 49 in src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, highest)

Calling PHPStan\Type\StaticTypeFactory::falsey() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 49 in src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, lowest)

Calling PHPStan\Type\StaticTypeFactory::falsey() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 49 in src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, highest)

Calling PHPStan\Type\StaticTypeFactory::falsey() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.

Check failure on line 49 in src/Rules/DisallowedConstructs/DisallowedShortTernaryRule.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, lowest)

Calling PHPStan\Type\StaticTypeFactory::falsey() is not covered by backward compatibility promise. The method might change in a minor PHPStan version.
new ConstantBooleanType(false),
);
}

return $falseyWithoutFalse;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,43 @@ public function testRule(): void
$this->analyse([__DIR__ . '/data/short-ternary.php'], [
[
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
3,
6,
],
[
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
4,
7,
],
[
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
13,
],
[
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
14,
],
[
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
31,
],
[
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
32,
],
[
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
37,
],
[
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
38,
],
[
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
49,
],
[
'Short ternary operator is not allowed. Use null coalesce operator if applicable or consider using long ternary.',
50,
],
]);
}
Expand Down
57 changes: 54 additions & 3 deletions tests/Rules/DisallowedConstructs/data/short-ternary.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
<?php

$foo = 123 ?: 456;
$bar = 123 ? : 456;
$baz = 123 ? 456 : 789;
/** @var int $cond */
$cond = 123;

$foo = $cond ?: 456;
$bar = $cond ? : 456;
$baz = $cond ? 456 : 789;

/** @var int|false $cond */
$cond = 123;

$foo = $cond ?: 456;
$bar = $cond ? : 456;

/** @var positive-int|false $ */
$cond = 123;

$foo = $cond ?: 456;
$bar = $cond ? : 456;

/** @var object|false $ */
$cond = 123;

$foo = $cond ?: 456;
$bar = $cond ? : 456;

/** @var string|false $ */
$cond = '123';

$foo = $cond ?: 456;
$bar = $cond ? : 456;

/** @var non-empty-string|false $ */
$cond = '123';

$foo = $cond ?: 456;
$bar = $cond ? : 456;

/** @var non-falsy-string|false $ */
$cond = '123';

$foo = $cond ?: 456;
$bar = $cond ? : 456;

/** @var array|false $ */
$cond = [123];

$foo = $cond ?: 456;
$bar = $cond ? : 456;

/** @var non-empty-array|false $ */
$cond = [123];

$foo = $cond ?: 456;
$bar = $cond ? : 456;
Loading