Skip to content

Commit ef21a58

Browse files
committed
refactor: use native types
1 parent 0dfa84d commit ef21a58

22 files changed

+183
-309
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
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

Lines changed: 4 additions & 1 deletion
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",
2728
"orchestra/testbench": "^9.0|^10.0",
28-
"phpunit/phpunit": "^10.5|^11.5.3"
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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

phpstan.neon

Lines changed: 14 additions & 0 deletions
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: 5
13+
14+
tmpDir: .phpstan.cache

rector.php

Lines changed: 16 additions & 0 deletions
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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,18 @@
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)
35-
{
36-
$this->report = $report;
37-
$this->subscription = $subscription;
38-
$this->message = $message;
39-
}
19+
public function __construct(public MessageSentReport $report, public PushSubscription $subscription, public WebPushMessage $message) {}
4020
}

src/Events/NotificationSent.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,18 @@
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)
35-
{
36-
$this->report = $report;
37-
$this->subscription = $subscription;
38-
$this->message = $message;
39-
}
19+
public function __construct(public MessageSentReport $report, public PushSubscription $subscription, public WebPushMessage $message) {}
4020
}

src/HasPushSubscriptions.php

Lines changed: 10 additions & 19 deletions
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<\NotificationChannels\WebPush\PushSubscription>
8071
*/
81-
public function routeNotificationForWebPush()
72+
public function routeNotificationForWebPush(): Collection
8273
{
8374
return $this->pushSubscriptions;
8475
}

src/PushSubscription.php

Lines changed: 9 additions & 15 deletions
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,15 @@ class PushSubscription extends Model
2828
/**
2929
* Create a new model instance.
3030
*
31-
* @param array $attributes
3231
* @return void
3332
*/
3433
public function __construct(array $attributes = [])
3534
{
36-
if (! isset($this->connection)) {
35+
if ($this->connection === null) {
3736
$this->setConnection(config('webpush.database_connection'));
3837
}
3938

40-
if (! isset($this->table)) {
39+
if ($this->table === null) {
4140
$this->setTable(config('webpush.table_name'));
4241
}
4342

@@ -46,22 +45,17 @@ public function __construct(array $attributes = [])
4645

4746
/**
4847
* Get the model related to the subscription.
49-
*
50-
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
5148
*/
52-
public function subscribable()
49+
public function subscribable(): MorphTo
5350
{
5451
return $this->morphTo();
5552
}
5653

5754
/**
58-
* Find a subscription by the given endpint.
59-
*
60-
* @param string $endpoint
61-
* @return static|null
55+
* Find a subscription by the given endpoint.
6256
*/
63-
public static function findByEndpoint($endpoint)
57+
public static function findByEndpoint(string $endpoint): ?static
6458
{
65-
return static::where('endpoint', $endpoint)->first();
59+
return static::firstWhere('endpoint', $endpoint);
6660
}
6761
}

src/ReportHandler.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,23 @@
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)
23-
{
24-
$this->events = $events;
25-
}
16+
public function __construct(protected \Illuminate\Contracts\Events\Dispatcher $events) {}
2617

2718
/**
2819
* 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
3420
*/
35-
public function handleReport($report, $subscription, $message)
21+
public function handleReport(MessageSentReport $report, PushSubscription $subscription, WebPushMessage $message): void
3622
{
3723
if ($report->isSuccess()) {
3824
$this->events->dispatch(new NotificationSent($report, $subscription, $message));

0 commit comments

Comments
 (0)