Skip to content

Commit 9ebed64

Browse files
fix: fix PHPStan
1 parent 2219ad2 commit 9ebed64

File tree

10 files changed

+44
-54
lines changed

10 files changed

+44
-54
lines changed

api/phpstan.dist.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ parameters:
1717
- src/Kernel.php
1818
ignoreErrors:
1919
- message: '#no (?:return|value) type specified in iterable type array\.#'
20+
- message: '#has invalid type Illuminate\\Http\\Request#'

api/src/DataFixtures/Factory/BookFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ protected function initialize(): static
8282
{
8383
return $this
8484
->afterInstantiate(function (Book $book): void {
85-
if ($book->book && $book->title && $book->author) {
85+
if (isset($book->book, $book->title, $book->author)) {
8686
return;
8787
}
8888

89-
if (!$book->book) {
89+
if (!isset($book->book)) {
9090
$book->book = 'https://openlibrary.org/books/OL' . self::faker()->unique()->randomNumber(7, true) . 'M.json';
9191
$book->title ??= self::faker()->text();
9292
$book->author ??= self::faker()->name();

api/src/DataFixtures/Factory/ReviewFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function initialize(): static
9090
}
9191

9292
// project specification: only create resource on OIDC server for known users (john.doe and chuck.norris)
93-
if (\in_array($object->user?->email, ['[email protected]', '[email protected]'], true)) {
93+
if (isset($object->user) && \in_array($object->user->email, ['[email protected]', '[email protected]'], true)) {
9494
$this->resourceHandler->create($object, $object->user, [
9595
'operation_name' => '/books/{bookId}/reviews/{id}{._format}',
9696
]);

api/src/Entity/Book.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class Book
111111
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
112112
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
113113
#[ORM\Id]
114-
private ?Uuid $id = null;
114+
private Uuid $id;
115115

116116
/**
117117
* @see https://schema.org/itemOffered
@@ -125,7 +125,7 @@ class Book
125125
#[BookUrl]
126126
#[Groups(groups: ['Book:read', 'Book:read:admin', 'Bookmark:read', 'Book:write'])]
127127
#[ORM\Column(unique: true)]
128-
public ?string $book = null;
128+
public string $book;
129129

130130
/**
131131
* @see https://schema.org/name
@@ -138,7 +138,7 @@ class Book
138138
)]
139139
#[Groups(groups: ['Book:read', 'Book:read:admin', 'Bookmark:read', 'Review:read:admin'])]
140140
#[ORM\Column(type: Types::TEXT)]
141-
public ?string $title = null;
141+
public string $title;
142142

143143
/**
144144
* @see https://schema.org/author
@@ -163,7 +163,7 @@ class Book
163163
#[Assert\NotNull]
164164
#[Groups(groups: ['Book:read', 'Book:read:admin', 'Bookmark:read', 'Book:write'])]
165165
#[ORM\Column(name: '`condition`', type: 'string', enumType: BookCondition::class)]
166-
public ?BookCondition $condition = null;
166+
public BookCondition $condition;
167167

168168
/**
169169
* An IRI of reviews.
@@ -198,7 +198,7 @@ public function __construct()
198198
$this->reviews = new ArrayCollection();
199199
}
200200

201-
public function getId(): ?Uuid
201+
public function getId(): Uuid
202202
{
203203
return $this->id;
204204
}

api/src/Entity/Bookmark.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ class Bookmark
6969
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
7070
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
7171
#[ORM\Id]
72-
private ?Uuid $id = null;
72+
private Uuid $id;
7373

7474
/**
7575
* @see https://schema.org/agent
7676
*/
7777
#[ApiProperty(types: ['https://schema.org/agent'])]
7878
#[ORM\ManyToOne(targetEntity: User::class)]
7979
#[ORM\JoinColumn(nullable: false)]
80-
public ?User $user = null;
80+
public User $user;
8181

8282
/**
8383
* @see https://schema.org/object
@@ -88,17 +88,17 @@ class Bookmark
8888
#[Groups(groups: ['Bookmark:read', 'Bookmark:write'])]
8989
#[ORM\ManyToOne(targetEntity: Book::class)]
9090
#[ORM\JoinColumn(nullable: false)]
91-
public ?Book $book = null;
91+
public Book $book;
9292

9393
/**
9494
* @see https://schema.org/startTime
9595
*/
9696
#[ApiProperty(types: ['https://schema.org/startTime'])]
9797
#[Groups(groups: ['Bookmark:read'])]
9898
#[ORM\Column(type: 'datetime_immutable')]
99-
public ?\DateTimeInterface $bookmarkedAt = null;
99+
public \DateTimeImmutable $bookmarkedAt;
100100

101-
public function getId(): ?Uuid
101+
public function getId(): Uuid
102102
{
103103
return $this->id;
104104
}

api/src/Entity/Parchment.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class Parchment
2727
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
2828
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
2929
#[ORM\Id]
30-
private ?Uuid $id = null;
30+
private Uuid $id;
3131

32-
public function getId(): ?Uuid
32+
public function getId(): Uuid
3333
{
3434
return $this->id;
3535
}
@@ -39,12 +39,12 @@ public function getId(): ?Uuid
3939
*/
4040
#[Assert\NotBlank(allowNull: false)]
4141
#[ORM\Column]
42-
public ?string $title = null;
42+
public string $title;
4343

4444
/**
4545
* A description of the item.
4646
*/
4747
#[Assert\NotBlank(allowNull: false)]
4848
#[ORM\Column]
49-
public ?string $description = null;
49+
public string $description;
5050
}

api/src/Entity/Review.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
// https://github.com/api-platform/admin/issues/370
5858
new Put(
5959
uriTemplate: '/admin/reviews/{id}{._format}',
60-
processor: ReviewPersistProcessor::class
6160
),
6261
new Delete(
6362
uriTemplate: '/admin/reviews/{id}{._format}',
@@ -167,7 +166,7 @@ class Review
167166
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
168167
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
169168
#[ORM\Id]
170-
private ?Uuid $id = null;
169+
private Uuid $id;
171170

172171
/**
173172
* @see https://schema.org/author
@@ -176,7 +175,7 @@ class Review
176175
#[Groups(groups: ['Review:read'])]
177176
#[ORM\ManyToOne(targetEntity: User::class)]
178177
#[ORM\JoinColumn(nullable: false)]
179-
public ?User $user = null;
178+
public User $user;
180179

181180
/**
182181
* @see https://schema.org/itemReviewed
@@ -186,15 +185,15 @@ class Review
186185
#[Groups(groups: ['Review:read', 'Review:write:admin'])]
187186
#[ORM\ManyToOne(targetEntity: Book::class, inversedBy: 'reviews')]
188187
#[ORM\JoinColumn(nullable: false, onDelete: 'CASCADE')]
189-
public ?Book $book = null;
188+
public Book $book;
190189

191190
/**
192191
* @see https://schema.org/datePublished
193192
*/
194193
#[ApiProperty(types: ['https://schema.org/datePublished'])]
195194
#[Groups(groups: ['Review:read'])]
196195
#[ORM\Column(type: 'datetime_immutable')]
197-
public ?\DateTimeInterface $publishedAt = null;
196+
public \DateTimeImmutable $publishedAt;
198197

199198
/**
200199
* @see https://schema.org/reviewBody
@@ -203,7 +202,7 @@ class Review
203202
#[Assert\NotBlank(allowNull: false)]
204203
#[Groups(groups: ['Review:read', 'Review:write'])]
205204
#[ORM\Column(type: Types::TEXT)]
206-
public ?string $body = null;
205+
public string $body;
207206

208207
/**
209208
* @see https://schema.org/reviewRating
@@ -213,7 +212,7 @@ class Review
213212
#[Assert\Range(min: 0, max: 5)]
214213
#[Groups(groups: ['Review:read', 'Review:write'])]
215214
#[ORM\Column(type: 'smallint')]
216-
public ?int $rating = null;
215+
public int $rating;
217216

218217
/**
219218
* @deprecated use the rating property instead
@@ -224,7 +223,7 @@ class Review
224223
#[ORM\Column(nullable: true)]
225224
public ?string $letter = null;
226225

227-
public function getId(): ?Uuid
226+
public function getId(): Uuid
228227
{
229228
return $this->id;
230229
}

api/src/Entity/User.php

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,31 +61,31 @@ class User implements UserInterface
6161
#[ORM\CustomIdGenerator(class: UuidGenerator::class)]
6262
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
6363
#[ORM\Id]
64-
private ?Uuid $id = null;
64+
private Uuid $id;
6565

6666
/**
6767
* @see https://schema.org/email
6868
*/
6969
#[ORM\Column(unique: true)]
70-
public ?string $email = null;
70+
public string $email;
7171

7272
/**
7373
* @see https://schema.org/givenName
7474
*/
7575
#[ApiProperty(types: ['https://schema.org/givenName'])]
7676
#[Groups(groups: ['User:read', 'Review:read'])]
7777
#[ORM\Column]
78-
public ?string $firstName = null;
78+
public string $firstName;
7979

8080
/**
8181
* @see https://schema.org/familyName
8282
*/
8383
#[ApiProperty(types: ['https://schema.org/familyName'])]
8484
#[Groups(groups: ['User:read', 'Review:read'])]
8585
#[ORM\Column]
86-
public ?string $lastName = null;
86+
public string $lastName;
8787

88-
public function getId(): ?Uuid
88+
public function getId(): Uuid
8989
{
9090
return $this->id;
9191
}
@@ -104,20 +104,16 @@ public function getRoles(): array
104104

105105
public function getUserIdentifier(): string
106106
{
107-
return (string) $this->email;
107+
return $this->email;
108108
}
109109

110110
/**
111111
* @see https://schema.org/name
112112
*/
113113
#[ApiProperty(iris: ['https://schema.org/name'])]
114114
#[Groups(groups: ['User:read', 'Review:read'])]
115-
public function getName(): ?string
115+
public function getName(): string
116116
{
117-
if (!$this->firstName && !$this->lastName) {
118-
return null;
119-
}
120-
121117
return trim(\sprintf('%s %s', $this->firstName, $this->lastName));
122118
}
123119
}

api/src/Security/Voter/OidcVoter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Symfony\Component\Security\Core\Exception\TokenNotFoundException;
1111
use Symfony\Component\Security\Http\AccessToken\AccessTokenExtractorInterface;
1212

13+
/**
14+
* @extends Voter<string, null|string|object>
15+
*/
1316
abstract class OidcVoter extends Voter
1417
{
1518
public function __construct(

api/src/State/Processor/ReviewPersistProcessor.php

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,33 +33,24 @@ public function __construct(
3333

3434
/**
3535
* @param Review $data
36+
* @param Post $operation
3637
*/
3738
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Review
3839
{
39-
// standard PUT
40-
if (isset($context['previous_data'])) {
41-
$data->user = $context['previous_data']->user;
42-
$data->publishedAt = $context['previous_data']->publishedAt;
43-
}
44-
4540
// prevent overriding user, for instance from admin
46-
if ($operation instanceof Post) {
47-
/** @phpstan-ignore-next-line */
48-
$data->user = $this->security->getUser();
49-
$data->publishedAt = $this->clock->now();
50-
}
41+
/** @phpstan-ignore-next-line */
42+
$data->user = $this->security->getUser();
43+
$data->publishedAt = $this->clock->now();
5144

5245
// save entity
5346
$data = $this->persistProcessor->process($data, $operation, $uriVariables, $context);
5447

5548
// create resource on OIDC server
56-
if ($operation instanceof Post) {
57-
// project specification: only create resource on OIDC server for known users (john.doe and chuck.norris)
58-
if (\in_array($data->user->email, ['[email protected]', '[email protected]'], true)) {
59-
$this->resourceHandler->create($data, $data->user, [
60-
'operation_name' => '/books/{bookId}/reviews/{id}{._format}',
61-
]);
62-
}
49+
// project specification: only create resource on OIDC server for known users (john.doe and chuck.norris)
50+
if (\in_array($data->user->email, ['[email protected]', '[email protected]'], true)) {
51+
$this->resourceHandler->create($data, $data->user, [
52+
'operation_name' => '/books/{bookId}/reviews/{id}{._format}',
53+
]);
6354
}
6455

6556
return $data;

0 commit comments

Comments
 (0)