Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPStan Fixes #998

Merged
merged 3 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ protected function getFormattedValue(Model $model, string $key, $value)
if ($model->hasGetMutator($key)) {
return $model->mutateAttribute($key, $value);
}

// hasAttributeMutator since 8.x
// @phpstan-ignore function.alreadyNarrowedType
if (method_exists($model, 'hasAttributeMutator') && $model->hasAttributeMutator($key)) {
return $model->mutateAttributeMarkedAttribute($key, $value);
}
Expand Down
1 change: 1 addition & 0 deletions src/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OwenIt\Auditing\Exceptions\AuditableTransitionException;
use OwenIt\Auditing\Exceptions\AuditingException;

// @phpstan-ignore trait.unused
trait Auditable
{
/**
Expand Down
4 changes: 3 additions & 1 deletion src/AuditableObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ protected function dispatchAudit(Auditable $model): void
return;
}

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

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

/**
Expand Down
5 changes: 4 additions & 1 deletion src/Contracts/Auditable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@

use Illuminate\Database\Eloquent\Relations\MorphMany;

/**
* @phpstan-require-extends \Illuminate\Database\Eloquent\Model
*/
interface Auditable
{
/**
* Auditable Model audits.
*
* @return MorphMany<\OwenIt\Auditing\Models\Audit>
* @return MorphMany<\OwenIt\Auditing\Models\Audit, \Illuminate\Database\Eloquent\Model>
*/
public function audits(): MorphMany;

Expand Down
1 change: 1 addition & 0 deletions src/Contracts/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@

interface Resolver
{
/** @return string */
public static function resolve(Auditable $auditable);
}
3 changes: 2 additions & 1 deletion src/Drivers/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function prune(Auditable $model): bool

return $model->audits()
->leftJoinSub(
$model->audits()->select($auditModel->getKeyName())->limit($threshold)->latest(),
$model->audits()->getQuery()
->select($auditModel->getKeyName())->limit($threshold)->latest(),
'audit_threshold',
function ($join) use ($auditModel) {
$join->on(
Expand Down
9 changes: 8 additions & 1 deletion src/Events/DispatchAudit.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ public function __serialize()
*/
public function __unserialize(array $values): void
{
$this->model = new $values['class'];
$model = new $values['class'];

if (! $model instanceof Auditable) {
return;
}

$this->model = $model;
$reflection = new ReflectionClass($this->model);
foreach ($values['model_data'] as $key => $value) {
$this->setModelPropertyValue($reflection, $key, $value);
Expand All @@ -82,6 +87,7 @@ public function __unserialize(array $values): void
/**
* Set the property value for the given property.
*
* @param ReflectionClass<Auditable> $reflection
* @param mixed $value
*/
protected function setModelPropertyValue(ReflectionClass $reflection, string $name, $value): void
Expand All @@ -96,6 +102,7 @@ protected function setModelPropertyValue(ReflectionClass $reflection, string $na
/**
* Get the property value for the given property.
*
* @param ReflectionClass<Auditable> $reflection
* @return mixed
*/
protected function getModelPropertyValue(ReflectionClass $reflection, string $name)
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Audit.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
/**
* @property string $tags
* @property string $event
* @property array $new_values
* @property array $old_values
* @property array<string,mixed> $new_values
* @property array<string,mixed> $old_values
* @property mixed $user
* @property mixed $auditable.
*/
Expand Down
5 changes: 3 additions & 2 deletions src/Resolvers/UrlResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Request;
use OwenIt\Auditing\Contracts\Auditable;
use OwenIt\Auditing\Contracts\Resolver;

class UrlResolver implements \OwenIt\Auditing\Contracts\Resolver
class UrlResolver implements Resolver
{
public static function resolve(Auditable $auditable): string
{
if (! empty($auditable->preloadedResolverData['url'] ?? null)) {
return $auditable->preloadedResolverData['url'];
return $auditable->preloadedResolverData['url'] ?? '';
}

if (App::runningInConsole()) {
Expand Down
5 changes: 3 additions & 2 deletions src/Resolvers/UserResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use OwenIt\Auditing\Contracts\Auditable;
use OwenIt\Auditing\Contracts\UserResolver as Resolver;

class UserResolver implements \OwenIt\Auditing\Contracts\UserResolver
class UserResolver implements Resolver
{
/**
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public static function resolve()
{
$guards = Config::get('audit.user.guards', [
\config('auth.defaults.guard')
Config::get('auth.defaults.guard')
]);

foreach ($guards as $guard) {
Expand Down
Loading