Skip to content

Commit

Permalink
Leverage str_contains/str_starts_with
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander M. Turek <me@derrabus.de>
  • Loading branch information
derrabus authored and nicolas-grekas committed Jul 21, 2021
1 parent a5383c7 commit c534a7a
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Encoder/CsvEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function decode($data, $format, array $context = [])
fwrite($handle, $data);
rewind($handle);

if (0 === strpos($data, self::UTF8_BOM)) {
if (str_starts_with($data, self::UTF8_BOM)) {
fseek($handle, \strlen(self::UTF8_BOM));
}

Expand Down
4 changes: 2 additions & 2 deletions Encoder/XmlEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ final protected function appendComment(\DOMNode $node, string $data): bool
final protected function isElementNameValid(string $name): bool
{
return $name &&
false === strpos($name, ' ') &&
!str_contains($name, ' ') &&
preg_match('#^[\pL_][\pL0-9._:-]*$#ui', $name);
}

Expand Down Expand Up @@ -421,7 +421,7 @@ private function buildXml(\DOMNode $parentNode, $data, string $xmlRootNodeName =
if (\is_array($data) || ($data instanceof \Traversable && (null === $this->serializer || !$this->serializer->supportsNormalization($data, $this->format)))) {
foreach ($data as $key => $data) {
//Ah this is the magic @ attribute types.
if (0 === strpos($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
if (str_starts_with($key, '@') && $this->isElementNameValid($attributeName = substr($key, 1))) {
if (!is_scalar($data)) {
$data = $this->serializer->normalize($data, $this->format, $this->context);
}
Expand Down
2 changes: 1 addition & 1 deletion Normalizer/AbstractObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ private function validateAndDenormalize(string $currentClass, string $attribute,
// PHP's json_decode automatically converts Numbers without a decimal part to integers.
// To circumvent this behavior, integers are converted to floats when denormalizing JSON based formats and when
// a float is expected.
if (Type::BUILTIN_TYPE_FLOAT === $builtinType && \is_int($data) && false !== strpos($format, JsonEncoder::FORMAT)) {
if (Type::BUILTIN_TYPE_FLOAT === $builtinType && \is_int($data) && str_contains($format, JsonEncoder::FORMAT)) {

This comment has been minimized.

Copy link
@defixje

defixje Feb 23, 2022

This will fail. $format may be null (as you can see in the parameter signature of this method) then the str_contains will fail because it expects an string instead of null

This comment has been minimized.

Copy link
@nicolas-grekas

nicolas-grekas Feb 23, 2022

Member

please send a PR to fix it, branch 4.4

This comment has been minimized.

Copy link
@defixje

defixje Feb 23, 2022

Will do

return (float) $data;
}

Expand Down
8 changes: 4 additions & 4 deletions Normalizer/GetSetMethodNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ private function isGetMethod(\ReflectionMethod $method): bool
return
!$method->isStatic() &&
(
((0 === strpos($method->name, 'get') && 3 < $methodLength) ||
(0 === strpos($method->name, 'is') && 2 < $methodLength) ||
(0 === strpos($method->name, 'has') && 3 < $methodLength)) &&
((str_starts_with($method->name, 'get') && 3 < $methodLength) ||
(str_starts_with($method->name, 'is') && 2 < $methodLength) ||
(str_starts_with($method->name, 'has') && 3 < $methodLength)) &&
0 === $method->getNumberOfRequiredParameters()
)
;
Expand All @@ -108,7 +108,7 @@ protected function extractAttributes($object, $format = null, array $context = [
continue;
}

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

if ($this->isAllowedAttribute($object, $attributeName, $format, $context)) {
$attributes[] = $attributeName;
Expand Down
4 changes: 2 additions & 2 deletions Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ protected function extractAttributes($object, $format = null, array $context = [
$name = $reflMethod->name;
$attributeName = null;

if (0 === strpos($name, 'get') || 0 === strpos($name, 'has')) {
if (str_starts_with($name, 'get') || str_starts_with($name, 'has')) {
// getters and hassers
$attributeName = substr($name, 3);

if (!$reflClass->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
}
} elseif (0 === strpos($name, 'is')) {
} elseif (str_starts_with($name, 'is')) {
// issers
$attributeName = substr($name, 2);

Expand Down

0 comments on commit c534a7a

Please sign in to comment.