Merge branch '4.4'

* 4.4:
  [VarDumper] Remove useless variable
  [Console] Fix incomplete output mock.
  [Serializer] Fixed docblocks and parameter names.
  [DI] fix dumping lazy proxies
  [VarDumper] Add types to private methods.
This commit is contained in:
Nicolas Grekas 2019-08-16 08:41:58 +02:00
commit 86a2a9df23
19 changed files with 42 additions and 41 deletions

View File

@ -496,7 +496,7 @@ EOF;
$proxyCode = substr(Kernel::stripComments($proxyCode), 5); $proxyCode = substr(Kernel::stripComments($proxyCode), 5);
} }
$proxyClasses[sprintf('%s.php', explode(' ', $proxyCode, 3)[1])] = $proxyCode; $proxyClasses[sprintf('%s.php', explode(' ', $this->inlineRequires ? substr($proxyCode, \strlen($code)) : $proxyCode, 3)[1])] = $proxyCode;
} }
return $proxyClasses; return $proxyClasses;

View File

@ -25,6 +25,8 @@ trait ClassResolverTrait
/** /**
* Gets a class name for a given class or instance. * Gets a class name for a given class or instance.
* *
* @param object|string $value
*
* @throws InvalidArgumentException If the class does not exists * @throws InvalidArgumentException If the class does not exists
*/ */
private function getClass($value): string private function getClass($value): string

View File

@ -298,18 +298,18 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function denormalize($data, $class, string $format = null, array $context = []) public function denormalize($data, $type, string $format = null, array $context = [])
{ {
if (!isset($context['cache_key'])) { if (!isset($context['cache_key'])) {
$context['cache_key'] = $this->getCacheKey($format, $context); $context['cache_key'] = $this->getCacheKey($format, $context);
} }
$allowedAttributes = $this->getAllowedAttributes($class, $context, true); $allowedAttributes = $this->getAllowedAttributes($type, $context, true);
$normalizedData = $this->prepareForDenormalization($data); $normalizedData = $this->prepareForDenormalization($data);
$extraAttributes = []; $extraAttributes = [];
$reflectionClass = new \ReflectionClass($class); $reflectionClass = new \ReflectionClass($type);
$object = $this->instantiateObject($normalizedData, $class, $context, $reflectionClass, $allowedAttributes, $format); $object = $this->instantiateObject($normalizedData, $type, $context, $reflectionClass, $allowedAttributes, $format);
$resolvedClass = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object); $resolvedClass = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object);
foreach ($normalizedData as $attribute => $value) { foreach ($normalizedData as $attribute => $value) {
@ -336,7 +336,7 @@ abstract class AbstractObjectNormalizer extends AbstractNormalizer
try { try {
$this->setAttributeValue($object, $attribute, $value, $format, $context); $this->setAttributeValue($object, $attribute, $value, $format, $context);
} catch (InvalidArgumentException $e) { } catch (InvalidArgumentException $e) {
throw new NotNormalizableValueException(sprintf('Failed to denormalize attribute "%s" value for class "%s": %s.', $attribute, $class, $e->getMessage()), $e->getCode(), $e); throw new NotNormalizableValueException(sprintf('Failed to denormalize attribute "%s" value for class "%s": %s.', $attribute, $type, $e->getMessage()), $e->getCode(), $e);
} }
} }

View File

@ -36,7 +36,7 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Serializer
* *
* @throws NotNormalizableValueException * @throws NotNormalizableValueException
*/ */
public function denormalize($data, $class, $format = null, array $context = []) public function denormalize($data, $type, $format = null, array $context = [])
{ {
if (null === $this->serializer) { if (null === $this->serializer) {
throw new BadMethodCallException('Please set a serializer before calling denormalize()!'); throw new BadMethodCallException('Please set a serializer before calling denormalize()!');
@ -44,12 +44,12 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Serializer
if (!\is_array($data)) { if (!\is_array($data)) {
throw new InvalidArgumentException('Data expected to be an array, '.\gettype($data).' given.'); throw new InvalidArgumentException('Data expected to be an array, '.\gettype($data).' given.');
} }
if ('[]' !== substr($class, -2)) { if ('[]' !== substr($type, -2)) {
throw new InvalidArgumentException('Unsupported class: '.$class); throw new InvalidArgumentException('Unsupported class: '.$type);
} }
$serializer = $this->serializer; $serializer = $this->serializer;
$class = substr($class, 0, -2); $type = substr($type, 0, -2);
$builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null; $builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null;
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
@ -57,7 +57,7 @@ class ArrayDenormalizer implements ContextAwareDenormalizerInterface, Serializer
throw new NotNormalizableValueException(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, $builtinType, \gettype($key))); throw new NotNormalizableValueException(sprintf('The type of the key "%s" must be "%s" ("%s" given).', $key, $builtinType, \gettype($key)));
} }
$data[$key] = $serializer->denormalize($value, $class, $format, $context); $data[$key] = $serializer->denormalize($value, $type, $format, $context);
} }
return $data; return $data;

View File

@ -33,9 +33,9 @@ class CustomNormalizer implements NormalizerInterface, DenormalizerInterface, Se
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function denormalize($data, $class, string $format = null, array $context = []) public function denormalize($data, $type, string $format = null, array $context = [])
{ {
$object = $this->extractObjectToPopulate($class, $context) ?: new $class(); $object = $this->extractObjectToPopulate($type, $context) ?: new $type();
$object->denormalize($this->serializer, $data, $format, $context); $object->denormalize($this->serializer, $data, $format, $context);
return $object; return $object;

View File

@ -89,14 +89,14 @@ class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, C
* @throws InvalidArgumentException * @throws InvalidArgumentException
* @throws NotNormalizableValueException * @throws NotNormalizableValueException
*/ */
public function denormalize($data, $class, string $format = null, array $context = []) public function denormalize($data, $type, string $format = null, array $context = [])
{ {
if (!preg_match('/^data:([a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}\/[a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}(;[a-z0-9\-]+\=[a-z0-9\-]+)?)?(;base64)?,[a-z0-9\!\$\&\\\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i', $data)) { if (!preg_match('/^data:([a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}\/[a-z0-9][a-z0-9\!\#\$\&\-\^\_\+\.]{0,126}(;[a-z0-9\-]+\=[a-z0-9\-]+)?)?(;base64)?,[a-z0-9\!\$\&\\\'\,\(\)\*\+\,\;\=\-\.\_\~\:\@\/\?\%\s]*\s*$/i', $data)) {
throw new NotNormalizableValueException('The provided "data:" URI is not valid.'); throw new NotNormalizableValueException('The provided "data:" URI is not valid.');
} }
try { try {
switch ($class) { switch ($type) {
case 'Symfony\Component\HttpFoundation\File\File': case 'Symfony\Component\HttpFoundation\File\File':
if (!class_exists(File::class)) { if (!class_exists(File::class)) {
throw new InvalidArgumentException(sprintf('Cannot denormalize to a "%s" without the HttpFoundation component installed. Try running "composer require symfony/http-foundation".', File::class)); throw new InvalidArgumentException(sprintf('Cannot denormalize to a "%s" without the HttpFoundation component installed. Try running "composer require symfony/http-foundation".', File::class));
@ -112,7 +112,7 @@ class DataUriNormalizer implements NormalizerInterface, DenormalizerInterface, C
throw new NotNormalizableValueException($exception->getMessage(), $exception->getCode(), $exception); throw new NotNormalizableValueException($exception->getMessage(), $exception->getCode(), $exception);
} }
throw new InvalidArgumentException(sprintf('The class parameter "%s" is not supported. It must be one of "SplFileInfo", "SplFileObject" or "Symfony\Component\HttpFoundation\File\File".', $class)); throw new InvalidArgumentException(sprintf('The class parameter "%s" is not supported. It must be one of "SplFileInfo", "SplFileObject" or "Symfony\Component\HttpFoundation\File\File".', $type));
} }
/** /**

View File

@ -69,7 +69,7 @@ class DateIntervalNormalizer implements NormalizerInterface, DenormalizerInterfa
* @throws InvalidArgumentException * @throws InvalidArgumentException
* @throws UnexpectedValueException * @throws UnexpectedValueException
*/ */
public function denormalize($data, $class, string $format = null, array $context = []) public function denormalize($data, $type, string $format = null, array $context = [])
{ {
if (!\is_string($data)) { if (!\is_string($data)) {
throw new InvalidArgumentException(sprintf('Data expected to be a string, %s given.', \gettype($data))); throw new InvalidArgumentException(sprintf('Data expected to be a string, %s given.', \gettype($data)));

View File

@ -76,7 +76,7 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface,
* *
* @throws NotNormalizableValueException * @throws NotNormalizableValueException
*/ */
public function denormalize($data, $class, string $format = null, array $context = []) public function denormalize($data, $type, string $format = null, array $context = [])
{ {
$dateTimeFormat = $context[self::FORMAT_KEY] ?? null; $dateTimeFormat = $context[self::FORMAT_KEY] ?? null;
$timezone = $this->getTimezone($context); $timezone = $this->getTimezone($context);
@ -86,13 +86,13 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface,
} }
if (null !== $dateTimeFormat) { if (null !== $dateTimeFormat) {
$object = \DateTime::class === $class ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone); $object = \DateTime::class === $type ? \DateTime::createFromFormat($dateTimeFormat, $data, $timezone) : \DateTimeImmutable::createFromFormat($dateTimeFormat, $data, $timezone);
if (false !== $object) { if (false !== $object) {
return $object; return $object;
} }
$dateTimeErrors = \DateTime::class === $class ? \DateTime::getLastErrors() : \DateTimeImmutable::getLastErrors(); $dateTimeErrors = \DateTime::class === $type ? \DateTime::getLastErrors() : \DateTimeImmutable::getLastErrors();
throw new NotNormalizableValueException(sprintf( throw new NotNormalizableValueException(sprintf(
'Parsing datetime string "%s" using format "%s" resulted in %d errors:'."\n".'%s', 'Parsing datetime string "%s" using format "%s" resulted in %d errors:'."\n".'%s',
@ -104,7 +104,7 @@ class DateTimeNormalizer implements NormalizerInterface, DenormalizerInterface,
} }
try { try {
return \DateTime::class === $class ? new \DateTime($data, $timezone) : new \DateTimeImmutable($data, $timezone); return \DateTime::class === $type ? new \DateTime($data, $timezone) : new \DateTimeImmutable($data, $timezone);
} catch (\Exception $e) { } catch (\Exception $e) {
throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e); throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e);
} }

View File

@ -48,7 +48,7 @@ class DateTimeZoneNormalizer implements NormalizerInterface, DenormalizerInterfa
* *
* @throws NotNormalizableValueException * @throws NotNormalizableValueException
*/ */
public function denormalize($data, $class, string $format = null, array $context = []) public function denormalize($data, $type, string $format = null, array $context = [])
{ {
if ('' === $data || null === $data) { if ('' === $data || null === $data) {
throw new NotNormalizableValueException('The data is either an empty string or null, you should pass a string that can be parsed as a DateTimeZone.'); throw new NotNormalizableValueException('The data is either an empty string or null, you should pass a string that can be parsed as a DateTimeZone.');

View File

@ -30,7 +30,7 @@ interface DenormalizerInterface
* Denormalizes data back into an object of the given class. * Denormalizes data back into an object of the given class.
* *
* @param mixed $data Data to restore * @param mixed $data Data to restore
* @param string $class The expected class to instantiate * @param string $type The expected class to instantiate
* @param string $format Format the given data was extracted from * @param string $format Format the given data was extracted from
* @param array $context Options available to the denormalizer * @param array $context Options available to the denormalizer
* *
@ -44,7 +44,7 @@ interface DenormalizerInterface
* @throws RuntimeException Occurs if the class cannot be instantiated * @throws RuntimeException Occurs if the class cannot be instantiated
* @throws ExceptionInterface Occurs for all the other cases of errors * @throws ExceptionInterface Occurs for all the other cases of errors
*/ */
public function denormalize($data, string $class, string $format = null, array $context = []); public function denormalize($data, string $type, string $format = null, array $context = []);
/** /**
* Checks whether the given class is supported for denormalization by this normalizer. * Checks whether the given class is supported for denormalization by this normalizer.

View File

@ -60,7 +60,7 @@ class JsonSerializableNormalizer extends AbstractNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function denormalize($data, $class, string $format = null, array $context = []) public function denormalize($data, $type, string $format = null, array $context = [])
{ {
throw new LogicException(sprintf('Cannot denormalize with "%s".', \JsonSerializable::class)); throw new LogicException(sprintf('Cannot denormalize with "%s".', \JsonSerializable::class));
} }

View File

@ -17,10 +17,9 @@ trait ObjectToPopulateTrait
* Extract the `object_to_populate` field from the context if it exists * Extract the `object_to_populate` field from the context if it exists
* and is an instance of the provided $class. * and is an instance of the provided $class.
* *
* @param string $class The class the object should be * @param string $class The class the object should be
* @param $context The denormalization context * @param string|null $key They in which to look for the object to populate.
* @param string $key They in which to look for the object to populate. * Keeps backwards compatibility with `AbstractNormalizer`.
* Keeps backwards compatibility with `AbstractNormalizer`.
* *
* @return object|null an object if things check out, null otherwise * @return object|null an object if things check out, null otherwise
*/ */

View File

@ -46,7 +46,7 @@ class AbstractNormalizerDummy extends AbstractNormalizer
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function denormalize($data, $class, $format = null, array $context = []) public function denormalize($data, $type, $format = null, array $context = [])
{ {
} }

View File

@ -363,13 +363,13 @@ class ArrayDenormalizerDummy implements DenormalizerInterface, SerializerAwareIn
* *
* @throws NotNormalizableValueException * @throws NotNormalizableValueException
*/ */
public function denormalize($data, $class, $format = null, array $context = []) public function denormalize($data, $type, $format = null, array $context = [])
{ {
$serializer = $this->serializer; $serializer = $this->serializer;
$class = substr($class, 0, -2); $type = substr($type, 0, -2);
foreach ($data as $key => $value) { foreach ($data as $key => $value) {
$data[$key] = $serializer->denormalize($value, $class, $format, $context); $data[$key] = $serializer->denormalize($value, $type, $format, $context);
} }
return $data; return $data;

View File

@ -23,7 +23,7 @@ class TestDenormalizer implements DenormalizerInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function denormalize($data, $class, $format = null, array $context = []) public function denormalize($data, $type, $format = null, array $context = [])
{ {
} }

View File

@ -191,7 +191,7 @@ class AmqpCaster
return $a; return $a;
} }
private static function extractFlags(int $flags) private static function extractFlags(int $flags): ConstStub
{ {
$flagsArray = []; $flagsArray = [];

View File

@ -49,7 +49,7 @@ class ArgsStub extends EnumStub
} }
} }
private static function getParameters(string $function, ?string $class) private static function getParameters(string $function, ?string $class): array
{ {
if (isset(self::$parameters[$k = $class.'::'.$function])) { if (isset(self::$parameters[$k = $class.'::'.$function])) {
return self::$parameters[$k]; return self::$parameters[$k];

View File

@ -261,7 +261,7 @@ class ExceptionCaster
return $a; return $a;
} }
private static function filterExceptionArray($xClass, array $a, string $xPrefix, int $filter) private static function filterExceptionArray(string $xClass, array $a, string $xPrefix, int $filter): array
{ {
if (isset($a[$xPrefix.'trace'])) { if (isset($a[$xPrefix.'trace'])) {
$trace = $a[$xPrefix.'trace']; $trace = $a[$xPrefix.'trace'];
@ -294,7 +294,7 @@ class ExceptionCaster
return $a; return $a;
} }
private static function traceUnshift(array &$trace, ?string $class, string $file, int $line) private static function traceUnshift(array &$trace, ?string $class, string $file, int $line): void
{ {
if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) { if (isset($trace[0]['file'], $trace[0]['line']) && $trace[0]['file'] === $file && $trace[0]['line'] === $line) {
return; return;
@ -306,7 +306,7 @@ class ExceptionCaster
]); ]);
} }
private static function extractSource(string $srcLines, int $line, int $srcContext, ?string $title, string $lang, string $file = null) private static function extractSource(string $srcLines, int $line, int $srcContext, ?string $title, string $lang, string $file = null): EnumStub
{ {
$srcLines = explode("\n", $srcLines); $srcLines = explode("\n", $srcLines);
$src = []; $src = [];

View File

@ -26,7 +26,7 @@ class FunctionsTest extends TestCase
ob_start(); ob_start();
$return = dump($var1); $return = dump($var1);
$out = ob_get_clean(); ob_end_clean();
$this->assertEquals($var1, $return); $this->assertEquals($var1, $return);
} }
@ -41,7 +41,7 @@ class FunctionsTest extends TestCase
ob_start(); ob_start();
$return = dump($var1, $var2, $var3); $return = dump($var1, $var2, $var3);
$out = ob_get_clean(); ob_end_clean();
$this->assertEquals([$var1, $var2, $var3], $return); $this->assertEquals([$var1, $var2, $var3], $return);
} }