Skip to content

Commit fef018e

Browse files
committed
phpstan update
1 parent 8d0555c commit fef018e

File tree

9 files changed

+44
-38
lines changed

9 files changed

+44
-38
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
},
2323
"require-dev": {
2424
"ext-pdo": "*",
25-
"phpstan/phpstan": "^1.2",
25+
"phpstan/phpstan": "^2.0",
2626
"phpunit/phpunit": "^10.0",
27-
"phpstan/phpstan-strict-rules": "^1.1",
28-
"phpstan/phpstan-deprecation-rules": "^1.0"
27+
"phpstan/phpstan-strict-rules": "^2.0",
28+
"phpstan/phpstan-deprecation-rules": "^2.0"
2929
},
3030
"autoload": {
3131
"psr-4": {"SimpleAsFuck\\ApiToolkit\\": "src/"}

src/Model/Client/Response.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public function getProtocolVersion(): string
7676
*/
7777
public function getHeaders(): array
7878
{
79+
/** @var array<string, array<string>> */
7980
return $this->response->getHeaders();
8081
}
8182

src/Model/Server/HeaderRule.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public function key(string $key, bool $caseSensitive = false): StringRule
3131
$values = $this->headerValues($caseSensitive);
3232
$values = $values[$caseSensitive ? $key : strtolower($key)] ?? [];
3333
$value = array_pop($values);
34-
/** @var mixed $value */
3534
return new StringRule($this->exceptionFactory, new RuleChain(), new Validated($value), 'Request header: '.$key);
3635
}
3736

@@ -45,9 +44,9 @@ public function keyOf(string $key, callable $callable, bool $caseSensitive = fal
4544
{
4645
$values = $this->headerValues($caseSensitive);
4746
$values = $values[$caseSensitive ? $key : strtolower($key)] ?? null;
48-
/** @var mixed $values */
4947
return new Collection(
5048
$this->exceptionFactory,
49+
/** @phpstan-ignore-next-line */
5150
new RuleChain(),
5251
new Validated($values),
5352
'Request header: '.$key,

src/Model/Server/QueryRule.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ final class QueryRule
1818
* @param Validated<array<mixed>> $queryParams
1919
*/
2020
public function __construct(
21-
private Exception $exceptionFactory,
22-
private Validated $queryParams
21+
private readonly Exception $exceptionFactory,
22+
private readonly Validated $queryParams
2323
) {
2424
}
2525

@@ -28,16 +28,22 @@ public function __construct(
2828
*/
2929
public function key(string $key): StringTypedKey
3030
{
31-
/** @var Validated<mixed> $validated */
32-
$validated = $this->queryParams;
3331
$ruleChain = new RuleChain();
3432
$valueName = 'Request query parameter: '.$key;
3533
return new StringTypedKey(
3634
$this->exceptionFactory,
35+
/** @phpstan-ignore-next-line */
3736
$ruleChain,
38-
$validated,
37+
$this->queryParams,
3938
$valueName,
40-
new Key($this->exceptionFactory, $ruleChain, $validated, $valueName, $key)
39+
new Key(
40+
$this->exceptionFactory,
41+
/** @phpstan-ignore-next-line */
42+
$ruleChain,
43+
$this->queryParams,
44+
$valueName,
45+
$key
46+
)
4147
);
4248
}
4349

@@ -48,12 +54,11 @@ public function key(string $key): StringTypedKey
4854
*/
4955
public function class(UserQueryRule $userQueryRule): ClassFromArray
5056
{
51-
/** @var Validated<mixed> $validated */
52-
$validated = $this->queryParams;
5357
return new ClassFromArray(
5458
$this->exceptionFactory,
59+
/** @phpstan-ignore-next-line */
5560
new RuleChain(),
56-
$validated,
61+
$this->queryParams,
5762
'Request query',
5863
$this,
5964
$userQueryRule

src/Service/Webhook/LaravelMysqlRepository.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function loadForDispatching(string $type, array $attributes): iterable
3939
}
4040

4141
foreach ($connection->table('WebhookWithoutAttribute')->get() as $webhookMap) {
42-
/** @var \stdClass $webhookMap */
42+
/** @phpstan-ignore-next-line */
4343
if (array_key_exists($webhookMap->webhookId, $webhookMatches)) {
4444
continue;
4545
}
@@ -49,15 +49,13 @@ public function loadForDispatching(string $type, array $attributes): iterable
4949

5050
$webhooks = [];
5151
foreach ($webhookMatches as $webhookId => $matchCount) {
52-
/** @var \stdClass|null $webhook */
52+
/** @var object{id: int, type: non-empty-string, listeningUrl: non-empty-string, priority: 50|100|200}|null $webhook */
5353
$webhook = $connection->table('Webhook')->where('id', '=', $webhookId)->where('type', '=', $type)->first();
5454
if ($webhook === null) {
5555
continue;
5656
}
5757

58-
/** @var non-empty-string $webhookId */
59-
$webhookId = (string) $webhook->id;
60-
$webhooks[] = new Webhook($webhookId, $webhook->type, new Params($webhook->listeningUrl, $webhook->priority, $attributes));
58+
$webhooks[] = new Webhook((string) $webhook->id, $webhook->type, new Params($webhook->listeningUrl, $webhook->priority, $attributes));
6159
}
6260

6361
usort($webhooks, static function (Webhook $a, Webhook $b) use ($webhookMatches): int {
@@ -161,14 +159,14 @@ private function getWebhooksForAttributes(ConnectionInterface $connection, array
161159
->where('key', '=', $key)
162160
->where('value', '=', $value)
163161
->get()
164-
->map(static fn ($row): int => $row->id)
162+
->map(static fn ($row) => $row->id)
165163
;
166164
foreach ($attributeIds as $attributeId) {
167165
/** @var Collection<int, int> $webhookIds */
168166
$webhookIds = $connection->table('WebhookRequiredAttribute')
169167
->where('webhookAttributeId', '=', $attributeId)
170168
->get()
171-
->map(static fn ($row): int => $row->webhookId)
169+
->map(static fn ($row) => $row->webhookId)
172170
;
173171

174172
foreach ($webhookIds as $webhookId) {

test/Database/Migration/LaravelMysqlWebhookTablesTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ public function test(): void
1919
DB::swap($connectionResolver);
2020

2121
$migration = include __DIR__.'/../../../src/Database/Migration/LaravelMysqlWebhookTables.php';
22-
22+
/** @phpstan-ignore-next-line */
2323
$migration->up();
2424

2525
self::assertNull($connection->selectOne('select * from Webhook'));
2626
self::assertNull($connection->selectOne('select * from WebhookAttribute'));
2727
self::assertNull($connection->selectOne('select * from WebhookRequiredAttribute'));
2828
self::assertNull($connection->selectOne('select * from WebhookWithoutAttribute'));
29-
29+
/** @phpstan-ignore-next-line */
3030
$migration->down();
3131

3232
try {
@@ -49,14 +49,14 @@ public function test(): void
4949
} catch (\Throwable $e) {
5050
self::assertInstanceOf(\PDOException::class, $e);
5151
}
52-
52+
/** @phpstan-ignore-next-line */
5353
$migration->up();
5454

5555
self::assertNull($connection->selectOne('select * from Webhook'));
5656
self::assertNull($connection->selectOne('select * from WebhookAttribute'));
5757
self::assertNull($connection->selectOne('select * from WebhookRequiredAttribute'));
5858
self::assertNull($connection->selectOne('select * from WebhookWithoutAttribute'));
59-
59+
/** @phpstan-ignore-next-line */
6060
$migration->down();
6161
}
6262
}

test/Model/Server/HeaderRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function setUp(): void
2121
'header' => ['test'],
2222
'Header1' => ['test1'],
2323
];
24-
/** @var array<array<string>> $testData */
24+
/** @phpstan-ignore-next-line */
2525
$this->rule = new HeaderRule(new UnexpectedValueException(), new Validated($testData));
2626
}
2727

test/Service/Webhook/ClientTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
declare(strict_types=1);
44

5-
65
use GuzzleHttp\Psr7\Response;
7-
use GuzzleHttp\RequestOptions as RequestOptions;
6+
use GuzzleHttp\RequestOptions;
87
use PHPUnit\Framework\TestCase;
98
use SimpleAsFuck\ApiToolkit\Model\Webhook\Params;
10-
use SimpleAsFuck\ApiToolkit\Model\Webhook\Priority as Priority;
9+
use SimpleAsFuck\ApiToolkit\Model\Webhook\Priority;
1110
use SimpleAsFuck\ApiToolkit\Model\Webhook\Webhook;
1211
use SimpleAsFuck\ApiToolkit\Service\Webhook\Client;
1312
use SimpleAsFuck\ApiToolkit\Service\Webhook\Config;
13+
use SimpleAsFuck\Validator\Factory\Validator;
1414

1515
/**
1616
* @covers \SimpleAsFuck\ApiToolkit\Service\Webhook\Client
@@ -33,11 +33,14 @@ public function testCallWebhooks(array $expectedRetryWebhooks, int $expectedCall
3333
$httpClient = $this->createMock(\GuzzleHttp\Client::class);
3434
$httpClient->method('request')->willReturnCallback(static function (string $method, string $uri, array $options) use (&$httpCalls): Response {
3535
$httpCalls++;
36-
if (($options[RequestOptions::JSON]->params->attributes[0]->value ?? null) === 'fail') {
36+
$jsonObject = Validator::make($options)->array()->key(RequestOptions::JSON)->object();
37+
$value = $jsonObject->property('params')->object()->property('attributes')->array()->key(0)->object()->property('value')->nullable();
38+
39+
if ($value === 'fail') {
3740
throw new \RuntimeException();
3841
}
3942

40-
if (($options[RequestOptions::JSON]->params->attributes[0]->value ?? null) === 'stop') {
43+
if ($value === 'stop') {
4144
return new Response(200, body: '{"stopDispatching":true}');
4245
}
4346

test/Service/Webhook/LaravelMysqlRepositoryTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static function setUpBeforeClass(): void
3636
DB::swap(self::$connectionResolver);
3737

3838
$migration = include __DIR__.'/../../../src/Database/Migration/LaravelMysqlWebhookTables.php';
39+
/** @phpstan-ignore-next-line */
3940
$migration->up();
4041
}
4142

@@ -55,6 +56,7 @@ public function tearDown(): void
5556
public static function tearDownAfterClass(): void
5657
{
5758
$migration = include __DIR__.'/../../../src/Database/Migration/LaravelMysqlWebhookTables.php';
59+
/** @phpstan-ignore-next-line */
5860
$migration->down();
5961
}
6062

@@ -88,12 +90,10 @@ public function testLoadForDispatching(array $expectedUrls, string $type, array
8890
$connection = self::$connectionResolver->connection('default');
8991

9092
$expectedWebhooks = array_map(function (string $url) use ($connection, $attributes): Webhook {
91-
/** @var \stdClass $dbWebhook */
93+
/** @var object{id: int, type: non-empty-string, listeningUrl: non-empty-string, priority: 50|100|200} $dbWebhook */
9294
$dbWebhook = $connection->selectOne('select * from Webhook where listeningUrl = ?', [$url]);
93-
/** @var non-empty-string $webhookId */
94-
$webhookId = (string) $dbWebhook->id;
9595
return new Webhook(
96-
$webhookId,
96+
(string) $dbWebhook->id,
9797
$dbWebhook->type,
9898
new Params(
9999
$dbWebhook->listeningUrl,
@@ -170,23 +170,23 @@ public function testSave(string $type, Params $params): void
170170
$this->config
171171
);
172172

173-
/** @var Webhook $webhook */
174173
$webhook = $repository->save($type, $params);
175174
self::assertNotNull($webhook);
176175
self::assertSame($type, $webhook->type());
177176
self::assertEquals($params, $webhook->params());
178177

179178
$connection = self::$connectionResolver->connection('default');
180179

181-
/** @var \stdClass $dbWebhook */
180+
/** @var object{id: int, type: string, listeningUrl: string, priority: int} $dbWebhook */
182181
$dbWebhook = $connection->selectOne('select * from Webhook where id = ?', [$webhook->id()]);
183182
self::assertSame($webhook->id(), (string) $dbWebhook->id);
184183
self::assertSame($type, $dbWebhook->type);
185184
self::assertSame($params->listeningUrl(), $dbWebhook->listeningUrl);
186185
self::assertSame($params->priority(), $dbWebhook->priority);
187186

187+
/** @var array<object{webhookId: int, webhookAttributeId: int}> $dbRequiredAttributeMaps */
188188
$dbRequiredAttributeMaps = $connection->select('select * from WebhookRequiredAttribute where webhookId = ?', [$webhook->id()]);
189-
/** @var \stdClass $dbWithoutAttribute */
189+
/** @var object{webhookId: int} $dbWithoutAttribute */
190190
$dbWithoutAttribute = $connection->selectOne('select * from WebhookWithoutAttribute where webhookId = ?', [$webhook->id()]);
191191

192192
if (count($params->attributes()) === 0) {

0 commit comments

Comments
 (0)