Skip to content

Commit

Permalink
PHP73 adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
magicsunday committed Aug 2, 2024
1 parent 75c54d9 commit db4168c
Show file tree
Hide file tree
Showing 31 changed files with 162 additions and 122 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +23,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2, php-cs-fixer
tools: composer:v2

- name: Validate composer.json and composer.lock
run: composer validate
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ composer.lock

# Cache
.php-cs-fixer.cache
.php_cs.cache
.phplint.cache
.phpunit.result.cache
.phpunit.cache
Expand Down
16 changes: 2 additions & 14 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
->setRiskyAllowed(true)
->setRules([
'@PSR12' => true,
'@PER-CS2.0' => true,
'@Symfony' => true,

// Additional custom rules
Expand All @@ -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' => [
Expand All @@ -79,7 +68,6 @@
],
'function_declaration' => [
'closure_function_spacing' => 'one',
'closure_fn_spacing' => 'one',
],
'binary_operator_spaces' => [
'operators' => [
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"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",
Expand Down
9 changes: 4 additions & 5 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
colors="true"
columns="max"
stderr="true"
bootstrap="./.build/vendor/autoload.php"
cacheDirectory=".phpunit.cache"
>
<testsuites>
<testsuite name="Integration Tests">
<directory>./test</directory>
</testsuite>
</testsuites>
<source>
<coverage>
<include>
<directory>./src</directory>
<directory suffix=".php">./src</directory>
</include>
</source>
</coverage>
</phpunit>
8 changes: 1 addition & 7 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
44 changes: 22 additions & 22 deletions src/JsonMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@

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;
Expand All @@ -27,15 +30,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 <[email protected]>
* @license https://opensource.org/licenses/MIT
*
* @link https://github.com/magicsunday/jsonmapper/
*
* @template TEntity
Expand All @@ -46,41 +46,41 @@ 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
* setter methods accept abstract classes or interfaces.
*
* @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.
Expand Down Expand Up @@ -308,7 +308,7 @@ private function getReflectionProperty(string $className, string $propertyName):
{
try {
return new ReflectionProperty($className, $propertyName);
} catch (ReflectionException) {
} catch (ReflectionException $exception) {
return null;
}
}
Expand Down Expand Up @@ -419,15 +419,15 @@ private function hasClassAnnotation(string $className, string $annotationName):
*
* @return mixed|null
*/
private function getDefaultValue(string $className, string $propertyName): mixed
private function getDefaultValue(string $className, string $propertyName)
{
$reflectionProperty = $this->getReflectionProperty($className, $propertyName);

if (!($reflectionProperty instanceof ReflectionProperty)) {
return null;
}

return $reflectionProperty->getDefaultValue();
return $reflectionProperty->getDeclaringClass()->getDefaultProperties()[$propertyName] ?? null;
}

/**
Expand All @@ -437,7 +437,7 @@ private function getDefaultValue(string $className, string $propertyName): mixed
*
* @return bool
*/
private function isNumericIndexArray(mixed $json): bool
private function isNumericIndexArray($json): bool
{
foreach ($json as $propertyName => $propertyValue) {
if (is_int($propertyName)) {
Expand All @@ -455,7 +455,7 @@ private function isNumericIndexArray(mixed $json): bool
*
* @return bool
*/
private function isIterableWithArraysOrObjects(mixed $json): bool
private function isIterableWithArraysOrObjects($json): bool
{
// Return false if JSON is not an array or object (is_iterable won't work here)
if (!is_array($json) && !is_object($json)) {
Expand Down Expand Up @@ -510,7 +510,7 @@ private function assertClassesExists(string $className, ?string $collectionClass
* @param string $name
* @param mixed $value
*/
private function setProperty(object $entity, string $name, mixed $value): void
private function setProperty(object $entity, string $name, $value): void
{
// Handle variadic setters
if (is_array($value)) {
Expand Down Expand Up @@ -570,7 +570,7 @@ private function getType(string $className, string $propertyName): Type
*
* @throws DomainException
*/
private function getValue(mixed $json, Type $type): mixed
private function getValue($json, Type $type)
{
if ((is_array($json) || is_object($json)) && $type->isCollection()) {
$collectionType = $this->getCollectionValueType($type);
Expand Down Expand Up @@ -652,7 +652,7 @@ private function getClassNameFromType(Type $type): string
*
* @throws DomainException
*/
private function getMappedClassName(string $className, mixed $json): string
private function getMappedClassName(string $className, $json): string
{
if (array_key_exists($className, $this->classMap)) {
$classNameOrClosure = $this->classMap[$className];
Expand Down Expand Up @@ -680,7 +680,7 @@ private function getMappedClassName(string $className, mixed $json): string
*
* @throws DomainException
*/
private function getClassName(mixed $json, Type $type): string
private function getClassName($json, Type $type): string
{
return $this->getMappedClassName(
$this->getClassNameFromType($type),
Expand All @@ -698,7 +698,7 @@ private function getClassName(mixed $json, Type $type): string
*
* @throws DomainException
*/
private function asCollection(mixed $json, Type $type): ?array
private function asCollection($json, Type $type): ?array
{
if ($json === null) {
return null;
Expand All @@ -723,7 +723,7 @@ private function asCollection(mixed $json, Type $type): ?array
*
* @throws DomainException
*/
private function asObject(mixed $json, Type $type): mixed
private function asObject($json, Type $type)
{
/** @var class-string<TEntity> $className */
$className = $this->getClassName($json, $type);
Expand Down Expand Up @@ -759,7 +759,7 @@ private function isCustomType(string $typeClassName): bool
*
* @return mixed
*/
private function callCustomClosure(mixed $json, string $typeClassName): mixed
private function callCustomClosure($json, string $typeClassName)
{
$callback = $this->types[$typeClassName];

Expand Down
3 changes: 2 additions & 1 deletion src/JsonMapper/Converter/CamelCasePropertyNameConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
*
* @author Rico Sonntag <[email protected]>
* @license https://opensource.org/licenses/MIT
*
* @link https://github.com/magicsunday/jsonmapper/
*/
class CamelCasePropertyNameConverter implements PropertyNameConverterInterface
{
/**
* @var Inflector
*/
private Inflector $inflector;
private $inflector;

/**
* CamelCasePropertyNameConverter constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*
* @author Rico Sonntag <[email protected]>
* @license https://opensource.org/licenses/MIT
*
* @link https://github.com/magicsunday/jsonmapper/
*/
interface PropertyNameConverterInterface
Expand Down
4 changes: 3 additions & 1 deletion test/Annotation/ReplacePropertyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@
*
* @author Rico Sonntag <[email protected]>
* @license https://opensource.org/licenses/MIT
*
* @link https://github.com/magicsunday/jsonmapper/
*/
class ReplacePropertyTest extends TestCase
{
/**
* Tests replacing a property.
*
* @test
*/
#[Test]
public function replaceProperty(): void
{
$result = $this->getJsonMapper()
Expand Down
3 changes: 2 additions & 1 deletion test/Classes/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*
* @author Rico Sonntag <[email protected]>
* @license https://opensource.org/licenses/MIT
*
* @link https://github.com/magicsunday/jsonmapper/
*/
class Base
Expand Down Expand Up @@ -61,7 +62,7 @@ class Base
/**
* @var string
*/
private string $privateProperty = '';
private $privateProperty = '';

/**
* @return string
Expand Down
Loading

0 comments on commit db4168c

Please sign in to comment.