diff --git a/composer.json b/composer.json index 0feb113..c80646e 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "phpdocumentor/reflection-docblock": "^5.0", "friendsofphp/php-cs-fixer": "^3.50", "overtrue/phplint": "^3.4 || ^9.0", - "phpunit/phpunit": "^9.0 || ^10.0 || ^11.0", + "phpunit/phpunit": "^10.0 || ^11.0", "phpstan/phpstan": "^1.10", "phpstan/phpstan-phpunit": "^1.3", "phpstan/phpstan-strict-rules": "^1.5", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..2a02ba0 --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,6 @@ +parameters: + ignoreErrors: + - + message: "#^Method MagicSunday\\\\JsonMapper\\:\\:makeInstance\\(\\) has parameter \\$constructorArguments with no value type specified in iterable type array\\.$#" + count: 1 + path: src/JsonMapper.php diff --git a/phpstan.neon b/phpstan.neon index be43f61..42a570d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,7 +1,7 @@ includes: - %currentWorkingDirectory%/.build/vendor/phpstan/phpstan-strict-rules/rules.neon - %currentWorkingDirectory%/.build/vendor/phpstan/phpstan-deprecation-rules/rules.neon -# - %currentWorkingDirectory%/phpstan-baseline.neon + - %currentWorkingDirectory%/phpstan-baseline.neon parameters: # You can currently choose from 10 levels (0 is the loosest and 9 is the strictest). diff --git a/phpunit.xml b/phpunit.xml index 927c362..4b92d24 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,19 +1,20 @@ - - - ./src - - ./test + + + ./src + + diff --git a/phpunit.xml.bak b/phpunit.xml.bak new file mode 100644 index 0000000..927c362 --- /dev/null +++ b/phpunit.xml.bak @@ -0,0 +1,19 @@ + + + + + ./src + + + + + ./test + + + diff --git a/src/JsonMapper.php b/src/JsonMapper.php index ea26363..7e5a92d 100644 --- a/src/JsonMapper.php +++ b/src/JsonMapper.php @@ -316,11 +316,11 @@ private function getReflectionProperty(string $className, string $propertyName): */ private function getReflectionClass(string $className): ?ReflectionClass { - try { - return new ReflectionClass($className); - } catch (ReflectionException $exception) { + if (!class_exists($className)) { return null; } + + return new ReflectionClass($className); } /** @@ -421,12 +421,7 @@ private function getDefaultValue(string $className, string $propertyName) return null; } - if (method_exists($reflectionProperty, 'getDefaultValue')) { - // PHP 8+, use getDefaultValue() method - return $reflectionProperty->getDefaultValue(); - } - - return $reflectionProperty->getDeclaringClass()->getDefaultProperties()[$propertyName] ?? null; + return $reflectionProperty->getDefaultValue(); } /** @@ -515,7 +510,11 @@ private function setProperty(object $entity, string $name, $value): void $parameters = $method->getParameters(); if ((count($parameters) === 1) && $parameters[0]->isVariadic()) { - $entity->$methodName(...$value); + $callable = [$entity, $methodName]; + + if (is_callable($callable)) { + call_user_func_array($callable, $value); + } return; } @@ -602,12 +601,7 @@ private function getValue($json, Type $type) */ public function getCollectionValueType(Type $type): Type { - // BC for symfony < 5.3 - if (!method_exists($type, 'getCollectionValueTypes')) { - $collectionValueType = $type->getCollectionValueType(); - } else { - $collectionValueType = $type->getCollectionValueTypes()[0] ?? null; - } + $collectionValueType = $type->getCollectionValueTypes()[0] ?? null; return $collectionValueType ?? $this->defaultType; } diff --git a/test/Classes/Person.php b/test/Classes/Person.php index c6303c6..01985bf 100644 --- a/test/Classes/Person.php +++ b/test/Classes/Person.php @@ -11,6 +11,8 @@ namespace MagicSunday\Test\Classes; +use AllowDynamicProperties; + /** * Class Person. * @@ -20,7 +22,7 @@ * * @property int $oscars Dynamic created property */ -#[\AllowDynamicProperties] +#[AllowDynamicProperties] class Person { /** diff --git a/test/JsonMapperTest.php b/test/JsonMapperTest.php index 48ee882..ccbec3c 100644 --- a/test/JsonMapperTest.php +++ b/test/JsonMapperTest.php @@ -42,7 +42,7 @@ class JsonMapperTest extends TestCase /** * @return string[][] */ - public function mapArrayOrCollectionWithIntegerKeysJsonDataProvider(): array + public static function mapArrayOrCollectionWithIntegerKeysJsonDataProvider(): array { return [ 'mapArray' => [ @@ -121,7 +121,7 @@ public function mapArrayOrCollectionWithStringKeys(): void /** * @return string[][] */ - public function mapSimpleArrayJsonDataProvider(): array + public static function mapSimpleArrayJsonDataProvider(): array { return [ 'mapSimpleArray' => [ @@ -160,7 +160,7 @@ public function mapSimpleArray(string $jsonString): void /** * @return string[][] */ - public function mapSimpleCollectionJsonDataProvider(): array + public static function mapSimpleCollectionJsonDataProvider(): array { return [ 'mapSimpleCollection' => [ @@ -199,7 +199,7 @@ public function mapSimpleCollection(string $jsonString): void /** * @return string[][] */ - public function mapCustomTypeJsonDataProvider(): array + public static function mapCustomTypeJsonDataProvider(): array { return [ 'mapCustomType' => [ @@ -247,7 +247,7 @@ static function ($value): ?CustomConstructor { /** * @return string[][] */ - public function mapSimpleTypesJsonDataProvider(): array + public static function mapSimpleTypesJsonDataProvider(): array { return [ 'mapCustomType' => [ @@ -292,7 +292,7 @@ public function mapSimpleTypesJson(string $jsonString): void /** * @return string[][] */ - public function mapObjectUsingCustomClassNameJsonDataProvider(): array + public static function mapObjectUsingCustomClassNameJsonDataProvider(): array { return [ 'mapCustomClassName' => [ @@ -553,7 +553,7 @@ public function mapNullToDefaultValueUsingAnnotation(): void /** * @return string[][] */ - public function mapPlainArrayJsonDataProvider(): array + public static function mapPlainArrayJsonDataProvider(): array { return [ 'mapPlainArray' => [ @@ -591,7 +591,7 @@ public function mapPlainArray(string $jsonString): void /** * @return string[][] */ - public function mapPlainArrayKeyValueJsonDataProvider(): array + public static function mapPlainArrayKeyValueJsonDataProvider(): array { return [ 'mapPlainArrayKeyValue' => [ @@ -628,9 +628,9 @@ public function mapPlainArrayKeyValue(string $jsonString): void self::assertIsObject($result); self::assertInstanceOf(stdClass::class, $result); - self::assertObjectHasAttribute('A', $result); + self::assertObjectHasProperty('A', $result); self::assertSame(1, $result->A); - self::assertObjectHasAttribute('Z', $result); + self::assertObjectHasProperty('Z', $result); self::assertSame(26, $result->Z); // Map plain array with key <=> value pair to a custom class @@ -642,9 +642,9 @@ public function mapPlainArrayKeyValue(string $jsonString): void self::assertIsObject($result); self::assertInstanceOf(MapPlainArrayKeyValueClass::class, $result); - self::assertObjectHasAttribute('a', $result); + self::assertObjectHasProperty('a', $result); self::assertSame(1, $result->a); - self::assertObjectHasAttribute('z', $result); + self::assertObjectHasProperty('z', $result); self::assertSame(26, $result->z); }