From f452fac4d5bf9cac39b827728d19dbb4f96ff4ea Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 19 Jan 2025 18:34:36 +0000 Subject: [PATCH 1/2] Add non-testcase assert call in FlipAssertRector --- .../AssertEqualsToSameRector/Fixture/skip.php | 19 ----------- .../cover_external_static_call.php.inc | 33 +++++++++++++++++++ src/Enum/PHPUnitClassName.php | 18 ++++++++++ src/NodeAnalyzer/TestsNodeAnalyzer.php | 20 +++++++++-- 4 files changed, 68 insertions(+), 22 deletions(-) delete mode 100644 rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip.php create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/FlipAssertRector/Fixture/cover_external_static_call.php.inc create mode 100644 src/Enum/PHPUnitClassName.php diff --git a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip.php b/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip.php deleted file mode 100644 index 1a7b0b71..00000000 --- a/rules-tests/CodeQuality/Rector/MethodCall/AssertEqualsToSameRector/Fixture/skip.php +++ /dev/null @@ -1,19 +0,0 @@ -assertEquals($expectedNull, $null); - - $bool = true; - $expectedBool = true; - $this->assertEquals($expectedBool, $bool); - } -} diff --git a/rules-tests/CodeQuality/Rector/MethodCall/FlipAssertRector/Fixture/cover_external_static_call.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/FlipAssertRector/Fixture/cover_external_static_call.php.inc new file mode 100644 index 00000000..b46bac6e --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/FlipAssertRector/Fixture/cover_external_static_call.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/src/Enum/PHPUnitClassName.php b/src/Enum/PHPUnitClassName.php new file mode 100644 index 00000000..51708c6a --- /dev/null +++ b/src/Enum/PHPUnitClassName.php @@ -0,0 +1,18 @@ +isInTestClass($node)) { - return false; + if ($node instanceof MethodCall) { + return $this->isInTestClass($node); + } + + if ($node instanceof StaticCall) { + $classType = $this->nodeTypeResolver->getType($node->class); + if ($classType instanceof ObjectType) { + if ($classType->isInstanceOf(PHPUnitClassName::TEST_CASE)->yes()) { + return true; + } + + if ($classType->isInstanceOf(PHPUnitClassName::ASSERT)->yes()) { + return true; + } + } } - return $node instanceof MethodCall || $node instanceof StaticCall; + return false; } } From a880233a8825d6b66eb81f3f7cd9890fc3739b04 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 19 Jan 2025 18:38:00 +0000 Subject: [PATCH 2/2] add null/false/true support --- .../Fixture/assert_same_null.php.inc | 31 +++++++++++++++++++ .../Rector/MethodCall/FlipAssertRector.php | 5 +++ 2 files changed, 36 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/FlipAssertRector/Fixture/assert_same_null.php.inc diff --git a/rules-tests/CodeQuality/Rector/MethodCall/FlipAssertRector/Fixture/assert_same_null.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/FlipAssertRector/Fixture/assert_same_null.php.inc new file mode 100644 index 00000000..7874bf5c --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/FlipAssertRector/Fixture/assert_same_null.php.inc @@ -0,0 +1,31 @@ +assertSame($result, null); + $this->assertNotSame($result, false); + } +} + +?> +----- +assertSame(null, $result); + $this->assertNotSame(false, $result); + } +} + +?> diff --git a/rules/CodeQuality/Rector/MethodCall/FlipAssertRector.php b/rules/CodeQuality/Rector/MethodCall/FlipAssertRector.php index 64a2e303..d762edb6 100644 --- a/rules/CodeQuality/Rector/MethodCall/FlipAssertRector.php +++ b/rules/CodeQuality/Rector/MethodCall/FlipAssertRector.php @@ -7,6 +7,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; use PhpParser\Node\Expr\ClassConstFetch; +use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Scalar; @@ -119,6 +120,10 @@ private function isScalarValue(Expr $expr): bool return true; } + if ($expr instanceof ConstFetch) { + return true; + } + return $expr instanceof ClassConstFetch; } }