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

Optional rule to disable float in every expression (not just assignments) #2

Merged
merged 5 commits into from
Jan 9, 2019
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"require": {
"php": "^7.2",
"nikic/php-parser": "^4.0",
"phpstan/phpstan": "^0.10"
"phpstan/phpstan": "^0.10.8"
},
"require-dev": {
"doctrine/coding-standard": "^5.0.0",
Expand Down
11 changes: 11 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ rules:
- Roave\PHPStan\Rules\Floats\DisallowFloatInFunctionSignatureRule
- Roave\PHPStan\Rules\Floats\DisallowFloatInMethodSignatureRule
- Roave\PHPStan\Rules\Floats\DisallowFloatPropertyTypeRule

parameters:
disallowFloatsEverywhere: false

conditionalTags:
Roave\PHPStan\Rules\Floats\DisallowFloatEverywhereRule:
phpstan.rules.rule: %disallowFloatsEverywhere%

services:
-
class: Roave\PHPStan\Rules\Floats\DisallowFloatEverywhereRule
46 changes: 46 additions & 0 deletions src/DisallowFloatEverywhereRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Roave\PHPStan\Rules\Floats;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

class DisallowFloatEverywhereRule implements Rule
{
public function getNodeType() : string
{
return Node\Expr::class;
}

/**
* @param Expr $node
*
* @return string[]
*/
public function processNode(Node $node, Scope $scope) : array
{
if ($node instanceof Node\Expr\AssignOp
|| $node instanceof Node\Expr\Assign
) {
return [];
}

$nodeType = $scope->getType($node);
if (! FloatTypeHelper::isFloat($nodeType)) {
return [];
}

return [
sprintf(
'Cannot have %s as a result type of this expression - floats are not allowed.',
$nodeType->describe(VerbosityLevel::typeOnly())
),
];
}
}
7 changes: 7 additions & 0 deletions tests/asset/assign.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ public function doFoo($float, $intOrFloat, string $str)
$test3 = $str;
}

public function doBar()
{
$test = 0;
$test += 1;
$test += 3.14;
}

}
12 changes: 12 additions & 0 deletions tests/asset/expr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace DisallowFloatsEverywhere;

function () {
$foo = 1.3;
$foo += 1.3;

foo(
3.14
);
};
4 changes: 4 additions & 0 deletions tests/src/DisallowFloatAssignedToVariableRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function testRule() : void
'Cannot assign float to $this->bar[\'test\'] - floats are not allowed.',
22,
],
[
'Cannot assign float to $test - floats are not allowed.',
31,
],
]);
}
}
39 changes: 39 additions & 0 deletions tests/src/DisallowFloatEverywhereRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Roave\PHPStanTest\Rules\Floats;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use Roave\PHPStan\Rules\Floats\DisallowFloatEverywhereRule;

class DisallowFloatEverywhereRuleTest extends RuleTestCase
{
protected function getRule() : Rule
{
return new DisallowFloatEverywhereRule();
}

public function testRule() : void
{
$this->analyse([__DIR__ . '/../asset/expr.php'], [
[
'Cannot have float as a result type of this expression - floats are not allowed.',
6,
],
[
'Cannot have float as a result type of this expression - floats are not allowed.',
7,
],
[
'Cannot have float as a result type of this expression - floats are not allowed.',
7,
],
[
'Cannot have float as a result type of this expression - floats are not allowed.',
10,
],
]);
}
}