diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c1b4c03..5a2ef98 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: [ '5.6' ] steps: - id: checkout @@ -62,18 +62,6 @@ jobs: run: | php-cs-fixer fix --dry-run --verbose - - id: phpstan - name: PHPStan - if: ${{ always() && steps.install.conclusion == 'success' }} - run: | - composer ci:test:php:phpstan -- --error-format=github - - - id: rector - name: Rector - if: ${{ always() && steps.install.conclusion == 'success' }} - run: | - composer ci:test:php:rector - - id: tests_unit name: Unit Tests if: ${{ always() && steps.install.conclusion == 'success' }} diff --git a/.gitignore b/.gitignore index c6fd842..5252f19 100644 --- a/.gitignore +++ b/.gitignore @@ -15,9 +15,9 @@ composer.lock # Cache .php-cs-fixer.cache +.php_cs.cache .phplint.cache .phpunit.result.cache .phpunit.cache # Custom -/test/ diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 4a50534..696f377 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -40,11 +40,10 @@ ->setRiskyAllowed(true) ->setRules([ '@PSR12' => true, - '@PER-CS2.0' => true, '@Symfony' => true, // Additional custom rules - 'declare_strict_types' => true, + 'declare_strict_types' => false, 'concat_space' => [ 'spacing' => 'one', ], @@ -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/.php_cs.cache b/.php_cs.cache new file mode 100644 index 0000000..df07821 --- /dev/null +++ b/.php_cs.cache @@ -0,0 +1 @@ +{"php":"8.2.17","version":"2.19.3:v2.19.3#75ac86f33fab4714ea5a39a396784d83ae3b5ed8","indent":" ","lineEnding":"\n","rules":{"blank_line_after_opening_tag":true,"braces":{"allow_single_line_anonymous_class_with_empty_body":true,"allow_single_line_closure":true},"compact_nullable_typehint":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_braces":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":true,"return_type_declaration":true,"short_scalar_cast":true,"single_blank_line_before_namespace":true,"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"visibility_required":true,"blank_line_after_namespace":true,"class_definition":{"single_line":true},"constant_case":true,"elseif":true,"function_declaration":{"closure_function_spacing":"one"},"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":true,"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":true,"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"array_syntax":{"syntax":"short"},"backtick_to_shell_exec":true,"binary_operator_spaces":{"operators":{"=":"align_single_space_minimal","=>":"align_single_space_minimal"}},"blank_line_before_statement":{"statements":["return","if","throw"]},"cast_spaces":true,"class_attributes_separation":{"elements":{"method":"one"}},"clean_namespace":true,"concat_space":{"spacing":"one"},"echo_tag_syntax":true,"fully_qualified_strict_types":true,"function_typehint_space":true,"general_phpdoc_tag_rename":{"replacements":{"inheritDocs":"inheritDoc"}},"include":true,"increment_style":true,"lambda_not_used_import":true,"linebreak_after_opening_tag":true,"magic_constant_casing":true,"magic_method_casing":true,"native_function_casing":true,"native_function_type_declaration_casing":true,"no_alias_language_construct_call":true,"no_alternative_syntax":true,"no_binary_string":true,"no_blank_lines_after_phpdoc":true,"no_empty_comment":true,"no_empty_phpdoc":true,"no_empty_statement":true,"no_extra_blank_lines":{"tokens":["case","continue","curly_brace_block","default","extra","parenthesis_brace_block","square_brace_block","switch","throw","use","use_trait"]},"no_leading_namespace_whitespace":true,"no_mixed_echo_print":true,"no_multiline_whitespace_around_double_arrow":true,"no_short_bool_cast":true,"no_singleline_whitespace_before_semicolons":true,"no_spaces_around_offset":true,"no_trailing_comma_in_list_call":true,"no_trailing_comma_in_singleline_array":true,"no_unneeded_control_parentheses":{"statements":["break","clone","continue","echo_print","return","switch_case","yield","yield_from"]},"no_unneeded_curly_braces":{"namespaces":true},"no_unset_cast":true,"no_unused_imports":true,"no_whitespace_before_comma_in_array":true,"normalize_index_brace":true,"object_operator_without_whitespace":true,"php_unit_fqcn_annotation":true,"php_unit_method_casing":true,"phpdoc_align":{"tags":["method","param","property","return","throws","type","var"]},"phpdoc_annotation_without_dot":true,"phpdoc_indent":true,"phpdoc_inline_tag_normalizer":true,"phpdoc_no_access":true,"phpdoc_no_package":true,"phpdoc_no_useless_inheritdoc":true,"phpdoc_return_self_reference":true,"phpdoc_scalar":true,"phpdoc_separation":true,"phpdoc_single_line_var_spacing":true,"phpdoc_summary":true,"phpdoc_tag_type":{"tags":{"inheritDoc":"inline"}},"phpdoc_trim":true,"phpdoc_trim_consecutive_blank_line_separation":true,"phpdoc_types":true,"phpdoc_types_order":{"null_adjustment":"always_last","sort_algorithm":"none"},"phpdoc_var_without_name":true,"protected_to_private":true,"semicolon_after_instruction":true,"single_line_comment_style":{"comment_types":["hash"]},"single_quote":true,"single_space_after_construct":true,"space_after_semicolon":{"remove_in_empty_for_expressions":true},"standardize_increment":true,"standardize_not_equals":true,"switch_continue_to_break":true,"trailing_comma_in_multiline":true,"trim_array_spaces":true,"unary_operator_spaces":true,"whitespace_after_comma_in_array":true,"yoda_style":{"equal":false,"identical":false,"less_and_greater":false,"always_move_variable":false},"header_comment":{"header":"This file is part of the package magicsunday\/jsonmapper.\n\nFor the full copyright and license information, please read the\nLICENSE file that was distributed with this source code.","comment_type":"PHPDoc","location":"after_open","separate":"both"},"no_alias_functions":true,"global_namespace_import":{"import_classes":true,"import_constants":true,"import_functions":true}},"hashes":{"src\/JsonMapper.php":267040816,"src\/JsonMapper\/Annotation\/ReplaceProperty.php":959442023,"src\/JsonMapper\/Annotation\/ReplaceNullWithDefaultValue.php":1601690386,"src\/JsonMapper\/Converter\/CamelCasePropertyNameConverter.php":2881120086,"src\/JsonMapper\/Converter\/PropertyNameConverterInterface.php":3642027963,"rector.php":497958059,"test\/Classes\/Simple.php":4224730192,"test\/Classes\/CustomConstructor.php":3617109242,"test\/Classes\/MultidimensionalArray.php":1188723047,"test\/Classes\/VariadicSetterClass.php":2400783425,"test\/Classes\/Person.php":2133098368,"test\/Classes\/PlainArrayClass.php":376542333,"test\/Classes\/VipPerson.php":2793682997,"test\/Classes\/MapPlainArrayKeyValueClass.php":167325638,"test\/Classes\/Collection.php":2436348255,"test\/Classes\/Initialized.php":3767725526,"test\/Classes\/CustomClass.php":2445941503,"test\/Classes\/ClassMap\/SourceItem.php":2276858995,"test\/Classes\/ClassMap\/CollectionTarget.php":1584121411,"test\/Classes\/ClassMap\/TargetItem.php":2295790439,"test\/Classes\/ClassMap\/CollectionSource.php":2644840146,"test\/Classes\/ReplacePropertyTestClass.php":1360745770,"test\/Classes\/Base.php":1962955433,"test\/Annotation\/ReplacePropertyTest.php":828584376,"test\/TestCase.php":1771366670,"test\/Converter\/CamelCasePropertyNameConverterTest.php":2408713842,"test\/JsonMapperTest.php":3896490306,"test\/Provider\/DataProvider.php":343500605}} \ No newline at end of file diff --git a/composer.json b/composer.json index c80646e..ff3d434 100644 --- a/composer.json +++ b/composer.json @@ -14,23 +14,18 @@ "issues": "https://github.com/magicsunday/jsonmapper/issues" }, "require": { - "php": ">=8.1.0 <8.4.0", + "php": ">=5.6.0 <7.0.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": "^3.4", + "symfony/property-access": "^3.4", + "doctrine/inflector": "^1.1", + "doctrine/annotations": "^1.4" }, "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", - "phpstan/phpstan": "^1.10", - "phpstan/phpstan-phpunit": "^1.3", - "phpstan/phpstan-strict-rules": "^1.5", - "phpstan/phpstan-deprecation-rules": "^1.1", - "rector/rector": "^1.0" + "phpdocumentor/reflection-docblock": "^3.3", + "friendsofphp/php-cs-fixer": "^2.19", + "overtrue/phplint": "^2.4", + "phpunit/phpunit": "^5.7" }, "suggest": { "phpdocumentor/reflection-docblock": "In order to use the PhpDocExtractor this library is required too." @@ -46,6 +41,9 @@ } }, "config": { + "platform": { + "php": "5.6" + }, "bin-dir": ".build/bin", "vendor-dir": ".build/vendor", "discard-changes": true, @@ -60,19 +58,8 @@ "ci:test:php:unit": [ "phpunit" ], - "ci:test:php:phpstan": [ - "phpstan analyze" - ], - "ci:test:php:phpstan:baseline": [ - "phpstan analyze --generate-baseline phpstan-baseline.neon --allow-empty-baseline" - ], - "ci:test:php:rector": [ - "rector process --config rector.php --dry-run" - ], "ci:test": [ "@ci:test:php:lint", - "@ci:test:php:phpstan", - "@ci:test:php:rector", "@ci:test:php:unit" ], "ci:cgl": [ diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon deleted file mode 100644 index 2a02ba0..0000000 --- a/phpstan-baseline.neon +++ /dev/null @@ -1,6 +0,0 @@ -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 deleted file mode 100644 index 4403dee..0000000 --- a/phpstan.neon +++ /dev/null @@ -1,18 +0,0 @@ -includes: - - %currentWorkingDirectory%/.build/vendor/phpstan/phpstan-strict-rules/rules.neon - - %currentWorkingDirectory%/.build/vendor/phpstan/phpstan-deprecation-rules/rules.neon - - %currentWorkingDirectory%/phpstan-baseline.neon - -parameters: - # You can currently choose from 10 levels (0 is the loosest and 9 is the strictest). - level: 8 - - paths: - - %currentWorkingDirectory%/src/ - - excludePaths: - - %currentWorkingDirectory%/test/ - - %currentWorkingDirectory%/.build/ - - # Ignored validations - checkGenericClassInNonGenericObjectType: false diff --git a/phpunit.xml b/phpunit.xml index 4b92d24..c35f58d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,20 +1,19 @@ ./test - - - ./src - - + + + ./src + + diff --git a/rector.php b/rector.php deleted file mode 100644 index 69ab608..0000000 --- a/rector.php +++ /dev/null @@ -1,62 +0,0 @@ -paths([ - __DIR__ . '/src', - ]); - - $rectorConfig->skip([ - __DIR__ . '/.build', - __DIR__ . '/test', - ]); - - $rectorConfig->phpstanConfig('phpstan.neon'); - $rectorConfig->importNames(); - $rectorConfig->removeUnusedImports(); - $rectorConfig->disableParallel(); - - // Define what rule sets will be applied - $rectorConfig->sets([ - SetList::EARLY_RETURN, - SetList::TYPE_DECLARATION, - SetList::CODING_STYLE, - SetList::CODE_QUALITY, - SetList::DEAD_CODE, - LevelSetList::UP_TO_PHP_74, - ]); - - // 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 7e5a92d..14b5683 100644 --- a/src/JsonMapper.php +++ b/src/JsonMapper.php @@ -7,15 +7,16 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday; +use function array_key_exists; use Closure; use Doctrine\Common\Annotations\Annotation; use Doctrine\Common\Annotations\AnnotationReader; use DomainException; +use function in_array; use InvalidArgumentException; +use function is_array; use MagicSunday\JsonMapper\Annotation\ReplaceNullWithDefaultValue; use MagicSunday\JsonMapper\Annotation\ReplaceProperty; use MagicSunday\JsonMapper\Converter\PropertyNameConverterInterface; @@ -27,15 +28,12 @@ use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface; use Symfony\Component\PropertyInfo\Type; -use function array_key_exists; -use function in_array; -use function is_array; - /** * JsonMapper. * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ * * @template TEntity @@ -46,19 +44,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 +64,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. @@ -93,7 +91,7 @@ class JsonMapper public function __construct( PropertyInfoExtractorInterface $extractor, PropertyAccessorInterface $accessor, - ?PropertyNameConverterInterface $nameConverter = null, + PropertyNameConverterInterface $nameConverter = null, array $classMap = [] ) { $this->extractor = $extractor; @@ -111,7 +109,7 @@ public function __construct( * * @return JsonMapper */ - public function addType(string $type, Closure $closure): JsonMapper + public function addType($type, Closure $closure) { $this->types[$type] = $closure; @@ -128,7 +126,7 @@ public function addType(string $type, Closure $closure): JsonMapper * * @return JsonMapper */ - public function addCustomClassMapEntry(string $className, Closure $closure): JsonMapper + public function addCustomClassMapEntry($className, Closure $closure) { $this->classMap[$className] = $closure; @@ -152,7 +150,7 @@ public function addCustomClassMapEntry(string $className, Closure $closure): Jso * @throws DomainException * @throws InvalidArgumentException */ - public function map($json, ?string $className = null, ?string $collectionClassName = null) + public function map($json, $className = null, $collectionClassName = null) { // Return plain JSON if no mapping classes are provided if ($className === null) { @@ -245,12 +243,12 @@ public function map($json, ?string $className = null, ?string $collectionClassNa * * @template T * - * @param class-string $className The class to instantiate - * @param array|null ...$constructorArguments The arguments of the constructor + * @param class-string $className The class to instantiate + * @param array|null $constructorArguments The arguments of the constructor * * @return T */ - private function makeInstance(string $className, ?array ...$constructorArguments) + private function makeInstance($className, array $constructorArguments = null) { /** @var T $instance */ $instance = new $className(...$constructorArguments); @@ -266,7 +264,7 @@ private function makeInstance(string $className, ?array ...$constructorArguments * * @return bool */ - private function isReplaceNullWithDefaultValueAnnotation(string $className, string $propertyName): bool + private function isReplaceNullWithDefaultValueAnnotation($className, $propertyName) { return $this->hasPropertyAnnotation( $className, @@ -282,7 +280,7 @@ private function isReplaceNullWithDefaultValueAnnotation(string $className, stri * * @return bool */ - private function isReplacePropertyAnnotation(string $className): bool + private function isReplacePropertyAnnotation($className) { return $this->hasClassAnnotation( $className, @@ -298,7 +296,7 @@ private function isReplacePropertyAnnotation(string $className): bool * * @return ReflectionProperty|null */ - private function getReflectionProperty(string $className, string $propertyName): ?ReflectionProperty + private function getReflectionProperty($className, $propertyName) { try { return new ReflectionProperty($className, $propertyName); @@ -314,7 +312,7 @@ private function getReflectionProperty(string $className, string $propertyName): * * @return ReflectionClass|null */ - private function getReflectionClass(string $className): ?ReflectionClass + private function getReflectionClass($className) { if (!class_exists($className)) { return null; @@ -331,7 +329,7 @@ private function getReflectionClass(string $className): ?ReflectionClass * * @return Annotation[]|object[] */ - private function extractPropertyAnnotations(string $className, string $propertyName): array + private function extractPropertyAnnotations($className, $propertyName) { $reflectionProperty = $this->getReflectionProperty($className, $propertyName); @@ -350,7 +348,7 @@ private function extractPropertyAnnotations(string $className, string $propertyN * * @return Annotation[]|object[] */ - private function extractClassAnnotations(string $className): array + private function extractClassAnnotations($className) { $reflectionClass = $this->getReflectionClass($className); @@ -371,7 +369,7 @@ private function extractClassAnnotations(string $className): array * * @return bool */ - private function hasPropertyAnnotation(string $className, string $propertyName, string $annotationName): bool + private function hasPropertyAnnotation($className, $propertyName, $annotationName) { $annotations = $this->extractPropertyAnnotations($className, $propertyName); @@ -392,7 +390,7 @@ private function hasPropertyAnnotation(string $className, string $propertyName, * * @return bool */ - private function hasClassAnnotation(string $className, string $annotationName): bool + private function hasClassAnnotation($className, $annotationName) { $annotations = $this->extractClassAnnotations($className); @@ -413,7 +411,7 @@ private function hasClassAnnotation(string $className, string $annotationName): * * @return mixed|null */ - private function getDefaultValue(string $className, string $propertyName) + private function getDefaultValue($className, $propertyName) { $reflectionProperty = $this->getReflectionProperty($className, $propertyName); @@ -421,7 +419,9 @@ private function getDefaultValue(string $className, string $propertyName) return null; } - return $reflectionProperty->getDefaultValue(); + $defaultProperties = $reflectionProperty->getDeclaringClass()->getDefaultProperties(); + + return isset($defaultProperties[$propertyName]) ? $defaultProperties[$propertyName] : null; } /** @@ -431,7 +431,7 @@ private function getDefaultValue(string $className, string $propertyName) * * @return bool */ - private function isNumericIndexArray($json): bool + private function isNumericIndexArray($json) { foreach ($json as $propertyName => $propertyValue) { if (is_int($propertyName)) { @@ -449,7 +449,7 @@ private function isNumericIndexArray($json): bool * * @return bool */ - private function isIterableWithArraysOrObjects($json): bool + private function isIterableWithArraysOrObjects($json) { foreach ($json as $propertyValue) { if (is_array($propertyValue)) { @@ -475,7 +475,7 @@ private function isIterableWithArraysOrObjects($json): bool * * @throws InvalidArgumentException */ - private function assertClassesExists(string $className, ?string $collectionClassName = null): void + private function assertClassesExists($className, $collectionClassName = null) { if (!class_exists($className)) { throw new InvalidArgumentException(sprintf('Class [%s] does not exist', $className)); @@ -499,7 +499,7 @@ private function assertClassesExists(string $className, ?string $collectionClass * @param string $name * @param mixed $value */ - private function setProperty(object $entity, string $name, $value): void + private function setProperty($entity, $name, $value) { // Handle variadic setters if (is_array($value)) { @@ -531,9 +531,11 @@ private function setProperty(object $entity, string $name, $value): void * * @return string[] */ - private function getProperties(string $className): array + private function getProperties($className) { - return $this->extractor->getProperties($className) ?? []; + $properties = $this->extractor->getProperties($className); + + return $properties !== null ? $properties : []; } /** @@ -544,9 +546,11 @@ private function getProperties(string $className): array * * @return Type */ - private function getType(string $className, string $propertyName): Type + private function getType($className, $propertyName) { - return $this->extractor->getTypes($className, $propertyName)[0] ?? $this->defaultType; + $types = $this->extractor->getTypes($className, $propertyName); + + return isset($types[0]) ? $types[0] : $this->defaultType; } /** @@ -599,11 +603,11 @@ private function getValue($json, Type $type) * * @return Type */ - public function getCollectionValueType(Type $type): Type + public function getCollectionValueType(Type $type) { - $collectionValueType = $type->getCollectionValueTypes()[0] ?? null; + $collectionValueType = $type->getCollectionValueType(); - return $collectionValueType ?? $this->defaultType; + return $collectionValueType !== null ? $collectionValueType : $this->defaultType; } /** @@ -615,7 +619,7 @@ public function getCollectionValueType(Type $type): Type * * @throws DomainException */ - private function getClassNameFromType(Type $type): string + private function getClassNameFromType(Type $type) { /** @var class-string|null $className */ $className = $type->getClassName(); @@ -641,7 +645,7 @@ private function getClassNameFromType(Type $type): string * * @throws DomainException */ - private function getMappedClassName(string $className, $json): string + private function getMappedClassName($className, $json) { if (array_key_exists($className, $this->classMap)) { $classNameOrClosure = $this->classMap[$className]; @@ -669,7 +673,7 @@ private function getMappedClassName(string $className, $json): string * * @throws DomainException */ - private function getClassName($json, Type $type): string + private function getClassName($json, Type $type) { return $this->getMappedClassName( $this->getClassNameFromType($type), @@ -687,7 +691,7 @@ private function getClassName($json, Type $type): string * * @throws DomainException */ - private function asCollection($json, Type $type): ?array + private function asCollection($json, $type) { if ($json === null) { return null; @@ -733,7 +737,7 @@ private function asObject($json, Type $type) * * @return bool */ - private function isCustomType(string $typeClassName): bool + private function isCustomType($typeClassName) { return array_key_exists($typeClassName, $this->types); } @@ -748,7 +752,7 @@ private function isCustomType(string $typeClassName): bool * * @return mixed */ - private function callCustomClosure($json, string $typeClassName) + private function callCustomClosure($json, $typeClassName) { $callback = $this->types[$typeClassName]; diff --git a/src/JsonMapper/Annotation/ReplaceNullWithDefaultValue.php b/src/JsonMapper/Annotation/ReplaceNullWithDefaultValue.php index 740c687..5991b3f 100644 --- a/src/JsonMapper/Annotation/ReplaceNullWithDefaultValue.php +++ b/src/JsonMapper/Annotation/ReplaceNullWithDefaultValue.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\JsonMapper\Annotation; use Doctrine\Common\Annotations\Annotation; diff --git a/src/JsonMapper/Annotation/ReplaceProperty.php b/src/JsonMapper/Annotation/ReplaceProperty.php index ae60a9d..a008fa8 100644 --- a/src/JsonMapper/Annotation/ReplaceProperty.php +++ b/src/JsonMapper/Annotation/ReplaceProperty.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\JsonMapper\Annotation; use Doctrine\Common\Annotations\Annotation; diff --git a/src/JsonMapper/Converter/CamelCasePropertyNameConverter.php b/src/JsonMapper/Converter/CamelCasePropertyNameConverter.php index e57aba3..8a687c8 100644 --- a/src/JsonMapper/Converter/CamelCasePropertyNameConverter.php +++ b/src/JsonMapper/Converter/CamelCasePropertyNameConverter.php @@ -7,35 +7,20 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\JsonMapper\Converter; -use Doctrine\Inflector\Inflector; -use Doctrine\Inflector\InflectorFactory; +use Doctrine\Common\Inflector\Inflector; /** * CamelCasePropertyNameConverter. * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class CamelCasePropertyNameConverter implements PropertyNameConverterInterface { - /** - * @var Inflector - */ - private Inflector $inflector; - - /** - * CamelCasePropertyNameConverter constructor. - */ - public function __construct() - { - $this->inflector = InflectorFactory::create()->build(); - } - /** * Convert the specified JSON property name to its PHP property name. * @@ -43,8 +28,8 @@ public function __construct() * * @return string */ - public function convert(string $name): string + public function convert($name) { - return $this->inflector->camelize($name); + return Inflector::camelize($name); } } diff --git a/src/JsonMapper/Converter/PropertyNameConverterInterface.php b/src/JsonMapper/Converter/PropertyNameConverterInterface.php index 80c3c28..e173a9b 100644 --- a/src/JsonMapper/Converter/PropertyNameConverterInterface.php +++ b/src/JsonMapper/Converter/PropertyNameConverterInterface.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\JsonMapper\Converter; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ interface PropertyNameConverterInterface @@ -27,5 +26,5 @@ interface PropertyNameConverterInterface * * @return string */ - public function convert(string $name): string; + public function convert($name); } diff --git a/test/Annotation/ReplacePropertyTest.php b/test/Annotation/ReplacePropertyTest.php index c8aa085..0e0cb01 100644 --- a/test/Annotation/ReplacePropertyTest.php +++ b/test/Annotation/ReplacePropertyTest.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Annotation; use MagicSunday\Test\Classes\ReplacePropertyTestClass; @@ -19,6 +17,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class ReplacePropertyTest extends TestCase @@ -28,7 +27,7 @@ class ReplacePropertyTest extends TestCase * * @test */ - public function replaceProperty(): void + public function replaceProperty() { $result = $this->getJsonMapper() ->map( diff --git a/test/Classes/Base.php b/test/Classes/Base.php index bc04379..f18567c 100644 --- a/test/Classes/Base.php +++ b/test/Classes/Base.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class Base @@ -61,12 +60,12 @@ class Base /** * @var string */ - private string $privateProperty = ''; + private $privateProperty = ''; /** * @return string */ - public function getPrivateProperty(): string + public function getPrivateProperty() { return $this->privateProperty; } @@ -74,7 +73,7 @@ public function getPrivateProperty(): string /** * @param string $privateProperty */ - public function setPrivateProperty(string $privateProperty): void + public function setPrivateProperty($privateProperty) { $this->privateProperty = $privateProperty; } diff --git a/test/Classes/ClassMap/CollectionSource.php b/test/Classes/ClassMap/CollectionSource.php index ff07cf5..e390fd0 100644 --- a/test/Classes/ClassMap/CollectionSource.php +++ b/test/Classes/ClassMap/CollectionSource.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes\ClassMap; use MagicSunday\Test\Classes\Collection; @@ -18,6 +16,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class CollectionSource extends Collection diff --git a/test/Classes/ClassMap/CollectionTarget.php b/test/Classes/ClassMap/CollectionTarget.php index a649180..44e5d68 100644 --- a/test/Classes/ClassMap/CollectionTarget.php +++ b/test/Classes/ClassMap/CollectionTarget.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes\ClassMap; use ArrayObject; @@ -18,6 +16,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class CollectionTarget extends ArrayObject diff --git a/test/Classes/ClassMap/SourceItem.php b/test/Classes/ClassMap/SourceItem.php index 0fcfdc2..ec6ccd2 100644 --- a/test/Classes/ClassMap/SourceItem.php +++ b/test/Classes/ClassMap/SourceItem.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes\ClassMap; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class SourceItem diff --git a/test/Classes/ClassMap/TargetItem.php b/test/Classes/ClassMap/TargetItem.php index f29954f..4807424 100644 --- a/test/Classes/ClassMap/TargetItem.php +++ b/test/Classes/ClassMap/TargetItem.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes\ClassMap; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class TargetItem diff --git a/test/Classes/Collection.php b/test/Classes/Collection.php index 0881d65..15fab55 100644 --- a/test/Classes/Collection.php +++ b/test/Classes/Collection.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; use ArrayAccess; @@ -19,6 +17,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ * * @template TKey of array-key diff --git a/test/Classes/CustomClass.php b/test/Classes/CustomClass.php index f3acf2b..e20b702 100644 --- a/test/Classes/CustomClass.php +++ b/test/Classes/CustomClass.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class CustomClass @@ -25,5 +24,5 @@ class CustomClass * * or alternatively use Collection */ - public array $persons = []; + public $persons = []; } diff --git a/test/Classes/CustomConstructor.php b/test/Classes/CustomConstructor.php index f1b1455..24f2904 100644 --- a/test/Classes/CustomConstructor.php +++ b/test/Classes/CustomConstructor.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class CustomConstructor @@ -30,7 +29,7 @@ class CustomConstructor * * @param string $name */ - public function __construct(string $name) + public function __construct($name) { $this->name = $name; } diff --git a/test/Classes/Initialized.php b/test/Classes/Initialized.php index fa29a41..8073ab2 100644 --- a/test/Classes/Initialized.php +++ b/test/Classes/Initialized.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class Initialized @@ -25,19 +24,19 @@ class Initialized * * @MagicSunday\JsonMapper\Annotation\ReplaceNullWithDefaultValue */ - public int $integer = 10; + public $integer = 10; /** * @var bool * * @MagicSunday\JsonMapper\Annotation\ReplaceNullWithDefaultValue */ - public bool $bool = false; + public $bool = false; /** * @var array * * @MagicSunday\JsonMapper\Annotation\ReplaceNullWithDefaultValue */ - public array $array = []; + public $array = []; } diff --git a/test/Classes/MapPlainArrayKeyValueClass.php b/test/Classes/MapPlainArrayKeyValueClass.php index 707f423..5d6ecbf 100644 --- a/test/Classes/MapPlainArrayKeyValueClass.php +++ b/test/Classes/MapPlainArrayKeyValueClass.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class MapPlainArrayKeyValueClass diff --git a/test/Classes/MultidimensionalArray.php b/test/Classes/MultidimensionalArray.php index 0f0a793..ba2dd5a 100644 --- a/test/Classes/MultidimensionalArray.php +++ b/test/Classes/MultidimensionalArray.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class MultidimensionalArray @@ -25,5 +24,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..6d4cf23 100644 --- a/test/Classes/Person.php +++ b/test/Classes/Person.php @@ -7,31 +7,27 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; -use AllowDynamicProperties; - /** * Class Person. * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ * * @property int $oscars Dynamic created property */ -#[AllowDynamicProperties] 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..52cf923 100644 --- a/test/Classes/PlainArrayClass.php +++ b/test/Classes/PlainArrayClass.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class PlainArrayClass @@ -23,12 +22,12 @@ class PlainArrayClass /** * @var array */ - public array $values; + public $values; /** * @return array */ - public function getValues(): array + public function getValues() { return $this->values; } diff --git a/test/Classes/ReplacePropertyTestClass.php b/test/Classes/ReplacePropertyTestClass.php index 90aa633..08fdaa1 100644 --- a/test/Classes/ReplacePropertyTestClass.php +++ b/test/Classes/ReplacePropertyTestClass.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; use MagicSunday\JsonMapper\Annotation\ReplaceProperty; @@ -18,6 +16,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ * * @ReplaceProperty("type", replaces="ftype") @@ -43,15 +42,15 @@ class ReplacePropertyTestClass /** * @return int */ - public function getType(): int + public function getType() { return $this->type; } /** - * @param int $type + * @param $type */ - public function setType(int $type): void + public function setType($type) { $this->type = $type; } diff --git a/test/Classes/Simple.php b/test/Classes/Simple.php index 944e88f..d17090f 100644 --- a/test/Classes/Simple.php +++ b/test/Classes/Simple.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class Simple @@ -23,14 +22,14 @@ class Simple /** * PHP7.4 typed property. */ - 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..b70a231 100644 --- a/test/Classes/VariadicSetterClass.php +++ b/test/Classes/VariadicSetterClass.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class VariadicSetterClass @@ -23,12 +22,12 @@ class VariadicSetterClass /** * @var int[] */ - private array $values; + private $values; /** * @return int[] */ - public function getValues(): array + public function getValues() { return $this->values; } @@ -38,7 +37,7 @@ public function getValues(): array * * @return void */ - public function setValues(int ...$values): void + public function setValues(...$values) { $this->values = $values; } diff --git a/test/Classes/VipPerson.php b/test/Classes/VipPerson.php index 9bcae8e..b573af1 100644 --- a/test/Classes/VipPerson.php +++ b/test/Classes/VipPerson.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Classes; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class VipPerson extends Person @@ -23,17 +22,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 579049a..4419ab5 100644 --- a/test/Converter/CamelCasePropertyNameConverterTest.php +++ b/test/Converter/CamelCasePropertyNameConverterTest.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Converter; use MagicSunday\JsonMapper\Converter\CamelCasePropertyNameConverter; @@ -19,6 +17,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class CamelCasePropertyNameConverterTest extends TestCase @@ -28,7 +27,7 @@ class CamelCasePropertyNameConverterTest extends TestCase * * @test */ - public function checkCamelCasePropertyNameConverter(): void + public function checkCamelCasePropertyNameConverter() { $converter = new CamelCasePropertyNameConverter(); diff --git a/test/JsonMapperTest.php b/test/JsonMapperTest.php index ccbec3c..6faed7a 100644 --- a/test/JsonMapperTest.php +++ b/test/JsonMapperTest.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test; use MagicSunday\Test\Classes\Base; @@ -35,6 +33,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class JsonMapperTest extends TestCase @@ -42,7 +41,7 @@ class JsonMapperTest extends TestCase /** * @return string[][] */ - public static function mapArrayOrCollectionWithIntegerKeysJsonDataProvider(): array + public static function mapArrayOrCollectionWithIntegerKeysJsonDataProvider() { return [ 'mapArray' => [ @@ -63,7 +62,7 @@ public static function mapArrayOrCollectionWithIntegerKeysJsonDataProvider(): ar * * @param string $jsonString */ - public function mapArrayOrCollection(string $jsonString): void + public function mapArrayOrCollection($jsonString) { $result = $this->getJsonMapper() ->map( @@ -83,7 +82,7 @@ public function mapArrayOrCollection(string $jsonString): void * * @test */ - public function mapArrayOrCollectionWithStringKeys(): void + public function mapArrayOrCollectionWithStringKeys() { $result = $this->getJsonMapper() ->map( @@ -121,7 +120,7 @@ public function mapArrayOrCollectionWithStringKeys(): void /** * @return string[][] */ - public static function mapSimpleArrayJsonDataProvider(): array + public static function mapSimpleArrayJsonDataProvider() { return [ 'mapSimpleArray' => [ @@ -139,7 +138,7 @@ public static function mapSimpleArrayJsonDataProvider(): array * * @param string $jsonString */ - public function mapSimpleArray(string $jsonString): void + public function mapSimpleArray($jsonString) { $result = $this->getJsonMapper() ->map( @@ -148,7 +147,7 @@ public function mapSimpleArray(string $jsonString): void ); self::assertInstanceOf(Base::class, $result); - self::assertIsArray($result->simpleArray); + self::assertInternalType('array', $result->simpleArray); self::assertCount(2, $result->simpleArray); self::assertContainsOnlyInstancesOf(Simple::class, $result->simpleArray); self::assertSame(1, $result->simpleArray[0]->id); @@ -160,7 +159,7 @@ public function mapSimpleArray(string $jsonString): void /** * @return string[][] */ - public static function mapSimpleCollectionJsonDataProvider(): array + public static function mapSimpleCollectionJsonDataProvider() { return [ 'mapSimpleCollection' => [ @@ -178,7 +177,7 @@ public static function mapSimpleCollectionJsonDataProvider(): array * * @param string $jsonString */ - public function mapSimpleCollection(string $jsonString): void + public function mapSimpleCollection($jsonString) { $result = $this->getJsonMapper() ->map( @@ -199,7 +198,7 @@ public function mapSimpleCollection(string $jsonString): void /** * @return string[][] */ - public static function mapCustomTypeJsonDataProvider(): array + public static function mapCustomTypeJsonDataProvider() { return [ 'mapCustomType' => [ @@ -217,12 +216,12 @@ public static function mapCustomTypeJsonDataProvider(): array * * @param string $jsonString */ - public function mapCustomType(string $jsonString): void + public function mapCustomType($jsonString) { $result = $this->getJsonMapper() ->addType( CustomConstructor::class, - static function ($value): ?CustomConstructor { + static function ($value) { if (is_array($value) && $value['name']) { return new CustomConstructor($value['name']); } @@ -247,7 +246,7 @@ static function ($value): ?CustomConstructor { /** * @return string[][] */ - public static function mapSimpleTypesJsonDataProvider(): array + public static function mapSimpleTypesJsonDataProvider() { return [ 'mapCustomType' => [ @@ -265,7 +264,7 @@ public static function mapSimpleTypesJsonDataProvider(): array * * @param string $jsonString */ - public function mapSimpleTypesJson(string $jsonString): void + public function mapSimpleTypesJson($jsonString) { $result = $this->getJsonMapper() ->map( @@ -292,7 +291,7 @@ public function mapSimpleTypesJson(string $jsonString): void /** * @return string[][] */ - public static function mapObjectUsingCustomClassNameJsonDataProvider(): array + public static function mapObjectUsingCustomClassNameJsonDataProvider() { return [ 'mapCustomClassName' => [ @@ -310,13 +309,13 @@ public static function mapObjectUsingCustomClassNameJsonDataProvider(): array * * @param string $jsonString */ - public function mapObjectUsingCustomClassName(string $jsonString): void + public function mapObjectUsingCustomClassName($jsonString) { $result = $this->getJsonMapper() ->addCustomClassMapEntry( Person::class, // Map each entry of the collection to a separate class - static function ($value): string { + static function ($value) { if ((is_array($value) && $value['is_vip']) || (($value instanceof stdClass) && $value->is_vip)) { return VipPerson::class; } @@ -331,7 +330,7 @@ static function ($value): string { self::assertInstanceOf(Base::class, $result); self::assertInstanceOf(CustomClass::class, $result->customClass); - self::assertIsArray($result->customClass->persons); + self::assertInternalType('array', $result->customClass->persons); self::assertCount(2, $result->customClass->persons); self::assertInstanceOf(Person::class, $result->customClass->persons[0]); @@ -349,7 +348,7 @@ static function ($value): string { * * @test */ - public function mapEmptyObject(): void + public function mapEmptyObject() { $result = $this->getJsonMapper() ->map( @@ -371,7 +370,7 @@ public function mapEmptyObject(): void * * @test */ - public function mapToPrivateProperty(): void + public function mapToPrivateProperty() { $result = $this->getJsonMapper() ->map( @@ -393,7 +392,7 @@ public function mapToPrivateProperty(): void * * @test */ - public function checkCamelCasePropertyConverter(): void + public function checkCamelCasePropertyConverter() { $result = $this->getJsonMapper() ->map( @@ -415,7 +414,7 @@ public function checkCamelCasePropertyConverter(): void * * @test */ - public function mapArrayOfObjects(): void + public function mapArrayOfObjects() { $result = $this->getJsonMapper() ->map( @@ -433,7 +432,7 @@ public function mapArrayOfObjects(): void Base::class ); - self::assertIsArray($result); + self::assertInternalType('array', $result); self::assertContainsOnlyInstancesOf(Base::class, $result); self::assertSame('foo', $result[0]->name); self::assertSame('bar', $result[1]->name); @@ -445,7 +444,7 @@ public function mapArrayOfObjects(): void * * @test */ - public function mapSingleObjectWithGivenCollection(): void + public function mapSingleObjectWithGivenCollection() { $result = $this->getJsonMapper() ->map( @@ -468,7 +467,7 @@ public function mapSingleObjectWithGivenCollection(): void * * @test */ - public function mapArrayOfArray(): void + public function mapArrayOfArray() { $result = $this->getJsonMapper() ->map( @@ -499,7 +498,7 @@ public function mapArrayOfArray(): void ); self::assertInstanceOf(MultidimensionalArray::class, $result); - self::assertIsArray($result->persons); + self::assertInternalType('array', $result->persons); self::assertContainsOnly('array', $result->persons); self::assertContainsOnlyInstancesOf(Person::class, $result->persons[0]); self::assertContainsOnlyInstancesOf(Person::class, $result->persons[1]); @@ -510,7 +509,7 @@ public function mapArrayOfArray(): void * * @test */ - public function mapInitialized(): void + public function mapInitialized() { $result = $this->getJsonMapper() ->map( @@ -530,17 +529,15 @@ public function mapInitialized(): void * * @test */ - public function mapNullToDefaultValueUsingAnnotation(): void + public function mapNullToDefaultValueUsingAnnotation() { $result = $this->getJsonMapper() ->map( - $this->getJsonAsArray(<<getJsonAsArray('{ "integer": null, "bool": null, "array": null -} -JSON), +}'), Initialized::class ); @@ -553,7 +550,7 @@ public function mapNullToDefaultValueUsingAnnotation(): void /** * @return string[][] */ - public static function mapPlainArrayJsonDataProvider(): array + public static function mapPlainArrayJsonDataProvider() { return [ 'mapPlainArray' => [ @@ -569,14 +566,14 @@ public static function mapPlainArrayJsonDataProvider(): array * * @test */ - public function mapPlainArray(string $jsonString): void + public function mapPlainArray($jsonString) { $result = $this->getJsonMapper() ->map( $this->getJsonAsArray($jsonString) ); - self::assertIsArray($result); + self::assertInternalType('array', $result); self::assertCount(26, $result); $result = $this->getJsonMapper() @@ -584,14 +581,14 @@ public function mapPlainArray(string $jsonString): void $this->getJsonAsObject($jsonString) ); - self::assertIsArray($result); + self::assertInternalType('array', $result); self::assertCount(26, $result); } /** * @return string[][] */ - public static function mapPlainArrayKeyValueJsonDataProvider(): array + public static function mapPlainArrayKeyValueJsonDataProvider() { return [ 'mapPlainArrayKeyValue' => [ @@ -607,14 +604,14 @@ public static function mapPlainArrayKeyValueJsonDataProvider(): array * * @test */ - public function mapPlainArrayKeyValue(string $jsonString): void + public function mapPlainArrayKeyValue($jsonString) { $result = $this->getJsonMapper() ->map( $this->getJsonAsArray($jsonString) ); - self::assertIsArray($result); + self::assertInternalType('array', $result); self::assertCount(26, $result); self::assertArrayHasKey('A', $result); self::assertSame(1, $result['A']); @@ -626,11 +623,11 @@ public function mapPlainArrayKeyValue(string $jsonString): void $this->getJsonAsObject($jsonString) ); - self::assertIsObject($result); + self::assertInternalType('object', $result); self::assertInstanceOf(stdClass::class, $result); - self::assertObjectHasProperty('A', $result); + self::assertObjectHasAttribute('A', $result); self::assertSame(1, $result->A); - self::assertObjectHasProperty('Z', $result); + self::assertObjectHasAttribute('Z', $result); self::assertSame(26, $result->Z); // Map plain array with key <=> value pair to a custom class @@ -640,11 +637,11 @@ public function mapPlainArrayKeyValue(string $jsonString): void MapPlainArrayKeyValueClass::class ); - self::assertIsObject($result); + self::assertInternalType('object', $result); self::assertInstanceOf(MapPlainArrayKeyValueClass::class, $result); - self::assertObjectHasProperty('a', $result); + self::assertObjectHasAttribute('a', $result); self::assertSame(1, $result->a); - self::assertObjectHasProperty('z', $result); + self::assertObjectHasAttribute('z', $result); self::assertSame(26, $result->z); } @@ -653,7 +650,7 @@ public function mapPlainArrayKeyValue(string $jsonString): void * * @test */ - public function variadicSetter(): void + public function variadicSetter() { $result = $this->getJsonMapper() ->map( @@ -681,7 +678,7 @@ public function variadicSetter(): void * * @test */ - public function plainArrayClass(): void + public function plainArrayClass() { $result = $this->getJsonMapper() ->map( @@ -709,7 +706,7 @@ public function plainArrayClass(): void * * @test */ - public function mappingBaseElementUsingClassMap(): void + public function mappingBaseElementUsingClassMap() { $result = $this->getJsonMapper([ SourceItem::class => TargetItem::class, @@ -733,7 +730,7 @@ public function mappingBaseElementUsingClassMap(): void * * @test */ - public function mappingCollectionElementsUsingClassMap(): void + public function mappingCollectionElementsUsingClassMap() { $result = $this->getJsonMapper([ SourceItem::class => TargetItem::class, diff --git a/test/Provider/DataProvider.php b/test/Provider/DataProvider.php index bc287c4..0fd9882 100644 --- a/test/Provider/DataProvider.php +++ b/test/Provider/DataProvider.php @@ -7,8 +7,6 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test\Provider; /** @@ -16,6 +14,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class DataProvider @@ -23,7 +22,7 @@ class DataProvider /** * @return string */ - public static function mapArrayJson(): string + public static function mapArrayJson() { return (string) file_get_contents(__DIR__ . '/_files/MapArray.json'); } @@ -31,7 +30,7 @@ public static function mapArrayJson(): string /** * @return string */ - public static function mapCollectionJson(): string + public static function mapCollectionJson() { return (string) file_get_contents(__DIR__ . '/_files/MapCollection.json'); } @@ -39,7 +38,7 @@ public static function mapCollectionJson(): string /** * @return string */ - public static function mapCustomTypeJson(): string + public static function mapCustomTypeJson() { return (string) file_get_contents(__DIR__ . '/_files/MapCustomType.json'); } @@ -47,7 +46,7 @@ public static function mapCustomTypeJson(): string /** * @return string */ - public static function mapSimpleArrayJson(): string + public static function mapSimpleArrayJson() { return (string) file_get_contents(__DIR__ . '/_files/MapSimpleArray.json'); } @@ -55,7 +54,7 @@ public static function mapSimpleArrayJson(): string /** * @return string */ - public static function mapSimpleCollectionJson(): string + public static function mapSimpleCollectionJson() { return (string) file_get_contents(__DIR__ . '/_files/MapSimpleCollection.json'); } @@ -63,7 +62,7 @@ public static function mapSimpleCollectionJson(): string /** * @return string */ - public static function mapSimpleTypesJson(): string + public static function mapSimpleTypesJson() { return (string) file_get_contents(__DIR__ . '/_files/MapSimpleTypes.json'); } @@ -71,7 +70,7 @@ public static function mapSimpleTypesJson(): string /** * @return string */ - public static function mapCustomClassNameJson(): string + public static function mapCustomClassNameJson() { return (string) file_get_contents(__DIR__ . '/_files/MapCustomClassName.json'); } @@ -79,7 +78,7 @@ public static function mapCustomClassNameJson(): string /** * @return string */ - public static function mapPlainArrayJson(): string + public static function mapPlainArrayJson() { return (string) file_get_contents(__DIR__ . '/_files/MapPlainArray.json'); } @@ -87,7 +86,7 @@ public static function mapPlainArrayJson(): string /** * @return string */ - public static function mapPlainArrayKeyValueJson(): string + public static function mapPlainArrayKeyValueJson() { return (string) file_get_contents(__DIR__ . '/_files/MapPlainArrayKeyValue.json'); } diff --git a/test/TestCase.php b/test/TestCase.php index d47fcad..4c25e07 100644 --- a/test/TestCase.php +++ b/test/TestCase.php @@ -7,12 +7,9 @@ * LICENSE file that was distributed with this source code. */ -declare(strict_types=1); - namespace MagicSunday\Test; use Closure; -use JsonException; use MagicSunday\JsonMapper; use MagicSunday\JsonMapper\Converter\CamelCasePropertyNameConverter; use Symfony\Component\PropertyAccess\PropertyAccess; @@ -25,6 +22,7 @@ * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT + * * @link https://github.com/magicsunday/jsonmapper/ */ class TestCase extends \PHPUnit\Framework\TestCase @@ -36,7 +34,7 @@ class TestCase extends \PHPUnit\Framework\TestCase * * @return JsonMapper */ - protected function getJsonMapper(array $classMap = []): JsonMapper + protected function getJsonMapper(array $classMap = []) { $listExtractors = [new ReflectionExtractor()]; $typeExtractors = [new PhpDocExtractor()]; @@ -57,15 +55,9 @@ protected function getJsonMapper(array $classMap = []): JsonMapper * * @return mixed[]|null */ - protected function getJsonAsArray(string $jsonString) + protected function getJsonAsArray($jsonString) { - try { - return json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR); - } catch (JsonException $exception) { - $this->addWarning('JSON: ' . $exception->getMessage() . "\n\n" . $exception->getTraceAsString()); - - return null; - } + return json_decode($jsonString, true); } /** @@ -75,14 +67,8 @@ protected function getJsonAsArray(string $jsonString) * * @return object|null */ - protected function getJsonAsObject(string $jsonString) + protected function getJsonAsObject($jsonString) { - try { - return json_decode($jsonString, false, 512, JSON_THROW_ON_ERROR); - } catch (JsonException $exception) { - $this->addWarning('JSON: ' . $exception->getMessage() . "\n\n" . $exception->getTraceAsString()); - - return null; - } + return json_decode($jsonString, false); } }