From 817966e85b2557975e04a8a55f710418e52ef71a Mon Sep 17 00:00:00 2001 From: "quentin.schmick" Date: Wed, 29 Jun 2022 15:01:10 -0400 Subject: [PATCH] Updated deps to better support laravel 9; Updated typehints --- composer.json | 10 ++++----- .../Commands/MakeModelAuditLogTable.php | 12 +++++------ src/Models/BaseModel.php | 21 +++++++------------ src/Observers/AuditLogObserver.php | 2 +- src/Traits/AuditLoggable.php | 2 +- 5 files changed, 21 insertions(+), 26 deletions(-) diff --git a/composer.json b/composer.json index 12af900..b4315b7 100644 --- a/composer.json +++ b/composer.json @@ -10,15 +10,15 @@ "homepage": "https://github.com/always-open/laravel-model-auditlog", "license": "MIT", "require": { - "php": "^7.3|^8.0", + "php": "^8.0.0|^8.1.0", "awobaz/compoships": "^2.0.3", "fico7489/laravel-pivot": "^3.0.1", - "laravel/framework": "^8.0|^9.0", - "always-open/laravel-process-stamps": "^5.0" + "laravel/framework": "^9.0", + "always-open/laravel-process-stamps": "^6.0" }, "require-dev": { - "doctrine/dbal": "^2.9|^3.0", - "orchestra/testbench": "^6.0", + "doctrine/dbal": "^3.0", + "orchestra/testbench": "^7.0", "phpunit/phpunit": "^9.0" }, "autoload": { diff --git a/src/Console/Commands/MakeModelAuditLogTable.php b/src/Console/Commands/MakeModelAuditLogTable.php index a0e81a7..e9ab97f 100644 --- a/src/Console/Commands/MakeModelAuditLogTable.php +++ b/src/Console/Commands/MakeModelAuditLogTable.php @@ -59,7 +59,7 @@ public function handle() * * @return string */ - public function generateAuditTableName($subject_model, array $config): string + public function generateAuditTableName(Model $subject_model, array $config): string { return $subject_model->getTable() . $config['table_suffix']; } @@ -70,7 +70,7 @@ public function generateAuditTableName($subject_model, array $config): string * * @return string */ - public function generateAuditModelName($subject_model, array $config): string + public function generateAuditModelName(Model $subject_model, array $config): string { return class_basename($subject_model) . $config['model_suffix']; } @@ -82,7 +82,7 @@ public function generateAuditModelName($subject_model, array $config): string * * @return string */ - public function getModelNamespace($subject_model): string + public function getModelNamespace(Model $subject_model): string { return (new ReflectionClass($subject_model))->getNamespaceName(); } @@ -93,7 +93,7 @@ public function getModelNamespace($subject_model): string * * @throws \ReflectionException */ - public function createModel($subject_model, array $config): void + public function createModel(Model $subject_model, array $config): void { $modelname = $this->generateAuditModelName($subject_model, $config); @@ -114,7 +114,7 @@ public function createModel($subject_model, array $config): void * @param Model $subject_model * @param array $config */ - public function createMigration($subject_model, array $config): void + public function createMigration(Model $subject_model, array $config): void { $tablename = $this->generateAuditTableName($subject_model, $config); $fileslug = "create_{$tablename}_table"; @@ -176,7 +176,7 @@ public function getStubWithReplacements(string $file, array $replacements): stri * * @return string */ - public function generateMigrationSubjectForeignKeys($subject_model, array $config): string + public function generateMigrationSubjectForeignKeys(Model $subject_model, array $config): string { if (Arr::get($config, 'enable_subject_foreign_keys') === true) { return '$table->foreign(\'subject_id\') diff --git a/src/Models/BaseModel.php b/src/Models/BaseModel.php index 4f786ff..8340b1b 100644 --- a/src/Models/BaseModel.php +++ b/src/Models/BaseModel.php @@ -17,7 +17,7 @@ abstract class BaseModel extends Model { public $casts = [ self::CREATED_AT => 'datetime:Y-m-d H:i:s.u', - self::CREATED_AT => 'datetime:Y-m-d H:i:s.u', + self::UPDATED_AT => 'datetime:Y-m-d H:i:s.u', 'occurred_at' => 'datetime:Y-m-d H:i:s.u', ]; @@ -42,7 +42,7 @@ public function recordChanges(int $event_type, Model $model): void /** * @param array $changes - * @param $model + * @param Model $model * * @return Collection */ @@ -68,7 +68,7 @@ public function passingChanges(array $changes, Model $model): Collection public function saveChanges(Collection $passing_changes, int $event_type, Model $model): void { $passing_changes - ->each(function ($change, $key) use ($event_type, $model) { + ->each(function ($change, string $key) use ($event_type, $model) { $log = new static(); $log->event_type = $event_type; $log->occurred_at = now(); @@ -94,7 +94,7 @@ public function saveChanges(Collection $passing_changes, int $event_type, Model /** * @param int $event_type - * @param $model + * @param Model $model * @param string $relationName * @param array $pivotIds */ @@ -114,9 +114,9 @@ public function recordPivotChanges(int $event_type, Model $model, string $relati } /** - * @param $pivot - * @param $model - * @param $pivotIds + * @param string $pivot + * @param Model $model + * @param array $pivotIds * * @return array */ @@ -181,24 +181,19 @@ public static function getChangesByType(int $event_type, Model $model): array switch ($event_type) { case EventType::CREATED: return $model->getAttributes(); - break; case EventType::RESTORED: return $model->getChanges(); - break; case EventType::FORCE_DELETED: return []; // if force deleted we want to stop execution here as there would be nothing to correlate records to - break; case EventType::DELETED: if (method_exists($model, 'getDeletedAtColumn')) { return $model->only($model->getDeletedAtColumn()); } return []; - break; case EventType::UPDATED: default: return $model->getDirty(); - break; } } @@ -223,7 +218,7 @@ public function getSubjectModelClassname(): string * * @return mixed */ - public function getSubjectModelClassInstance() + public function getSubjectModelClassInstance() : mixed { $class = $this->getSubjectModelClassname(); diff --git a/src/Observers/AuditLogObserver.php b/src/Observers/AuditLogObserver.php index 5574345..efe0cd5 100644 --- a/src/Observers/AuditLogObserver.php +++ b/src/Observers/AuditLogObserver.php @@ -70,7 +70,7 @@ public function pivotDetached(Model $model, string $relationName, array $pivotId * * @return mixed */ - protected function getAuditLogModel(Model $model) + protected function getAuditLogModel(Model $model) : mixed { return $model->getAuditLogModelInstance(); } diff --git a/src/Traits/AuditLoggable.php b/src/Traits/AuditLoggable.php index fe28ae6..4135614 100644 --- a/src/Traits/AuditLoggable.php +++ b/src/Traits/AuditLoggable.php @@ -28,7 +28,7 @@ public function getAuditLogModelName(): string * * @return mixed */ - public function getAuditLogModelInstance() + public function getAuditLogModelInstance() : mixed { $class = $this->getAuditLogModelName();