Skip to content

Commit 23c6a0c

Browse files
committed
Use native method parameter and return type declarations
I don't want to enforce the logger to be a \Psr\Log\LoggerInterface, both to keep BC and to allow light-weight software that does not depend on the PSR interface classes. BC break if you extend JsonMapper. Related: #252
1 parent af56135 commit 23c6a0c

2 files changed

Lines changed: 81 additions & 94 deletions

File tree

src/JsonMapper.php

Lines changed: 75 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,12 @@ public function map($json, $object)
340340
/**
341341
* Convert a type name to a fully namespaced type name.
342342
*
343-
* @param string|null $type Type name (simple type or class name)
344-
* @param string $strNs Base namespace that gets prepended to the type name
343+
* @param $type Type name (simple type or class name)
344+
* @param $strNs Base namespace that gets prepended to the type name
345345
*
346-
* @return string|null Fully-qualified type name with namespace
346+
* @return Fully-qualified type name with namespace
347347
*/
348-
protected function getFullNamespace($type, $strNs)
348+
protected function getFullNamespace(?string $type, string $strNs): ?string
349349
{
350350
if ($type === null || $type === '' || $type[0] === '\\' || $strNs === '') {
351351
return $type;
@@ -362,14 +362,12 @@ protected function getFullNamespace($type, $strNs)
362362
/**
363363
* Check required properties exist in json
364364
*
365-
* @param array $providedProperties array with json properties
366-
* @param ReflectionClass $rc Reflection class to check
365+
* @param $providedProperties Array with json properties
366+
* @param $rc Reflection class to check
367367
*
368368
* @throws JsonMapper_Exception
369-
*
370-
* @return void
371369
*/
372-
protected function checkMissingData($providedProperties, ReflectionClass $rc)
370+
protected function checkMissingData(array $providedProperties, ReflectionClass $rc): void
373371
{
374372
foreach ($rc->getProperties() as $property) {
375373
$rprop = $rc->getProperty($property->name);
@@ -393,12 +391,10 @@ protected function checkMissingData($providedProperties, ReflectionClass $rc)
393391
* This is to avoid confusion between those that were actually passed
394392
* as NULL, and those that weren't provided at all.
395393
*
396-
* @param object $object Object to remove properties from
397-
* @param array $providedProperties Array with JSON properties
398-
*
399-
* @return void
394+
* @param $object Object to remove properties from
395+
* @param $providedProperties Array with JSON properties
400396
*/
401-
protected function removeUndefinedAttributes($object, $providedProperties)
397+
protected function removeUndefinedAttributes(object $object, array $providedProperties): void
402398
{
403399
foreach (get_object_vars($object) as $propertyName => $dummy) {
404400
if (!isset($providedProperties[$propertyName])) {
@@ -410,21 +406,22 @@ protected function removeUndefinedAttributes($object, $providedProperties)
410406
/**
411407
* Map an array
412408
*
413-
* @param array $json JSON array structure from json_decode()
414-
* @param mixed $array Array or ArrayObject that gets filled with
415-
* data from $json
416-
* @param string $class Class name for children objects.
417-
* All children will get mapped onto this type.
418-
* Supports class names and simple types
419-
* like "string" and nullability "string|null".
420-
* Pass "null" to not convert any values
421-
* @param string $parent_key Defines the key this array belongs to
422-
* in order to aid debugging.
409+
* @param $json JSON array structure from json_decode()
410+
* @param $array Array or ArrayObject that gets filled with
411+
* data from $json
412+
* @param $class Class name for children objects.
413+
* All children will get mapped onto this type.
414+
* Supports class names and simple types
415+
* like "string" and nullability "string|null".
416+
* Pass "null" to not convert any values
417+
* @param $parent_key Defines the key this array belongs to
418+
* in order to aid debugging.
423419
*
424420
* @return mixed Mapped $array is returned
425421
*/
426-
public function mapArray($json, $array, $class = null, $parent_key = '')
427-
{
422+
public function mapArray(
423+
array|object $json, mixed $array, ?string $class = null, string $parent_key = ''
424+
): mixed {
428425
$isNullable = $this->isNullable($class);
429426
$class = $this->removeNullable($class);
430427
$originalClass = $class;
@@ -495,16 +492,16 @@ public function mapArray($json, $array, $class = null, $parent_key = '')
495492
* Try to find out if a property exists in a given class.
496493
* Checks property first, falls back to setter method.
497494
*
498-
* @param ReflectionClass $rc Reflection class to check
499-
* @param string $name Property name
495+
* @param $rc Reflection class to check
496+
* @param $name Property name
500497
*
501498
* @return array First value: if the property exists
502499
* Second value: the accessor to use (
503500
* ReflectionMethod or ReflectionProperty, or null)
504501
* Third value: type of the property
505502
* Fourth value: if the property is nullable
506503
*/
507-
protected function inspectProperty(ReflectionClass $rc, $name)
504+
protected function inspectProperty(ReflectionClass $rc, string $name): array
508505
{
509506
//try setter method first
510507
$setter = 'set' . $this->getCamelCaseName($name);
@@ -626,11 +623,11 @@ protected function inspectProperty(ReflectionClass $rc, $name)
626623
/**
627624
* Removes - and _ and makes the next letter uppercase
628625
*
629-
* @param string $name Property name
626+
* @param $name Property name
630627
*
631628
* @return string CamelCasedVariableName
632629
*/
633-
protected function getCamelCaseName($name)
630+
protected function getCamelCaseName(string $name): string
634631
{
635632
return str_replace(
636633
' ', '', ucwords(str_replace(array('_', '-'), ' ', $name))
@@ -642,11 +639,11 @@ protected function getCamelCaseName($name)
642639
*
643640
* Technically you may use them, but they are awkward to access.
644641
*
645-
* @param string $name Property name
642+
* @param $name Property name
646643
*
647644
* @return string Name without hyphen
648645
*/
649-
protected function getSafeName($name)
646+
protected function getSafeName(string $name): string
650647
{
651648
if (strpos($name, '-') !== false) {
652649
$name = $this->getCamelCaseName($name);
@@ -661,15 +658,13 @@ protected function getSafeName($name)
661658
* Checks if the setter or the property are public are made before
662659
* calling this method.
663660
*
664-
* @param object $object Object to set property on
665-
* @param object $accessor ReflectionMethod or ReflectionProperty
666-
* @param mixed $value Value of property
667-
*
668-
* @return void
661+
* @param $object Object to set property on
662+
* @param $accessor ReflectionMethod or ReflectionProperty
663+
* @param $value Value of property
669664
*/
670665
protected function setProperty(
671-
$object, $accessor, $value
672-
) {
666+
object $object, ReflectionMethod|ReflectionProperty $accessor, mixed $value
667+
): void {
673668
if ($accessor instanceof ReflectionProperty) {
674669
$accessor->setValue($object, $value);
675670
} else if (is_array($value) && $this->hasVariadicArrayType($accessor)) {
@@ -686,15 +681,15 @@ protected function setProperty(
686681
* This method exists to be overwritten in child classes,
687682
* so you can do dependency injection or so.
688683
*
689-
* @param string $class Class name to instantiate
690-
* @param boolean $useParameter Pass $parameter to the constructor or not
691-
* @param mixed $jvalue Constructor parameter (the json value)
684+
* @param $class Class name to instantiate
685+
* @param $useParameter Pass $parameter to the constructor or not
686+
* @param $jvalue Constructor parameter (the json value)
692687
*
693688
* @return object Freshly created object
694689
*/
695690
protected function createInstance(
696-
$class, $useParameter = false, $jvalue = null
697-
) {
691+
string $class, bool $useParameter = false, mixed $jvalue = null
692+
): object {
698693
if ($useParameter) {
699694
if (is_subclass_of($class, \BackedEnum::class)) {
700695
return $class::from($jvalue);
@@ -719,12 +714,12 @@ protected function createInstance(
719714
*
720715
* Lets you override class names via the $classMap property.
721716
*
722-
* @param string|null $type Type name to map
723-
* @param mixed $jvalue Constructor parameter (the json value)
717+
* @param $type Type name to map
718+
* @param $jvalue Constructor parameter (the json value)
724719
*
725-
* @return string|null The mapped type/class name
720+
* @return ?string The mapped type/class name
726721
*/
727-
protected function getMappedType($type, $jvalue = null)
722+
protected function getMappedType(?string $type, mixed $jvalue = null): ?string
728723
{
729724
if (isset($this->classMap[$type ?? ''])) {
730725
$target = $this->classMap[$type];
@@ -749,13 +744,13 @@ protected function getMappedType($type, $jvalue = null)
749744
/**
750745
* Checks if the given type is a "simple type"
751746
*
752-
* @param string $type type name from gettype()
747+
* @param $type type name from gettype()
753748
*
754-
* @return boolean True if it is a simple PHP type
749+
* @return boole True if it is a simple PHP type
755750
*
756751
* @see isFlatType()
757752
*/
758-
protected function isSimpleType($type)
753+
protected function isSimpleType(string $type): bool
759754
{
760755
return $type == 'string'
761756
|| $type == 'boolean' || $type == 'bool'
@@ -768,12 +763,12 @@ protected function isSimpleType($type)
768763
/**
769764
* Checks if the object is of this type or has this type as one of its parents
770765
*
771-
* @param string $type class name of type being required
772-
* @param mixed $value Some PHP value to be tested
766+
* @param $type class name of type being required
767+
* @param $value Some PHP value to be tested
773768
*
774-
* @return boolean True if $object has type of $type
769+
* @return bool True if $object has type of $type
775770
*/
776-
protected function isObjectOfSameType($type, $value)
771+
protected function isObjectOfSameType(string $type, mixed $value): bool
777772
{
778773
if (false === is_object($value)) {
779774
return false;
@@ -786,13 +781,13 @@ protected function isObjectOfSameType($type, $value)
786781
* Checks if the given type is a type that is not nested
787782
* (simple type except array, object and mixed)
788783
*
789-
* @param string $type type name from gettype()
784+
* @param $type type name from gettype()
790785
*
791-
* @return boolean True if it is a non-nested PHP type
786+
* @return bool True if it is a non-nested PHP type
792787
*
793788
* @see isSimpleType()
794789
*/
795-
protected function isFlatType($type)
790+
protected function isFlatType(string $type): bool
796791
{
797792
return $type == 'NULL'
798793
|| $type == 'string'
@@ -805,11 +800,9 @@ protected function isFlatType($type)
805800
* Returns true if type is an array of elements
806801
* (bracket notation)
807802
*
808-
* @param string $strType type to be matched
809-
*
810-
* @return bool
803+
* @param $strType Type to be matched
811804
*/
812-
protected function isArrayOfType($strType)
805+
protected function isArrayOfType(string $strType): bool
813806
{
814807
return substr($strType, -2) === '[]';
815808
}
@@ -818,13 +811,11 @@ protected function isArrayOfType($strType)
818811
* Returns true if accessor is a method and has only one parameter
819812
* which is variadic ("...$args").
820813
*
821-
* @param ReflectionMethod|ReflectionProperty|null $accessor accessor
822-
* to set value
823-
*
824-
* @return bool
814+
* @param $accessor Accessor to set value
825815
*/
826-
protected function hasVariadicArrayType($accessor)
827-
{
816+
protected function hasVariadicArrayType(
817+
ReflectionMethod|ReflectionProperty|null $accessor
818+
): bool {
828819
if (!$accessor instanceof ReflectionMethod) {
829820
return false;
830821
}
@@ -843,11 +834,11 @@ protected function hasVariadicArrayType($accessor)
843834
/**
844835
* Checks if the given type is nullable
845836
*
846-
* @param string $type type name from the phpdoc param
837+
* @param $type type name from the phpdoc param
847838
*
848-
* @return boolean True if it is nullable
839+
* @return bool True if it is nullable
849840
*/
850-
protected function isNullable($type)
841+
protected function isNullable(?string $type): bool
851842
{
852843
return stripos('|' . $type . '|', '|null|') !== false
853844
|| strpos('|' . $type, '|?') !== false;
@@ -856,11 +847,11 @@ protected function isNullable($type)
856847
/**
857848
* Remove the 'null' section of a type
858849
*
859-
* @param string|null $type type name from the phpdoc param
850+
* @param $type type name from the phpdoc param
860851
*
861-
* @return string|null The new type value
852+
* @return ?string The new type value
862853
*/
863-
protected function removeNullable($type)
854+
protected function removeNullable(?string $type): ?string
864855
{
865856
if ($type === null) {
866857
return null;
@@ -875,11 +866,11 @@ protected function removeNullable($type)
875866
* Get a string representation of the reflection type.
876867
* Required because named, union and intersection types need to be handled.
877868
*
878-
* @param ReflectionType $type Native PHP type
869+
* @param $type Native PHP type
879870
*
880871
* @return string "foo|bar"
881872
*/
882-
protected function stringifyReflectionType(ReflectionType $type)
873+
protected function stringifyReflectionType(ReflectionType $type): string
883874
{
884875
if ($type instanceof ReflectionNamedType) {
885876
return ($type->isBuiltin() ? '' : '\\') . $type->getName();
@@ -899,13 +890,13 @@ function (ReflectionNamedType $type) {
899890
/**
900891
* Copied from PHPUnit 3.7.29, Util/Test.php
901892
*
902-
* @param string $docblock Full method docblock
893+
* @param $docblock Full method docblock
903894
*
904895
* @return array Array of arrays.
905896
* Key is the "@"-name like "param",
906897
* each value is an array of the rest of the @-lines
907898
*/
908-
protected static function parseAnnotations($docblock)
899+
protected static function parseAnnotations(string $docblock): array
909900
{
910901
$annotations = array();
911902
// Strip away the docblock header and footer
@@ -927,13 +918,11 @@ protected static function parseAnnotations($docblock)
927918
/**
928919
* Log a message to the $logger object
929920
*
930-
* @param string $level Logging level
931-
* @param string $message Text to log
932-
* @param array $context Additional information
933-
*
934-
* @return void
921+
* @param $level Logging level
922+
* @param $message Text to log
923+
* @param $context Additional information
935924
*/
936-
protected function log($level, $message, array $context = array())
925+
protected function log(string $level, string $message, array $context = array()): void
937926
{
938927
if ($this->logger) {
939928
$this->logger->log($level, $message, $context);
@@ -944,10 +933,8 @@ protected function log($level, $message, array $context = array())
944933
* Sets a logger instance on the object
945934
*
946935
* @param \Psr\Log\LoggerInterface $logger PSR-3 compatible logger object
947-
*
948-
* @return void
949936
*/
950-
public function setLogger($logger)
937+
public function setLogger($logger): void
951938
{
952939
$this->logger = $logger;
953940
}

tests/support/JsonMapperTest/DependencyInjector.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ class JsonMapperTest_DependencyInjector extends JsonMapper
77
* This method exists to be overwritten in child classes,
88
* so you can do dependency injection or so.
99
*
10-
* @param string $class Class name to instantiate
11-
* @param boolean $useParameter Pass $parameter to the constructor or not
12-
* @param mixed $parameter Constructor parameter
10+
* @param $class Class name to instantiate
11+
* @param $useParameter Pass $parameter to the constructor or not
12+
* @param $parameter Constructor parameter
1313
*
1414
* @return object Freshly created object
1515
*/
1616
public function createInstance(
17-
$class, $useParameter = false, $parameter = null
18-
) {
17+
string $class, bool $useParameter = false, mixed $parameter = null
18+
): object {
1919
$object = parent::createInstance($class, $useParameter, $parameter);
2020

2121
//dummy dependency injection
@@ -24,4 +24,4 @@ public function createInstance(
2424
return $object;
2525
}
2626
}
27-
?>
27+
?>

0 commit comments

Comments
 (0)