Skip to content

[BUGFIX] Check error level is higher #1233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/guides-cli/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/guides-cli/src/Command/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
14 changes: 11 additions & 3 deletions packages/guides-cli/src/Logger/SpyProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace phpDocumentor\Guides\Cli\Logger;

use Monolog\Level;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
use Psr\Log\LogLevel;
Expand All @@ -27,19 +28,26 @@
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
{
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;
}

Expand Down
20 changes: 19 additions & 1 deletion packages/guides-cli/tests/unit/Logger/SpyProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
}
}
5 changes: 5 additions & 0 deletions packages/guides/src/Settings/ProjectSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace phpDocumentor\Guides\Settings;

use phpDocumentor\FileSystem\Finder\Exclude;
use Psr\Log\LogLevel;

final class ProjectSettings
{
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 0 additions & 20 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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\\<phpDocumentor\\\\Guides\\\\Nodes\\\\DocumentNode\\>, mixed given\\.$#"
count: 1
Expand Down Expand Up @@ -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
Expand Down
Loading