From 68effe62ca59b9e5b1286a534650306b8a4d4293 Mon Sep 17 00:00:00 2001 From: Rico Sonntag Date: Sat, 2 Mar 2024 09:16:13 +0100 Subject: [PATCH] Apply php-cs-fixer rules --- .github/workflows/phpcs.yml | 26 ------ .github/workflows/phpstan.yml | 26 ------ .github/workflows/phpunit.yml | 28 ------ .php-cs-fixer.dist.php | 7 ++ composer.json | 14 +-- rector.php | 6 +- src/JsonMapper.php | 89 +++++++++++-------- .../ReplaceNullWithDefaultValue.php | 1 + src/JsonMapper/Annotation/ReplaceProperty.php | 1 + .../CamelCasePropertyNameConverter.php | 2 +- .../PropertyNameConverterInterface.php | 2 +- test/Annotation/ReplacePropertyTest.php | 4 +- test/Classes/Simple.php | 4 +- .../CamelCasePropertyNameConverterTest.php | 2 +- test/JsonMapperTest.php | 7 +- test/TestCase.php | 12 +-- 16 files changed, 88 insertions(+), 143 deletions(-) delete mode 100644 .github/workflows/phpcs.yml delete mode 100644 .github/workflows/phpstan.yml delete mode 100644 .github/workflows/phpunit.yml diff --git a/.github/workflows/phpcs.yml b/.github/workflows/phpcs.yml deleted file mode 100644 index e3113af..0000000 --- a/.github/workflows/phpcs.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: PHP_CodeSniffer - -on: [push, pull_request] - -jobs: - run: - runs-on: ${{ matrix.operating-system }} - - strategy: - matrix: - operating-system: [ubuntu-22.04] - php-version: ['7.4', '8.0', '8.1', '8.2'] - - name: Testing PHP ${{ matrix.php-version }} on ${{ matrix.operating-system }} - - steps: - - uses: shivammathur/setup-php@master - with: - php-version: ${{ matrix.php-version }} - extensions: json,intl,mbstring - - - uses: actions/checkout@v2 - - - run: composer validate - - run: composer install --no-progress - - run: vendor/bin/phpcs src/ --standard=PSR12 diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml deleted file mode 100644 index c8b6fbd..0000000 --- a/.github/workflows/phpstan.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: PHPStan - -on: [push, pull_request] - -jobs: - run: - runs-on: ${{ matrix.operating-system }} - - strategy: - matrix: - operating-system: [ubuntu-22.04] - php-version: ['7.4', '8.0', '8.1', '8.2'] - - name: Testing PHP ${{ matrix.php-version }} on ${{ matrix.operating-system }} - - steps: - - uses: shivammathur/setup-php@master - with: - php-version: ${{ matrix.php-version }} - extensions: json,intl,mbstring - - - uses: actions/checkout@v2 - - - run: composer validate - - run: composer install --no-progress - - run: vendor/bin/phpstan analyse -c phpstan.neon diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml deleted file mode 100644 index 9a8fe5b..0000000 --- a/.github/workflows/phpunit.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: PHPUnit - -on: [push, pull_request] - -jobs: - run: - runs-on: ${{ matrix.operating-system }} - - strategy: - matrix: - operating-system: [ubuntu-22.04] - php-version: ['7.4', '8.0', '8.1', '8.2'] - - name: Testing PHP ${{ matrix.php-version }} on ${{ matrix.operating-system }} - - steps: - - uses: shivammathur/setup-php@master - with: - php-version: ${{ matrix.php-version }} - extensions: json,intl,mbstring - coverage: pcov - - - uses: actions/checkout@v2 - - - run: composer validate - - run: composer install --no-progress - - run: composer require phpdocumentor/reflection-docblock - - run: vendor/bin/phpunit --coverage-clover=tests/coverage.xml diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index 6bb7206..4a50534 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -93,6 +93,13 @@ 'less_and_greater' => false, 'always_move_variable' => false, ], + 'blank_line_before_statement' => [ + 'statements' => [ + 'return', + 'if', + 'throw', + ], + ], ]) ->setFinder( PhpCsFixer\Finder::create() diff --git a/composer.json b/composer.json index 0220ce0..8d20697 100644 --- a/composer.json +++ b/composer.json @@ -14,16 +14,16 @@ "issues": "https://github.com/magicsunday/jsonmapper/issues" }, "require": { - "php": "^7.4 || ^8.0", + "php": ">=7.4.0 <8.1.0", "ext-json": "*", - "symfony/property-info": "^5.2", - "symfony/property-access": "^5.2", - "doctrine/inflector": "^2.0", - "doctrine/annotations": "^1.13" + "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" }, "require-dev": { - "phpdocumentor/reflection-docblock": "^5.3", - "friendsofphp/php-cs-fixer": "^3.1", + "phpdocumentor/reflection-docblock": "^4.0 || ^5.0", + "friendsofphp/php-cs-fixer": "^3.50", "overtrue/phplint": "^3.4 || ^9.0", "phpunit/phpunit": "^9.0 || ^10.0 || ^11.0", "phpstan/phpstan": "^1.10", diff --git a/rector.php b/rector.php index 6ffca37..69ab608 100644 --- a/rector.php +++ b/rector.php @@ -25,11 +25,11 @@ return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ __DIR__ . '/src', - __DIR__ . '/test', ]); $rectorConfig->skip([ - __DIR__ . '/vendor', + __DIR__ . '/.build', + __DIR__ . '/test', ]); $rectorConfig->phpstanConfig('phpstan.neon'); @@ -44,7 +44,7 @@ SetList::CODING_STYLE, SetList::CODE_QUALITY, SetList::DEAD_CODE, - LevelSetList::UP_TO_PHP_81, + LevelSetList::UP_TO_PHP_74, ]); // Skip some rules diff --git a/src/JsonMapper.php b/src/JsonMapper.php index d2b9dd3..e8d6e52 100644 --- a/src/JsonMapper.php +++ b/src/JsonMapper.php @@ -32,7 +32,7 @@ use function is_array; /** - * JsonMapper + * JsonMapper. * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT @@ -56,7 +56,7 @@ class JsonMapper /** * The property name converter instance. * - * @var null|PropertyNameConverterInterface + * @var PropertyNameConverterInterface|null */ protected ?PropertyNameConverterInterface $nameConverter; @@ -87,13 +87,13 @@ class JsonMapper * * @param PropertyInfoExtractorInterface $extractor * @param PropertyAccessorInterface $accessor - * @param null|PropertyNameConverterInterface $nameConverter A name converter instance + * @param PropertyNameConverterInterface|null $nameConverter A name converter instance * @param string[]|Closure[] $classMap A class map to override the class names */ public function __construct( PropertyInfoExtractorInterface $extractor, PropertyAccessorInterface $accessor, - PropertyNameConverterInterface $nameConverter = null, + ?PropertyNameConverterInterface $nameConverter = null, array $classMap = [] ) { $this->extractor = $extractor; @@ -114,6 +114,7 @@ public function __construct( public function addType(string $type, Closure $closure): JsonMapper { $this->types[$type] = $closure; + return $this; } @@ -130,6 +131,7 @@ public function addType(string $type, Closure $closure): JsonMapper public function addCustomClassMapEntry(string $className, Closure $closure): JsonMapper { $this->classMap[$className] = $closure; + return $this; } @@ -137,20 +139,20 @@ public function addCustomClassMapEntry(string $className, Closure $closure): Jso * Maps the JSON to the specified class entity. * * @param mixed $json The JSON to map - * @param null|class-string $className The class name of the initial element - * @param null|class-string $collectionClassName The class name of a collection used to assign + * @param class-string|null $className The class name of the initial element + * @param class-string|null $collectionClassName The class name of a collection used to assign * the initial elements * * @phpstan-return ($collectionClassName is class-string * ? TEntityCollection * : ($className is class-string ? TEntity : null|mixed)) * - * @return null|mixed|TEntityCollection|TEntity + * @return mixed|TEntityCollection|TEntity|null * * @throws DomainException * @throws InvalidArgumentException */ - public function map($json, string $className = null, string $collectionClassName = null) + public function map($json, ?string $className = null, ?string $collectionClassName = null) { // Return plain JSON if no mapping classes are provided if ($className === null) { @@ -211,7 +213,7 @@ public function map($json, string $className = null, string $collectionClassName } } - if ($this->nameConverter) { + if ($this->nameConverter instanceof PropertyNameConverterInterface) { $propertyName = $this->nameConverter->convert($propertyName); } @@ -248,7 +250,7 @@ public function map($json, string $className = null, string $collectionClassName * * @return T */ - private function makeInstance(string $className, ...$constructorArguments) + private function makeInstance(string $className, ?array ...$constructorArguments) { /** @var T $instance */ $instance = new $className(...$constructorArguments); @@ -294,7 +296,7 @@ private function isReplacePropertyAnnotation(string $className): bool * @param class-string $className The class name of the initial element * @param string $propertyName The name of the property * - * @return null|ReflectionProperty + * @return ReflectionProperty|null */ private function getReflectionProperty(string $className, string $propertyName): ?ReflectionProperty { @@ -310,7 +312,7 @@ private function getReflectionProperty(string $className, string $propertyName): * * @param class-string $className The class name of the initial element * - * @return null|ReflectionClass + * @return ReflectionClass|null */ private function getReflectionClass(string $className): ?ReflectionClass { @@ -332,15 +334,13 @@ private function getReflectionClass(string $className): ?ReflectionClass private function extractPropertyAnnotations(string $className, string $propertyName): array { $reflectionProperty = $this->getReflectionProperty($className, $propertyName); - $annotations = []; - if ($reflectionProperty) { - /** @var Annotation[] $annotations */ - $annotations = (new AnnotationReader()) + if ($reflectionProperty instanceof ReflectionProperty) { + return (new AnnotationReader()) ->getPropertyAnnotations($reflectionProperty); } - return $annotations; + return []; } /** @@ -350,22 +350,20 @@ private function extractPropertyAnnotations(string $className, string $propertyN * * @return Annotation[] */ - private function extractClassAnnotations($className): array + private function extractClassAnnotations(string $className): array { $reflectionClass = $this->getReflectionClass($className); - $annotations = []; - if ($reflectionClass !== null) { - /** @var Annotation[] $annotations */ - $annotations = (new AnnotationReader()) + if ($reflectionClass instanceof ReflectionClass) { + return (new AnnotationReader()) ->getClassAnnotations($reflectionClass); } - return $annotations; + return []; } /** - * Returns TRUE if the property has the given annotation + * Returns TRUE if the property has the given annotation. * * @param class-string $className The class name of the initial element * @param string $propertyName The name of the property @@ -413,13 +411,13 @@ private function hasClassAnnotation(string $className, string $annotationName): * @param class-string $className The class name of the initial element * @param string $propertyName The name of the property * - * @return null|mixed + * @return mixed|null */ private function getDefaultValue(string $className, string $propertyName) { $reflectionProperty = $this->getReflectionProperty($className, $propertyName); - if (!$reflectionProperty) { + if (!($reflectionProperty instanceof ReflectionProperty)) { return null; } @@ -459,9 +457,15 @@ private function isNumericIndexArray($json): bool private function isIterableWithArraysOrObjects($json): bool { foreach ($json as $propertyValue) { - if (!is_array($propertyValue) && !is_object($propertyValue)) { - return false; + if (is_array($propertyValue)) { + continue; } + + if (is_object($propertyValue)) { + continue; + } + + return false; } return true; @@ -471,20 +475,26 @@ private function isIterableWithArraysOrObjects($json): bool * Assert that the given classes exists. * * @param class-string $className The class name of the initial element - * @param null|class-string $collectionClassName The class name of a collection used to + * @param class-string|null $collectionClassName The class name of a collection used to * assign the initial elements * * @throws InvalidArgumentException */ - private function assertClassesExists(string $className, string $collectionClassName = null): void + private function assertClassesExists(string $className, ?string $collectionClassName = null): void { if (!class_exists($className)) { - throw new InvalidArgumentException("Class [$className] does not exist"); + throw new InvalidArgumentException(sprintf('Class [%s] does not exist', $className)); } - if ($collectionClassName && !class_exists($collectionClassName)) { - throw new InvalidArgumentException("Class [$collectionClassName] does not exist"); + if (!$collectionClassName) { + return; } + + if (class_exists($collectionClassName)) { + return; + } + + throw new InvalidArgumentException(sprintf('Class [%s] does not exist', $collectionClassName)); } /** @@ -504,8 +514,9 @@ private function setProperty(object $entity, string $name, $value): void $method = new ReflectionMethod($entity, $methodName); $parameters = $method->getParameters(); - if ((count($parameters) === 1) && ($parameters[0]->isVariadic())) { + if ((count($parameters) === 1) && $parameters[0]->isVariadic()) { $entity->$methodName(...$value); + return; } } @@ -545,7 +556,7 @@ private function getType(string $className, string $propertyName): Type * @param mixed $json * @param Type $type * - * @return null|mixed + * @return mixed|null * * @throws DomainException */ @@ -612,7 +623,7 @@ public function getCollectionValueType(Type $type): Type */ private function getClassNameFromType(Type $type): string { - /** @var null|class-string $className */ + /** @var class-string|null $className */ $className = $type->getClassName(); // @codeCoverageIgnoreStart @@ -620,6 +631,7 @@ private function getClassNameFromType(Type $type): string // This should never happen throw new DomainException('Type has no valid class name'); } + // @codeCoverageIgnoreEnd return $className; @@ -677,7 +689,7 @@ private function getClassName($json, Type $type): string * @param mixed $json * @param Type $type * - * @return null|mixed[] + * @return mixed[]|null * * @throws DomainException */ @@ -702,7 +714,7 @@ private function asCollection($json, Type $type): ?array * @param mixed $json * @param Type $type * - * @return null|mixed + * @return mixed|null * * @throws DomainException */ @@ -745,6 +757,7 @@ private function isCustomType(string $typeClassName): bool private function callCustomClosure($json, string $typeClassName) { $callback = $this->types[$typeClassName]; + return $callback($json); } } diff --git a/src/JsonMapper/Annotation/ReplaceNullWithDefaultValue.php b/src/JsonMapper/Annotation/ReplaceNullWithDefaultValue.php index d19304e..740c687 100644 --- a/src/JsonMapper/Annotation/ReplaceNullWithDefaultValue.php +++ b/src/JsonMapper/Annotation/ReplaceNullWithDefaultValue.php @@ -22,6 +22,7 @@ * instead of an empty array that can be expected. * * @Annotation + * * @Target({"PROPERTY"}) */ final class ReplaceNullWithDefaultValue extends Annotation diff --git a/src/JsonMapper/Annotation/ReplaceProperty.php b/src/JsonMapper/Annotation/ReplaceProperty.php index 3d9ea1f..ae60a9d 100644 --- a/src/JsonMapper/Annotation/ReplaceProperty.php +++ b/src/JsonMapper/Annotation/ReplaceProperty.php @@ -22,6 +22,7 @@ * @ReplaceProperty("foo", replaces="bar") * * @Annotation + * * @Target({"CLASS"}) */ final class ReplaceProperty extends Annotation diff --git a/src/JsonMapper/Converter/CamelCasePropertyNameConverter.php b/src/JsonMapper/Converter/CamelCasePropertyNameConverter.php index ecc8689..e57aba3 100644 --- a/src/JsonMapper/Converter/CamelCasePropertyNameConverter.php +++ b/src/JsonMapper/Converter/CamelCasePropertyNameConverter.php @@ -15,7 +15,7 @@ use Doctrine\Inflector\InflectorFactory; /** - * CamelCasePropertyNameConverter + * CamelCasePropertyNameConverter. * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT diff --git a/src/JsonMapper/Converter/PropertyNameConverterInterface.php b/src/JsonMapper/Converter/PropertyNameConverterInterface.php index fc0ac8e..80c3c28 100644 --- a/src/JsonMapper/Converter/PropertyNameConverterInterface.php +++ b/src/JsonMapper/Converter/PropertyNameConverterInterface.php @@ -12,7 +12,7 @@ namespace MagicSunday\JsonMapper\Converter; /** - * PropertyNameConverterInterface + * PropertyNameConverterInterface. * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT diff --git a/test/Annotation/ReplacePropertyTest.php b/test/Annotation/ReplacePropertyTest.php index 18bb258..c8aa085 100644 --- a/test/Annotation/ReplacePropertyTest.php +++ b/test/Annotation/ReplacePropertyTest.php @@ -15,7 +15,7 @@ use MagicSunday\Test\TestCase; /** - * Class ReplacePropertyTest + * Class ReplacePropertyTest. * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT @@ -44,7 +44,7 @@ public function replaceProperty(): void ReplacePropertyTestClass::class ); -// self::assertInstanceOf(ReplacePropertyTestClass::class, $result); + // self::assertInstanceOf(ReplacePropertyTestClass::class, $result); self::assertSame(123, $result->getType()); self::assertSame('This is my name', $result->name); self::assertSame('Default value', $result->untouchedProperty); diff --git a/test/Classes/Simple.php b/test/Classes/Simple.php index 65cf31e..944e88f 100644 --- a/test/Classes/Simple.php +++ b/test/Classes/Simple.php @@ -21,12 +21,12 @@ class Simple { /** - * PHP7.4 typed property + * PHP7.4 typed property. */ public int $id; /** - * PHP7.4 typed property + * PHP7.4 typed property. * * @var string */ diff --git a/test/Converter/CamelCasePropertyNameConverterTest.php b/test/Converter/CamelCasePropertyNameConverterTest.php index 8582cd5..579049a 100644 --- a/test/Converter/CamelCasePropertyNameConverterTest.php +++ b/test/Converter/CamelCasePropertyNameConverterTest.php @@ -15,7 +15,7 @@ use PHPUnit\Framework\TestCase; /** - * Class CamelCasePropertyNameConverterTest + * Class CamelCasePropertyNameConverterTest. * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT diff --git a/test/JsonMapperTest.php b/test/JsonMapperTest.php index 0b5d952..48ee882 100644 --- a/test/JsonMapperTest.php +++ b/test/JsonMapperTest.php @@ -31,7 +31,7 @@ use stdClass; /** - * Class JsonMapperTest + * Class JsonMapperTest. * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT @@ -58,6 +58,7 @@ public function mapArrayOrCollectionWithIntegerKeysJsonDataProvider(): array * Tests mapping an array or collection of objects. * * @dataProvider mapArrayOrCollectionWithIntegerKeysJsonDataProvider + * * @test * * @param string $jsonString @@ -357,9 +358,9 @@ public function mapEmptyObject(): void "simple": null } JSON - ), + ), Base::class - ); + ); self::assertInstanceOf(Base::class, $result); self::assertNull($result->simple); diff --git a/test/TestCase.php b/test/TestCase.php index c11e56d..d47fcad 100644 --- a/test/TestCase.php +++ b/test/TestCase.php @@ -21,7 +21,7 @@ use Symfony\Component\PropertyInfo\PropertyInfoExtractor; /** - * Class JsonMapperTest + * Class JsonMapperTest. * * @author Rico Sonntag * @license https://opensource.org/licenses/MIT @@ -38,8 +38,8 @@ class TestCase extends \PHPUnit\Framework\TestCase */ protected function getJsonMapper(array $classMap = []): JsonMapper { - $listExtractors = [ new ReflectionExtractor() ]; - $typeExtractors = [ new PhpDocExtractor() ]; + $listExtractors = [new ReflectionExtractor()]; + $typeExtractors = [new PhpDocExtractor()]; $extractor = new PropertyInfoExtractor($listExtractors, $typeExtractors); return new JsonMapper( @@ -55,7 +55,7 @@ protected function getJsonMapper(array $classMap = []): JsonMapper * * @param string $jsonString * - * @return null|mixed[] + * @return mixed[]|null */ protected function getJsonAsArray(string $jsonString) { @@ -63,6 +63,7 @@ protected function getJsonAsArray(string $jsonString) return json_decode($jsonString, true, 512, JSON_THROW_ON_ERROR); } catch (JsonException $exception) { $this->addWarning('JSON: ' . $exception->getMessage() . "\n\n" . $exception->getTraceAsString()); + return null; } } @@ -72,7 +73,7 @@ protected function getJsonAsArray(string $jsonString) * * @param string $jsonString * - * @return null|object + * @return object|null */ protected function getJsonAsObject(string $jsonString) { @@ -80,6 +81,7 @@ protected function getJsonAsObject(string $jsonString) return json_decode($jsonString, false, 512, JSON_THROW_ON_ERROR); } catch (JsonException $exception) { $this->addWarning('JSON: ' . $exception->getMessage() . "\n\n" . $exception->getTraceAsString()); + return null; } }