From 9cd6f342736932ee4f758e032630f52646d8774f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Tue, 19 Mar 2024 21:19:59 +0100 Subject: [PATCH] styles: Leverage the PHP8 new str functions Related to #1929. --- .php-cs-fixer.php | 3 +-- .../EnvManipulatorCodeDetector.php | 4 ++-- .../AutoReview/IntegrationGroup/IntegrationGroupTest.php | 4 ++-- .../AutoReview/IntegrationGroup/IoCodeDetector.php | 4 ++-- tests/phpunit/AutoReview/SourceTestClassNameScheme.php | 4 ++-- tests/phpunit/Console/E2ETest.php | 9 +++++---- tests/phpunit/Process/Runner/InitialTestsRunnerTest.php | 4 ++-- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php index 8beaab0d53..a358142d4a 100644 --- a/.php-cs-fixer.php +++ b/.php-cs-fixer.php @@ -127,8 +127,7 @@ 'syntax' => 'short', ], 'logical_operators' => true, - // TODO: enable - 'modernize_strpos' => false, + 'modernize_strpos' => true, 'native_constant_invocation' => true, 'native_function_invocation' => [ 'include' => ['@internal'], diff --git a/tests/phpunit/AutoReview/EnvVariableManipulation/EnvManipulatorCodeDetector.php b/tests/phpunit/AutoReview/EnvVariableManipulation/EnvManipulatorCodeDetector.php index c2508798f0..636dcacf8b 100644 --- a/tests/phpunit/AutoReview/EnvVariableManipulation/EnvManipulatorCodeDetector.php +++ b/tests/phpunit/AutoReview/EnvVariableManipulation/EnvManipulatorCodeDetector.php @@ -37,7 +37,7 @@ use Infection\CannotBeInstantiated; use function sprintf; -use function strpos; +use function str_contains; final class EnvManipulatorCodeDetector { @@ -56,7 +56,7 @@ final class EnvManipulatorCodeDetector public static function codeManipulatesEnvVariables(string $code): bool { foreach (self::getStatements() as $statement) { - if (strpos($code, $statement) !== false) { + if (str_contains($code, $statement)) { return true; } } diff --git a/tests/phpunit/AutoReview/IntegrationGroup/IntegrationGroupTest.php b/tests/phpunit/AutoReview/IntegrationGroup/IntegrationGroupTest.php index 0f499bd74d..9380bbe679 100644 --- a/tests/phpunit/AutoReview/IntegrationGroup/IntegrationGroupTest.php +++ b/tests/phpunit/AutoReview/IntegrationGroup/IntegrationGroupTest.php @@ -40,7 +40,7 @@ use PHPUnit\Framework\TestCase; use ReflectionClass; use function sprintf; -use function strpos; +use function str_contains; final class IntegrationGroupTest extends TestCase { @@ -69,7 +69,7 @@ public function test_the_test_cases_requiring_io_operations_belongs_to_the_integ ), ); - if (strpos($phpDoc, '@group integration') === false) { + if (!str_contains($phpDoc, '@group integration')) { $this->fail(sprintf( <<<'TXT' Expected the test case "%s" to have the annotation `@group integration` as I/O operations have been diff --git a/tests/phpunit/AutoReview/IntegrationGroup/IoCodeDetector.php b/tests/phpunit/AutoReview/IntegrationGroup/IoCodeDetector.php index b70b94d14d..1be69f28ae 100644 --- a/tests/phpunit/AutoReview/IntegrationGroup/IoCodeDetector.php +++ b/tests/phpunit/AutoReview/IntegrationGroup/IoCodeDetector.php @@ -40,7 +40,7 @@ use function Safe\file_get_contents; use function Safe\preg_match_all; use function sprintf; -use function strpos; +use function str_contains; final class IoCodeDetector { @@ -148,7 +148,7 @@ final class IoCodeDetector public static function codeContainsIoOperations(string $code): bool { foreach (self::getStatements() as $statement) { - if (strpos($code, $statement) !== false) { + if (str_contains($code, $statement)) { return true; } } diff --git a/tests/phpunit/AutoReview/SourceTestClassNameScheme.php b/tests/phpunit/AutoReview/SourceTestClassNameScheme.php index b84fb5386d..91d36f72a1 100644 --- a/tests/phpunit/AutoReview/SourceTestClassNameScheme.php +++ b/tests/phpunit/AutoReview/SourceTestClassNameScheme.php @@ -38,8 +38,8 @@ use Infection\CannotBeInstantiated; use function Safe\preg_match; use function Safe\preg_replace; +use function str_contains; use function str_replace; -use function strpos; final class SourceTestClassNameScheme { @@ -56,7 +56,7 @@ public static function getSourceClassName(string $testCaseClassName): string public static function getTestClassName(string $sourceClassName): string { - if (strpos($sourceClassName, 'Infection\\Tests') !== false) { + if (str_contains($sourceClassName, 'Infection\\Tests')) { return $sourceClassName . 'Test'; } diff --git a/tests/phpunit/Console/E2ETest.php b/tests/phpunit/Console/E2ETest.php index 2bbab81bde..30673f8653 100644 --- a/tests/phpunit/Console/E2ETest.php +++ b/tests/phpunit/Console/E2ETest.php @@ -60,8 +60,9 @@ use function Safe\getcwd; use function Safe\ini_get; use function sprintf; +use function str_contains; use function str_replace; -use function strpos; +use function str_starts_with; use Symfony\Component\Console\Input\ArgvInput; use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\Finder\Finder; @@ -105,7 +106,7 @@ protected function setUp(): void } // Without overcommit this test fails with `proc_open(): fork failed - Cannot allocate memory` - if (strpos(PHP_OS, 'Linux') === 0 && + if (str_starts_with(PHP_OS, 'Linux') && is_readable('/proc/sys/vm/overcommit_memory') && (int) file_get_contents('/proc/sys/vm/overcommit_memory') === 2) { $this->markTestSkipped('This test needs copious amounts of virtual memory. It will fail unless it is allowed to overcommit memory.'); @@ -281,7 +282,7 @@ private function installComposerDeps(): void foreach ($map as $namespace => $paths) { foreach ($paths as $path) { - if (strpos($path, $vendorDir) !== false) { + if (str_contains($path, $vendorDir)) { // Skip known dependency from autoloading continue 2; } @@ -294,7 +295,7 @@ private function installComposerDeps(): void foreach ($mapPsr0 as $namespace => $paths) { foreach ($paths as $path) { - if (strpos($path, $vendorDir) !== false) { + if (str_contains($path, $vendorDir)) { // Skip known dependency from autoloading continue 2; } diff --git a/tests/phpunit/Process/Runner/InitialTestsRunnerTest.php b/tests/phpunit/Process/Runner/InitialTestsRunnerTest.php index d87f6ddf6e..8a6d8016fa 100644 --- a/tests/phpunit/Process/Runner/InitialTestsRunnerTest.php +++ b/tests/phpunit/Process/Runner/InitialTestsRunnerTest.php @@ -46,7 +46,7 @@ use const PHP_SAPI; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use function strpos; +use function str_contains; use Symfony\Component\Process\Exception\RuntimeException; use Symfony\Component\Process\InputStream; use Symfony\Component\Process\PhpExecutableFinder; @@ -154,7 +154,7 @@ public function test_it_stops_the_process_execution_on_the_first_error(): void $this->runner->run($testFrameworkExtraOptions, $phpExtraOptions, $skipCoverage); } catch (RuntimeException $e) { // Signal 11, AKA "segmentation fault", is not something we can do anything about - if (extension_loaded('xdebug') && strpos($e->getMessage(), 'The process has been signaled with signal "11"') !== false) { + if (extension_loaded('xdebug') && str_contains($e->getMessage(), 'The process has been signaled with signal "11"')) { $this->markTestIncomplete($e->getMessage()); }