Skip to content

Commit 93095d6

Browse files
authored
Merge pull request #29 from undabot/fix/static_analysis
feat/attributes
2 parents a6e314a + 410ca75 commit 93095d6

File tree

102 files changed

+754
-959
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+754
-959
lines changed

composer.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"phpstan/extension-installer": "^1.3",
2626
"phpstan/phpstan": "^1.10",
2727
"phpstan/phpstan-beberlei-assert": "^1.1",
28-
"thecodingmachine/phpstan-strict-rules": "^1.0"
28+
"thecodingmachine/phpstan-strict-rules": "^1.0",
29+
"rector/rector": "^1.0"
2930
},
3031
"autoload": {
3132
"psr-4": {
@@ -41,6 +42,9 @@
4142
"lint": [
4243
"PHP_CS_FIXER_IGNORE_ENV=1 php-cs-fixer fix --diff --ansi --dry-run"
4344
],
45+
"lint:fix": [
46+
"php-cs-fixer fix --diff --ansi --using-cache=no"
47+
],
4448
"phpstan": [
4549
"php -d memory_limit=-1 vendor/bin/phpstan analyse -n --ansi --no-progress"
4650
],
@@ -49,6 +53,7 @@
4953
],
5054
"qc": [
5155
"@lint",
56+
"@lint-fix",
5257
"@phpstan",
5358
"@test"
5459
]

composer.lock

+60-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml.dist

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
3-
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache">
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.1/phpunit.xsd" backupGlobals="false" colors="true" bootstrap="vendor/autoload.php" cacheDirectory=".phpunit.cache"
4+
displayDetailsOnTestsThatTriggerDeprecations="true"
5+
displayDetailsOnTestsThatTriggerWarnings="true"
6+
>
47
<coverage>
58
<report>
69
<clover outputFile="tests/_reports/logs/clover.xml"/>

rector.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Doctrine\Set\DoctrineSetList;
7+
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
8+
use Rector\Php80\ValueObject\AnnotationToAttribute;
9+
use Rector\Symfony\Set\SensiolabsSetList;
10+
use Rector\Symfony\Set\SymfonySetList;
11+
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
12+
13+
return static function (RectorConfig $rectorConfig): void {
14+
// Paths for Rector to act upon
15+
$rectorConfig->paths([
16+
__DIR__ . '/src',
17+
__DIR__ . '/tests',
18+
]);
19+
20+
$rectorConfig->sets([
21+
DoctrineSetList::ANNOTATIONS_TO_ATTRIBUTES,
22+
SymfonySetList::ANNOTATIONS_TO_ATTRIBUTES,
23+
SensiolabsSetList::ANNOTATIONS_TO_ATTRIBUTES,
24+
]);
25+
$rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [
26+
new AnnotationToAttribute(UniqueEntity::class),
27+
]);
28+
};

src/Bridge/OpenApi/Model/Api.php

+6-23
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,6 @@
1212

1313
class Api implements Contract\Api
1414
{
15-
/** @var string */
16-
private $title;
17-
18-
/** @var string */
19-
private $version;
20-
21-
/** @var string */
22-
private $description;
23-
24-
/** @var null|string */
25-
private $email;
26-
2715
/** @var Endpoint[] */
2816
private $endpoints = [];
2917

@@ -34,17 +22,7 @@ class Api implements Contract\Api
3422
private $schemas = [];
3523

3624
/** @todo add support for security schemas */
37-
public function __construct(
38-
string $title,
39-
string $version,
40-
string $description,
41-
?string $email = null
42-
) {
43-
$this->title = $title;
44-
$this->version = $version;
45-
$this->description = $description;
46-
$this->email = $email;
47-
}
25+
public function __construct(private string $title, private string $version, private string $description, private ?string $email = null) {}
4826

4927
public function addEndpoint(Endpoint $endpoint): void
5028
{
@@ -102,4 +80,9 @@ public function toOpenApi(): array
10280

10381
return $api;
10482
}
83+
84+
public function getEmail(): ?string
85+
{
86+
return $this->email;
87+
}
10588
}

src/Bridge/OpenApi/Model/JsonApi/Endpoint/CreateResourceEndpoint.php

+4-17
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,18 @@
1414

1515
class CreateResourceEndpoint implements Endpoint
1616
{
17-
/** @var ReadSchema */
18-
private $readSchema;
19-
20-
/** @var CreateSchema */
21-
private $createSchema;
22-
23-
/** @var string */
24-
private $path;
25-
2617
/** @var Response[] */
27-
private $responses;
18+
private array $responses;
2819

2920
/**
3021
* @param Response[] $errorResponses
3122
*/
3223
public function __construct(
33-
ReadSchema $readSchema,
34-
CreateSchema $createSchema,
35-
string $path,
24+
private ReadSchema $readSchema,
25+
private CreateSchema $createSchema,
26+
private string $path,
3627
array $errorResponses = []
3728
) {
38-
$this->createSchema = $createSchema;
39-
$this->readSchema = $readSchema;
40-
$this->path = $path;
41-
4229
$this->responses = array_merge(
4330
[
4431
new ResourceCreatedResponse($this->readSchema),

src/Bridge/OpenApi/Model/JsonApi/Endpoint/GetResourceEndpoint.php

+8-18
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,33 @@
1515

1616
class GetResourceEndpoint implements Endpoint
1717
{
18-
/** @var ReadSchema */
19-
private $readSchema;
20-
21-
/** @var string */
22-
private $path;
23-
2418
/** @var Response[] */
25-
private $responses;
19+
private array $responses;
2620

2721
/** @var ReadSchema[] */
28-
private $includes;
29-
30-
/** @var null|mixed[] */
31-
private $fields;
22+
private array $includes;
3223

3324
/**
3425
* @param ReadSchema[] $includes
3526
* @param null|mixed[] $fields
3627
* @param mixed[] $errorResponses
3728
*/
3829
public function __construct(
39-
ReadSchema $readSchema,
40-
string $path,
30+
private ReadSchema $readSchema,
31+
private string $path,
4132
array $includes,
42-
?array $fields,
33+
private ?array $fields,
4334
array $errorResponses = []
4435
) {
4536
Assertion::allIsInstanceOf($includes, ReadSchema::class);
46-
$this->readSchema = $readSchema;
47-
$this->path = $path;
4837
$this->includes = $includes;
4938

50-
$this->responses = array_merge([
39+
/** @var Response[] $mergedResponses */
40+
$mergedResponses = array_merge([
5141
new ResourceResponse($this->readSchema, $this->includes),
5242
], $errorResponses);
5343

54-
$this->fields = $fields;
44+
$this->responses = $mergedResponses;
5545
}
5646

5747
public function getMethod(): string

src/Bridge/OpenApi/Model/JsonApi/Endpoint/ResourceCollectionEndpoint.php

+28-43
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,52 @@
44

55
namespace Undabot\SymfonyJsonApi\Bridge\OpenApi\Model\JsonApi\Endpoint;
66

7+
use Assert\Assertion;
78
use Undabot\SymfonyJsonApi\Bridge\OpenApi\Contract\Endpoint;
89
use Undabot\SymfonyJsonApi\Bridge\OpenApi\Contract\Response;
910
use Undabot\SymfonyJsonApi\Bridge\OpenApi\Contract\Schema;
1011
use Undabot\SymfonyJsonApi\Bridge\OpenApi\Model\JsonApi\Response\CollectionResponse;
12+
use Undabot\SymfonyJsonApi\Bridge\OpenApi\Model\JsonApi\Schema\Filter\Filter;
1113
use Undabot\SymfonyJsonApi\Bridge\OpenApi\Model\JsonApi\Schema\Filter\FilterSetQueryParam;
1214
use Undabot\SymfonyJsonApi\Bridge\OpenApi\Model\JsonApi\Schema\Query\IncludeQueryParam;
1315
use Undabot\SymfonyJsonApi\Bridge\OpenApi\Model\JsonApi\Schema\Resource\ReadSchema;
1416

1517
class ResourceCollectionEndpoint implements Endpoint
1618
{
17-
/** @var ReadSchema */
18-
private $schema;
19-
20-
/** @var string */
21-
private $path;
22-
2319
/** @var Response[] */
24-
private $responses;
25-
26-
/** @var mixed[] */
27-
private $filters;
28-
29-
/** @var mixed[] */
30-
private $includes;
31-
32-
/** @var mixed[] */
33-
private $fields;
34-
35-
/** @var mixed[] */
36-
private $sorts;
37-
38-
/** @var null|Schema */
39-
private $pagination;
20+
private array $responses;
4021

4122
/**
42-
* @param mixed[] $filters
43-
* @param mixed[] $sorts
44-
* @param mixed[] $includes
45-
* @param mixed[] $fields
46-
* @param mixed[] $errorResponses
23+
* @param Filter[] $filters
24+
* @param array<string, ReadSchema> $includes
4725
*/
4826
public function __construct(
49-
ReadSchema $schema,
50-
string $path,
51-
array $filters = [],
52-
array $sorts = [],
53-
array $includes = [],
54-
array $fields = [],
55-
?Schema $pagination = null,
27+
private ReadSchema $schema,
28+
private string $path,
29+
private array $filters = [],
30+
/**
31+
* @var mixed[]
32+
*
33+
* @psalm-suppress UnusedProperty
34+
*/
35+
private array $sorts = [],
36+
private array $includes = [],
37+
/** @var mixed[] */
38+
private array $fields = [],
39+
private ?Schema $pagination = null,
5640
array $errorResponses = []
5741
) {
58-
$this->schema = $schema;
59-
$this->path = $path;
60-
$this->includes = $includes;
42+
Assertion::allIsInstanceOf($this->includes, ReadSchema::class);
6143

62-
$this->responses = array_merge(
44+
/** @var Response[] $responses */
45+
$responses = array_merge(
6346
[
6447
new CollectionResponse($this->schema, $this->includes),
6548
],
6649
$errorResponses
6750
);
6851

69-
$this->filters = $filters;
70-
$this->sorts = $sorts;
71-
$this->fields = $fields;
72-
$this->pagination = $pagination;
52+
$this->responses = $responses;
7353
}
7454

7555
public function getMethod(): string
@@ -101,6 +81,7 @@ public function getParams(): array
10181
}
10282

10383
if (false === empty($this->filters)) {
84+
Assertion::allIsInstanceOf($this->filters, Filter::class);
10485
$filterSet = new FilterSetQueryParam('filter', $this->filters);
10586
$queryParams[] = $filterSet->toOpenApi();
10687
}
@@ -116,7 +97,6 @@ public function toOpenApi(): array
11697
{
11798
$responses = [];
11899

119-
/** @var Response $response */
120100
foreach ($this->responses as $response) {
121101
$responses[$response->getStatusCode()] = $response->toOpenApi();
122102
}
@@ -130,4 +110,9 @@ public function toOpenApi(): array
130110
'responses' => $responses,
131111
];
132112
}
113+
114+
public function getSorts(): array
115+
{
116+
return $this->sorts;
117+
}
133118
}

0 commit comments

Comments
 (0)