Skip to content

Commit

Permalink
Task/crm 63243 php 8.1 (#8)
Browse files Browse the repository at this point in the history
* [CRM-63243] Adjust code for php ^8.1 - added changelog

* [CRM-63243] Adjust code for php ^8.1 - updated packages + fixed code

* [CRM-63243] Adjust code for php ^8.1 - Added cs-fixer

* [CRM-63243] Adjust code for php ^8.1 - Changed php version in travis

* [CRM-63243] Adjust code for php ^8.1 - Added rector config

* [CRM-63243] Adjust code for php ^8.1 - Added phpstan config

* [CRM-63243] Adjust code for php ^8.1

* [CRM-63243] Adjust code for php ^8.1 - Updated phpunit package + refactor tests

---------

Co-authored-by: Dawid Jaruniak <[email protected]>
  • Loading branch information
Grand-xy and dawid-jaruniak-rst authored Jun 14, 2024
1 parent 2100972 commit 9f96ebe
Show file tree
Hide file tree
Showing 30 changed files with 331 additions and 282 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
composer.lock
vendor
vendor
.idea

.phpunit.result.cache
.phpunit.cache

.phpstan
.php-cs-fixer.cache
14 changes: 14 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

$finder = Symfony\Component\Finder\Finder::create()
->in(__DIR__.'/src')
->in(__DIR__.'/test')
->exclude('vendor');

$config = new PhpCsFixer\Config();

return $config->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
])
->setFinder($finder);
14 changes: 7 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ env:

matrix:
include:
- php: 7.2
- php: 8.1
env:
- DEPS=lowest
- php: 7.2
- php: 8.1
env:
- DEPS=latest
- php: 7.3
- php: 8.2
env:
- DEPS=lowest
- php: 7.3
- php: 8.2
env:
- DEPS=latest
- php: 7.4
- php: 8.3
env:
- DEPS=lowest
- php: 7.4
- php: 8.3
env:
- DEPS=latest

Expand All @@ -49,4 +49,4 @@ after_script:
- if [[ $TEST_COVERAGE == 'true' ]]; then vendor/bin/php-coveralls -v ; fi

notifications:
email: false
email: false
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Changelog

## 2.0.0
* [TASK][CRM-63243](https://jira.trans.eu/browse/CRM-63243) Adjust code for php ^8.1
18 changes: 14 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
{
"name": "rstgroup/object-builder",
"description": "Dynamic build of object",
"version": "2.0.0",
"license": "MIT",
"keywords": [],
"authors": [
{
"name": "Team ABW",
"email": "[email protected]"
}
],
"require": {
"php": "^7.2",
"phpstan/phpdoc-parser": "^0.3.0 || ^0.4.0 || ^0.5.0",
"roave/better-reflection": "^3.0 || ^4.0"
"php": "^8.1",
"phpstan/phpdoc-parser": "^1.29.0",
"roave/better-reflection": "~6.25.0"
},
"require-dev": {
"phpunit/phpunit": "^7.2"
"friendsofphp/php-cs-fixer": "^3.58",
"phpstan/phpstan": "^1.11",
"phpunit/phpunit": "^10.0.0",
"rector/rector": "^1.1"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: 6
tmpDir: .phpstan
paths:
- src
- test
43 changes: 20 additions & 23 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
<phpunit bootstrap="vendor/autoload.php"
backupStaticAttributes="false"
<?xml version="1.0"?>
<phpunit
bootstrap="vendor/autoload.php"
backupGlobals="false"
colors="true"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
verbose="true">

<testsuites>
<testsuite name="unit">
<directory>test/unit/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<directory suffix=".php">test</directory>
</exclude>
</whitelist>
</filter>

</phpunit>
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="unit">
<directory>test/unit/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory suffix=".php">test</directory>
</exclude>
</source>
</phpunit>
32 changes: 32 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
__DIR__ . '/test',
]);

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
]);

$rectorConfig->import(SetList::CODE_QUALITY);
$rectorConfig->import(SetList::TYPE_DECLARATION);
$rectorConfig->import(SetList::CODING_STYLE);

$rectorConfig->sets([
PHPUnitSetList::PHPUNIT_70,
PHPUnitSetList::PHPUNIT_80,
PHPUnitSetList::PHPUNIT_90,
PHPUnitSetList::PHPUNIT_100,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
]);

};
9 changes: 8 additions & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace RstGroup\ObjectBuilder;

interface Builder
{
/**
* @param string $class
* @param mixed[] $data
* @return object
*/
public function build(string $class, array $data): object;
}
5 changes: 4 additions & 1 deletion src/Builder/ParameterNameStrategy.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace RstGroup\ObjectBuilder\Builder;

interface ParameterNameStrategy
{
public function isFulfilled(string $parameterName): bool;

public function getName(string $parameterName): string;
}
4 changes: 3 additions & 1 deletion src/Builder/ParameterNameStrategy/Simple.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace RstGroup\ObjectBuilder\Builder\ParameterNameStrategy;

Expand Down
4 changes: 3 additions & 1 deletion src/Builder/ParameterNameStrategy/SnakeCase.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace RstGroup\ObjectBuilder\Builder\ParameterNameStrategy;

Expand Down
46 changes: 25 additions & 21 deletions src/Builder/Reflection.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace RstGroup\ObjectBuilder\Builder;

use Iterator;
use PhpParser\Node\Stmt;
use PHPStan\PhpDocParser\Lexer\Lexer;
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionParameter;
use Roave\BetterReflection\BetterReflection;
use RstGroup\ObjectBuilder\Builder;
Expand All @@ -18,34 +23,30 @@

final class Reflection implements Builder
{
private $parameterNameStrategy;

public function __construct(ParameterNameStrategy $parameterNameStrategy)
public function __construct(private readonly ParameterNameStrategy $parameterNameStrategy)
{
$this->parameterNameStrategy = $parameterNameStrategy;
}

/**
* @param mixed[] $data
* @throws BuilderException
*/
public function build(string $class, array $data): object
{
try {
$classReflection = new ReflectionClass($class);

/** @var ReflectionMethod $constructorMethod */
/** @var ReflectionMethod|null $constructor */
$constructor = $classReflection->getConstructor();

$parameters = iterator_to_array($this->collect($constructor, $data));

return new $class(...$parameters);
} catch (Throwable $exception) {
throw new BuilderException('Cant build object', 0, $exception);
} catch (Throwable $throwable) {
throw new BuilderException('Cant build object', 0, $throwable);
}
}

private function collect(ReflectionMethod $constructor, array $data): iterable
private function collect(ReflectionMethod $constructor, array $data): Iterator
{
foreach ($constructor->getParameters() as $parameter) {
$name = $parameter->getName();
Expand Down Expand Up @@ -84,17 +85,19 @@ private function parameterDataIsInData(string $parameterName, array $data): bool
}

/**
* @param mixed $data
* @return mixed
* @throws BuilderException
* @throws ReflectionException
*/
private function buildParameter(ReflectionParameter $parameter, $data, ReflectionMethod $constructor)
private function buildParameter(ReflectionParameter $parameter, mixed $data, ReflectionMethod $constructor): mixed
{
$class = $parameter->getClass();
$parameterType = $parameter->getType();

if ($parameterType instanceof ReflectionNamedType && !$parameterType->isBuiltin()) {
$parameterClass = new ReflectionClass($parameterType->getName());

if (null !== $class) {
$name = $class->getName();
$name = $parameterClass->getName();
/** @var ReflectionMethod $constructorMethod */
$constructorMethod = $class->getConstructor();
$constructorMethod = $parameterClass->getConstructor();
$parameters = [];

if (null !== $constructorMethod) {
Expand All @@ -104,7 +107,7 @@ private function buildParameter(ReflectionParameter $parameter, $data, Reflectio
return new $name(...$parameters);
}

if ($parameter->isArray()) {
if ($parameterType instanceof ReflectionNamedType && $parameterType->getName() === 'array') {
$parser = new PhpDocParser(new TypeParser(), new ConstExprParser());
$node = $parser->parse(new TokenIterator((new Lexer())->tokenize($constructor->getDocComment())));
foreach ($node->getParamTagValues() as $node) {
Expand All @@ -113,6 +116,7 @@ private function buildParameter(ReflectionParameter $parameter, $data, Reflectio
if ($this->isScalar($typeName)) {
continue;
}

$list = [];

$parser = (new BetterReflection())->phpParser();
Expand All @@ -122,7 +126,7 @@ private function buildParameter(ReflectionParameter $parameter, $data, Reflectio
$uses = $this->getUseStmts($namespace);
$namespaces = $this->getUsesNamespaces($uses);

foreach($data as $objectConstructorData) {
foreach ($data as $objectConstructorData) {
$list[] = $this->build(
$this->getFullClassName($typeName, $namespaces, $constructor->getDeclaringClass()),
$objectConstructorData
Expand Down Expand Up @@ -170,7 +174,7 @@ private function getUseStmts(Stmt\Namespace_ $node): array
$uses = [];
foreach ($node->stmts as $node) {
if ($node instanceof Stmt\Use_) {
$uses[]= $node;
$uses[] = $node;
}
}

Expand All @@ -197,7 +201,7 @@ private function getFullClassName(string $name, array $namespaces, ReflectionCla
return $name;
}

if (0 === count($namespaces)) {
if ([] === $namespaces) {
return $class->getNamespaceName() . '\\' . $name;
}

Expand Down
5 changes: 3 additions & 2 deletions src/BuilderException.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace RstGroup\ObjectBuilder;

use Exception;

final class BuilderException extends Exception
{

}
Loading

0 comments on commit 9f96ebe

Please sign in to comment.