From 7bc613ffed5a63b07ed94301f3a1ed2c9a9a7229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 8 Apr 2024 12:25:17 +0200 Subject: [PATCH] Leverage PHP 8.0 string functions --- src/functions.php | 3 ++- tests/UnifiedSpecTests/Constraint/Matches.php | 6 +++--- tests/UnifiedSpecTests/UnifiedTestRunner.php | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/functions.php b/src/functions.php index 1f1d5f722..bb7dfb23c 100644 --- a/src/functions.php +++ b/src/functions.php @@ -45,6 +45,7 @@ use function is_string; use function MongoDB\BSON\fromPHP; use function MongoDB\BSON\toPHP; +use function str_ends_with; use function substr; /** @@ -497,7 +498,7 @@ function create_field_path_type_map(array $typeMap, string $fieldPath): array /* Special case if we want to convert an array, in which case we need to * ensure that the field containing the array is exposed as an array, * instead of the type given in the type map's array key. */ - if (substr($fieldPath, -2, 2) === '.$') { + if (str_ends_with($fieldPath, '.$')) { $typeMap['fieldPaths'][substr($fieldPath, 0, -2)] = 'array'; } diff --git a/tests/UnifiedSpecTests/Constraint/Matches.php b/tests/UnifiedSpecTests/Constraint/Matches.php index 8cc86fa00..caf391f40 100644 --- a/tests/UnifiedSpecTests/Constraint/Matches.php +++ b/tests/UnifiedSpecTests/Constraint/Matches.php @@ -37,7 +37,7 @@ use function PHPUnit\Framework\logicalOr; use function range; use function sprintf; -use function strpos; +use function str_starts_with; use function strrchr; /** @@ -373,7 +373,7 @@ private static function getOperatorName(BSONDocument $document): string { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps foreach ($document as $key => $_) { - if (strpos((string) $key, '$$') === 0) { + if (str_starts_with((string) $key, '$$')) { return $key; } } @@ -394,7 +394,7 @@ private static function isOperator(BSONDocument $document): bool // phpcs:ignore Squiz.NamingConventions.ValidVariableName.NotCamelCaps foreach ($document as $key => $_) { - return strpos((string) $key, '$$') === 0; + return str_starts_with((string) $key, '$$'); } throw new LogicException('should not reach this point'); diff --git a/tests/UnifiedSpecTests/UnifiedTestRunner.php b/tests/UnifiedSpecTests/UnifiedTestRunner.php index 75f795875..1a7e14395 100644 --- a/tests/UnifiedSpecTests/UnifiedTestRunner.php +++ b/tests/UnifiedSpecTests/UnifiedTestRunner.php @@ -35,6 +35,7 @@ use function PHPUnit\Framework\assertNotFalse; use function preg_replace; use function sprintf; +use function str_starts_with; use function strlen; use function strpos; use function substr_replace; @@ -528,7 +529,7 @@ private function createContext(): Context // We assume the internal client URI has multiple mongos hosts $multiMongosUri = $this->internalClientUri; - if (strpos($multiMongosUri, 'mongodb+srv://') === 0) { + if (str_starts_with($multiMongosUri, 'mongodb+srv://')) { /* TODO: If an SRV URI is provided, we can consider connecting and * checking the topology for multiple mongoses and then selecting a * single mongos to reconstruct a single mongos URI; however, that