Skip to content

Commit

Permalink
Casting a division to float is not always useless
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 3, 2016
1 parent 2853c91 commit 5bbf809
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Rules/Cast/UselessCastRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\Cast\Object_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\FloatType;

class UselessCastRule implements \PHPStan\Rules\Rule
{
Expand All @@ -32,6 +33,9 @@ public function processNode(Node $node, Scope $scope): array
}

$castType = $scope->getType($node);
if ($castType instanceof FloatType && $node->expr instanceof Node\Expr\BinaryOp\Div) {
return [];
}

if (get_class($expressionType) === get_class($castType)) {
return [
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Rules/Cast/data/useless-cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@
/** @var $nullableString string|null */
$nullableString = 'foo';
$foo = (string) $nullableString;

$foo = (float) (6 / 2);

0 comments on commit 5bbf809

Please sign in to comment.