Skip to content

Commit c534a7a

Browse files
derrabusnicolas-grekas
authored andcommitted
Leverage str_contains/str_starts_with
Signed-off-by: Alexander M. Turek <me@derrabus.de>
1 parent a5383c7 commit c534a7a

File tree

5 files changed

+10
-10
lines changed

5 files changed

+10
-10
lines changed

Encoder/CsvEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public function decode($data, $format, array $context = [])
148148
fwrite($handle, $data);
149149
rewind($handle);
150150

151-
if (0 === strpos($data, self::UTF8_BOM)) {
151+
if (str_starts_with($data, self::UTF8_BOM)) {
152152
fseek($handle, \strlen(self::UTF8_BOM));
153153
}
154154

Encoder/XmlEncoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ final protected function appendComment(\DOMNode $node, string $data): bool
295295
final protected function isElementNameValid(string $name): bool
296296
{
297297
return $name &&
298-
false === strpos($name, ' ') &&
298+
!str_contains($name, ' ') &&
299299
preg_match('#^[\pL_][\pL0-9._:-]*$#ui', $name);
300300
}
301301

@@ -421,7 +421,7 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName =
421421
if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data, $this->format)))) {
422422
foreach ($data as $key => $data) {
423423
//Ah this is the magic @ attribute types.
424-
if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
424+
if (str_starts_with($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
425425
if (!is_scalar($data)) {
426426
$data = $this->serializer->normalize($data, $this->format, $this->context);
427427
}

Normalizer/AbstractObjectNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
462462
// PHP's json_decode automatically converts Numbers without a decimal part to integers.
463463
// To circumvent this behavior, integers are converted to floats when denormalizing JSON based formats and when
464464
// a float is expected.
465-
if (Type::BUILTIN_TYPE_FLOAT === $builtinType && \is_int($data) && false !== strpos($format, JsonEncoder::FORMAT)) {
465+
if (Type::BUILTIN_TYPE_FLOAT === $builtinType && \is_int($data) && str_contains($format, JsonEncoder::FORMAT)) {
466466
return (float) $data;
467467
}
468468

Normalizer/GetSetMethodNormalizer.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ private function isGetMethod(\ReflectionMethod $method): bool
8686
return
8787
!$method->isStatic() &&
8888
(
89-
((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
90-
(0 === strpos($method->name, 'is') && 2 < $methodLength) ||
91-
(0 === strpos($method->name, 'has') && 3 < $methodLength)) &&
89+
((str_starts_with($method->name, 'get') && 3 < $methodLength) ||
90+
(str_starts_with($method->name, 'is') && 2 < $methodLength) ||
91+
(str_starts_with($method->name, 'has') && 3 < $methodLength)) &&
9292
0 === $method->getNumberOfRequiredParameters()
9393
)
9494
;
@@ -108,7 +108,7 @@ protected function extractAttributes($object, $format = null, array $context = [
108108
continue;
109109
}
110110

111-
$attributeName = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));
111+
$attributeName = lcfirst(substr($method->name, str_starts_with($method->name, 'is') ? 2 : 3));
112112

113113
if ($this->isAllowedAttribute($object, $attributeName, $format, $context)) {
114114
$attributes[] = $attributeName;

Normalizer/ObjectNormalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ protected function extractAttributes($object, $format = null, array $context = [
8282
$name = $reflMethod->name;
8383
$attributeName = null;
8484

85-
if (0 === strpos($name, 'get') || 0 === strpos($name, 'has')) {
85+
if (str_starts_with($name, 'get') || str_starts_with($name, 'has')) {
8686
// getters and hassers
8787
$attributeName = substr($name, 3);
8888

8989
if (!$reflClass->hasProperty($attributeName)) {
9090
$attributeName = lcfirst($attributeName);
9191
}
92-
} elseif (0 === strpos($name, 'is')) {
92+
} elseif (str_starts_with($name, 'is')) {
9393
// issers
9494
$attributeName = substr($name, 2);
9595

0 commit comments

Comments
 (0)