Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit 5dbcdeb

Browse files
authored
Merge pull request #12 from BinaryKitten/pstan-8
PStan level 8
2 parents b699d6a + a7fce76 commit 5dbcdeb

16 files changed

Lines changed: 115 additions & 24 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ phpstan.neon
99
testbench.yaml
1010
vendor
1111
node_modules
12+
pstan-error.json

config/barstool.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
return [
46

57
/*

database/factories/BarstoolFactory.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace CraigPotter\Barstool\Database\Factories;
46

57
use CraigPotter\Barstool\Models\Barstool;
68
use Saloon\Http\Connectors\NullConnector;
79
use Illuminate\Database\Eloquent\Factories\Factory;
810
use CraigPotter\Barstool\Tests\Fixtures\Requests\SoloUserRequest;
911

12+
/**
13+
* @extends Factory<Barstool>
14+
*/
1015
class BarstoolFactory extends Factory
1116
{
1217
protected $model = Barstool::class;
1318

14-
public function definition()
19+
/**
20+
* @return array<string, mixed>
21+
*/
22+
public function definition(): array
1523
{
1624
return [
1725
'uuid' => $this->faker->uuid,

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 5
5+
level: 8
66
paths:
77
- src
88
- config

pint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"preset": "laravel",
33
"rules": {
4-
"ordered_imports": {"sort_algorithm":"length"}
4+
"ordered_imports": {"sort_algorithm":"length"},
5+
"declare_strict_types": true
56
}
67
}

src/Barstool.php

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace CraigPotter\Barstool;
46

57
use Saloon\Http\Response;
68
use Illuminate\Support\Str;
79
use InvalidArgumentException;
810
use Saloon\Http\PendingRequest;
11+
use Psr\Http\Message\UriInterface;
12+
use Saloon\Contracts\Body\BodyRepository;
913
use Saloon\Repositories\Body\StreamBodyRepository;
1014
use Saloon\Exceptions\Request\FatalRequestException;
1115

@@ -44,6 +48,17 @@ public static function record(PendingRequest|Response|FatalRequestException $dat
4448
};
4549
}
4650

51+
/**
52+
* @return array{
53+
* connector_class: class-string,
54+
* request_class: class-string,
55+
* method: string,
56+
* url: string,
57+
* request_headers: array<string, string>|null,
58+
* request_body: BodyRepository|string|null,
59+
* successful: false
60+
* }
61+
*/
4762
private static function getRequestData(PendingRequest $request): array
4863
{
4964
$body = $request->body();
@@ -63,6 +78,16 @@ private static function getRequestData(PendingRequest $request): array
6378
];
6479
}
6580

81+
/**
82+
* @return array{
83+
* url: UriInterface,
84+
* status: 'failed'|'successful',
85+
* response_headers: array<string, mixed>,
86+
* response_body: string,
87+
* response_status: int,
88+
* successful: bool
89+
* }
90+
*/
6691
private static function getResponseData(Response $response): array
6792
{
6893
$responseBody = self::getResponseBody($response);
@@ -77,6 +102,17 @@ private static function getResponseData(Response $response): array
77102
];
78103
}
79104

105+
/**
106+
* @return array{
107+
* url: UriInterface,
108+
* status: 'fatal',
109+
* response_headers: null,
110+
* response_body: null,
111+
* response_status: null,
112+
* successful: false,
113+
* fatal_error: string
114+
* }
115+
*/
80116
private static function getFatalData(FatalRequestException $exception): array
81117
{
82118
return [
@@ -102,14 +138,16 @@ private static function recordRequest(PendingRequest $data): void
102138
$entry->save();
103139
}
104140

105-
private static function recordResponse(Response|PendingRequest $data): void
141+
private static function recordResponse(Response $data): void
106142
{
107-
$uuid = $data->getPsrRequest()->getHeader('X-Barstool-UUID')[0] ?? null;
143+
$psrRequest = $data->getPsrRequest();
144+
145+
$uuid = $psrRequest->getHeader('X-Barstool-UUID')[0] ?? null;
108146
if (is_null($uuid)) {
109147
return;
110148
}
111149

112-
$entry = Models\Barstool::where('uuid', $uuid)->first();
150+
$entry = Models\Barstool::query()->firstWhere('uuid', $uuid);
113151

114152
if ($entry) {
115153
$entry->fill([
@@ -120,20 +158,22 @@ private static function recordResponse(Response|PendingRequest $data): void
120158
}
121159
}
122160

123-
/**
124-
* @param Response|PendingRequest $data
125-
*/
126-
public static function calculateDuration(Response|PendingRequest|FatalRequestException $data): mixed
161+
public static function calculateDuration(Response|PendingRequest $data): int
127162
{
128-
return $data->getConnector()->config()->get('barstool-response-time', microtime(true) * 1000) - $data->getConnector()->config()->get('barstool-request-time');
163+
$config = $data->getConnector()->config();
164+
165+
$requestTime = (int) $config->get('barstool-request-time');
166+
$responseTime = (int) $config->get('barstool-response-time', microtime(true) * 1000);
167+
168+
return $responseTime - $requestTime;
129169
}
130170

131171
private static function recordFatal(FatalRequestException $data): void
132172
{
133173
$pendingRequest = $data->getPendingRequest();
134174
$uuid = $pendingRequest->headers()->get('X-Barstool-UUID');
135175

136-
$entry = Models\Barstool::where('uuid', $uuid)->first();
176+
$entry = Models\Barstool::query()->firstWhere('uuid', $uuid);
137177

138178
if ($entry) {
139179
$entry->fill([
@@ -147,7 +187,7 @@ private static function recordFatal(FatalRequestException $data): void
147187
/**
148188
* Get the supported content types for response bodies.
149189
*
150-
* @return array<string>
190+
* @return string[]
151191
*/
152192
private static function supportedContentTypes(): array
153193
{
@@ -160,6 +200,9 @@ private static function supportedContentTypes(): array
160200
];
161201
}
162202

203+
/**
204+
* @return array<string, string>|null
205+
*/
163206
public static function getRequestHeaders(PendingRequest $request): ?array
164207
{
165208
$excludedHeaders = config('barstool.excluded_request_headers', []);

src/BarstoolServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace CraigPotter\Barstool;
46

57
use Saloon\Config;
@@ -21,7 +23,7 @@ public function configurePackage(Package $package): void
2123
->hasMigration('create_barstools_table');
2224
}
2325

24-
public function packageRegistered()
26+
public function packageRegistered(): void
2527
{
2628
Config::globalMiddleware()
2729
->onFatalException(function (FatalRequestException $exception) {

src/Models/Barstool.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace CraigPotter\Barstool\Models;
46

7+
use Carbon\CarbonInterface;
58
use Illuminate\Database\Eloquent\Model;
69
use Illuminate\Database\Eloquent\MassPrunable;
710
use Illuminate\Database\Eloquent\Factories\HasFactory;
11+
use CraigPotter\Barstool\Database\Factories\BarstoolFactory;
12+
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
813

914
/**
1015
* @property string $uuid
16+
* @property CarbonInterface $created_at
1117
*/
1218
class Barstool extends Model
1319
{
20+
/** @use HasFactory<BarstoolFactory> */
1421
use HasFactory;
15-
use MassPrunable;
16-
17-
const UPDATED_AT = null;
1822

19-
public function __construct(array $attributes = [])
20-
{
21-
parent::__construct($attributes);
23+
use MassPrunable;
2224

23-
$this->setConnection(config('barstool.connection'));
24-
}
25+
public const string|null UPDATED_AT = null;
2526

2627
protected $fillable = [
2728
'connector_class',
@@ -44,11 +45,28 @@ public function __construct(array $attributes = [])
4445
'successful' => 'boolean',
4546
];
4647

48+
/**
49+
* @param array<string, mixed> $attributes
50+
*/
51+
public function __construct(array $attributes = [])
52+
{
53+
parent::__construct($attributes);
54+
55+
$this->setConnection(config('barstool.connection'));
56+
}
57+
4758
/**
4859
* Get the prunable model query.
60+
*
61+
* @return EloquentBuilder<static>
4962
*/
50-
public function prunable()
63+
public function prunable(): EloquentBuilder
5164
{
52-
return static::where('created_at', '<=', now()->subDays(config('barstool.keep_for_days')));
65+
return static::query()
66+
->where(
67+
'created_at',
68+
'<=',
69+
now()->subDays(config('barstool.keep_for_days', 0))
70+
);
5371
}
5472
}

tests/ArchTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
it('will not use debugging functions')
46
->expect(['dd', 'dump', 'ray'])
57
->each->not->toBeUsed();

tests/BarstoolTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
use Saloon\Http\Faking\MockClient;
46
use Saloon\Http\Faking\MockResponse;
57
use Illuminate\Support\Facades\Artisan;

0 commit comments

Comments
 (0)