From 3035fa06cde8ad58f6217704880162c882a45365 Mon Sep 17 00:00:00 2001 From: Philip Iezzi Date: Thu, 10 Feb 2022 09:49:45 +0100 Subject: [PATCH] Support Laravel 9 --- CHANGELOG.md | 12 ++++---- composer.json | 10 +++---- src/Concerns/ReplacesBindings.php | 4 +-- src/Config.php | 38 +++++------------------- src/FileName.php | 12 ++------ src/Formatter.php | 36 +++++----------------- src/Listeners/SqlQueryLogSubscriber.php | 8 ++--- src/Providers/EventServiceProvider.php | 4 +-- src/SqlLogger.php | 2 -- src/SqlQuery.php | 28 ++++------------- src/Writer.php | 14 --------- tests/Unit/SqlQueryLogSubscriberTest.php | 25 ++++++++++++++++ 12 files changed, 66 insertions(+), 127 deletions(-) create mode 100644 tests/Unit/SqlQueryLogSubscriberTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 012dd0b..eac17fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,14 +1,16 @@ # CHANGELOG -## [v0.9.2 (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v0.9.1...main) +## [1.0.x (Unreleased)](https://github.com/onlime/laravel-sql-reporter/compare/v1.0.0...main) -## [v0.9.1 (2021-06-03)](https://github.com/onlime/laravel-sql-reporter/releases/tag/v0.9.1) +## [v1.0.0 (2022-02-10)](https://github.com/onlime/laravel-sql-reporter/releases/tag/compare/v0.9.1...v1.0.0) + +- Support Laravel 9 +- Added some function return types and cleaned up phpdoc comments. + +## [v0.9.1 (2021-06-03)](https://github.com/onlime/laravel-sql-reporter/releases/tag/compare/v0.9...v0.9.1) -### Added - Added new config param `queries.exclude_pattern` (env var `SQL_REPORTER_QUERIES_EXCLUDE_PATTERN`) to narrow down queries to be logged without bloating include pattern regex. - Added unit tests for `Writer`, testing query include/exclude patterns and min exec time. - -### Changed - Renamed `SQL_REPORTER_QUERIES_PATTERN` env var to `SQL_REPORTER_QUERIES_INCLUDE_PATTERN` - Renamed methods in `Writer` for clarity. - Improved testability of `Writer::writeQuery()` by returning true if query was written to log. diff --git a/composer.json b/composer.json index 1bedef1..f17ea33 100644 --- a/composer.json +++ b/composer.json @@ -15,15 +15,15 @@ ], "require": { "php": "^8.0", - "illuminate/support": "8.*", - "nesbot/carbon": "^2.0", - "illuminate/filesystem": "8.*", - "illuminate/container": "8.*" + "illuminate/support": "^8.0|^9.0", + "illuminate/filesystem": "^8.0|^9.0", + "illuminate/container": "^8.0|^9.0", + "nesbot/carbon": "^2.0" }, "require-dev": { "phpunit/phpunit": "^9.5", "mockery/mockery": "^1.0", - "laravel/framework": "8.*" + "laravel/framework": "^8.0|^9.0" }, "autoload": { "psr-4": { diff --git a/src/Concerns/ReplacesBindings.php b/src/Concerns/ReplacesBindings.php index 43ac3a1..bb0000d 100644 --- a/src/Concerns/ReplacesBindings.php +++ b/src/Concerns/ReplacesBindings.php @@ -14,7 +14,7 @@ trait ReplacesBindings * * @return string */ - protected function replaceBindings($sql, array $bindings) + protected function replaceBindings(string $sql, array $bindings): string { $generalRegex = $this->getRegex(); @@ -53,7 +53,7 @@ protected function value($value) * * @return string */ - protected function getNamedParameterRegex($name) + protected function getNamedParameterRegex($name): string { if (mb_substr($name, 0, 1) == ':') { $name = mb_substr($name, 1); diff --git a/src/Config.php b/src/Config.php index d8b82a0..0d13906 100644 --- a/src/Config.php +++ b/src/Config.php @@ -8,15 +8,13 @@ class Config { /** * Config constructor. - * - * @param ConfigRepository $repository */ - public function __construct(protected ConfigRepository $repository) {} + public function __construct( + protected ConfigRepository $repository + ) {} /** * Get directory where log files should be saved. - * - * @return string */ public function logDirectory(): string { @@ -25,28 +23,22 @@ public function logDirectory(): string /** * Whether query execution time should be converted to seconds. - * - * @return bool */ public function useSeconds(): bool { - return (bool) $this->repository->get('sql-reporter.general.use_seconds'); + return (bool)$this->repository->get('sql-reporter.general.use_seconds'); } /** * Get suffix for console logs. - * - * @return string */ public function consoleSuffix(): string { - return (string) $this->repository->get('sql-reporter.general.console_log_suffix'); + return (string)$this->repository->get('sql-reporter.general.console_log_suffix'); } /** * Get file extension for logs. - * - * @return string */ public function fileExtension(): string { @@ -55,18 +47,14 @@ public function fileExtension(): string /** * Whether all queries should be logged. - * - * @return bool */ public function queriesEnabled(): bool { - return (bool) $this->repository->get('sql-reporter.queries.enabled'); + return (bool)$this->repository->get('sql-reporter.queries.enabled'); } /** * Minimum execution time (in milliseconds) for queries to be logged. - * - * @return float */ public function queriesMinExecTime(): float { @@ -75,18 +63,14 @@ public function queriesMinExecTime(): float /** * Whether SQL log should be overridden for each request. - * - * @return bool */ public function queriesOverrideLog(): bool { - return (bool) $this->repository->get('sql-reporter.queries.override_log'); + return (bool)$this->repository->get('sql-reporter.queries.override_log'); } /** * Get include pattern for queries. - * - * @return string */ public function queriesIncludePattern(): string { @@ -95,8 +79,6 @@ public function queriesIncludePattern(): string /** * Get exclude pattern for queries. - * - * @return string */ public function queriesExcludePattern(): string { @@ -105,8 +87,6 @@ public function queriesExcludePattern(): string /** * Get file name (without extension) for all queries. - * - * @return string */ public function queriesFileName(): string { @@ -115,8 +95,6 @@ public function queriesFileName(): string /** * Get header fields that should be printed in header before query loglines. - * - * @return array */ public function headerFields(): array { @@ -125,8 +103,6 @@ public function headerFields(): array /** * Get query format that should be used to save query. - * - * @return string */ public function entryFormat(): string { diff --git a/src/FileName.php b/src/FileName.php index 4e64b38..cbad6b8 100644 --- a/src/FileName.php +++ b/src/FileName.php @@ -21,7 +21,7 @@ public function __construct( /** * Create file name for query log. */ - public function getLogfile() + public function getLogfile(): string { return $this->parseFileName($this->config->queriesFileName()) . @@ -31,22 +31,16 @@ public function getLogfile() /** * Get file suffix. - * - * @return string */ - protected function suffix() + protected function suffix(): string { return $this->app->runningInConsole() ? $this->config->consoleSuffix() : ''; } /** * Parse file name to include date in it. - * - * @param string $fileName - * - * @return string */ - protected function parseFileName($fileName) + protected function parseFileName(string $fileName): string { return preg_replace_callback('#(\[.*\])#U', function ($matches) { $format = str_replace(['[',']'], [], $matches[1]); diff --git a/src/Formatter.php b/src/Formatter.php index 0b4478e..47822dc 100644 --- a/src/Formatter.php +++ b/src/Formatter.php @@ -5,7 +5,6 @@ use Carbon\Carbon; use Illuminate\Container\Container; use Illuminate\Support\Arr; -use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Request; @@ -29,11 +28,8 @@ public function __construct( /** * Get formatted single query line(s). - * - * @param SqlQuery $query - * @return string */ - public function getLine(SqlQuery $query) + public function getLine(SqlQuery $query): string { $replace = [ '[query_nr]' => $query->number(), @@ -48,10 +44,8 @@ public function getLine(SqlQuery $query) /** * Get formatted header lines. - * - * @return string */ - public function getHeader() + public function getHeader(): string { $headerFields = $this->config->headerFields(); if (empty($headerFields)) { @@ -97,21 +91,16 @@ public function getHeader() /** * Format time. - * - * @param float $time - * @return string */ - protected function time($time) + protected function time(float $time): string { return $this->config->useSeconds() ? ($time / 1000.0) . 's' : $time . 'ms'; } /** * Get origin line. - * - * @return string */ - protected function originLine() + protected function originLine(): string { return $this->app->runningInConsole() ? '(console) ' . $this->getArtisanLine() @@ -120,21 +109,16 @@ protected function originLine() /** * Get query line. - * - * @param SqlQuery $query - * @return string */ - protected function getQueryLine(SqlQuery $query) + protected function getQueryLine(SqlQuery $query): string { return $query->get() . ';'; } /** * Get Artisan line. - * - * @return string */ - protected function getArtisanLine() + protected function getArtisanLine(): string { $command = $this->app['request']->server('argv', []); @@ -147,20 +131,16 @@ protected function getArtisanLine() /** * Get request line. - * - * @return string */ - protected function getRequestLine() + protected function getRequestLine(): string { return $this->app['request']->method() . ' ' . $this->app['request']->fullUrl(); } /** * Get separator line. - * - * @return string */ - protected function separatorLine() + protected function separatorLine(): string { return '-- ' . str_repeat('-', 50); } diff --git a/src/Listeners/SqlQueryLogSubscriber.php b/src/Listeners/SqlQueryLogSubscriber.php index 5b5c913..b898387 100644 --- a/src/Listeners/SqlQueryLogSubscriber.php +++ b/src/Listeners/SqlQueryLogSubscriber.php @@ -3,6 +3,7 @@ namespace Onlime\LaravelSqlReporter\Listeners; use Illuminate\Console\Events\CommandFinished; +use Illuminate\Events\Dispatcher; use Illuminate\Foundation\Http\Events\RequestHandled; use Onlime\LaravelSqlReporter\SqlLogger; @@ -20,10 +21,10 @@ public function __construct( /** * Register the listeners for the subscriber. * - * @param \Illuminate\Events\Dispatcher $events + * @param Dispatcher $events * @return void */ - public function subscribe($events) + public function subscribe(Dispatcher $events) { $events->listen( [ @@ -36,9 +37,6 @@ public function subscribe($events) /** * Handle the event. - * - * @param object $event - * @return void */ public function handle($event) { diff --git a/src/Providers/EventServiceProvider.php b/src/Providers/EventServiceProvider.php index d8d4130..60cc39a 100644 --- a/src/Providers/EventServiceProvider.php +++ b/src/Providers/EventServiceProvider.php @@ -54,10 +54,8 @@ public function boot() /** * Get package config file location. - * - * @return bool|string */ - protected function configFileLocation() + protected function configFileLocation(): string { return realpath(__DIR__ . '/../../config/sql-reporter.php'); } diff --git a/src/SqlLogger.php b/src/SqlLogger.php index 16ac3cc..7cdf620 100644 --- a/src/SqlLogger.php +++ b/src/SqlLogger.php @@ -8,8 +8,6 @@ class SqlLogger { /** * Number of executed queries. - * - * @var int */ private int $queryNumber = 0; diff --git a/src/SqlQuery.php b/src/SqlQuery.php index 7a01b75..1e99d9e 100644 --- a/src/SqlQuery.php +++ b/src/SqlQuery.php @@ -8,14 +8,6 @@ class SqlQuery { use ReplacesBindings; - /** - * SqlQuery constructor. - * - * @param int $number - * @param string $sql - * @param array $bindings - * @param float $time - */ public function __construct( private int $number, private string $sql, @@ -25,50 +17,40 @@ public function __construct( /** * Get query number. - * - * @return int */ - public function number() + public function number(): int { return $this->number; } /** * Get raw SQL (without bindings). - * - * @return string */ - public function raw() + public function raw(): string { return $this->sql; } /** * Get bindings. - * - * @return array */ - public function bindings() + public function bindings(): array { return $this->bindings; } /** * Get query execution time. - * - * @return float */ - public function time() + public function time(): float { return $this->time; } /** * Get full query with values from bindings inserted. - * - * @return string */ - public function get() + public function get(): string { return $this->replaceBindings($this->sql, $this->bindings); } diff --git a/src/Writer.php b/src/Writer.php index 4488303..def6261 100644 --- a/src/Writer.php +++ b/src/Writer.php @@ -7,18 +7,9 @@ class Writer /** * Log record counter. * This is only used to count queries that are actually logged. - * - * @var int */ private int $logCount = 0; - /** - * Writer constructor. - * - * @param Formatter $formatter - * @param Config $config - * @param FileName $fileName - */ public function __construct( private Formatter $formatter, private Config $config, @@ -68,8 +59,6 @@ protected function createDirectoryIfNotExists(int $queryNumber): bool /** * Get directory where file should be located. - * - * @return string */ protected function directory(): string { @@ -78,9 +67,6 @@ protected function directory(): string /** * Verify whether query should be logged. - * - * @param SqlQuery $query - * @return bool */ protected function shouldLogQuery(SqlQuery $query): bool { diff --git a/tests/Unit/SqlQueryLogSubscriberTest.php b/tests/Unit/SqlQueryLogSubscriberTest.php new file mode 100644 index 0000000..6f3a23f --- /dev/null +++ b/tests/Unit/SqlQueryLogSubscriberTest.php @@ -0,0 +1,25 @@ +logger = Mockery::mock(SqlLogger::class); + } + +// /** @test */ +// public function foo_bar() +// { +// // TODO +// } +}