[Serializer] optims and cleanup

This commit is contained in:
Kévin Dunglas 2018-02-11 19:19:04 +01:00 committed by Nicolas Grekas
parent 478fbdc241
commit 8ee83879eb
13 changed files with 65 additions and 65 deletions

View File

@ -34,16 +34,16 @@ class Groups
public function __construct(array $data)
{
if (!isset($data['value']) || !$data['value']) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', get_class($this)));
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" cannot be empty.', \get_class($this)));
}
if (!is_array($data['value'])) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an array of strings.', get_class($this)));
if (!\is_array($data['value'])) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an array of strings.', \get_class($this)));
}
foreach ($data['value'] as $group) {
if (!is_string($group)) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an array of strings.', get_class($this)));
if (!\is_string($group)) {
throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be an array of strings.', \get_class($this)));
}
}

View File

@ -96,7 +96,7 @@ class JsonEncoder implements EncoderInterface, DecoderInterface
*/
public static function getLastErrorMessage()
{
if (function_exists('json_last_error_msg')) {
if (\function_exists('json_last_error_msg')) {
return json_last_error_msg();
}

View File

@ -179,7 +179,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
*/
final protected function appendXMLString(\DOMNode $node, $val)
{
if (strlen($val) > 0) {
if (\strlen($val) > 0) {
$frag = $this->dom->createDocumentFragment();
$frag->appendXML($val);
$node->appendChild($frag);
@ -260,17 +260,17 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
$value = $this->parseXmlValue($node);
if (!count($data)) {
if (!\count($data)) {
return $value;
}
if (!is_array($value)) {
if (!\is_array($value)) {
$data['#'] = $value;
return $data;
}
if (1 === count($value) && key($value)) {
if (1 === \count($value) && key($value)) {
$data[key($value)] = current($value);
return $data;
@ -326,7 +326,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
return $node->nodeValue;
}
if (1 === $node->childNodes->length && in_array($node->firstChild->nodeType, array(XML_TEXT_NODE, XML_CDATA_SECTION_NODE))) {
if (1 === $node->childNodes->length && \in_array($node->firstChild->nodeType, array(XML_TEXT_NODE, XML_CDATA_SECTION_NODE))) {
return $node->firstChild->nodeValue;
}
@ -351,7 +351,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
}
foreach ($value as $key => $val) {
if (is_array($val) && 1 === count($val)) {
if (\is_array($val) && 1 === \count($val)) {
$value[$key] = current($val);
}
}
@ -374,7 +374,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
{
$append = true;
if (is_array($data) || ($data instanceof \Traversable && !$this->serializer->supportsNormalization($data, $this->format))) {
if (\is_array($data) || ($data instanceof \Traversable && !$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))) {
@ -384,7 +384,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
$parentNode->setAttribute($attributeName, $data);
} elseif ('#' === $key) {
$append = $this->selectNodeType($parentNode, $data);
} elseif (is_array($data) && false === is_numeric($key)) {
} elseif (\is_array($data) && false === is_numeric($key)) {
// Is this array fully numeric keys?
if (ctype_digit(implode('', array_keys($data)))) {
/*
@ -408,7 +408,7 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
return $append;
}
if (is_object($data)) {
if (\is_object($data)) {
$data = $this->serializer->normalize($data, $this->format, $this->context);
if (null !== $data && !is_scalar($data)) {
return $this->buildXml($parentNode, $data, $xmlRootNodeName);
@ -477,22 +477,22 @@ class XmlEncoder extends SerializerAwareEncoder implements EncoderInterface, Dec
*/
private function selectNodeType(\DOMNode $node, $val)
{
if (is_array($val)) {
if (\is_array($val)) {
return $this->buildXml($node, $val);
} elseif ($val instanceof \SimpleXMLElement) {
$child = $this->dom->importNode(dom_import_simplexml($val), true);
$node->appendChild($child);
} elseif ($val instanceof \Traversable) {
$this->buildXml($node, $val);
} elseif (is_object($val)) {
} elseif (\is_object($val)) {
return $this->selectNodeType($node, $this->serializer->normalize($val, $this->format, $this->context));
} elseif (is_numeric($val)) {
return $this->appendText($node, (string) $val);
} elseif (is_string($val) && $this->needsCdataWrapping($val)) {
} elseif (\is_string($val) && $this->needsCdataWrapping($val)) {
return $this->appendCData($node, $val);
} elseif (is_string($val)) {
} elseif (\is_string($val)) {
return $this->appendText($node, $val);
} elseif (is_bool($val)) {
} elseif (\is_bool($val)) {
return $this->appendText($node, (int) $val);
} elseif ($val instanceof \DOMNode) {
$child = $this->dom->importNode($val, true);

View File

@ -55,7 +55,7 @@ class AttributeMetadata implements AttributeMetadataInterface
*/
public function addGroup($group)
{
if (!in_array($group, $this->groups)) {
if (!\in_array($group, $this->groups)) {
$this->groups[] = $group;
}
}

View File

@ -40,7 +40,7 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
{
$class = $this->getClass($value);
if (!$class) {
throw new InvalidArgumentException(sprintf('Cannot create metadata for non-objects. Got: "%s"', gettype($value)));
throw new InvalidArgumentException(sprintf('Cannot create metadata for non-objects. Got: "%s"', \gettype($value)));
}
if (isset($this->loadedClasses[$class])) {
@ -96,10 +96,10 @@ class ClassMetadataFactory implements ClassMetadataFactoryInterface
*/
private function getClass($value)
{
if (!is_object($value) && !is_string($value)) {
if (!\is_object($value) && !\is_string($value)) {
return false;
}
return ltrim(is_object($value) ? get_class($value) : $value, '\\');
return ltrim(\is_object($value) ? \get_class($value) : $value, '\\');
}
}

View File

@ -40,7 +40,7 @@ class LoaderChain implements LoaderInterface
{
foreach ($loaders as $loader) {
if (!$loader instanceof LoaderInterface) {
throw new MappingException(sprintf('Class %s is expected to implement LoaderInterface', get_class($loader)));
throw new MappingException(sprintf('Class %s is expected to implement LoaderInterface', \get_class($loader)));
}
}

View File

@ -30,7 +30,7 @@ class YamlFileLoader extends FileLoader
*
* @var array
*/
private $classes = null;
private $classes;
/**
* {@inheritdoc}
@ -53,7 +53,7 @@ class YamlFileLoader extends FileLoader
}
// not an array
if (!is_array($classes)) {
if (!\is_array($classes)) {
throw new MappingException(sprintf('The file "%s" must contain a YAML array.', $this->file));
}
@ -66,7 +66,7 @@ class YamlFileLoader extends FileLoader
$yaml = $this->classes[$classMetadata->getName()];
if (isset($yaml['attributes']) && is_array($yaml['attributes'])) {
if (isset($yaml['attributes']) && \is_array($yaml['attributes'])) {
$attributesMetadata = $classMetadata->getAttributesMetadata();
foreach ($yaml['attributes'] as $attribute => $data) {
@ -78,12 +78,12 @@ class YamlFileLoader extends FileLoader
}
if (isset($data['groups'])) {
if (!is_array($data['groups'])) {
if (!\is_array($data['groups'])) {
throw new MappingException(sprintf('The "groups" key must be an array of strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
}
foreach ($data['groups'] as $group) {
if (!is_string($group)) {
if (!\is_string($group)) {
throw new MappingException(sprintf('Group names must be strings in "%s" for the attribute "%s" of the class "%s".', $this->file, $attribute, $classMetadata->getName()));
}

View File

@ -36,11 +36,11 @@ class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
*/
public function normalize($propertyName)
{
if (null === $this->attributes || in_array($propertyName, $this->attributes)) {
if (null === $this->attributes || \in_array($propertyName, $this->attributes)) {
$lcPropertyName = lcfirst($propertyName);
$snakeCasedName = '';
$len = strlen($lcPropertyName);
$len = \strlen($lcPropertyName);
for ($i = 0; $i < $len; ++$i) {
if (ctype_upper($lcPropertyName[$i])) {
$snakeCasedName .= '_'.strtolower($lcPropertyName[$i]);
@ -68,7 +68,7 @@ class CamelCaseToSnakeCaseNameConverter implements NameConverterInterface
$camelCasedName = lcfirst($camelCasedName);
}
if (null === $this->attributes || in_array($camelCasedName, $this->attributes)) {
if (null === $this->attributes || \in_array($camelCasedName, $this->attributes)) {
return $camelCasedName;
}

View File

@ -100,7 +100,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
*/
public function setCircularReferenceHandler($circularReferenceHandler)
{
if (!is_callable($circularReferenceHandler)) {
if (!\is_callable($circularReferenceHandler)) {
throw new InvalidArgumentException('The given circular reference handler is not callable.');
}
@ -121,7 +121,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
public function setCallbacks(array $callbacks)
{
foreach ($callbacks as $attribute => $callback) {
if (!is_callable($callback)) {
if (!\is_callable($callback)) {
throw new InvalidArgumentException(sprintf(
'The given callback for attribute "%s" is not callable.',
$attribute
@ -220,10 +220,10 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
protected function handleCircularReference($object)
{
if ($this->circularReferenceHandler) {
return call_user_func($this->circularReferenceHandler, $object);
return \call_user_func($this->circularReferenceHandler, $object);
}
throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d)', get_class($object), $this->circularReferenceLimit));
throw new CircularReferenceException(sprintf('A circular reference has been detected when serializing the object of class "%s" (configured limit: %d)', \get_class($object), $this->circularReferenceLimit));
}
/**
@ -253,13 +253,13 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
*/
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
{
if (!$this->classMetadataFactory || !isset($context[static::GROUPS]) || !is_array($context[static::GROUPS])) {
if (!$this->classMetadataFactory || !isset($context[static::GROUPS]) || !\is_array($context[static::GROUPS])) {
return false;
}
$allowedAttributes = array();
foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
if (count(array_intersect($attributeMetadata->getGroups(), $context[static::GROUPS]))) {
if (array_intersect($attributeMetadata->getGroups(), $context[static::GROUPS])) {
$allowedAttributes[] = $attributesAsString ? $attributeMetadata->getName() : $attributeMetadata;
}
}
@ -302,7 +302,7 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
{
if (
isset($context[static::OBJECT_TO_POPULATE]) &&
is_object($context[static::OBJECT_TO_POPULATE]) &&
\is_object($context[static::OBJECT_TO_POPULATE]) &&
$context[static::OBJECT_TO_POPULATE] instanceof $class
) {
$object = $context[static::OBJECT_TO_POPULATE];
@ -320,11 +320,11 @@ abstract class AbstractNormalizer extends SerializerAwareNormalizer implements N
$paramName = $constructorParameter->name;
$key = $this->nameConverter ? $this->nameConverter->normalize($paramName) : $paramName;
$allowed = false === $allowedAttributes || in_array($paramName, $allowedAttributes);
$ignored = in_array($paramName, $this->ignoredAttributes);
$allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes);
$ignored = \in_array($paramName, $this->ignoredAttributes);
if (method_exists($constructorParameter, 'isVariadic') && $constructorParameter->isVariadic()) {
if ($allowed && !$ignored && (isset($data[$key]) || array_key_exists($key, $data))) {
if (!is_array($data[$paramName])) {
if (!\is_array($data[$paramName])) {
throw new RuntimeException(sprintf('Cannot create an instance of %s from serialized data because the variadic parameter %s can only accept an array.', $class, $constructorParameter->name));
}

View File

@ -58,17 +58,17 @@ class GetSetMethodNormalizer extends AbstractNormalizer
foreach ($reflectionMethods as $method) {
if ($this->isGetMethod($method)) {
$attributeName = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));
if (in_array($attributeName, $this->ignoredAttributes)) {
if (\in_array($attributeName, $this->ignoredAttributes)) {
continue;
}
if (false !== $allowedAttributes && !in_array($attributeName, $allowedAttributes)) {
if (false !== $allowedAttributes && !\in_array($attributeName, $allowedAttributes)) {
continue;
}
$attributeValue = $method->invoke($object);
if (isset($this->callbacks[$attributeName])) {
$attributeValue = call_user_func($this->callbacks[$attributeName], $attributeValue);
$attributeValue = \call_user_func($this->callbacks[$attributeName], $attributeValue);
}
if (null !== $attributeValue && !is_scalar($attributeValue)) {
if (!$this->serializer instanceof NormalizerInterface) {
@ -108,13 +108,13 @@ class GetSetMethodNormalizer extends AbstractNormalizer
$attribute = $this->nameConverter->denormalize($attribute);
}
$allowed = false === $allowedAttributes || in_array($attribute, $allowedAttributes);
$ignored = in_array($attribute, $this->ignoredAttributes);
$allowed = false === $allowedAttributes || \in_array($attribute, $allowedAttributes);
$ignored = \in_array($attribute, $this->ignoredAttributes);
if ($allowed && !$ignored) {
$setter = 'set'.ucfirst($attribute);
if (in_array($setter, $classMethods) && !$reflectionClass->getMethod($setter)->isStatic()) {
if (\in_array($setter, $classMethods) && !$reflectionClass->getMethod($setter)->isStatic()) {
$object->$setter($value);
}
}
@ -128,7 +128,7 @@ class GetSetMethodNormalizer extends AbstractNormalizer
*/
public function supportsNormalization($data, $format = null)
{
return is_object($data) && !$data instanceof \Traversable && $this->supports(get_class($data));
return \is_object($data) && !$data instanceof \Traversable && $this->supports(\get_class($data));
}
/**
@ -166,7 +166,7 @@ class GetSetMethodNormalizer extends AbstractNormalizer
*/
private function isGetMethod(\ReflectionMethod $method)
{
$methodLength = strlen($method->name);
$methodLength = \strlen($method->name);
return
!$method->isStatic() &&

View File

@ -45,7 +45,7 @@ class ObjectNormalizer extends AbstractNormalizer
*/
public function supportsNormalization($data, $format = null)
{
return is_object($data) && !$data instanceof \Traversable;
return \is_object($data) && !$data instanceof \Traversable;
}
/**
@ -98,14 +98,14 @@ class ObjectNormalizer extends AbstractNormalizer
}
foreach ($attributes as $attribute) {
if (in_array($attribute, $this->ignoredAttributes)) {
if (\in_array($attribute, $this->ignoredAttributes)) {
continue;
}
$attributeValue = $this->propertyAccessor->getValue($object, $attribute);
if (isset($this->callbacks[$attribute])) {
$attributeValue = call_user_func($this->callbacks[$attribute], $attributeValue);
$attributeValue = \call_user_func($this->callbacks[$attribute], $attributeValue);
}
if (null !== $attributeValue && !is_scalar($attributeValue)) {
@ -150,8 +150,8 @@ class ObjectNormalizer extends AbstractNormalizer
$attribute = $this->nameConverter->denormalize($attribute);
}
$allowed = false === $allowedAttributes || in_array($attribute, $allowedAttributes);
$ignored = in_array($attribute, $this->ignoredAttributes);
$allowed = false === $allowedAttributes || \in_array($attribute, $allowedAttributes);
$ignored = \in_array($attribute, $this->ignoredAttributes);
if ($allowed && !$ignored) {
try {

View File

@ -50,11 +50,11 @@ class PropertyNormalizer extends AbstractNormalizer
$allowedAttributes = $this->getAllowedAttributes($object, $context, true);
foreach ($reflectionObject->getProperties() as $property) {
if (in_array($property->name, $this->ignoredAttributes) || $property->isStatic()) {
if (\in_array($property->name, $this->ignoredAttributes) || $property->isStatic()) {
continue;
}
if (false !== $allowedAttributes && !in_array($property->name, $allowedAttributes)) {
if (false !== $allowedAttributes && !\in_array($property->name, $allowedAttributes)) {
continue;
}
@ -66,7 +66,7 @@ class PropertyNormalizer extends AbstractNormalizer
$attributeValue = $property->getValue($object);
if (isset($this->callbacks[$property->name])) {
$attributeValue = call_user_func($this->callbacks[$property->name], $attributeValue);
$attributeValue = \call_user_func($this->callbacks[$property->name], $attributeValue);
}
if (null !== $attributeValue && !is_scalar($attributeValue)) {
if (!$this->serializer instanceof NormalizerInterface) {
@ -105,8 +105,8 @@ class PropertyNormalizer extends AbstractNormalizer
$propertyName = $this->nameConverter->denormalize($propertyName);
}
$allowed = false === $allowedAttributes || in_array($propertyName, $allowedAttributes);
$ignored = in_array($propertyName, $this->ignoredAttributes);
$allowed = false === $allowedAttributes || \in_array($propertyName, $allowedAttributes);
$ignored = \in_array($propertyName, $this->ignoredAttributes);
if ($allowed && !$ignored && $reflectionClass->hasProperty($propertyName)) {
$property = $reflectionClass->getProperty($propertyName);
@ -131,7 +131,7 @@ class PropertyNormalizer extends AbstractNormalizer
*/
public function supportsNormalization($data, $format = null)
{
return is_object($data) && !$data instanceof \Traversable && $this->supports(get_class($data));
return \is_object($data) && !$data instanceof \Traversable && $this->supports(\get_class($data));
}
/**

View File

@ -132,7 +132,7 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
return $data;
}
if (is_array($data) || $data instanceof \Traversable) {
if (\is_array($data) || $data instanceof \Traversable) {
$normalized = array();
foreach ($data as $key => $val) {
$normalized[$key] = $this->normalize($val, $format, $context);
@ -141,12 +141,12 @@ class Serializer implements SerializerInterface, NormalizerInterface, Denormaliz
return $normalized;
}
if (is_object($data)) {
if (\is_object($data)) {
if (!$this->normalizers) {
throw new LogicException('You must register at least one normalizer to be able to normalize objects.');
}
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', get_class($data)));
throw new UnexpectedValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', \get_class($data)));
}
throw new UnexpectedValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));