Skip to content

Commit 4dc94aa

Browse files
authored
Merge pull request #1597 from schmittjoh/type-array-phpstan-type
Add PHPStan type for JMS serialization Type
2 parents f5c6227 + f321c26 commit 4dc94aa

26 files changed

+205
-60
lines changed

src/AbstractVisitor.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@
77
use JMS\Serializer\Exception\NonFloatCastableTypeException;
88
use JMS\Serializer\Exception\NonIntCastableTypeException;
99
use JMS\Serializer\Exception\NonStringCastableTypeException;
10+
use JMS\Serializer\Type\Type;
1011

1112
/**
1213
* @internal
14+
*
15+
* @phpstan-import-type TypeArray from Type
1316
*/
1417
abstract class AbstractVisitor implements VisitorInterface
1518
{
@@ -31,6 +34,9 @@ public function prepare($data)
3134
return $data;
3235
}
3336

37+
/**
38+
* @param TypeArray $typeArray
39+
*/
3440
protected function getElementType(array $typeArray): ?array
3541
{
3642
if (false === isset($typeArray['params'][0])) {

src/Construction/ObjectConstructorInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66

77
use JMS\Serializer\DeserializationContext;
88
use JMS\Serializer\Metadata\ClassMetadata;
9+
use JMS\Serializer\Type\Type;
910
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
1011

1112
/**
1213
* Implementations of this interface construct new objects during deserialization.
1314
*
1415
* @author Johannes M. Schmitt <[email protected]>
16+
*
17+
* @phpstan-import-type TypeArray from Type
1518
*/
1619
interface ObjectConstructorInterface
1720
{
@@ -22,7 +25,7 @@ interface ObjectConstructorInterface
2225
* "unserialize" techniques, reflection, or other means.
2326
*
2427
* @param mixed $data
25-
* @param array $type ["name" => string, "params" => array]
28+
* @param TypeArray $type
2629
*/
2730
public function construct(DeserializationVisitorInterface $visitor, ClassMetadata $metadata, $data, array $type, DeserializationContext $context): ?object;
2831
}

src/EventDispatcher/Event.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
namespace JMS\Serializer\EventDispatcher;
66

77
use JMS\Serializer\Context;
8+
use JMS\Serializer\Type\Type;
89
use JMS\Serializer\VisitorInterface;
910

11+
/**
12+
* @phpstan-import-type TypeArray from Type
13+
*/
1014
class Event
1115
{
1216
/**
@@ -15,7 +19,7 @@ class Event
1519
private $propagationStopped = false;
1620

1721
/**
18-
* @var array
22+
* @var TypeArray
1923
*/
2024
protected $type;
2125

@@ -24,6 +28,9 @@ class Event
2428
*/
2529
private $context;
2630

31+
/**
32+
* @param TypeArray $type
33+
*/
2734
public function __construct(Context $context, array $type)
2835
{
2936
$this->context = $context;

src/EventDispatcher/ObjectEvent.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
namespace JMS\Serializer\EventDispatcher;
66

77
use JMS\Serializer\Context;
8+
use JMS\Serializer\Type\Type;
89

10+
/**
11+
* @phpstan-import-type TypeArray from Type
12+
*/
913
class ObjectEvent extends Event
1014
{
1115
/**
@@ -15,6 +19,7 @@ class ObjectEvent extends Event
1519

1620
/**
1721
* @param mixed $object
22+
* @param TypeArray $type
1823
*/
1924
public function __construct(Context $context, $object, array $type)
2025
{

src/EventDispatcher/PreDeserializeEvent.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
namespace JMS\Serializer\EventDispatcher;
66

77
use JMS\Serializer\DeserializationContext;
8+
use JMS\Serializer\Type\Type;
89

10+
/**
11+
* @phpstan-import-type TypeArray from Type
12+
*/
913
class PreDeserializeEvent extends Event
1014
{
1115
/**
@@ -15,7 +19,7 @@ class PreDeserializeEvent extends Event
1519

1620
/**
1721
* @param mixed $data
18-
* @param array $type
22+
* @param TypeArray $type
1923
*/
2024
public function __construct(DeserializationContext $context, $data, array $type)
2125
{

src/Exception/NonVisitableTypeException.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44

55
namespace JMS\Serializer\Exception;
66

7+
use JMS\Serializer\Type\Type;
8+
79
use function get_debug_type;
810

11+
/**
12+
* @phpstan-import-type TypeArray from Type
13+
*/
914
final class NonVisitableTypeException extends RuntimeException
1015
{
1116
/**
1217
* @param mixed $data
13-
* @param array{name: string} $type
18+
* @param TypeArray $type
1419
* @param RuntimeException|null $previous
1520
*
1621
* @return NonVisitableTypeException

src/GraphNavigator/DeserializationGraphNavigator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use JMS\Serializer\Handler\HandlerRegistryInterface;
2424
use JMS\Serializer\Metadata\ClassMetadata;
2525
use JMS\Serializer\NullAwareVisitorInterface;
26+
use JMS\Serializer\Type\Type;
2627
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
2728
use Metadata\MetadataFactoryInterface;
2829

@@ -33,6 +34,8 @@
3334
* on visitors, or custom handlers to process its nodes.
3435
*
3536
* @author Johannes M. Schmitt <[email protected]>
37+
*
38+
* @phpstan-import-type TypeArray from Type
3639
*/
3740
final class DeserializationGraphNavigator extends GraphNavigator implements GraphNavigatorInterface
3841
{
@@ -97,7 +100,7 @@ public function __construct(
97100
* Called for each node of the graph that is being traversed.
98101
*
99102
* @param mixed $data the data depends on the direction, and type of visitor
100-
* @param array|null $type array has the format ["name" => string, "params" => array]
103+
* @param TypeArray|null $type array has the format ["name" => string, "params" => array]
101104
*
102105
* @return mixed the return value depends on the direction, and type of visitor
103106
*/
@@ -252,6 +255,9 @@ private function resolveMetadata($data, ClassMetadata $metadata): ?ClassMetadata
252255
return $this->metadataFactory->getMetadataForClass($metadata->discriminatorMap[$typeValue]);
253256
}
254257

258+
/**
259+
* @param TypeArray $type
260+
*/
255261
private function afterVisitingObject(ClassMetadata $metadata, object $object, array $type): void
256262
{
257263
$this->context->decreaseDepth();

src/GraphNavigator/SerializationGraphNavigator.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use JMS\Serializer\Metadata\ClassMetadata;
2828
use JMS\Serializer\NullAwareVisitorInterface;
2929
use JMS\Serializer\SerializationContext;
30+
use JMS\Serializer\Type\Type;
3031
use JMS\Serializer\Visitor\SerializationVisitorInterface;
3132
use JMS\Serializer\VisitorInterface;
3233
use Metadata\MetadataFactoryInterface;
@@ -40,6 +41,8 @@
4041
* on visitors, or custom handlers to process its nodes.
4142
*
4243
* @author Johannes M. Schmitt <[email protected]>
44+
*
45+
* @phpstan-import-type TypeArray from Type
4346
*/
4447
final class SerializationGraphNavigator extends GraphNavigator
4548
{
@@ -112,7 +115,7 @@ public function initialize(VisitorInterface $visitor, Context $context): void
112115
* Called for each node of the graph that is being traversed.
113116
*
114117
* @param mixed $data the data depends on the direction, and type of visitor
115-
* @param array|null $type array has the format ["name" => string, "params" => array]
118+
* @param TypeArray|null $type array has the format ["name" => string, "params" => array]
116119
*
117120
* @return mixed the return value depends on the direction, and type of visitor
118121
*/
@@ -296,6 +299,9 @@ private function isRootNullAllowed(): bool
296299
return $this->context->hasAttribute('allows_root_null') && $this->context->getAttribute('allows_root_null') && 0 === $this->context->getVisitingSet()->count();
297300
}
298301

302+
/**
303+
* @param TypeArray $type
304+
*/
299305
private function afterVisitingObject(ClassMetadata $metadata, object $object, array $type): void
300306
{
301307
$this->context->stopVisiting($object);

src/GraphNavigatorInterface.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
namespace JMS\Serializer;
66

77
use JMS\Serializer\Exception\NotAcceptableException;
8+
use JMS\Serializer\Type\Type;
89

10+
/**
11+
* @phpstan-import-type TypeArray from Type
12+
*/
913
interface GraphNavigatorInterface
1014
{
1115
public const DIRECTION_SERIALIZATION = 1;
@@ -21,7 +25,7 @@ public function initialize(VisitorInterface $visitor, Context $context): void;
2125
* Called for each node of the graph that is being traversed.
2226
*
2327
* @param mixed $data the data depends on the direction, and type of visitor
24-
* @param array|null $type array has the format ["name" => string, "params" => array]
28+
* @param TypeArray|null $type array has the format ["name" => string, "params" => array]
2529
*
2630
* @return mixed the return value depends on the direction, and type of visitor
2731
*

src/Handler/ArrayCollectionHandler.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@
1414
use JMS\Serializer\GraphNavigatorInterface;
1515
use JMS\Serializer\Metadata\PropertyMetadata;
1616
use JMS\Serializer\SerializationContext;
17+
use JMS\Serializer\Type\Type;
1718
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
1819
use JMS\Serializer\Visitor\SerializationVisitorInterface;
1920

21+
/**
22+
* @phpstan-import-type TypeArray from Type
23+
*/
2024
final class ArrayCollectionHandler implements SubscribingHandlerInterface
2125
{
2226
public const COLLECTION_TYPES = [
@@ -75,6 +79,8 @@ public static function getSubscribingMethods()
7579
}
7680

7781
/**
82+
* @param TypeArray $type
83+
*
7884
* @return array|\ArrayObject
7985
*/
8086
public function serializeCollection(SerializationVisitorInterface $visitor, Collection $collection, array $type, SerializationContext $context)
@@ -102,6 +108,7 @@ public function serializeCollection(SerializationVisitorInterface $visitor, Coll
102108

103109
/**
104110
* @param mixed $data
111+
* @param TypeArray $type
105112
*/
106113
public function deserializeCollection(
107114
DeserializationVisitorInterface $visitor,

0 commit comments

Comments
 (0)