diff options
| -rw-r--r-- | .github/workflows/coverage.yml | 5 | ||||
| -rw-r--r-- | .github/workflows/lint.yml | 25 | ||||
| -rw-r--r-- | composer.json | 10 | ||||
| -rw-r--r-- | composer.lock | 157 | ||||
| -rw-r--r-- | phpstan.neon | 5 | ||||
| -rw-r--r-- | src/Format.php | 50 | ||||
| -rw-r--r-- | src/Loggr.php | 1 | ||||
| -rw-r--r-- | src/Message.php | 1 | ||||
| -rw-r--r-- | tests/FileSystemDriverTest.php | 4 |
9 files changed, 242 insertions, 16 deletions
diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index a1441fe..6bdaf14 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -1,4 +1,7 @@ -on: push +push: + branches: [ "main" ] +pull_request: + branches: [ "main" ] name: Coverage jobs: coverage: diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..fad6051 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,25 @@ +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] +name: Lint +jobs: + lint: + name: Lint + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Set up php 8.3 + uses: shivammathur/setup-php@v2 + with: + php-version: "8.3" + + - name: Set-up Composer + uses: php-actions/composer@v6 + + - name: Run phpstan + run: ./vendor/bin/phpstan diff --git a/composer.json b/composer.json index 5cc0125..83327a3 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,14 @@ }, "require-dev": { "phpunit/phpunit": "11", - "mockery/mockery": "^1.6" + "mockery/mockery": "^1.6", + "phpstan/phpstan": "^2.0", + "phpstan/phpstan-mockery": "^2.0", + "phpstan/extension-installer": "^1.4" + }, + "config": { + "allow-plugins": { + "phpstan/extension-installer": true + } } } diff --git a/composer.lock b/composer.lock index 230d82c..8e1cda9 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "68f8adea0f9a14bea333767682748c66", + "content-hash": "8db78070bd8f6669783c69389435993b", "packages": [], "packages-dev": [ { @@ -378,6 +378,161 @@ "time": "2022-02-21T01:04:05+00:00" }, { + "name": "phpstan/extension-installer", + "version": "1.4.3", + "source": { + "type": "git", + "url": "https://github.com/phpstan/extension-installer.git", + "reference": "85e90b3942d06b2326fba0403ec24fe912372936" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/extension-installer/zipball/85e90b3942d06b2326fba0403ec24fe912372936", + "reference": "85e90b3942d06b2326fba0403ec24fe912372936", + "shasum": "" + }, + "require": { + "composer-plugin-api": "^2.0", + "php": "^7.2 || ^8.0", + "phpstan/phpstan": "^1.9.0 || ^2.0" + }, + "require-dev": { + "composer/composer": "^2.0", + "php-parallel-lint/php-parallel-lint": "^1.2.0", + "phpstan/phpstan-strict-rules": "^0.11 || ^0.12 || ^1.0" + }, + "type": "composer-plugin", + "extra": { + "class": "PHPStan\\ExtensionInstaller\\Plugin" + }, + "autoload": { + "psr-4": { + "PHPStan\\ExtensionInstaller\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Composer plugin for automatic installation of PHPStan extensions", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "issues": "https://github.com/phpstan/extension-installer/issues", + "source": "https://github.com/phpstan/extension-installer/tree/1.4.3" + }, + "time": "2024-09-04T20:21:43+00:00" + }, + { + "name": "phpstan/phpstan", + "version": "2.0.1", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "ab4e9b4415a5fc9e4d27f7fe16c8bc9d067dcd6d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/ab4e9b4415a5fc9e4d27f7fe16c8bc9d067dcd6d", + "reference": "ab4e9b4415a5fc9e4d27f7fe16c8bc9d067dcd6d", + "shasum": "" + }, + "require": { + "php": "^7.4|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + } + ], + "time": "2024-11-11T15:43:04+00:00" + }, + { + "name": "phpstan/phpstan-mockery", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan-mockery.git", + "reference": "89a949d0ac64298e88b7c7fa00caee565c198394" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan-mockery/zipball/89a949d0ac64298e88b7c7fa00caee565c198394", + "reference": "89a949d0ac64298e88b7c7fa00caee565c198394", + "shasum": "" + }, + "require": { + "php": "^7.4 || ^8.0", + "phpstan/phpstan": "^2.0" + }, + "require-dev": { + "mockery/mockery": "^1.6.11", + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/phpstan-phpunit": "^2.0", + "phpstan/phpstan-strict-rules": "^2.0", + "phpunit/phpunit": "^9.6" + }, + "type": "phpstan-extension", + "extra": { + "phpstan": { + "includes": [ + "extension.neon" + ] + } + }, + "autoload": { + "psr-4": { + "PHPStan\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan Mockery extension", + "support": { + "issues": "https://github.com/phpstan/phpstan-mockery/issues", + "source": "https://github.com/phpstan/phpstan-mockery/tree/2.0.0" + }, + "time": "2024-10-14T03:18:12+00:00" + }, + { "name": "phpunit/php-code-coverage", "version": "11.0.7", "source": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..dab5814 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: 10 + paths: + - src + - tests
\ No newline at end of file diff --git a/src/Format.php b/src/Format.php index 3909a7d..d6764f0 100644 --- a/src/Format.php +++ b/src/Format.php @@ -20,7 +20,7 @@ enum Format * @param Message $message The message to be serialized. * @return string The serialized message. */ - public function serialize(Message $message, ?DateTime $dateTime = new DateTime): string + public function serialize(Message $message, DateTime $dateTime = new DateTime): string { return match($this) { self::JSON => $this->serializeJson($message, $dateTime), @@ -38,15 +38,26 @@ enum Format */ private function serializeJson(Message $message, DateTime $dateTime): string { - return json_encode([ + /** @var string $trace_file */ + $trace_file = $message->trace['file']; + /** @var string $trace_line */ + $trace_line = $message->trace['line']; + + $json = json_encode([ 'date' => $dateTime->format('Y-m-d H:i:s'), 'level' => $message->level->value, 'context' => $message->context, 'trace' => [ - 'file' => pathinfo($message->trace['file'], PATHINFO_FILENAME), - 'line' => $message->trace['line'], + 'file' => pathinfo($trace_file, PATHINFO_FILENAME), + 'line' => $trace_line, ], ]); + + if ($json === false) { + return ''; + } + + return $json; } /** @@ -57,18 +68,23 @@ enum Format */ private function serializeLaravel(Message $message, DateTime $dateTime): string { + /** @var string $trace_file */ + $trace_file = $message->trace['file']; + // Date $date = $dateTime->format('Y-m-d H:i:s'); $line = "[$date] "; // File name and level - $filename = pathinfo($message->trace['file'], PATHINFO_FILENAME); + $filename = pathinfo($trace_file, PATHINFO_FILENAME); $line .= "{$filename}.{$message->level->value}: "; // Context if (is_array($message->context) || is_object($message->context)) { $line .= json_encode($message->context); - } else { + } + + if (is_string($message->context) || is_numeric($message->context)) { $line .= $message->context; } @@ -83,18 +99,23 @@ enum Format */ private function serializeSymfony(Message $message, DateTime $dateTime): string { + /** @var string $trace_file */ + $trace_file = $message->trace['file']; + // Date $date = $dateTime->format('Y-m-d\TH:i:s.uP'); $line = "[$date] "; // File name and level - $filename = pathinfo($message->trace['file'], PATHINFO_FILENAME); + $filename = pathinfo($trace_file, PATHINFO_FILENAME); $line .= "{$filename}.{$message->level->value}: "; // Context if (is_array($message->context) || is_object($message->context)) { $line .= json_encode($message->context); - } else { + } + + if (is_string($message->context) || is_numeric($message->context)) { $line .= $message->context; } @@ -109,24 +130,31 @@ enum Format */ private function serializeIntelliJ(Message $message, DateTime $dateTime): string { + /** @var string $trace_file */ + $trace_file = $message->trace['file']; + /** @var string $trace_line */ + $trace_line = $message->trace['line']; + // Date $date = $dateTime->format('Y-m-d H:i:s'); $line = "{$date} "; // Line number - $line .= "[{$message->trace['line']}] "; + $line .= "[{$trace_line}] "; // Level $line .= "{$message->level->value} "; // Filename - $filename = pathinfo($message->trace['file'], PATHINFO_FILENAME); + $filename = pathinfo($trace_file, PATHINFO_FILENAME); $line .= "- {$filename} - "; // Context if (is_array($message->context) || is_object($message->context)) { $line .= json_encode($message->context); - } else { + } + + if (is_string($message->context) || is_numeric($message->context)) { $line .= $message->context; } diff --git a/src/Loggr.php b/src/Loggr.php index a8af9e5..1d5a58d 100644 --- a/src/Loggr.php +++ b/src/Loggr.php @@ -13,6 +13,7 @@ namespace Asko\Loggr; */ class Loggr { + /** @var array<string, mixed> $trace */ private array $trace; /** diff --git a/src/Message.php b/src/Message.php index 4af49e7..972b01a 100644 --- a/src/Message.php +++ b/src/Message.php @@ -9,6 +9,7 @@ readonly class Message { public function __construct( public Level $level, + /** @var array<string, mixed> $trace */ public array $trace = [], public mixed $context = null, ) {} diff --git a/tests/FileSystemDriverTest.php b/tests/FileSystemDriverTest.php index c28c489..a05b5a9 100644 --- a/tests/FileSystemDriverTest.php +++ b/tests/FileSystemDriverTest.php @@ -126,7 +126,7 @@ class FileSystemDriverTest extends MockeryTestCase $this->assertCount(8, $lines); $first_line = $lines[0]; - $last_line = end($lines); + $last_line = $lines[count($lines) - 1]; $full_date = (new DateTime())->format('Y-m-d H:i:s'); $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.EMERGENCY: test", $first_line); $this->assertStringContainsString("[{$full_date}] FileSystemDriverTest.DEBUG: test", $last_line); @@ -161,7 +161,7 @@ class FileSystemDriverTest extends MockeryTestCase $driver->shouldReceive('log')->passthru(); $driver->shouldReceive('exists')->andReturn(true); $driver->shouldReceive('isWriteable')->andReturns(true); - $driver->shouldReceive('write')->withArgs(function ($path, $data) use (&$result) { + $driver->shouldReceive('write')->withArgs(function (string $path, string $data) use (&$result) { $result .= $data; return true; |
