Skip to content

Commit

Permalink
Get rid of unused code and automated simplifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Czarnecki committed Dec 9, 2023
1 parent ea21613 commit 81f5e43
Show file tree
Hide file tree
Showing 28 changed files with 80 additions and 74 deletions.
4 changes: 2 additions & 2 deletions src/AbstractVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ protected function getElementType(array $typeArray): ?array

if (isset($typeArray['params'][1]) && \is_array($typeArray['params'][1])) {
return $typeArray['params'][1];
} else {
return $typeArray['params'][0];
}

return $typeArray['params'][0];
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Accessor/DefaultAccessorStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getValue(object $object, PropertyMetadata $metadata, Serializati
return $accessor($object, $metadata->name);
} catch (\Error $e) {
// handle uninitialized properties in PHP >= 7.4
if (preg_match('/^Typed property ([\w\\\\@]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage(), $matches)) {
if (preg_match('/^Typed property ([\w\\\\@]+)::\$(\w+) must not be accessed before initialization$/', $e->getMessage())) {
throw new UninitializedPropertyException(sprintf('Uninitialized property "%s::$%s".', $metadata->class, $metadata->name), 0, $e);
}

Expand Down
4 changes: 3 additions & 1 deletion src/Annotation/ReadOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

declare(strict_types=1);

class_alias('JMS\Serializer\Annotation\DeprecatedReadOnly', 'JMS\Serializer\Annotation\ReadOnly');
use JMS\Serializer\Annotation\DeprecatedReadOnly;

class_alias(DeprecatedReadOnly::class, 'JMS\Serializer\Annotation\ReadOnly');
4 changes: 3 additions & 1 deletion src/Construction/DoctrineObjectConstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public function construct(DeserializationVisitorInterface $visitor, ClassMetadat

if (is_array($data) && !array_key_exists($propertyMetadata->serializedName, $data)) {
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
} elseif (is_object($data) && !property_exists($data, $propertyMetadata->serializedName)) {
}

if (is_object($data) && !property_exists($data, $propertyMetadata->serializedName)) {
return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
}

Expand Down
4 changes: 2 additions & 2 deletions src/DeserializationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getDepth(): int

public function increaseDepth(): void
{
$this->depth += 1;
++$this->depth;
}

public function decreaseDepth(): void
Expand All @@ -39,6 +39,6 @@ public function decreaseDepth(): void
throw new LogicException('Depth cannot be smaller than zero.');
}

$this->depth -= 1;
--$this->depth;
}
}
2 changes: 1 addition & 1 deletion src/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function hasListeners(string $eventName, string $class, string $format):
$this->classListeners[$eventName][$class][$format] = $this->initializeListeners($eventName, $class, $format);
}

return !!$this->classListeners[$eventName][$class][$format];
return (bool) $this->classListeners[$eventName][$class][$format];
}

public function dispatch(string $eventName, string $class, string $format, Event $event): void
Expand Down
20 changes: 10 additions & 10 deletions src/Exclusion/GroupsExclusionStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ public function shouldSkipProperty(PropertyMetadata $property, Context $navigato
}

return $this->shouldSkipUsingGroups($property, $groups);
} else {
if (!$property->groups) {
return !isset($this->groups[self::DEFAULT_GROUP]);
}
}

foreach ($property->groups as $group) {
if (is_scalar($group) && isset($this->groups[$group])) {
return false;
}
}
if (!$property->groups) {
return !isset($this->groups[self::DEFAULT_GROUP]);
}

return true;
foreach ($property->groups as $group) {
if (is_scalar($group) && isset($this->groups[$group])) {
return false;
}
}

return true;
}

private function shouldSkipUsingGroups(PropertyMetadata $property, array $groups): bool
Expand Down
2 changes: 1 addition & 1 deletion src/GraphNavigator/DeserializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function accept($data, ?array $type = null)
// could also simply be an artifical type.
if (null !== $handler = $this->handlerRegistry->getHandler(GraphNavigatorInterface::DIRECTION_DESERIALIZATION, $type['name'], $this->format)) {
try {
$rs = \call_user_func($handler, $this->visitor, $data, $type, $this->context);
$rs = $handler($this->visitor, $data, $type, $this->context);
$this->context->decreaseDepth();

return $rs;
Expand Down
2 changes: 1 addition & 1 deletion src/GraphNavigator/SerializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function accept($data, ?array $type = null)
// could also simply be an artifical type.
if (null !== $handler = $this->handlerRegistry->getHandler(GraphNavigatorInterface::DIRECTION_SERIALIZATION, $type['name'], $this->format)) {
try {
$rs = \call_user_func($handler, $this->visitor, $data, $type, $this->context);
$rs = $handler($this->visitor, $data, $type, $this->context);
$this->context->stopVisiting($data);

return $rs;
Expand Down
1 change: 0 additions & 1 deletion src/Handler/DateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ private function parseDateTime($data, array $type, bool $immutable = false): \Da

private function parseDateInterval(string $data): \DateInterval
{
$dateInterval = null;
try {
$f = 0.0;
if (preg_match('~\.\d+~', $data, $match)) {
Expand Down
14 changes: 7 additions & 7 deletions src/Handler/EnumHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public function serializeEnum(
$valueType = isset($type['params'][2]) ? ['name' => $type['params'][2]] : null;

return $context->getNavigator()->accept($enum->value, $valueType);
} else {
return $context->getNavigator()->accept($enum->name);
}

return $context->getNavigator()->accept($enum->name);
}

/**
Expand All @@ -81,12 +81,12 @@ public function deserializeEnum(DeserializationVisitorInterface $visitor, $data,
}

return $enumType::from($caseValue);
} else {
if (!$ref->hasCase($caseValue)) {
throw new InvalidMetadataException(sprintf('The type "%s" does not have the case "%s"', $ref->getName(), $caseValue));
}
}

return $ref->getCase($caseValue)->getValue();
if (!$ref->hasCase($caseValue)) {
throw new InvalidMetadataException(sprintf('The type "%s" does not have the case "%s"', $ref->getName(), $caseValue));
}

return $ref->getCase($caseValue)->getValue();
}
}
4 changes: 2 additions & 2 deletions src/Handler/FormErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ private function getErrorMessage(FormError $error): ?string
if (null !== $error->getMessagePluralization()) {
if ($this->translator instanceof TranslatorContract) {
return $this->translator->trans($error->getMessageTemplate(), ['%count%' => $error->getMessagePluralization()] + $error->getMessageParameters(), $this->translationDomain);
} else {
return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), $this->translationDomain);
}

return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), $this->translationDomain);
}

return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), $this->translationDomain);
Expand Down
6 changes: 1 addition & 5 deletions src/Handler/HandlerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ public function registerHandler(int $direction, string $typeName, string $format
*/
public function getHandler(int $direction, string $typeName, string $format)
{
if (!isset($this->handlers[$direction][$typeName][$format])) {
return null;
}

return $this->handlers[$direction][$typeName][$format];
return $this->handlers[$direction][$typeName][$format] ?? null;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/JsonSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function visitDouble(float $data, array $type)
*/
public function visitArray(array $data, array $type)
{
\array_push($this->dataStack, $data);
$this->dataStack[] = $data;

$rs = isset($type['params'][1]) ? new \ArrayObject() : [];

Expand Down Expand Up @@ -117,7 +117,7 @@ public function visitArray(array $data, array $type)

public function startVisitingObject(ClassMetadata $metadata, object $data, array $type): void
{
\array_push($this->dataStack, $this->data);
$this->dataStack[] = $this->data;
$this->data = true === $metadata->isMap ? new \ArrayObject() : [];
}

Expand Down
4 changes: 3 additions & 1 deletion src/Metadata/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ public function merge(MergeableInterface $object): void
$this->discriminatorBaseClass,
$this->discriminatorBaseClass
));
} elseif (!$this->discriminatorFieldName && $object->discriminatorFieldName) {
}

if (!$this->discriminatorFieldName && $object->discriminatorFieldName) {
$this->discriminatorFieldName = $object->discriminatorFieldName;
$this->discriminatorMap = $object->discriminatorMap;
}
Expand Down
6 changes: 1 addition & 5 deletions src/Metadata/Driver/AbstractDoctrineTypeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ protected function tryLoadingDoctrineMetadata(string $className): ?DoctrineClass

protected function normalizeFieldType(string $type): ?string
{
if (!isset($this->fieldMapping[$type])) {
return null;
}

return $this->fieldMapping[$type];
return $this->fieldMapping[$type] ?? null;
}
}
12 changes: 9 additions & 3 deletions src/Metadata/Driver/AnnotationOrAttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,19 @@ public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadat
if ($annot instanceof PreSerialize) {
$classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->name));
continue 2;
} elseif ($annot instanceof PostDeserialize) {
}

if ($annot instanceof PostDeserialize) {
$classMetadata->addPostDeserializeMethod(new MethodMetadata($name, $method->name));
continue 2;
} elseif ($annot instanceof PostSerialize) {
}

if ($annot instanceof PostSerialize) {
$classMetadata->addPostSerializeMethod(new MethodMetadata($name, $method->name));
continue 2;
} elseif ($annot instanceof VirtualProperty) {
}

if ($annot instanceof VirtualProperty) {
$virtualPropertyMetadata = new VirtualPropertyMetadata($name, $method->name);
$propertiesMetadata[] = $virtualPropertyMetadata;
$propertiesAnnotations[] = $methodAnnotations;
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Driver/DefaultValuePropertyDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata

\assert($classMetadata instanceof SerializerClassMetadata);

foreach ($classMetadata->propertyMetadata as $key => $propertyMetadata) {
foreach ($classMetadata->propertyMetadata as $propertyMetadata) {
\assert($propertyMetadata instanceof PropertyMetadata);
if (null !== $propertyMetadata->hasDefault) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Driver/DocBlockDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata

// We base our scan on the internal driver's property list so that we
// respect any internal allow/blocklist like in the AnnotationDriver
foreach ($classMetadata->propertyMetadata as $key => $propertyMetadata) {
foreach ($classMetadata->propertyMetadata as $propertyMetadata) {
// If the inner driver provides a type, don't guess anymore.
if ($propertyMetadata->type) {
continue;
Expand Down
8 changes: 4 additions & 4 deletions src/Metadata/Driver/DocBlockDriver/DocBlockTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ private function hasGlobalNamespacePrefix(string $typeHint): bool
private function gatherGroupUseStatements(string $classContents): array
{
$foundUseStatements = [];
preg_match_all(self::GROUP_USE_STATEMENTS_REGEX, $classContents, $foundGroupUseStatements);
for ($useStatementIndex = 0; $useStatementIndex < count($foundGroupUseStatements[0]); $useStatementIndex++) {
$found = preg_match_all(self::GROUP_USE_STATEMENTS_REGEX, $classContents, $foundGroupUseStatements);
for ($useStatementIndex = 0; $useStatementIndex < $found; $useStatementIndex++) {
foreach (explode(',', $foundGroupUseStatements[2][$useStatementIndex]) as $singleUseStatement) {
$foundUseStatements[] = trim($foundGroupUseStatements[1][$useStatementIndex]) . trim($singleUseStatement);
}
Expand All @@ -318,8 +318,8 @@ private function gatherGroupUseStatements(string $classContents): array
private function gatherSingleUseStatements(string $classContents): array
{
$foundUseStatements = [];
preg_match_all(self::SINGLE_USE_STATEMENTS_REGEX, $classContents, $foundSingleUseStatements);
for ($useStatementIndex = 0; $useStatementIndex < count($foundSingleUseStatements[0]); $useStatementIndex++) {
$found = preg_match_all(self::SINGLE_USE_STATEMENTS_REGEX, $classContents, $foundSingleUseStatements);
for ($useStatementIndex = 0; $useStatementIndex < $found; $useStatementIndex++) {
$foundUseStatements[] = trim($foundSingleUseStatements[1][$useStatementIndex]);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Metadata/Driver/XmlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected function loadMetadataFromFile(\ReflectionClass $class, string $path):
}

if (null !== $excludeIf = $pElem->attributes()->{'expose-if'}) {
$pMetadata->excludeIf = $this->parseExpression('!(' . (string) $excludeIf . ')');
$pMetadata->excludeIf = $this->parseExpression('!(' . $excludeIf . ')');
$isExpose = true;
}

Expand Down Expand Up @@ -348,8 +348,8 @@ protected function loadMetadataFromFile(\ReflectionClass $class, string $path):
}

if (
(ExclusionPolicy::NONE === (string) $exclusionPolicy && !$isExclude)
|| (ExclusionPolicy::ALL === (string) $exclusionPolicy && $isExpose)
(ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
|| (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)
) {
$metadata->addPropertyMetadata($pMetadata);
}
Expand Down
5 changes: 1 addition & 4 deletions src/Metadata/PropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,7 @@ public static function isCollectionList(?array $type = null): bool

public static function isCollectionMap(?array $type = null): bool
{
return is_array($type)
&& 'array' === $type['name']
&& isset($type['params'][0])
&& isset($type['params'][1]);
return isset($type['params'][0], $type['params'][1]) && is_array($type) && 'array' === $type['name'];
}

protected function serializeToArray(): array
Expand Down
4 changes: 3 additions & 1 deletion src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ private function findInitialType(?string $type, SerializationContext $context):
{
if (null !== $type) {
return $type;
} elseif ($context->hasAttribute('initial_type')) {
}

if ($context->hasAttribute('initial_type')) {
return $context->getAttribute('initial_type');
}

Expand Down
4 changes: 2 additions & 2 deletions src/Type/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ protected function getType(&$value)

// Recognize quoted strings
case "'" === $value[0]:
$value = str_replace("''", "'", substr($value, 1, strlen($value) - 2));
$value = str_replace("''", "'", substr($value, 1, -1));

return self::T_STRING;

case '"' === $value[0]:
$value = str_replace('""', '"', substr($value, 1, strlen($value) - 2));
$value = str_replace('""', '"', substr($value, 1, -1));

return self::T_STRING;

Expand Down
12 changes: 8 additions & 4 deletions src/Type/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,21 @@ private function visit()
}

if (Lexer::T_FLOAT === $this->lexer->token->type) {
return floatval($this->lexer->token->value);
} elseif (Lexer::T_INTEGER === $this->lexer->token->type) {
return intval($this->lexer->token->value);
return (float) $this->lexer->token->value;
}

if (Lexer::T_INTEGER === $this->lexer->token->type) {
return (int) $this->lexer->token->value;
} elseif (Lexer::T_NULL === $this->lexer->token->type) {
return null;
} elseif (Lexer::T_STRING === $this->lexer->token->type) {
return $this->lexer->token->value;
} elseif (Lexer::T_IDENTIFIER === $this->lexer->token->type) {
if ($this->lexer->isNextToken(Lexer::T_TYPE_START)) {
return $this->visitCompoundType();
} elseif ($this->lexer->isNextToken(Lexer::T_ARRAY_START)) {
}

if ($this->lexer->isNextToken(Lexer::T_ARRAY_START)) {
return $this->visitArrayType();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Visitor/Factory/XmlSerializationVisitorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function setDefaultEncoding(string $encoding): self

public function setFormatOutput(bool $formatOutput): self
{
$this->formatOutput = (bool) $formatOutput;
$this->formatOutput = $formatOutput;

return $this;
}
Expand Down
8 changes: 5 additions & 3 deletions src/XmlDeserializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,13 @@ public function visitBoolean($data, array $type): bool

if ('true' === $data || '1' === $data) {
return true;
} elseif ('false' === $data || '0' === $data) {
}

if ('false' === $data || '0' === $data) {
return false;
} else {
throw new RuntimeException(sprintf('Could not convert data to boolean. Expected "true", "false", "1" or "0", but got %s.', json_encode($data)));
}

throw new RuntimeException(sprintf('Could not convert data to boolean. Expected "true", "false", "1" or "0", but got %s.', json_encode($data)));
}

/**
Expand Down
Loading

0 comments on commit 81f5e43

Please sign in to comment.