From af000eee65fb9888cc8183e694a0f7bbfe99e317 Mon Sep 17 00:00:00 2001 From: Rico Sonntag Date: Mon, 28 Nov 2022 07:32:38 +0100 Subject: [PATCH] PHP73 adjustments --- .github/workflows/ci.yml | 2 +- .php-cs-fixer.dist.php | 16 +-- composer.json | 18 +-- phpunit.xml | 9 +- rector.php | 8 +- src/JsonMapper.php | 16 +-- .../CamelCasePropertyNameConverter.php | 2 +- test/Annotation/ReplacePropertyTest.php | 3 +- test/Classes/Base.php | 2 +- test/Classes/CustomClass.php | 2 +- test/Classes/Initialized.php | 13 ++- test/Classes/MultidimensionalArray.php | 2 +- test/Classes/Person.php | 4 +- test/Classes/PlainArrayClass.php | 2 +- test/Classes/Simple.php | 8 +- test/Classes/VariadicSetterClass.php | 2 +- test/Classes/VipPerson.php | 6 +- .../CamelCasePropertyNameConverterTest.php | 3 +- test/JsonMapperTest.php | 108 +++++++++++------- test/TestCase.php | 4 +- 20 files changed, 122 insertions(+), 108 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c1b4c03..14838d0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: strategy: fail-fast: false matrix: - php: [ '8.1', '8.2', '8.3' ] + php: [ '7.3', '7.4.', '8.0' ] steps: - id: checkout diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 4a50534..0042ae2 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -40,7 +40,6 @@ ->setRiskyAllowed(true) ->setRules([ '@PSR12' => true, - '@PER-CS2.0' => true, '@Symfony' => true, // Additional custom rules @@ -57,19 +56,9 @@ 'phpdoc_to_comment' => false, 'phpdoc_no_alias_tag' => false, 'no_superfluous_phpdoc_tags' => false, - 'phpdoc_separation' => [ - 'groups' => [ - [ - 'author', - 'license', - 'link', - ], - ], - ], + 'phpdoc_separation' => true, 'no_alias_functions' => true, - 'whitespace_after_comma_in_array' => [ - 'ensure_single_space' => true, - ], + 'whitespace_after_comma_in_array' => true, 'single_line_throw' => false, 'self_accessor' => false, 'global_namespace_import' => [ @@ -79,7 +68,6 @@ ], 'function_declaration' => [ 'closure_function_spacing' => 'one', - 'closure_fn_spacing' => 'one', ], 'binary_operator_spaces' => [ 'operators' => [ diff --git a/composer.json b/composer.json index c80646e..cbe7593 100644 --- a/composer.json +++ b/composer.json @@ -14,18 +14,18 @@ "issues": "https://github.com/magicsunday/jsonmapper/issues" }, "require": { - "php": ">=8.1.0 <8.4.0", + "php": ">=7.3.0 <8.1.0", "ext-json": "*", - "symfony/property-info": "^6.0 || ^7.0", - "symfony/property-access": "^6.0 || ^7.0", - "doctrine/inflector": "^2.0", - "doctrine/annotations": "^2.0" + "symfony/property-info": "^5.0 || ^6.0 || ^7.0", + "symfony/property-access": "^5.0 || ^6.0 || ^7.0", + "doctrine/inflector": "^1.0 || ^2.0", + "doctrine/annotations": "^1.0 || ^2.0" }, "require-dev": { - "phpdocumentor/reflection-docblock": "^5.0", - "friendsofphp/php-cs-fixer": "^3.50", - "overtrue/phplint": "^3.4 || ^9.0", - "phpunit/phpunit": "^10.0 || ^11.0", + "phpdocumentor/reflection-docblock": "^4.0 || ^5.0", + "friendsofphp/php-cs-fixer": "^2.0 || ^3.0", + "overtrue/phplint": "^2.0 || ^3.0 || ^9.0", + "phpunit/phpunit": "^9.0 || ^10.0 || ^11.0", "phpstan/phpstan": "^1.10", "phpstan/phpstan-phpunit": "^1.3", "phpstan/phpstan-strict-rules": "^1.5", diff --git a/phpunit.xml b/phpunit.xml index 4b92d24..5e23aa0 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,20 +1,19 @@ ./test - + - ./src + ./src - + diff --git a/rector.php b/rector.php index 6ce49fb..07882f5 100644 --- a/rector.php +++ b/rector.php @@ -15,9 +15,6 @@ use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector; use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector; use Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector; -use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector; -use Rector\Php80\Rector\FunctionLike\MixedTypeRector; -use Rector\Php81\Rector\Property\ReadOnlyPropertyRector; use Rector\Set\ValueObject\LevelSetList; use Rector\Set\ValueObject\SetList; use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector; @@ -44,17 +41,14 @@ SetList::CODING_STYLE, SetList::CODE_QUALITY, SetList::DEAD_CODE, - LevelSetList::UP_TO_PHP_81, + LevelSetList::UP_TO_PHP_73, ]); // Skip some rules $rectorConfig->skip([ CatchExceptionNameMatchingTypeRector::class, - ClassPropertyAssignToConstructorPromotionRector::class, LocallyCalledStaticMethodToNonStaticRector::class, - MixedTypeRector::class, ParamTypeByMethodCallTypeRector::class, - ReadOnlyPropertyRector::class, RemoveUselessParamTagRector::class, RemoveUselessReturnTagRector::class, RemoveUselessVarTagRector::class, diff --git a/src/JsonMapper.php b/src/JsonMapper.php index 83a8ce5..fb329c2 100644 --- a/src/JsonMapper.php +++ b/src/JsonMapper.php @@ -46,19 +46,19 @@ class JsonMapper /** * @var PropertyInfoExtractorInterface */ - private PropertyInfoExtractorInterface $extractor; + private $extractor; /** * @var PropertyAccessorInterface */ - private PropertyAccessorInterface $accessor; + private $accessor; /** * The property name converter instance. * * @var PropertyNameConverterInterface|null */ - protected ?PropertyNameConverterInterface $nameConverter; + protected $nameConverter; /** * Override class names that JsonMapper uses to create objects. Useful when your @@ -66,21 +66,21 @@ class JsonMapper * * @var string[]|Closure[] */ - private array $classMap; + private $classMap; /** * The default value type instance. * * @var Type */ - private Type $defaultType; + private $defaultType; /** * The custom types. * * @var Closure[] */ - private array $types = []; + private $types = []; /** * JsonMapper constructor. @@ -302,7 +302,7 @@ private function getReflectionProperty(string $className, string $propertyName): { try { return new ReflectionProperty($className, $propertyName); - } catch (ReflectionException) { + } catch (ReflectionException $exception) { return null; } } @@ -421,7 +421,7 @@ private function getDefaultValue(string $className, string $propertyName) return null; } - return $reflectionProperty->getDefaultValue(); + return $reflectionProperty->getDeclaringClass()->getDefaultProperties()[$propertyName] ?? null; } /** diff --git a/src/JsonMapper/Converter/CamelCasePropertyNameConverter.php b/src/JsonMapper/Converter/CamelCasePropertyNameConverter.php index e57aba3..6553377 100644 --- a/src/JsonMapper/Converter/CamelCasePropertyNameConverter.php +++ b/src/JsonMapper/Converter/CamelCasePropertyNameConverter.php @@ -26,7 +26,7 @@ class CamelCasePropertyNameConverter implements PropertyNameConverterInterface /** * @var Inflector */ - private Inflector $inflector; + private $inflector; /** * CamelCasePropertyNameConverter constructor. diff --git a/test/Annotation/ReplacePropertyTest.php b/test/Annotation/ReplacePropertyTest.php index ec97bcf..c18b8e2 100644 --- a/test/Annotation/ReplacePropertyTest.php +++ b/test/Annotation/ReplacePropertyTest.php @@ -26,8 +26,9 @@ class ReplacePropertyTest extends TestCase { /** * Tests replacing a property. + * + * @test */ - #[Test] public function replaceProperty(): void { $result = $this->getJsonMapper() diff --git a/test/Classes/Base.php b/test/Classes/Base.php index bc04379..16eca29 100644 --- a/test/Classes/Base.php +++ b/test/Classes/Base.php @@ -61,7 +61,7 @@ class Base /** * @var string */ - private string $privateProperty = ''; + private $privateProperty = ''; /** * @return string diff --git a/test/Classes/CustomClass.php b/test/Classes/CustomClass.php index f3acf2b..6e35644 100644 --- a/test/Classes/CustomClass.php +++ b/test/Classes/CustomClass.php @@ -25,5 +25,5 @@ class CustomClass * * or alternatively use Collection */ - public array $persons = []; + public $persons = []; } diff --git a/test/Classes/Initialized.php b/test/Classes/Initialized.php index fa29a41..9b12592 100644 --- a/test/Classes/Initialized.php +++ b/test/Classes/Initialized.php @@ -11,6 +11,7 @@ namespace MagicSunday\Test\Classes; +use MagicSunday\JsonMapper\Annotation\ReplaceNullWithDefaultValue; /** * Class Initialized. * @@ -23,21 +24,21 @@ class Initialized /** * @var int * - * @MagicSunday\JsonMapper\Annotation\ReplaceNullWithDefaultValue + * @ReplaceNullWithDefaultValue */ - public int $integer = 10; + public $integer = 10; /** * @var bool * - * @MagicSunday\JsonMapper\Annotation\ReplaceNullWithDefaultValue + * @ReplaceNullWithDefaultValue */ - public bool $bool = false; + public $bool = false; /** * @var array * - * @MagicSunday\JsonMapper\Annotation\ReplaceNullWithDefaultValue + * @ReplaceNullWithDefaultValue */ - public array $array = []; + public $array = []; } diff --git a/test/Classes/MultidimensionalArray.php b/test/Classes/MultidimensionalArray.php index 0f0a793..f232a0e 100644 --- a/test/Classes/MultidimensionalArray.php +++ b/test/Classes/MultidimensionalArray.php @@ -25,5 +25,5 @@ class MultidimensionalArray * * or alternatively use @var array> */ - public array $persons; + public $persons; } diff --git a/test/Classes/Person.php b/test/Classes/Person.php index 01985bf..33fb8fd 100644 --- a/test/Classes/Person.php +++ b/test/Classes/Person.php @@ -28,10 +28,10 @@ class Person /** * @var bool */ - public bool $is_vip = false; + public $is_vip = false; /** * @var string */ - public string $name; + public $name; } diff --git a/test/Classes/PlainArrayClass.php b/test/Classes/PlainArrayClass.php index c019362..524d0b6 100644 --- a/test/Classes/PlainArrayClass.php +++ b/test/Classes/PlainArrayClass.php @@ -23,7 +23,7 @@ class PlainArrayClass /** * @var array */ - public array $values; + public $values; /** * @return array diff --git a/test/Classes/Simple.php b/test/Classes/Simple.php index 944e88f..c941c52 100644 --- a/test/Classes/Simple.php +++ b/test/Classes/Simple.php @@ -21,16 +21,18 @@ class Simple { /** - * PHP7.4 typed property. + * PHP7.4 typed property + * + * @var int */ - public int $id; + public $id; /** * PHP7.4 typed property. * * @var string */ - public string $name; + public $name; /** * @var int diff --git a/test/Classes/VariadicSetterClass.php b/test/Classes/VariadicSetterClass.php index c7c5dea..b6b60d6 100644 --- a/test/Classes/VariadicSetterClass.php +++ b/test/Classes/VariadicSetterClass.php @@ -23,7 +23,7 @@ class VariadicSetterClass /** * @var int[] */ - private array $values; + private $values; /** * @return int[] diff --git a/test/Classes/VipPerson.php b/test/Classes/VipPerson.php index 9bcae8e..841610f 100644 --- a/test/Classes/VipPerson.php +++ b/test/Classes/VipPerson.php @@ -23,17 +23,17 @@ class VipPerson extends Person /** * @var bool */ - public bool $is_vip = true; + public $is_vip = true; /** * Number of oscars won. * * @var int */ - public int $oscars; + public $oscars; /** * @var string */ - public string $name; + public $name; } diff --git a/test/Converter/CamelCasePropertyNameConverterTest.php b/test/Converter/CamelCasePropertyNameConverterTest.php index 2805fb6..f7e5d69 100644 --- a/test/Converter/CamelCasePropertyNameConverterTest.php +++ b/test/Converter/CamelCasePropertyNameConverterTest.php @@ -26,8 +26,9 @@ class CamelCasePropertyNameConverterTest extends TestCase { /** * Tests mapping json properties to camel case. + * + * @test */ - #[Test] public function checkCamelCasePropertyNameConverter(): void { $converter = new CamelCasePropertyNameConverter(); diff --git a/test/JsonMapperTest.php b/test/JsonMapperTest.php index df54f2e..ccbec3c 100644 --- a/test/JsonMapperTest.php +++ b/test/JsonMapperTest.php @@ -27,8 +27,7 @@ use MagicSunday\Test\Classes\Simple; use MagicSunday\Test\Classes\VariadicSetterClass; use MagicSunday\Test\Classes\VipPerson; -use PHPUnit\Framework\Attributes\DataProvider; -use PHPUnit\Framework\Attributes\Test; +use MagicSunday\Test\Provider\DataProvider; use stdClass; /** @@ -47,10 +46,10 @@ public static function mapArrayOrCollectionWithIntegerKeysJsonDataProvider(): ar { return [ 'mapArray' => [ - Provider\DataProvider::mapArrayJson(), + DataProvider::mapArrayJson(), ], 'mapCollection' => [ - Provider\DataProvider::mapCollectionJson(), + DataProvider::mapCollectionJson(), ], ]; } @@ -58,10 +57,12 @@ public static function mapArrayOrCollectionWithIntegerKeysJsonDataProvider(): ar /** * Tests mapping an array or collection of objects. * + * @dataProvider mapArrayOrCollectionWithIntegerKeysJsonDataProvider + * + * @test + * * @param string $jsonString */ - #[Test] - #[DataProvider('mapArrayOrCollectionWithIntegerKeysJsonDataProvider')] public function mapArrayOrCollection(string $jsonString): void { $result = $this->getJsonMapper() @@ -79,8 +80,9 @@ public function mapArrayOrCollection(string $jsonString): void /** * Tests mapping an array or collection of objects. + * + * @test */ - #[Test] public function mapArrayOrCollectionWithStringKeys(): void { $result = $this->getJsonMapper() @@ -123,7 +125,7 @@ public static function mapSimpleArrayJsonDataProvider(): array { return [ 'mapSimpleArray' => [ - Provider\DataProvider::mapSimpleArrayJson(), + DataProvider::mapSimpleArrayJson(), ], ]; } @@ -131,10 +133,12 @@ public static function mapSimpleArrayJsonDataProvider(): array /** * Tests mapping an array of objects to a property. * + * @dataProvider mapSimpleArrayJsonDataProvider + * + * @test + * * @param string $jsonString */ - #[Test] - #[DataProvider('mapSimpleArrayJsonDataProvider')] public function mapSimpleArray(string $jsonString): void { $result = $this->getJsonMapper() @@ -160,7 +164,7 @@ public static function mapSimpleCollectionJsonDataProvider(): array { return [ 'mapSimpleCollection' => [ - Provider\DataProvider::mapSimpleCollectionJson(), + DataProvider::mapSimpleCollectionJson(), ], ]; } @@ -168,10 +172,12 @@ public static function mapSimpleCollectionJsonDataProvider(): array /** * Tests mapping a collection of objects to a property. * + * @dataProvider mapSimpleCollectionJsonDataProvider + * + * @test + * * @param string $jsonString */ - #[Test] - #[DataProvider('mapSimpleCollectionJsonDataProvider')] public function mapSimpleCollection(string $jsonString): void { $result = $this->getJsonMapper() @@ -197,7 +203,7 @@ public static function mapCustomTypeJsonDataProvider(): array { return [ 'mapCustomType' => [ - Provider\DataProvider::mapCustomTypeJson(), + DataProvider::mapCustomTypeJson(), ], ]; } @@ -205,10 +211,12 @@ public static function mapCustomTypeJsonDataProvider(): array /** * Tests mapping a value using a custom type mapper closure. * + * @dataProvider mapCustomTypeJsonDataProvider + * + * @test + * * @param string $jsonString */ - #[Test] - #[DataProvider('mapCustomTypeJsonDataProvider')] public function mapCustomType(string $jsonString): void { $result = $this->getJsonMapper() @@ -243,7 +251,7 @@ public static function mapSimpleTypesJsonDataProvider(): array { return [ 'mapCustomType' => [ - Provider\DataProvider::mapSimpleTypesJson(), + DataProvider::mapSimpleTypesJson(), ], ]; } @@ -251,10 +259,12 @@ public static function mapSimpleTypesJsonDataProvider(): array /** * Tests mapping simple types. * + * @dataProvider mapSimpleTypesJsonDataProvider + * + * @test + * * @param string $jsonString */ - #[Test] - #[DataProvider('mapSimpleTypesJsonDataProvider')] public function mapSimpleTypesJson(string $jsonString): void { $result = $this->getJsonMapper() @@ -286,7 +296,7 @@ public static function mapObjectUsingCustomClassNameJsonDataProvider(): array { return [ 'mapCustomClassName' => [ - Provider\DataProvider::mapCustomClassNameJson(), + DataProvider::mapCustomClassNameJson(), ], ]; } @@ -294,10 +304,12 @@ public static function mapObjectUsingCustomClassNameJsonDataProvider(): array /** * Tests mapping an object using a custom class name provider closure. * + * @dataProvider mapObjectUsingCustomClassNameJsonDataProvider + * + * @test + * * @param string $jsonString */ - #[Test] - #[DataProvider('mapObjectUsingCustomClassNameJsonDataProvider')] public function mapObjectUsingCustomClassName(string $jsonString): void { $result = $this->getJsonMapper() @@ -334,8 +346,9 @@ static function ($value): string { /** * Tests mapping null to an object not failing. + * + * @test */ - #[Test] public function mapEmptyObject(): void { $result = $this->getJsonMapper() @@ -355,8 +368,9 @@ public function mapEmptyObject(): void /** * Tests mapping a value to a private property using a setter method. + * + * @test */ - #[Test] public function mapToPrivateProperty(): void { $result = $this->getJsonMapper() @@ -376,8 +390,9 @@ public function mapToPrivateProperty(): void /** * Tests mapping json properties to camel case. + * + * @test */ - #[Test] public function checkCamelCasePropertyConverter(): void { $result = $this->getJsonMapper() @@ -397,8 +412,9 @@ public function checkCamelCasePropertyConverter(): void /** * Tests mapping a JSON array with objects into a plain PHP array with objects of given class. + * + * @test */ - #[Test] public function mapArrayOfObjects(): void { $result = $this->getJsonMapper() @@ -426,8 +442,9 @@ public function mapArrayOfObjects(): void /** * Tests mapping a JSON object into an PHP object ignoring a given collection class as the * JSON does not contain a collection. + * + * @test */ - #[Test] public function mapSingleObjectWithGivenCollection(): void { $result = $this->getJsonMapper() @@ -448,8 +465,9 @@ public function mapSingleObjectWithGivenCollection(): void /** * Tests mapping of a multidimensional array. + * + * @test */ - #[Test] public function mapArrayOfArray(): void { $result = $this->getJsonMapper() @@ -489,8 +507,9 @@ public function mapArrayOfArray(): void /** * Tests mapping of values with an initial value. + * + * @test */ - #[Test] public function mapInitialized(): void { $result = $this->getJsonMapper() @@ -508,8 +527,9 @@ public function mapInitialized(): void /** * Tests mapping of default values using @MagicSunday\JsonMapper\Annotation\ReplaceNullWithDefaultValue * annotation in case JSON contains NULL. + * + * @test */ - #[Test] public function mapNullToDefaultValueUsingAnnotation(): void { $result = $this->getJsonMapper() @@ -537,16 +557,18 @@ public static function mapPlainArrayJsonDataProvider(): array { return [ 'mapPlainArray' => [ - Provider\DataProvider::mapPlainArrayJson(), + DataProvider::mapPlainArrayJson(), ], ]; } /** + * @dataProvider mapPlainArrayJsonDataProvider + * * @param string $jsonString + * + * @test */ - #[Test] - #[DataProvider('mapPlainArrayJsonDataProvider')] public function mapPlainArray(string $jsonString): void { $result = $this->getJsonMapper() @@ -573,16 +595,18 @@ public static function mapPlainArrayKeyValueJsonDataProvider(): array { return [ 'mapPlainArrayKeyValue' => [ - Provider\DataProvider::mapPlainArrayKeyValueJson(), + DataProvider::mapPlainArrayKeyValueJson(), ], ]; } /** + * @dataProvider mapPlainArrayKeyValueJsonDataProvider + * * @param string $jsonString + * + * @test */ - #[Test] - #[DataProvider('mapPlainArrayKeyValueJsonDataProvider')] public function mapPlainArrayKeyValue(string $jsonString): void { $result = $this->getJsonMapper() @@ -626,8 +650,9 @@ public function mapPlainArrayKeyValue(string $jsonString): void /** * Tests settings a class property using a variadic setter method. + * + * @test */ - #[Test] public function variadicSetter(): void { $result = $this->getJsonMapper() @@ -653,8 +678,9 @@ public function variadicSetter(): void /** * Tests settings a plain array. + * + * @test */ - #[Test] public function plainArrayClass(): void { $result = $this->getJsonMapper() @@ -680,8 +706,9 @@ public function plainArrayClass(): void /** * Tests mapping an object to a custom class using a class map entry. + * + * @test */ - #[Test] public function mappingBaseElementUsingClassMap(): void { $result = $this->getJsonMapper([ @@ -703,8 +730,9 @@ public function mappingBaseElementUsingClassMap(): void /** * Tests mapping a collection of objects to a custom class using a class map entry. + * + * @test */ - #[Test] public function mappingCollectionElementsUsingClassMap(): void { $result = $this->getJsonMapper([ diff --git a/test/TestCase.php b/test/TestCase.php index 8ae253b..5647419 100644 --- a/test/TestCase.php +++ b/test/TestCase.php @@ -57,7 +57,7 @@ protected function getJsonMapper(array $classMap = []): JsonMapper * * @return mixed|null */ - protected function getJsonAsArray(string $jsonString): mixed + protected function getJsonAsArray(string $jsonString) { try { return json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR); @@ -73,7 +73,7 @@ protected function getJsonAsArray(string $jsonString): mixed * * @return mixed|null */ - protected function getJsonAsObject(string $jsonString): mixed + protected function getJsonAsObject(string $jsonString) { try { return json_decode($jsonString, false, 512, JSON_THROW_ON_ERROR);