Skip to content

Commit 916045c

Browse files
author
farhadzand
committed
fix: resolve PHPStan static analysis issues and improve code quality
- Fix PHPStan Level 5 analysis errors with strict rules enabled - Remove references to deleted ModelAudited event and AuditModelChanges listener - Improve type safety with better null checking and method existence validation - Fix scope method return types and route handling in services - Update unit tests to work with direct logging architecture - Clean up unused imports and enhance test setup - Add PHPStan ignore rule for dynamic query builder method calls - Update CHANGELOG.md for version 1.3.1 All tests passing (39 tests, 103 assertions) ✅ PHPStan analysis clean (0 errors) ✅
1 parent f9e08bc commit 916045c

File tree

7 files changed

+57
-21
lines changed

7 files changed

+57
-21
lines changed

CHANGELOG.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
### Documentation
1717

18+
## [1.3.1] - 2025-06-06
19+
20+
### Fixed
21+
- Fixed PHPStan static analysis issues at Level 5 with strict rules
22+
- Removed references to deleted `ModelAudited` event and `AuditModelChanges` listener classes
23+
- Improved type safety with better null and type checking in route handling
24+
- Enhanced method existence checks in `AuditBuilder` service
25+
- Fixed scope method return types in `EloquentAuditLog` model
26+
- Updated unit tests to work with direct logging architecture (removed event dependencies)
27+
- Cleaned up unused imports and improved code organization
28+
29+
### Changed
30+
- Added PHPStan ignore rule for dynamic method call warnings in query scopes
31+
- Enhanced test setup with proper service mocking for unit tests
32+
- Improved code quality and maintainability
33+
1834
## [1.3.0] - 2025-06-06
1935

2036
### Added
@@ -138,7 +154,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
138154
- Query scopes for efficient log retrieval
139155
- Batch operations for performance optimization
140156

141-
[Unreleased]: https://github.com/iamfarhad/laravel-audit-log/compare/1.3.0...HEAD
157+
[Unreleased]: https://github.com/iamfarhad/laravel-audit-log/compare/1.3.1...HEAD
158+
[1.3.1]: https://github.com/iamfarhad/laravel-audit-log/compare/1.3.0...1.3.1
142159
[1.3.0]: https://github.com/iamfarhad/laravel-audit-log/compare/1.2.1...1.3.0
143160
[1.2.1]: https://github.com/iamfarhad/laravel-audit-log/compare/1.2.0...1.2.1
144161
[1.2.0]: https://github.com/iamfarhad/laravel-audit-log/compare/1.1.0...1.2.0

phpstan.neon

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ parameters:
3030
-
3131
identifier: missingType.generics
3232
reportUnmatched: false
33-
- '#Construct empty\(\) is not allowed. Use more strict comparison.#'
33+
- '#Construct empty\(\) is not allowed. Use more strict comparison.#'
34+
- '#Dynamic call to static method Illuminate\\Database\\Query\\Builder::whereNotNull\(\)#'

src/AuditLoggerServiceProvider.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
use Illuminate\Support\ServiceProvider;
88
use iamfarhad\LaravelAuditLog\DTOs\AuditLog;
99
use iamfarhad\LaravelAuditLog\Drivers\MySQLDriver;
10-
use iamfarhad\LaravelAuditLog\Events\ModelAudited;
1110
use iamfarhad\LaravelAuditLog\Services\AuditLogger;
12-
use Illuminate\Support\Facades\Event as EventFacade;
1311
use iamfarhad\LaravelAuditLog\Services\CauserResolver;
1412
use iamfarhad\LaravelAuditLog\Contracts\AuditLogInterface;
15-
use iamfarhad\LaravelAuditLog\Listeners\AuditModelChanges;
1613
use iamfarhad\LaravelAuditLog\Contracts\CauserResolverInterface;
1714

1815
final class AuditLoggerServiceProvider extends ServiceProvider
@@ -23,7 +20,7 @@ final class AuditLoggerServiceProvider extends ServiceProvider
2320
public function register(): void
2421
{
2522
$this->mergeConfigFrom(
26-
__DIR__.'/../config/audit-logger.php',
23+
__DIR__ . '/../config/audit-logger.php',
2724
'audit-logger'
2825
);
2926

@@ -32,7 +29,7 @@ public function register(): void
3229
// Register the causer resolver
3330
$this->app->singleton(
3431
CauserResolverInterface::class,
35-
fn ($app) => isset($app['config']['audit-logger.causer']['resolver']) && $app['config']['audit-logger.causer']['resolver']
32+
fn($app) => isset($app['config']['audit-logger.causer']['resolver']) && $app['config']['audit-logger.causer']['resolver']
3633
? $app->make($app['config']['audit-logger.causer']['resolver'])
3734
: new CauserResolver(
3835
guard: $app['config']['audit-logger.causer']['guard'] ?? null,
@@ -59,11 +56,10 @@ public function boot(): void
5956
// Publish config
6057
if ($this->app->runningInConsole()) {
6158
$this->publishes([
62-
__DIR__.'/../config/audit-logger.php' => config_path('audit-logger.php'),
59+
__DIR__ . '/../config/audit-logger.php' => config_path('audit-logger.php'),
6360
], 'audit-logger-config');
6461
}
6562

66-
// Register event listeners
67-
EventFacade::listen(ModelAudited::class, [AuditModelChanges::class, 'handle']);
63+
// Event listeners removed - using direct logging approach
6864
}
6965
}

src/Models/EloquentAuditLog.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,12 @@ public function scopeForSource(Builder $query, string $source): Builder
106106

107107
public function scopeFromConsole(Builder $query): Builder
108108
{
109-
return $query->whereNotNull('source')
110-
->where('source', 'not like', 'App\\Http\\Controllers\\%')
111-
->where('source', 'not like', 'App\\\\Http\\\\Controllers\\\\%');
109+
/** @var Builder $query */
110+
$query->whereNotNull('source');
111+
$query->where('source', 'not like', 'App\\Http\\Controllers\\%');
112+
$query->where('source', 'not like', 'App\\\\Http\\\\Controllers\\\\%');
113+
114+
return $query;
112115
}
113116

114117
public function scopeFromHttp(Builder $query): Builder
@@ -127,7 +130,7 @@ public function scopeFromCommand(Builder $query, string $command): Builder
127130

128131
public function scopeFromController(Builder $query, ?string $controller = null): Builder
129132
{
130-
if ($controller) {
133+
if ($controller !== null && $controller !== '') {
131134
return $query->where('source', 'like', "%{$controller}%");
132135
}
133136

src/Services/AuditBuilder.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,13 @@ public function log(): void
104104
$this->newValues = $this->model->getAuditableAttributes($this->newValues);
105105
}
106106

107+
// Ensure the model has the getAuditEntityType method
108+
$entityType = method_exists($this->model, 'getAuditEntityType')
109+
? $this->model->getAuditEntityType()
110+
: get_class($this->model);
111+
107112
app(AuditLogger::class)->log(new AuditLog(
108-
entityType: $this->model->getAuditEntityType(),
113+
entityType: $entityType,
109114
entityId: $this->model->getKey(),
110115
action: $this->action,
111116
oldValues: $this->oldValues,
@@ -142,9 +147,9 @@ private function getSource(): ?string
142147
return 'console';
143148
}
144149

145-
if ($route = Request::route()) {
150+
$route = Request::route();
151+
if ($route !== null && is_object($route) && method_exists($route, 'getActionName')) {
146152
$controller = $route->getActionName();
147-
148153
return is_string($controller) ? $controller : 'http';
149154
}
150155

src/Services/AuditLogger.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public function getSource(): ?string
5050
}
5151
}
5252

53-
if ($route = Request::route()) {
53+
$route = Request::route();
54+
if ($route !== null && is_object($route) && method_exists($route, 'getActionName')) {
5455
$controller = $route->getActionName();
55-
5656
return is_string($controller) ? $controller : 'http';
5757
}
5858

tests/Unit/AuditBuilderTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,30 @@
55
namespace iamfarhad\LaravelAuditLog\Tests\Unit;
66

77
use Mockery;
8-
use Illuminate\Support\Facades\Event;
98
use iamfarhad\LaravelAuditLog\Tests\TestCase;
109
use iamfarhad\LaravelAuditLog\Services\AuditBuilder;
10+
use iamfarhad\LaravelAuditLog\Services\AuditLogger;
1111

1212
final class AuditBuilderTest extends TestCase
1313
{
1414
protected function setUp(): void
1515
{
1616
parent::setUp();
17-
Event::fake();
17+
18+
// Create a fake AuditLogger since the class is final
19+
$fakeLogger = new class {
20+
public function log(\iamfarhad\LaravelAuditLog\Contracts\AuditLogInterface $log): void
21+
{
22+
// Do nothing - this is a fake for unit tests
23+
}
24+
25+
public function batch(array $logs): void
26+
{
27+
// Do nothing - this is a fake for unit tests
28+
}
29+
};
30+
31+
$this->app->instance(AuditLogger::class, $fakeLogger);
1832
}
1933

2034
public function test_can_build_and_log_custom_audit_event_with_fluent_api(): void

0 commit comments

Comments
 (0)