Skip to content

Commit e66ca4c

Browse files
committed
Fix PHPStan errors: new static() in trait and Eloquent method access on interface
- Add @phpstan-consistent-constructor to SubscribableMailMessage so that new static() inside the SubscribableNotification trait is recognised as safe. Without this annotation PHPStan cannot verify that subclasses share the same constructor signature. - Add is_a($modelClass, Model::class, true) to the UnsubscribeController guard so PHPStan narrows $modelClass to class-string<Model> in addition to class-string<CanUnsubscribe>. Larastan knows Model declares where() and getRouteKeyName(), eliminating the two 'method.notFound' errors. The check is also correct at runtime: subscribers must be Eloquent models. https://claude.ai/code/session_01R4pAjWwGY8xKspsdU8xnsy
1 parent e9f25f8 commit e66ca4c

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

src/Controllers/UnsubscribeController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace YlsIdeas\SubscribableNotifications\Controllers;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Illuminate\Database\Eloquent\Relations\Relation;
67
use Illuminate\Http\Request;
78
use Illuminate\Routing\Controller;
@@ -21,7 +22,9 @@ public function __invoke(Request $request, string $subscriberType, mixed $subscr
2122
{
2223
$modelClass = Relation::getMorphedModel($subscriberType) ?? $subscriberType;
2324

24-
if (! class_exists($modelClass) || ! is_a($modelClass, CanUnsubscribe::class, true)) {
25+
if (! class_exists($modelClass)
26+
|| ! is_a($modelClass, CanUnsubscribe::class, true)
27+
|| ! is_a($modelClass, Model::class, true)) {
2528
abort(403, __('Could not process unsubscribe request'));
2629
}
2730

src/Messages/SubscribableMailMessage.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Notifications\Messages\MailMessage;
66
use YlsIdeas\SubscribableNotifications\Concerns\SubscribableNotification;
77

8+
/** @phpstan-consistent-constructor */
89
class SubscribableMailMessage extends MailMessage
910
{
1011
use SubscribableNotification;

0 commit comments

Comments
 (0)