From 3a4abd4a1087983ded570bd19c8bb07a7cdb0a95 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 29 Jun 2026 16:25:02 +0200 Subject: [PATCH] [CodeQuality] Detect Behat context by interface, not class name suffix isBehatContext() matched any class whose name ends with 'Context' via str_ends_with(). That false-positives on production classes like SchemaValidationContext, rewriting their assert() calls into \PHPUnit\Framework\Assert:: calls and pulling PHPUnit into non-test code. Detect a real Behat context by whether it implements Behat\Behat\Context\Context instead. --- .../Fixture/assert_compare_context.php.inc | 8 ++++++-- .../Fixture/assert_null_compare_context.php.inc | 8 ++++++-- .../Fixture/method_exists_context.php.inc | 8 ++++++-- .../Fixture/simple_context.php.inc | 8 ++++++-- .../Fixture/skip_non_behat_context_class.php.inc | 11 +++++++++++ .../FuncCall/AssertFuncCallToPHPUnitAssertRector.php | 9 ++++----- 6 files changed, 39 insertions(+), 13 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/skip_non_behat_context_class.php.inc diff --git a/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_compare_context.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_compare_context.php.inc index c58b51445..9b3f4ee90 100644 --- a/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_compare_context.php.inc +++ b/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_compare_context.php.inc @@ -2,7 +2,9 @@ namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture; -final class AssertCompareContext +use Behat\Behat\Context\Context; + +final class AssertCompareContext implements Context { public function some($response) { @@ -17,7 +19,9 @@ final class AssertCompareContext namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture; -final class AssertCompareContext +use Behat\Behat\Context\Context; + +final class AssertCompareContext implements Context { public function some($response) { diff --git a/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_null_compare_context.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_null_compare_context.php.inc index f2c349651..3330f3291 100644 --- a/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_null_compare_context.php.inc +++ b/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/assert_null_compare_context.php.inc @@ -2,7 +2,9 @@ namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture; -final class AssertNullCompareContext +use Behat\Behat\Context\Context; + +final class AssertNullCompareContext implements Context { public function some($response) { @@ -16,7 +18,9 @@ final class AssertNullCompareContext namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture; -final class AssertNullCompareContext +use Behat\Behat\Context\Context; + +final class AssertNullCompareContext implements Context { public function some($response) { diff --git a/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/method_exists_context.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/method_exists_context.php.inc index 59edebfa0..971739146 100644 --- a/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/method_exists_context.php.inc +++ b/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/method_exists_context.php.inc @@ -2,7 +2,9 @@ namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture; -final class MethodExistsContext +use Behat\Behat\Context\Context; + +final class MethodExistsContext implements Context { public function some($response) { @@ -16,7 +18,9 @@ final class MethodExistsContext namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture; -final class MethodExistsContext +use Behat\Behat\Context\Context; + +final class MethodExistsContext implements Context { public function some($response) { diff --git a/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/simple_context.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/simple_context.php.inc index 8c16d4857..96abbb26d 100644 --- a/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/simple_context.php.inc +++ b/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/simple_context.php.inc @@ -2,7 +2,9 @@ namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture; -final class SimpleContext +use Behat\Behat\Context\Context; + +final class SimpleContext implements Context { public function some($response) { @@ -18,7 +20,9 @@ final class SimpleContext namespace Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\Fixture; -final class SimpleContext +use Behat\Behat\Context\Context; + +final class SimpleContext implements Context { public function some($response) { diff --git a/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/skip_non_behat_context_class.php.inc b/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/skip_non_behat_context_class.php.inc new file mode 100644 index 000000000..15944442d --- /dev/null +++ b/rules-tests/CodeQuality/Rector/FuncCall/AssertFuncCallToPHPUnitAssertRector/Fixture/skip_non_behat_context_class.php.inc @@ -0,0 +1,11 @@ +getClassReflection() instanceof ClassReflection) { + $classReflection = $scope->getClassReflection(); + if (! $classReflection instanceof ClassReflection) { return false; } - $className = $scope->getClassReflection() - ->getName(); - // special case with static call - return str_ends_with($className, 'Context'); + return $classReflection->is(BehatClassName::CONTEXT); } /**