diff --git a/src/Accessor/DefaultAccessorStrategy.php b/src/Accessor/DefaultAccessorStrategy.php index c3e45d819..450f892fe 100644 --- a/src/Accessor/DefaultAccessorStrategy.php +++ b/src/Accessor/DefaultAccessorStrategy.php @@ -51,7 +51,7 @@ public function __construct(?ExpressionEvaluatorInterface $evaluator = null) public function getValue(object $object, PropertyMetadata $metadata, SerializationContext $context) { if ($metadata instanceof StaticPropertyMetadata) { - return $metadata->getValue(null); + return $metadata->getValue(); } if ($metadata instanceof ExpressionPropertyMetadata) { diff --git a/src/ContextFactory/CallableContextFactory.php b/src/ContextFactory/CallableContextFactory.php index 54e1c7ec8..a6aebe541 100644 --- a/src/ContextFactory/CallableContextFactory.php +++ b/src/ContextFactory/CallableContextFactory.php @@ -21,9 +21,6 @@ public function __construct(callable $callable) $this->callable = $callable; } - /** - * @return mixed - */ protected function createContext(): Context { $callable = $this->callable; diff --git a/src/GraphNavigator/SerializationGraphNavigator.php b/src/GraphNavigator/SerializationGraphNavigator.php index 31ecd01ba..046934e84 100644 --- a/src/GraphNavigator/SerializationGraphNavigator.php +++ b/src/GraphNavigator/SerializationGraphNavigator.php @@ -29,6 +29,8 @@ use JMS\Serializer\VisitorInterface; use Metadata\MetadataFactoryInterface; +use function assert; + /** * Handles traversal along the object graph. * @@ -37,7 +39,7 @@ * * @author Johannes M. Schmitt */ -final class SerializationGraphNavigator extends GraphNavigator implements GraphNavigatorInterface +final class SerializationGraphNavigator extends GraphNavigator { /** * @var SerializationVisitorInterface @@ -97,6 +99,8 @@ public function __construct( public function initialize(VisitorInterface $visitor, Context $context): void { + assert($context instanceof SerializationContext); + parent::initialize($visitor, $context); $this->shouldSerializeNull = $context->shouldSerializeNull(); } diff --git a/src/Handler/ArrayCollectionHandler.php b/src/Handler/ArrayCollectionHandler.php index ae839bd7f..f441720e2 100644 --- a/src/Handler/ArrayCollectionHandler.php +++ b/src/Handler/ArrayCollectionHandler.php @@ -75,7 +75,7 @@ public function serializeCollection(SerializationVisitorInterface $visitor, Coll if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipClass($context->getMetadataFactory()->getMetadataForClass(\get_class($collection)), $context)) { $context->startVisiting($collection); - return $visitor->visitArray([], $type, $context); + return $visitor->visitArray([], $type); } } diff --git a/src/Handler/StdClassHandler.php b/src/Handler/StdClassHandler.php index 2e4686a67..983a9f1bc 100644 --- a/src/Handler/StdClassHandler.php +++ b/src/Handler/StdClassHandler.php @@ -40,13 +40,13 @@ public static function getSubscribingMethods() public function serializeStdClass(SerializationVisitorInterface $visitor, \stdClass $stdClass, array $type, SerializationContext $context) { $classMetadata = $context->getMetadataFactory()->getMetadataForClass('stdClass'); - $visitor->startVisitingObject($classMetadata, $stdClass, ['name' => 'stdClass'], $context); + $visitor->startVisitingObject($classMetadata, $stdClass, ['name' => 'stdClass']); foreach ((array) $stdClass as $name => $value) { $metadata = new StaticPropertyMetadata('stdClass', $name, $value); $visitor->visitProperty($metadata, $value); } - return $visitor->endVisitingObject($classMetadata, $stdClass, ['name' => 'stdClass'], $context); + return $visitor->endVisitingObject($classMetadata, $stdClass, ['name' => 'stdClass']); } } diff --git a/src/JsonDeserializationVisitor.php b/src/JsonDeserializationVisitor.php index e4f8d8637..95a2ca74d 100644 --- a/src/JsonDeserializationVisitor.php +++ b/src/JsonDeserializationVisitor.php @@ -45,8 +45,9 @@ public function __construct( /** * {@inheritdoc} */ - public function visitNull($data, array $type): void + public function visitNull($data, array $type) { + return null; } /** diff --git a/src/Visitor/DeserializationVisitorInterface.php b/src/Visitor/DeserializationVisitorInterface.php index 523979fd4..cf6a3f3c9 100644 --- a/src/Visitor/DeserializationVisitorInterface.php +++ b/src/Visitor/DeserializationVisitorInterface.php @@ -23,39 +23,31 @@ interface DeserializationVisitorInterface extends VisitorInterface * @param mixed $data * @param array $type * - * @return mixed + * @return null */ - public function visitNull($data, array $type): void; + public function visitNull($data, array $type); /** * @param mixed $data * @param array $type - * - * @return mixed */ public function visitString($data, array $type): string; /** * @param mixed $data * @param array $type - * - * @return mixed */ public function visitBoolean($data, array $type): bool; /** * @param mixed $data * @param array $type - * - * @return mixed */ public function visitDouble($data, array $type): float; /** * @param mixed $data * @param array $type - * - * @return mixed */ public function visitInteger($data, array $type): int; @@ -70,7 +62,7 @@ public function visitDiscriminatorMapProperty($data, ClassMetadata $metadata): s * @param mixed $data * @param array $type * - * @return mixed + * @return array */ public function visitArray($data, array $type): array; diff --git a/src/XmlDeserializationVisitor.php b/src/XmlDeserializationVisitor.php index 28861997f..a14f0835e 100644 --- a/src/XmlDeserializationVisitor.php +++ b/src/XmlDeserializationVisitor.php @@ -118,8 +118,9 @@ private function emptyStringToSpaceCharacter($data): string /** * {@inheritdoc} */ - public function visitNull($data, array $type): void + public function visitNull($data, array $type) { + return null; } /** diff --git a/tests/Handler/DateHandlerTest.php b/tests/Handler/DateHandlerTest.php index 5b099557d..258a88e94 100644 --- a/tests/Handler/DateHandlerTest.php +++ b/tests/Handler/DateHandlerTest.php @@ -64,17 +64,15 @@ public function testSerializeDate(array $params) */ public function testDeserializeDateInterval($dateInterval, $expected) { - $context = $this->getMockBuilder(SerializationContext::class)->getMock(); - $visitor = $this->getMockBuilder(DeserializationVisitorInterface::class)->getMock(); $visitor->method('visitString')->with('2017-06-18'); - $deserialized = $this->handler->deserializeDateIntervalFromJson($visitor, $dateInterval, [], $context); + $deserialized = $this->handler->deserializeDateIntervalFromJson($visitor, $dateInterval, []); if (isset($deserialized->f)) { - $this->assertEquals($expected['f'], $deserialized->f); + self::assertEquals($expected['f'], $deserialized->f); } - $this->assertEquals($expected['s'], $deserialized->s); + self::assertEquals($expected['s'], $deserialized->s); } public function getDeserializeDateInterval() diff --git a/tests/Handler/FormErrorHandlerTest.php b/tests/Handler/FormErrorHandlerTest.php index 47cdfbe68..c92539cc7 100644 --- a/tests/Handler/FormErrorHandlerTest.php +++ b/tests/Handler/FormErrorHandlerTest.php @@ -4,10 +4,8 @@ namespace JMS\Serializer\Tests\Handler; -use JMS\Serializer\GraphNavigatorInterface; use JMS\Serializer\Handler\FormErrorHandler; use JMS\Serializer\JsonSerializationVisitor; -use JMS\Serializer\SerializationContext; use JMS\Serializer\Visitor\Factory\JsonSerializationVisitorFactory; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; @@ -53,11 +51,9 @@ class FormErrorHandlerTest extends TestCase protected function setUp(): void { $this->handler = new FormErrorHandler(new Translator('en')); - $navigator = $this->getMockBuilder(GraphNavigatorInterface::class)->getMock(); - $context = SerializationContext::create(); - $this->visitor = (new JsonSerializationVisitorFactory())->getVisitor($navigator, $context); + $this->visitor = (new JsonSerializationVisitorFactory())->getVisitor(); $this->dispatcher = new EventDispatcher(); - $this->factory = $this->getMockBuilder('Symfony\Component\Form\FormFactoryInterface')->getMock(); + $this->factory = $this->getMockBuilder(FormFactoryInterface::class)->getMock(); } protected function tearDown(): void diff --git a/tests/Serializer/EventDispatcher/Subscriber/SymfonyValidatorValidatorSubscriberTest.php b/tests/Serializer/EventDispatcher/Subscriber/SymfonyValidatorValidatorSubscriberTest.php index 2e03ebb02..d318a421b 100644 --- a/tests/Serializer/EventDispatcher/Subscriber/SymfonyValidatorValidatorSubscriberTest.php +++ b/tests/Serializer/EventDispatcher/Subscriber/SymfonyValidatorValidatorSubscriberTest.php @@ -7,43 +7,45 @@ use JMS\Serializer\DeserializationContext; use JMS\Serializer\EventDispatcher\EventDispatcher; use JMS\Serializer\EventDispatcher\ObjectEvent; -use JMS\Serializer\EventDispatcher\Subscriber\SymfonyValidatorSubscriber; use JMS\Serializer\EventDispatcher\Subscriber\SymfonyValidatorValidatorSubscriber; use JMS\Serializer\Exception\ValidationFailedException; use JMS\Serializer\SerializerBuilder; +use JMS\Serializer\Tests\Fixtures\AuthorList; use PHPUnit\Framework\TestCase; +use stdClass; use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationList; +use Symfony\Component\Validator\Validator\ValidatorInterface; class SymfonyValidatorValidatorSubscriberTest extends TestCase { private $validator; - /** @var SymfonyValidatorSubscriber */ + /** @var SymfonyValidatorValidatorSubscriber */ private $subscriber; - public function testValidate() + public function testValidate(): void { - $obj = new \stdClass(); + $obj = new stdClass(); - $this->validator->expects($this->once()) + $this->validator->expects(self::once()) ->method('validate') ->with($obj, null, ['foo']) - ->will($this->returnValue(new ConstraintViolationList())); + ->willReturn(new ConstraintViolationList()); $context = DeserializationContext::create()->setAttribute('validation_groups', ['foo']); $this->subscriber->onPostDeserialize(new ObjectEvent($context, $obj, [])); } - public function testValidateThrowsExceptionWhenListIsNotEmpty() + public function testValidateThrowsExceptionWhenListIsNotEmpty(): void { - $obj = new \stdClass(); + $obj = new stdClass(); - $this->validator->expects($this->once()) + $this->validator->expects(self::once()) ->method('validate') ->with($obj, null, ['foo']) - ->will($this->returnValue(new ConstraintViolationList([new ConstraintViolation('foo', 'foo', [], 'a', 'b', 'c')]))); + ->willReturn(new ConstraintViolationList([new ConstraintViolation('foo', 'foo', [], 'a', 'b', 'c')])); $context = DeserializationContext::create()->setAttribute('validation_groups', ['foo']); @@ -53,20 +55,20 @@ public function testValidateThrowsExceptionWhenListIsNotEmpty() $this->subscriber->onPostDeserialize(new ObjectEvent($context, $obj, [])); } - public function testValidatorIsNotCalledWhenNoGroupsAreSet() + public function testValidatorIsNotCalledWhenNoGroupsAreSet(): void { - $this->validator->expects($this->never()) + $this->validator->expects(self::never()) ->method('validate'); - $this->subscriber->onPostDeserialize(new ObjectEvent(DeserializationContext::create(), new \stdClass(), [])); + $this->subscriber->onPostDeserialize(new ObjectEvent(DeserializationContext::create(), new stdClass(), [])); } - public function testValidationIsOnlyPerformedOnRootObject() + public function testValidationIsOnlyPerformedOnRootObject(): void { - $this->validator->expects($this->once()) + $this->validator->expects(self::once()) ->method('validate') - ->with($this->isInstanceOf('JMS\Serializer\Tests\Fixtures\AuthorList'), null, ['Foo']) - ->will($this->returnValue(new ConstraintViolationList())); + ->with(self::isInstanceOf(AuthorList::class), null, ['Foo']) + ->willReturn(new ConstraintViolationList()); $subscriber = $this->subscriber; $list = SerializerBuilder::create() @@ -76,7 +78,7 @@ public function testValidationIsOnlyPerformedOnRootObject() ->build() ->deserialize( '{"authors":[{"full_name":"foo"},{"full_name":"bar"}]}', - 'JMS\Serializer\Tests\Fixtures\AuthorList', + AuthorList::class, 'json', DeserializationContext::create()->setAttribute('validation_groups', ['Foo']) ); @@ -86,7 +88,7 @@ public function testValidationIsOnlyPerformedOnRootObject() protected function setUp(): void { - $this->validator = $this->getMockBuilder('Symfony\Component\Validator\Validator\ValidatorInterface')->getMock(); + $this->validator = $this->getMockBuilder(ValidatorInterface::class)->getMock(); $this->subscriber = new SymfonyValidatorValidatorSubscriber($this->validator); } }