From 7240b33c7b8ff551cc374bd8822adae3bb042cae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Machulda?= Date: Fri, 26 Apr 2024 11:28:23 +0200 Subject: [PATCH 1/2] Feat: Require comparisons to be strict (fixes #90) --- ecs.php | 3 +++ tests/Integration/Fixtures/Basic.correct.php.inc | 4 ++-- tests/Integration/Fixtures/Basic.wrong.php.inc | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/ecs.php b/ecs.php index 5d49511..bb9667c 100644 --- a/ecs.php +++ b/ecs.php @@ -122,6 +122,7 @@ use PhpCsFixer\Fixer\Semicolon\NoSinglelineWhitespaceBeforeSemicolonsFixer; use PhpCsFixer\Fixer\Semicolon\SpaceAfterSemicolonFixer; use PhpCsFixer\Fixer\Strict\DeclareStrictTypesFixer; +use PhpCsFixer\Fixer\Strict\StrictComparisonFixer; use PhpCsFixer\Fixer\Strict\StrictParamFixer; use PhpCsFixer\Fixer\StringNotation\SingleQuoteFixer; use PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer; @@ -358,6 +359,8 @@ DeclareStrictTypesFixer::class, // Functions should be used with `$strict` param set to `true` StrictParamFixer::class, + // Comparisons should be strict, `===` or `!==` must be used for comparisons + StrictComparisonFixer::class, // Convert double quotes to single quotes for simple strings SingleQuoteFixer::class, // Remove extra spaces in a nullable typehint diff --git a/tests/Integration/Fixtures/Basic.correct.php.inc b/tests/Integration/Fixtures/Basic.correct.php.inc index 922a41a..57ebb7b 100644 --- a/tests/Integration/Fixtures/Basic.correct.php.inc +++ b/tests/Integration/Fixtures/Basic.correct.php.inc @@ -14,8 +14,8 @@ class Basic $lambdaWithUnusedImport = function () { return 'foo'; }; // NoUselessSprintfFixer $uselessSprintf = 'bar'; - // SingleSpaceAfterConstructFixer - if ($a == $b) { + // SingleSpaceAfterConstructFixer, StrictComparisonFixer + if ($a === $b || $bazLength !== 3) { return true; } diff --git a/tests/Integration/Fixtures/Basic.wrong.php.inc b/tests/Integration/Fixtures/Basic.wrong.php.inc index aa795ae..d56ac43 100644 --- a/tests/Integration/Fixtures/Basic.wrong.php.inc +++ b/tests/Integration/Fixtures/Basic.wrong.php.inc @@ -13,8 +13,8 @@ class Basic $lambdaWithUnusedImport = function () use ($fooBar) { return 'foo'; }; // NoUselessSprintfFixer $uselessSprintf = sprintf('bar'); - // SingleSpaceAfterConstructFixer - if ($a == $b) { return true; } + // SingleSpaceAfterConstructFixer, StrictComparisonFixer + if ($a == $b || $bazLength != 3) { return true; } return false; // BlankLineBeforeStatementFixer } From 5ca8737fc14585504dba786bf88e4fd92005888d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Machulda?= Date: Fri, 26 Apr 2024 11:32:21 +0200 Subject: [PATCH 2/2] Chore: Add test to ensure strict param is used on functions --- tests/Integration/Fixtures/Basic.correct.php.inc | 2 ++ tests/Integration/Fixtures/Basic.wrong.php.inc | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/Integration/Fixtures/Basic.correct.php.inc b/tests/Integration/Fixtures/Basic.correct.php.inc index 57ebb7b..1d11928 100644 --- a/tests/Integration/Fixtures/Basic.correct.php.inc +++ b/tests/Integration/Fixtures/Basic.correct.php.inc @@ -14,6 +14,8 @@ class Basic $lambdaWithUnusedImport = function () { return 'foo'; }; // NoUselessSprintfFixer $uselessSprintf = 'bar'; + // StrictParamFixer + $useStrictParam = in_array(1337, $fooBar, true); // SingleSpaceAfterConstructFixer, StrictComparisonFixer if ($a === $b || $bazLength !== 3) { return true; diff --git a/tests/Integration/Fixtures/Basic.wrong.php.inc b/tests/Integration/Fixtures/Basic.wrong.php.inc index d56ac43..34081b7 100644 --- a/tests/Integration/Fixtures/Basic.wrong.php.inc +++ b/tests/Integration/Fixtures/Basic.wrong.php.inc @@ -13,6 +13,8 @@ class Basic $lambdaWithUnusedImport = function () use ($fooBar) { return 'foo'; }; // NoUselessSprintfFixer $uselessSprintf = sprintf('bar'); + // StrictParamFixer + $useStrictParam = in_array(1337, $fooBar); // SingleSpaceAfterConstructFixer, StrictComparisonFixer if ($a == $b || $bazLength != 3) { return true; } return false; // BlankLineBeforeStatementFixer