Skip to content
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
7 changes: 7 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ rules:
- PHPStan\Rules\VariableVariables\VariableStaticPropertyFetchRule
- PHPStan\Rules\VariableVariables\VariableVariablesRule

conditionalTags:
PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule:
phpstan.rules.rule: %featureToggles.bleedingEdge%
Copy link
Contributor

@staabm staabm Jun 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ondrejmirtes We are using bleeding edge to test @readonly and stuff in phpstan, but this rule would create thousands of errors for us in legacy apps.

wouldn‘t it make sense to have a additional feature flag, so one can enable bleeding edge but don‘t enable this rule?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either we want to have this enabled for everyone when PHPStan 2.0 + phpstan-strict-rules 2.0 are released, or we don't.

In the first case (enable for everyone), the additional feature toggle doesn't make sense. Bleeding edge is a preview of what's to come in the next major version.

I made a Twitter poll about this: https://twitter.com/OndrejMirtes/status/1537418180888514560

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW - instead of setting a feature toggle to false or whatever, you can always write a regex to ignore these problems.


services:
-
class: PHPStan\Rules\BooleansInConditions\BooleanRuleHelper
Expand All @@ -71,3 +75,6 @@ services:
universalObjectCratesClasses: %universalObjectCratesClasses%
tags:
- phpstan.rules.rule

-
class: PHPStan\Rules\DisallowedConstructs\DisallowedLooseComparisonRule
44 changes: 44 additions & 0 deletions src/Rules/DisallowedConstructs/DisallowedLooseComparisonRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\DisallowedConstructs;

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\NotEqual;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

/**
* @implements Rule<BinaryOp>
*/
class DisallowedLooseComparisonRule implements Rule
{

public function getNodeType(): string
{
return BinaryOp::class;
}

public function processNode(Node $node, Scope $scope): array
{
if ($node instanceof Equal) {
return [
RuleErrorBuilder::message(
'Loose comparison via "==" is not allowed.'
)->tip('Use strict comparison via "===" instead.')->build(),
];
}
if ($node instanceof NotEqual) {
return [
RuleErrorBuilder::message(
'Loose comparison via "!=" is not allowed.'
)->tip('Use strict comparison via "!==" instead.')->build(),
];
}

return [];
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\DisallowedConstructs;

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

/**
* @extends RuleTestCase<DisallowedLooseComparisonRule>
*/
class DisallowedLooseComparisonRuleTest extends RuleTestCase
{

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

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/weak-comparison.php'], [
[
'Loose comparison via "==" is not allowed.',
3,
'Use strict comparison via "===" instead.',
],
[
'Loose comparison via "!=" is not allowed.',
5,
'Use strict comparison via "!==" instead.',
],
]);
}

}
6 changes: 6 additions & 0 deletions tests/Rules/DisallowedConstructs/data/weak-comparison.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$bool1 = 123 == 456;
$bool2 = 123 === 456;
$bool3 = 123 != 456;
$bool4 = 123 !== 456;