Skip to content

Commit 8eaebfe

Browse files
refactor: use native types (#209)
* refactor: use native types * fix: require testbench 9.2 at minimum * style: empty constructors * refactor: declare generics * refactor: webpushmessage implements arrayable * style: remove space
1 parent 0dfa84d commit 8eaebfe

24 files changed

+216
-301
lines changed

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
stability: [prefer-lowest, prefer-stable]
1717
include:
1818
- laravel: 11.*
19-
testbench: 9.*
19+
testbench: ^9.2
2020
- laravel: 12.*
2121
testbench: 10.*
2222

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ composer.phar
55
composer.lock
66
.phpunit.result.cache
77
.phpunit.cache
8+
.phpstan.cache

composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
"minishlink/web-push": "^9.0"
2424
},
2525
"require-dev": {
26+
"laravel/pint": "^1.21",
2627
"mockery/mockery": "^1.0",
27-
"orchestra/testbench": "^9.0|^10.0",
28-
"phpunit/phpunit": "^10.5|^11.5.3"
28+
"orchestra/testbench": "^9.2|^10.0",
29+
"larastan/larastan": "^3.1",
30+
"phpunit/phpunit": "^10.5|^11.5.3",
31+
"rector/rector": "^2.0"
2932
},
3033
"autoload": {
3134
"psr-4": {

phpstan-baseline.neon

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: '#^Method NotificationChannels\\WebPush\\PushSubscription\:\:findByEndpoint\(\) should return static\(NotificationChannels\\WebPush\\PushSubscription\)\|null but returns NotificationChannels\\WebPush\\PushSubscription\|null\.$#'
5+
identifier: return.type
6+
count: 1
7+
path: src/PushSubscription.php
8+
9+
-
10+
message: '#^Method NotificationChannels\\WebPush\\Test\\User\:\:pushSubscriptions\(\) should return Illuminate\\Database\\Eloquent\\Relations\\MorphMany\<NotificationChannels\\WebPush\\PushSubscription, \$this\(NotificationChannels\\WebPush\\Test\\User\)\> but returns Illuminate\\Database\\Eloquent\\Relations\\MorphMany\<Illuminate\\Database\\Eloquent\\Model, \$this\(NotificationChannels\\WebPush\\Test\\User\)\>\.$#'
11+
identifier: return.type
12+
count: 1
13+
path: tests/User.php
14+
15+
-
16+
message: '#^Unable to resolve the template type TRelatedModel in call to method Illuminate\\Database\\Eloquent\\Model\:\:morphMany\(\)$#'
17+
identifier: argument.templateType
18+
count: 1
19+
path: tests/User.php

phpstan.neon

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
includes:
2+
- ./vendor/larastan/larastan/extension.neon
3+
- ./phpstan-baseline.neon
4+
5+
parameters:
6+
7+
paths:
8+
- src
9+
- tests
10+
- migrations/create_push_subscriptions_table.php.stub
11+
12+
level: 6
13+
14+
tmpDir: .phpstan.cache

rector.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
use Rector\Config\RectorConfig;
4+
5+
return RectorConfig::configure()
6+
->withPaths([
7+
__DIR__.'/src',
8+
__DIR__.'/tests',
9+
])
10+
->withPreparedSets(
11+
deadCode: true,
12+
codeQuality: true,
13+
codingStyle: true,
14+
typeDeclarations: true,
15+
)
16+
->withPhpSets(php82: true);

src/Events/NotificationFailed.php

+5-22
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,21 @@
33
namespace NotificationChannels\WebPush\Events;
44

55
use Illuminate\Queue\SerializesModels;
6+
use Minishlink\WebPush\MessageSentReport;
7+
use NotificationChannels\WebPush\PushSubscription;
8+
use NotificationChannels\WebPush\WebPushMessage;
69

710
class NotificationFailed
811
{
912
use SerializesModels;
1013

11-
/**
12-
* @var \Minishlink\WebPush\MessageSentReport
13-
*/
14-
public $report;
15-
16-
/**
17-
* @var \NotificationChannels\WebPush\PushSubscription
18-
*/
19-
public $subscription;
20-
21-
/**
22-
* @var \NotificationChannels\WebPush\WebPushMessage
23-
*/
24-
public $message;
25-
2614
/**
2715
* Create a new event instance.
2816
*
29-
* @param \Minishlink\WebPush\MessageSentReport $report
30-
* @param \NotificationChannels\WebPush\PushSubscription $subscription
31-
* @param \NotificationChannels\WebPush\WebPushMessage $message
3217
* @return void
3318
*/
34-
public function __construct($report, $subscription, $message)
19+
public function __construct(public MessageSentReport $report, public PushSubscription $subscription, public WebPushMessage $message)
3520
{
36-
$this->report = $report;
37-
$this->subscription = $subscription;
38-
$this->message = $message;
21+
//
3922
}
4023
}

src/Events/NotificationSent.php

+5-22
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,21 @@
33
namespace NotificationChannels\WebPush\Events;
44

55
use Illuminate\Queue\SerializesModels;
6+
use Minishlink\WebPush\MessageSentReport;
7+
use NotificationChannels\WebPush\PushSubscription;
8+
use NotificationChannels\WebPush\WebPushMessage;
69

710
class NotificationSent
811
{
912
use SerializesModels;
1013

11-
/**
12-
* @var \Minishlink\WebPush\MessageSentReport
13-
*/
14-
public $report;
15-
16-
/**
17-
* @var \NotificationChannels\WebPush\PushSubscription
18-
*/
19-
public $subscription;
20-
21-
/**
22-
* @var \NotificationChannels\WebPush\WebPushMessage
23-
*/
24-
public $message;
25-
2614
/**
2715
* Create a new event instance.
2816
*
29-
* @param \Minishlink\WebPush\MessageSentReport $report
30-
* @param \NotificationChannels\WebPush\PushSubscription $subscription
31-
* @param \NotificationChannels\WebPush\WebPushMessage $message
3217
* @return void
3318
*/
34-
public function __construct($report, $subscription, $message)
19+
public function __construct(public MessageSentReport $report, public PushSubscription $subscription, public WebPushMessage $message)
3520
{
36-
$this->report = $report;
37-
$this->subscription = $subscription;
38-
$this->message = $message;
21+
//
3922
}
4023
}

src/HasPushSubscriptions.php

+10-19
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@
22

33
namespace NotificationChannels\WebPush;
44

5+
use Illuminate\Database\Eloquent\Collection;
6+
use Illuminate\Database\Eloquent\Relations\MorphMany;
7+
58
trait HasPushSubscriptions
69
{
710
/**
811
* Get all of the subscriptions.
912
*
10-
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
13+
* @return \Illuminate\Database\Eloquent\Relations\MorphMany<PushSubscription, $this>
1114
*/
12-
public function pushSubscriptions()
15+
public function pushSubscriptions(): MorphMany
1316
{
1417
return $this->morphMany(config('webpush.model'), 'subscribable');
1518
}
1619

1720
/**
1821
* Update (or create) subscription.
19-
*
20-
* @param string $endpoint
21-
* @param string|null $key
22-
* @param string|null $token
23-
* @param string|null $contentEncoding
24-
* @return \NotificationChannels\WebPush\PushSubscription
2522
*/
26-
public function updatePushSubscription($endpoint, $key = null, $token = null, $contentEncoding = null)
23+
public function updatePushSubscription(string $endpoint, ?string $key = null, ?string $token = null, ?string $contentEncoding = null): PushSubscription
2724
{
2825
$subscription = app(config('webpush.model'))->findByEndpoint($endpoint);
2926

@@ -50,23 +47,17 @@ public function updatePushSubscription($endpoint, $key = null, $token = null, $c
5047

5148
/**
5249
* Determine if the model owns the given subscription.
53-
*
54-
* @param \NotificationChannels\WebPush\PushSubscription $subscription
55-
* @return bool
5650
*/
57-
public function ownsPushSubscription($subscription)
51+
public function ownsPushSubscription(PushSubscription $subscription): bool
5852
{
5953
return (string) $subscription->subscribable_id === (string) $this->getKey() &&
6054
$subscription->subscribable_type === $this->getMorphClass();
6155
}
6256

6357
/**
6458
* Delete subscription by endpoint.
65-
*
66-
* @param string $endpoint
67-
* @return void
6859
*/
69-
public function deletePushSubscription($endpoint)
60+
public function deletePushSubscription(string $endpoint): void
7061
{
7162
$this->pushSubscriptions()
7263
->where('endpoint', $endpoint)
@@ -76,9 +67,9 @@ public function deletePushSubscription($endpoint)
7667
/**
7768
* Get all of the subscriptions.
7869
*
79-
* @return \Illuminate\Database\Eloquent\Collection
70+
* @return \Illuminate\Database\Eloquent\Collection<array-key, \NotificationChannels\WebPush\PushSubscription>
8071
*/
81-
public function routeNotificationForWebPush()
72+
public function routeNotificationForWebPush(): Collection
8273
{
8374
return $this->pushSubscriptions;
8475
}

src/PushSubscription.php

+11-14
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
namespace NotificationChannels\WebPush;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\MorphTo;
67

78
/**
9+
* @property int|string $subscribable_id
10+
* @property class-string $subscribable_type
811
* @property string $endpoint
912
* @property string|null $public_key
1013
* @property string|null $auth_token
1114
* @property string|null $content_encoding
12-
* @property \Illuminate\Database\Eloquent\Model $subscribable
1315
*/
1416
class PushSubscription extends Model
1517
{
1618
/**
1719
* The attributes that are mass assignable.
18-
*
19-
* @var array
2020
*/
2121
protected $fillable = [
2222
'endpoint',
@@ -28,16 +28,16 @@ class PushSubscription extends Model
2828
/**
2929
* Create a new model instance.
3030
*
31-
* @param array $attributes
31+
* @param array<string, mixed> $attributes
3232
* @return void
3333
*/
3434
public function __construct(array $attributes = [])
3535
{
36-
if (! isset($this->connection)) {
36+
if ($this->connection === null) {
3737
$this->setConnection(config('webpush.database_connection'));
3838
}
3939

40-
if (! isset($this->table)) {
40+
if ($this->table === null) {
4141
$this->setTable(config('webpush.table_name'));
4242
}
4343

@@ -47,21 +47,18 @@ public function __construct(array $attributes = [])
4747
/**
4848
* Get the model related to the subscription.
4949
*
50-
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
50+
* @return \Illuminate\Database\Eloquent\Relations\MorphTo<\Illuminate\Database\Eloquent\Model, $this>
5151
*/
52-
public function subscribable()
52+
public function subscribable(): MorphTo
5353
{
5454
return $this->morphTo();
5555
}
5656

5757
/**
58-
* Find a subscription by the given endpint.
59-
*
60-
* @param string $endpoint
61-
* @return static|null
58+
* Find a subscription by the given endpoint.
6259
*/
63-
public static function findByEndpoint($endpoint)
60+
public static function findByEndpoint(string $endpoint): ?static
6461
{
65-
return static::where('endpoint', $endpoint)->first();
62+
return static::firstWhere('endpoint', $endpoint);
6663
}
6764
}

src/ReportHandler.php

+4-15
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,26 @@
22

33
namespace NotificationChannels\WebPush;
44

5-
use Illuminate\Contracts\Events\Dispatcher;
5+
use Minishlink\WebPush\MessageSentReport;
66
use NotificationChannels\WebPush\Events\NotificationFailed;
77
use NotificationChannels\WebPush\Events\NotificationSent;
88

99
class ReportHandler implements ReportHandlerInterface
1010
{
11-
/**
12-
* @var \Illuminate\Contracts\Events\Dispatcher
13-
*/
14-
protected $events;
15-
1611
/**
1712
* Create a new report handler.
1813
*
19-
* @param \Illuminate\Contracts\Events\Dispatcher $events
2014
* @return void
2115
*/
22-
public function __construct(Dispatcher $events)
16+
public function __construct(protected \Illuminate\Contracts\Events\Dispatcher $events)
2317
{
24-
$this->events = $events;
18+
//
2519
}
2620

2721
/**
2822
* Handle a message sent report.
29-
*
30-
* @param \Minishlink\WebPush\MessageSentReport $report
31-
* @param \NotificationChannels\WebPush\PushSubscription $subscription
32-
* @param \NotificationChannels\WebPush\WebPushMessage $message
33-
* @return void
3423
*/
35-
public function handleReport($report, $subscription, $message)
24+
public function handleReport(MessageSentReport $report, PushSubscription $subscription, WebPushMessage $message): void
3625
{
3726
if ($report->isSuccess()) {
3827
$this->events->dispatch(new NotificationSent($report, $subscription, $message));

src/ReportHandlerInterface.php

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,12 @@
22

33
namespace NotificationChannels\WebPush;
44

5+
use Minishlink\WebPush\MessageSentReport;
6+
57
interface ReportHandlerInterface
68
{
79
/**
810
* Handle a message sent report.
9-
*
10-
* @param \Minishlink\WebPush\MessageSentReport $report
11-
* @param \NotificationChannels\WebPush\PushSubscription $subscription
12-
* @param \NotificationChannels\WebPush\WebPushMessage $message
13-
* @return void
1411
*/
15-
public function handleReport($report, $subscription, $message);
12+
public function handleReport(MessageSentReport $report, PushSubscription $subscription, WebPushMessage $message): void;
1613
}

0 commit comments

Comments
 (0)