Skip to content

Commit f789933

Browse files
erikn69willpower232parallels999
authored
PHPStan Fixes (#998)
* Make PHPStan happy * Update src/Events/DispatchAudit.php Co-authored-by: Will Power <[email protected]> * Update src/Resolvers/UrlResolver.php Co-authored-by: parallels999 <[email protected]> --------- Co-authored-by: Will Power <[email protected]> Co-authored-by: parallels999 <[email protected]>
1 parent a241d88 commit f789933

File tree

10 files changed

+29
-11
lines changed

10 files changed

+29
-11
lines changed

src/Audit.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ protected function getFormattedValue(Model $model, string $key, $value)
128128
if ($model->hasGetMutator($key)) {
129129
return $model->mutateAttribute($key, $value);
130130
}
131-
131+
// hasAttributeMutator since 8.x
132+
// @phpstan-ignore function.alreadyNarrowedType
132133
if (method_exists($model, 'hasAttributeMutator') && $model->hasAttributeMutator($key)) {
133134
return $model->mutateAttributeMarkedAttribute($key, $value);
134135
}

src/Auditable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OwenIt\Auditing\Exceptions\AuditableTransitionException;
1919
use OwenIt\Auditing\Exceptions\AuditingException;
2020

21+
// @phpstan-ignore trait.unused
2122
trait Auditable
2223
{
2324
/**

src/AuditableObserver.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ protected function dispatchAudit(Auditable $model): void
9393
return;
9494
}
9595

96+
// @phpstan-ignore method.notFound
9697
$model->preloadResolverData();
9798
if (!Config::get('audit.queue.enable', false)) {
9899
Auditor::execute($model);
@@ -104,7 +105,8 @@ protected function dispatchAudit(Auditable $model): void
104105
}
105106

106107
// Unload the relations to prevent large amounts of unnecessary data from being serialized.
107-
app()->make('events')->dispatch(new DispatchAudit($model->withoutRelations()));
108+
$model->withoutRelations();
109+
app()->make('events')->dispatch(new DispatchAudit($model));
108110
}
109111

110112
/**

src/Contracts/Auditable.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55
use Illuminate\Database\Eloquent\Relations\MorphMany;
66

7+
/**
8+
* @phpstan-require-extends \Illuminate\Database\Eloquent\Model
9+
*/
710
interface Auditable
811
{
912
/**
1013
* Auditable Model audits.
1114
*
12-
* @return MorphMany<\OwenIt\Auditing\Models\Audit>
15+
* @return MorphMany<\OwenIt\Auditing\Models\Audit, \Illuminate\Database\Eloquent\Model>
1316
*/
1417
public function audits(): MorphMany;
1518

src/Contracts/Resolver.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44

55
interface Resolver
66
{
7+
/** @return string */
78
public static function resolve(Auditable $auditable);
89
}

src/Drivers/Database.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ public function prune(Auditable $model): bool
2828

2929
return $model->audits()
3030
->leftJoinSub(
31-
$model->audits()->select($auditModel->getKeyName())->limit($threshold)->latest(),
31+
$model->audits()->getQuery()
32+
->select($auditModel->getKeyName())->limit($threshold)->latest(),
3233
'audit_threshold',
3334
function ($join) use ($auditModel) {
3435
$join->on(

src/Events/DispatchAudit.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,13 @@ public function __serialize()
7171
*/
7272
public function __unserialize(array $values): void
7373
{
74-
$this->model = new $values['class'];
74+
$model = new $values['class'];
7575

76+
if (! $model instanceof Auditable) {
77+
return;
78+
}
79+
80+
$this->model = $model;
7681
$reflection = new ReflectionClass($this->model);
7782
foreach ($values['model_data'] as $key => $value) {
7883
$this->setModelPropertyValue($reflection, $key, $value);
@@ -82,6 +87,7 @@ public function __unserialize(array $values): void
8287
/**
8388
* Set the property value for the given property.
8489
*
90+
* @param ReflectionClass<Auditable> $reflection
8591
* @param mixed $value
8692
*/
8793
protected function setModelPropertyValue(ReflectionClass $reflection, string $name, $value): void
@@ -96,6 +102,7 @@ protected function setModelPropertyValue(ReflectionClass $reflection, string $na
96102
/**
97103
* Get the property value for the given property.
98104
*
105+
* @param ReflectionClass<Auditable> $reflection
99106
* @return mixed
100107
*/
101108
protected function getModelPropertyValue(ReflectionClass $reflection, string $name)

src/Models/Audit.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
/**
88
* @property string $tags
99
* @property string $event
10-
* @property array $new_values
11-
* @property array $old_values
10+
* @property array<string,mixed> $new_values
11+
* @property array<string,mixed> $old_values
1212
* @property mixed $user
1313
* @property mixed $auditable.
1414
*/

src/Resolvers/UrlResolver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
use Illuminate\Support\Facades\App;
66
use Illuminate\Support\Facades\Request;
77
use OwenIt\Auditing\Contracts\Auditable;
8+
use OwenIt\Auditing\Contracts\Resolver;
89

9-
class UrlResolver implements \OwenIt\Auditing\Contracts\Resolver
10+
class UrlResolver implements Resolver
1011
{
1112
public static function resolve(Auditable $auditable): string
1213
{
1314
if (! empty($auditable->preloadedResolverData['url'] ?? null)) {
14-
return $auditable->preloadedResolverData['url'];
15+
return $auditable->preloadedResolverData['url'] ?? '';
1516
}
1617

1718
if (App::runningInConsole()) {

src/Resolvers/UserResolver.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@
55
use Illuminate\Support\Facades\Auth;
66
use Illuminate\Support\Facades\Config;
77
use OwenIt\Auditing\Contracts\Auditable;
8+
use OwenIt\Auditing\Contracts\UserResolver as Resolver;
89

9-
class UserResolver implements \OwenIt\Auditing\Contracts\UserResolver
10+
class UserResolver implements Resolver
1011
{
1112
/**
1213
* @return \Illuminate\Contracts\Auth\Authenticatable|null
1314
*/
1415
public static function resolve()
1516
{
1617
$guards = Config::get('audit.user.guards', [
17-
\config('auth.defaults.guard')
18+
Config::get('auth.defaults.guard')
1819
]);
1920

2021
foreach ($guards as $guard) {

0 commit comments

Comments
 (0)