Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit 3a5d69f

Browse files
committed
Switch to php8 only
- Add rector - Enhance code style rules
1 parent f2f781b commit 3a5d69f

14 files changed

+159
-100
lines changed

.php-cs-fixer.dist.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
->in(['src', 'tests'])
55
;
66
$config = new PhpCsFixer\Config();
7-
$config
8-
->setFinder($finder)
7+
$config->setRules(
8+
[
9+
'@PSR12' => true,
10+
'array_syntax' => ['syntax' => 'short'],
11+
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline', 'keep_multiple_spaces_after_comma' => false],
12+
]
13+
)->setFinder($finder)
914
;
1015

1116
return $config;

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
This library provides a simple way to create a json api using [json-schema](http://json-schema.org/) to validate the request.
88
You can leverage most of the input validation tasks (variables types, length/content-constraints, lists containing just certain items, etc.)
99
to the json schema validator and work with the data right away.
10-
[![Code Coverage](https://scrutinizer-ci.com/g/usox/json-schema-api/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/usox/json-schema-api/?branch=master)
1110
## Json-schema
1211

1312
Every method needs a corresponding schema-file which describes, how the request data should like alike.

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
],
1212
"require": {
13-
"php": "^7.4 || ^8.0",
13+
"php": "^8.0",
1414
"ext-json": "*",
1515
"opis/json-schema": "^2.1",
1616
"php-http/discovery": "^1.13",
@@ -39,7 +39,8 @@
3939
"phpstan/phpstan": "^0.12.80",
4040
"phpstan/phpstan-mockery": "^0.12.12",
4141
"phpstan/phpstan-strict-rules": "^0.12.9",
42-
"phpunit/phpunit": "^9.5"
42+
"phpunit/phpunit": "^9.5",
43+
"rector/rector": "^0.11.21"
4344
},
4445
"config": {
4546
"sort-packages": true

composer.lock

Lines changed: 70 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rector.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQualityStrict\Rector\Variable\MoveVariableDeclarationNearReferenceRector;
6+
use Rector\Core\Configuration\Option;
7+
use Rector\Php74\Rector\Property\TypedPropertyRector;
8+
use Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector;
9+
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
10+
use Rector\Php80\Rector\If_\NullsafeOperatorRector;
11+
use Rector\Set\ValueObject\SetList;
12+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
13+
14+
return static function (ContainerConfigurator $containerConfigurator): void {
15+
$parameters = $containerConfigurator->parameters();
16+
17+
$parameters->set(Option::PHPSTAN_FOR_RECTOR_PATH, getcwd() . '/phpstan.neon');
18+
$parameters->set(Option::PATHS, [__DIR__ . '/src', __DIR__ . '/tests']);
19+
$parameters->set(Option::AUTO_IMPORT_NAMES, true);
20+
21+
$containerConfigurator->import(SetList::CODE_QUALITY_STRICT);
22+
$containerConfigurator->import(SetList::DEAD_CODE);
23+
$containerConfigurator->import(SetList::PHP_80);
24+
25+
$services = $containerConfigurator->services();
26+
$services->set(TypedPropertyRector::class);
27+
$services->set(ClassPropertyAssignToConstructorPromotionRector::class);
28+
$services->set(RemoveUnusedVariableInCatchRector::class);
29+
$services->set(MoveVariableDeclarationNearReferenceRector::class);
30+
};

src/Dispatch/MethodDispatcher.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Usox\JsonSchemaApi\Dispatch;
66

7+
use Usox\JsonSchemaApi\Contract\ApiMethodInterface;
78
use Opis\JsonSchema\Helper;
89
use Psr\Http\Message\ServerRequestInterface;
910
use stdClass;
@@ -19,20 +20,12 @@
1920

2021
final class MethodDispatcher implements MethodDispatcherInterface
2122
{
22-
private SchemaLoaderInterface $schemaLoader;
23-
24-
private MethodValidatorInterface $methodValidator;
25-
26-
private MethodProviderInterface $methodProvider;
27-
2823
public function __construct(
29-
SchemaLoaderInterface $schemaLoader,
30-
MethodValidatorInterface $methodValidator,
31-
MethodProviderInterface $methodProvider
32-
) {
33-
$this->schemaLoader = $schemaLoader;
34-
$this->methodValidator = $methodValidator;
35-
$this->methodProvider = $methodProvider;
24+
private SchemaLoaderInterface $schemaLoader,
25+
private MethodValidatorInterface $methodValidator,
26+
private MethodProviderInterface $methodProvider
27+
)
28+
{
3629
}
3730

3831
/**
@@ -59,7 +52,7 @@ public function dispatch(
5952
}
6053

6154
$handler = $this->methodProvider->lookup($methodName);
62-
if ($handler === null) {
55+
if (!$handler instanceof ApiMethodInterface) {
6356
throw new MethodNotFoundException(
6457
'Method not found',
6558
StatusCode::BAD_REQUEST

src/Dispatch/MethodValidator.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,8 @@
1414

1515
final class MethodValidator implements MethodValidatorInterface
1616
{
17-
private Validator $schemaValidator;
18-
19-
private ErrorFormatter $errorFormatter;
20-
21-
public function __construct(
22-
Validator $schemaValidator,
23-
ErrorFormatter $errorFormatter
24-
) {
25-
$this->schemaValidator = $schemaValidator;
26-
$this->errorFormatter = $errorFormatter;
17+
public function __construct(private Validator $schemaValidator, private ErrorFormatter $errorFormatter)
18+
{
2719
}
2820

2921
/**

src/Dispatch/RequestValidator.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Usox\JsonSchemaApi\Dispatch;
66

7+
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaInvalidException;
8+
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotFoundException;
9+
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotLoadableException;
710
use Opis\JsonSchema\Validator;
811
use Psr\Http\Message\ServerRequestInterface;
912
use stdClass;
@@ -13,22 +16,14 @@
1316

1417
final class RequestValidator implements RequestValidatorInterface
1518
{
16-
private SchemaLoaderInterface $schemaLoader;
17-
18-
private Validator $schemaValidator;
19-
20-
public function __construct(
21-
SchemaLoaderInterface $schemaLoader,
22-
Validator $schemaValidator
23-
) {
24-
$this->schemaLoader = $schemaLoader;
25-
$this->schemaValidator = $schemaValidator;
19+
public function __construct(private SchemaLoaderInterface $schemaLoader, private Validator $schemaValidator)
20+
{
2621
}
2722

2823
/**
29-
* @throws Exception\SchemaInvalidException
30-
* @throws Exception\SchemaNotFoundException
31-
* @throws Exception\SchemaNotLoadableException
24+
* @throws SchemaInvalidException
25+
* @throws SchemaNotFoundException
26+
* @throws SchemaNotLoadableException
3227
* @throws JsonInvalidException
3328
* @throws RequestMalformedException
3429
*/

src/Dispatch/RequestValidatorInterface.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Usox\JsonSchemaApi\Dispatch;
44

5+
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaInvalidException;
6+
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotFoundException;
7+
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotLoadableException;
58
use Psr\Http\Message\ServerRequestInterface;
69
use stdClass;
710
use Usox\JsonSchemaApi\Dispatch\Exception\JsonInvalidException;
@@ -10,9 +13,9 @@
1013
interface RequestValidatorInterface
1114
{
1215
/**
13-
* @throws Exception\SchemaInvalidException
14-
* @throws Exception\SchemaNotFoundException
15-
* @throws Exception\SchemaNotLoadableException
16+
* @throws SchemaInvalidException
17+
* @throws SchemaNotFoundException
18+
* @throws SchemaNotLoadableException
1619
* @throws JsonInvalidException
1720
* @throws RequestMalformedException
1821
*/

src/Dispatch/SchemaLoader.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Usox\JsonSchemaApi\Dispatch;
66

7+
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotFoundException;
8+
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotLoadableException;
79
use stdClass;
810
use Teapot\StatusCode;
911
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaInvalidException;
@@ -13,15 +15,15 @@ final class SchemaLoader implements SchemaLoaderInterface
1315
/**
1416
* Loads and returns the schema content
1517
*
16-
* @throws Exception\SchemaInvalidException
17-
* @throws Exception\SchemaNotFoundException
18-
* @throws Exception\SchemaNotLoadableException
18+
* @throws SchemaInvalidException
19+
* @throws SchemaNotFoundException
20+
* @throws SchemaNotLoadableException
1921
*/
2022
public function load(
2123
string $schemaFilePath
2224
): stdClass {
2325
if (file_exists($schemaFilePath) === false) {
24-
throw new Exception\SchemaNotFoundException(
26+
throw new SchemaNotFoundException(
2527
sprintf('Schema file `%s` not found', $schemaFilePath),
2628
StatusCode::INTERNAL_SERVER_ERROR
2729
);
@@ -30,7 +32,7 @@ public function load(
3032
$fileContent = @file_get_contents($schemaFilePath);
3133

3234
if ($fileContent === false) {
33-
throw new Exception\SchemaNotLoadableException(
35+
throw new SchemaNotLoadableException(
3436
sprintf('Schema file `%s` not loadable', $schemaFilePath),
3537
StatusCode::INTERNAL_SERVER_ERROR
3638
);

0 commit comments

Comments
 (0)