diff --git a/packages/guides-cli/composer.json b/packages/guides-cli/composer.json index 29c94a6da..09df05b5f 100644 --- a/packages/guides-cli/composer.json +++ b/packages/guides-cli/composer.json @@ -26,7 +26,7 @@ ], "require": { "php": "^8.1", - "monolog/monolog": "^2.9 || ^3.0", + "monolog/monolog": "^3.0", "phpdocumentor/guides": "^1.0 || ^2.0", "phpdocumentor/guides-restructured-text": "^1.0 || ^2.0", "symfony/config": "^5.4 || ^6.3 || ^7.0", diff --git a/packages/guides-cli/src/Command/Run.php b/packages/guides-cli/src/Command/Run.php index aeebc66ca..da7cff393 100644 --- a/packages/guides-cli/src/Command/Run.php +++ b/packages/guides-cli/src/Command/Run.php @@ -256,7 +256,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } if ($settings->isFailOnError()) { - $spyProcessor = new SpyProcessor($settings->getFailOnError()); + $spyProcessor = new SpyProcessor($settings->getFailOnError() ?? LogLevel::WARNING); $this->logger->pushProcessor($spyProcessor); } diff --git a/packages/guides-cli/src/Logger/SpyProcessor.php b/packages/guides-cli/src/Logger/SpyProcessor.php index 7a89faeff..05965fa39 100644 --- a/packages/guides-cli/src/Logger/SpyProcessor.php +++ b/packages/guides-cli/src/Logger/SpyProcessor.php @@ -13,6 +13,7 @@ namespace phpDocumentor\Guides\Cli\Logger; +use Monolog\Level; use Monolog\LogRecord; use Monolog\Processor\ProcessorInterface; use Psr\Log\LogLevel; @@ -27,9 +28,16 @@ final class SpyProcessor implements ProcessorInterface { private bool $hasBeenCalled = false; + private Level $level; - public function __construct(private string|null $level = LogLevel::WARNING) + /** @param LogLevel::* $level */ + public function __construct(string|null $level = LogLevel::WARNING) { + if ($level === null) { + $level = LogLevel::WARNING; + } + + $this->level = Level::fromName(strtolower($level)); } public function hasBeenCalled(): bool @@ -37,9 +45,9 @@ public function hasBeenCalled(): bool return $this->hasBeenCalled; } - public function __invoke(array|LogRecord $record): array|LogRecord + public function __invoke(LogRecord $record): LogRecord { - if (strtolower($record['level_name']) === $this->level) { + if ($this->level->includes($record->level)) { $this->hasBeenCalled = true; } diff --git a/packages/guides-cli/tests/unit/Logger/SpyProcessorTest.php b/packages/guides-cli/tests/unit/Logger/SpyProcessorTest.php index 4ead9c969..112240798 100644 --- a/packages/guides-cli/tests/unit/Logger/SpyProcessorTest.php +++ b/packages/guides-cli/tests/unit/Logger/SpyProcessorTest.php @@ -13,7 +13,11 @@ namespace phpDocumentor\Guides\Cli\Logger; +use DateTimeImmutable; +use Monolog\Level; +use Monolog\LogRecord; use PHPUnit\Framework\TestCase; +use Psr\Log\LogLevel; final class SpyProcessorTest extends TestCase { @@ -27,7 +31,21 @@ public function testHasBeenCalledReturnsFalseByDefault(): void public function testItKnowsWhenALogIsEmitted(): void { $process = new SpyProcessor(); - $process(['channel' => 'test', 'level_name' => 'warning']); + $process(new LogRecord(new DateTimeImmutable(), 'test', Level::Warning, 'test message')); self::assertTrue($process->hasBeenCalled()); } + + public function testItKnowsWhenAErrorIsEmitted(): void + { + $process = new SpyProcessor(); + $process(new LogRecord(new DateTimeImmutable(), 'test', Level::Error, 'test message')); + self::assertTrue($process->hasBeenCalled()); + } + + public function testIsNotCalledWhenLevelIsTolow(): void + { + $process = new SpyProcessor(LogLevel::ERROR); + $process(new LogRecord(new DateTimeImmutable(), 'test', Level::Warning, 'test message')); + self::assertFalse($process->hasBeenCalled()); + } } diff --git a/packages/guides/src/Settings/ProjectSettings.php b/packages/guides/src/Settings/ProjectSettings.php index 2c2bc28a0..35ec5ee66 100644 --- a/packages/guides/src/Settings/ProjectSettings.php +++ b/packages/guides/src/Settings/ProjectSettings.php @@ -14,6 +14,7 @@ namespace phpDocumentor\Guides\Settings; use phpDocumentor\FileSystem\Finder\Exclude; +use Psr\Log\LogLevel; final class ProjectSettings { @@ -32,6 +33,8 @@ final class ProjectSettings /** @var string[] */ private array $outputFormats = ['html']; private string $logPath = 'php://stder'; + + /** @var LogLevel::*|null */ private string|null $failOnError = null; private bool $showProgressBar = true; private bool $linksRelative = false; @@ -135,11 +138,13 @@ public function isFailOnError(): bool return $this->failOnError !== null; } + /** @return LogLevel::* */ public function getFailOnError(): string|null { return $this->failOnError; } + /** @param LogLevel::* $logLevel */ public function setFailOnError(string $logLevel): void { $this->failOnError = $logLevel; diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d897bc64f..af4ba3b32 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -20,11 +20,6 @@ parameters: count: 6 path: packages/guides-cli/src/Command/Run.php - - - message: "#^Parameter \\#1 \\$callback of method Monolog\\\\Logger\\:\\:pushProcessor\\(\\) expects callable\\(Monolog\\\\LogRecord\\)\\: Monolog\\\\LogRecord, phpDocumentor\\\\Guides\\\\Cli\\\\Logger\\\\SpyProcessor given\\.$#" - count: 1 - path: packages/guides-cli/src/Command/Run.php - - message: "#^Parameter \\#1 \\$documents of class phpDocumentor\\\\Guides\\\\Handlers\\\\CompileDocumentsCommand constructor expects array\\, mixed given\\.$#" count: 1 @@ -65,21 +60,6 @@ parameters: count: 1 path: packages/guides-cli/src/DependencyInjection/ContainerFactory.php - - - message: "#^Method phpDocumentor\\\\Guides\\\\Cli\\\\Logger\\\\SpyProcessor\\:\\:__invoke\\(\\) has parameter \\$record with no value type specified in iterable type array\\.$#" - count: 1 - path: packages/guides-cli/src/Logger/SpyProcessor.php - - - - message: "#^Method phpDocumentor\\\\Guides\\\\Cli\\\\Logger\\\\SpyProcessor\\:\\:__invoke\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: packages/guides-cli/src/Logger/SpyProcessor.php - - - - message: "#^Return type \\(array\\|Monolog\\\\LogRecord\\) of method phpDocumentor\\\\Guides\\\\Cli\\\\Logger\\\\SpyProcessor\\:\\:__invoke\\(\\) should be covariant with return type \\(Monolog\\\\LogRecord\\) of method Monolog\\\\Processor\\\\ProcessorInterface\\:\\:__invoke\\(\\)$#" - count: 1 - path: packages/guides-cli/src/Logger/SpyProcessor.php - - message: "#^Cannot call method end\\(\\) on Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeParentInterface\\|null\\.$#" count: 1