From 07d2784d0a9dd869b8fefa1f5b0ad772f7a39b6c Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 2 Nov 2025 08:11:46 +0100 Subject: [PATCH 1/5] Use TypeSystem in AssertSameBooleanExpectedRule --- src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php | 5 +++-- tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php b/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php index fd76f7c4..18ddcf3a 100644 --- a/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php +++ b/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php @@ -40,18 +40,19 @@ public function processNode(Node $node, Scope $scope): array if (!($expectedArgumentValue instanceof ConstFetch)) { return []; } + $expectedArgumentType = $scope->getType($expectedArgumentValue); if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { return []; } - if ($expectedArgumentValue->name->toLowerString() === 'true') { + if ($expectedArgumentType->isTrue()->yes()) { return [ RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')->identifier('phpunit.assertTrue')->build(), ]; } - if ($expectedArgumentValue->name->toLowerString() === 'false') { + if ($expectedArgumentType->isFalse()->yes()) { return [ RuleErrorBuilder::message('You should use assertFalse() instead of assertSame() when expecting "false"')->identifier('phpunit.assertFalse')->build(), ]; diff --git a/tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php b/tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php index 1fe31df9..3d7c2597 100644 --- a/tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php +++ b/tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php @@ -31,6 +31,14 @@ public function testRule(): void 'You should use assertTrue() instead of assertSame() when expecting "true"', 26, ], + [ + 'You should use assertTrue() instead of assertSame() when expecting "true"', + 31, + ], + [ + 'You should use assertFalse() instead of assertSame() when expecting "false"', + 32, + ], [ 'You should use assertTrue() instead of assertSame() when expecting "true"', 74, From 4d6fe4ef8d6e04cc604b02c20888d0997fad98a5 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 2 Nov 2025 08:23:51 +0100 Subject: [PATCH 2/5] Update assert-same-boolean-expected.php --- tests/Rules/PHPUnit/data/assert-same-boolean-expected.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Rules/PHPUnit/data/assert-same-boolean-expected.php b/tests/Rules/PHPUnit/data/assert-same-boolean-expected.php index dccd2ceb..5804aaf2 100644 --- a/tests/Rules/PHPUnit/data/assert-same-boolean-expected.php +++ b/tests/Rules/PHPUnit/data/assert-same-boolean-expected.php @@ -75,6 +75,14 @@ public function testNonLowercase(): void \PHPUnit\Framework\Assert::assertSame(False, 'foo'); } + public function testMaybeTrueFalse(): void + { + $a = rand(0, 1) ? true : 'foo'; + \PHPUnit\Framework\Assert::assertSame($a, 'foo'); + $a = rand(0, 1) ? false : 'foo'; + \PHPUnit\Framework\Assert::assertSame($a, 'foo'); + } + } const PHPSTAN_PHPUNIT_TRUE = true; From fd94ce4bec015e984db40c3365e8d3183fa4f9a1 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 2 Nov 2025 08:34:59 +0100 Subject: [PATCH 3/5] test maybe true/false ConstFetch --- .../PHPUnit/data/assert-same-boolean-expected.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/Rules/PHPUnit/data/assert-same-boolean-expected.php b/tests/Rules/PHPUnit/data/assert-same-boolean-expected.php index 5804aaf2..d6f7f14a 100644 --- a/tests/Rules/PHPUnit/data/assert-same-boolean-expected.php +++ b/tests/Rules/PHPUnit/data/assert-same-boolean-expected.php @@ -83,6 +83,18 @@ public function testMaybeTrueFalse(): void \PHPUnit\Framework\Assert::assertSame($a, 'foo'); } + public function testConstMaybeTrueFalse(): void + { + if ( + !defined('MY_TEST_CONST') + ) { + return; + } + if (MY_TEST_CONST !== true && MY_TEST_CONST !== false) { + return; + } + \PHPUnit\Framework\Assert::assertSame(MY_TEST_CONST, 'foo'); + } } const PHPSTAN_PHPUNIT_TRUE = true; From d598321694315b80992b7007636387a3ea8bbcd3 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 2 Nov 2025 09:25:31 +0100 Subject: [PATCH 4/5] Discard changes to src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php --- src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php b/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php index 18ddcf3a..fd76f7c4 100644 --- a/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php +++ b/src/Rules/PHPUnit/AssertSameBooleanExpectedRule.php @@ -40,19 +40,18 @@ public function processNode(Node $node, Scope $scope): array if (!($expectedArgumentValue instanceof ConstFetch)) { return []; } - $expectedArgumentType = $scope->getType($expectedArgumentValue); if (!AssertRuleHelper::isMethodOrStaticCallOnAssert($node, $scope)) { return []; } - if ($expectedArgumentType->isTrue()->yes()) { + if ($expectedArgumentValue->name->toLowerString() === 'true') { return [ RuleErrorBuilder::message('You should use assertTrue() instead of assertSame() when expecting "true"')->identifier('phpunit.assertTrue')->build(), ]; } - if ($expectedArgumentType->isFalse()->yes()) { + if ($expectedArgumentValue->name->toLowerString() === 'false') { return [ RuleErrorBuilder::message('You should use assertFalse() instead of assertSame() when expecting "false"')->identifier('phpunit.assertFalse')->build(), ]; From 0660edfc41d7a8d6a64a46e0a9b8567f0ab9102d Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sun, 2 Nov 2025 09:25:36 +0100 Subject: [PATCH 5/5] Discard changes to tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php --- tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php b/tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php index 3d7c2597..1fe31df9 100644 --- a/tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php +++ b/tests/Rules/PHPUnit/AssertSameBooleanExpectedRuleTest.php @@ -31,14 +31,6 @@ public function testRule(): void 'You should use assertTrue() instead of assertSame() when expecting "true"', 26, ], - [ - 'You should use assertTrue() instead of assertSame() when expecting "true"', - 31, - ], - [ - 'You should use assertFalse() instead of assertSame() when expecting "false"', - 32, - ], [ 'You should use assertTrue() instead of assertSame() when expecting "true"', 74,

AltStyle によって変換されたページ (->オリジナル) /