Skip to content

Commit 85345ac

Browse files
authored
Merge pull request #1233 from phpDocumentor/fix/error-reporting
[BUGFIX] Check error level is higher
2 parents 2198f48 + 0207ce4 commit 85345ac

File tree

6 files changed

+37
-26
lines changed

6 files changed

+37
-26
lines changed

packages/guides-cli/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
],
2727
"require": {
2828
"php": "^8.1",
29-
"monolog/monolog": "^2.9 || ^3.0",
29+
"monolog/monolog": "^3.0",
3030
"phpdocumentor/guides": "^1.0 || ^2.0",
3131
"phpdocumentor/guides-restructured-text": "^1.0 || ^2.0",
3232
"symfony/config": "^5.4 || ^6.3 || ^7.0",

packages/guides-cli/src/Command/Run.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
256256
}
257257

258258
if ($settings->isFailOnError()) {
259-
$spyProcessor = new SpyProcessor($settings->getFailOnError());
259+
$spyProcessor = new SpyProcessor($settings->getFailOnError() ?? LogLevel::WARNING);
260260
$this->logger->pushProcessor($spyProcessor);
261261
}
262262

packages/guides-cli/src/Logger/SpyProcessor.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace phpDocumentor\Guides\Cli\Logger;
1515

16+
use Monolog\Level;
1617
use Monolog\LogRecord;
1718
use Monolog\Processor\ProcessorInterface;
1819
use Psr\Log\LogLevel;
@@ -27,19 +28,26 @@
2728
final class SpyProcessor implements ProcessorInterface
2829
{
2930
private bool $hasBeenCalled = false;
31+
private Level $level;
3032

31-
public function __construct(private string|null $level = LogLevel::WARNING)
33+
/** @param LogLevel::* $level */
34+
public function __construct(string|null $level = LogLevel::WARNING)
3235
{
36+
if ($level === null) {
37+
$level = LogLevel::WARNING;
38+
}
39+
40+
$this->level = Level::fromName(strtolower($level));
3341
}
3442

3543
public function hasBeenCalled(): bool
3644
{
3745
return $this->hasBeenCalled;
3846
}
3947

40-
public function __invoke(array|LogRecord $record): array|LogRecord
48+
public function __invoke(LogRecord $record): LogRecord
4149
{
42-
if (strtolower($record['level_name']) === $this->level) {
50+
if ($this->level->includes($record->level)) {
4351
$this->hasBeenCalled = true;
4452
}
4553

packages/guides-cli/tests/unit/Logger/SpyProcessorTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
namespace phpDocumentor\Guides\Cli\Logger;
1515

16+
use DateTimeImmutable;
17+
use Monolog\Level;
18+
use Monolog\LogRecord;
1619
use PHPUnit\Framework\TestCase;
20+
use Psr\Log\LogLevel;
1721

1822
final class SpyProcessorTest extends TestCase
1923
{
@@ -27,7 +31,21 @@ public function testHasBeenCalledReturnsFalseByDefault(): void
2731
public function testItKnowsWhenALogIsEmitted(): void
2832
{
2933
$process = new SpyProcessor();
30-
$process(['channel' => 'test', 'level_name' => 'warning']);
34+
$process(new LogRecord(new DateTimeImmutable(), 'test', Level::Warning, 'test message'));
3135
self::assertTrue($process->hasBeenCalled());
3236
}
37+
38+
public function testItKnowsWhenAErrorIsEmitted(): void
39+
{
40+
$process = new SpyProcessor();
41+
$process(new LogRecord(new DateTimeImmutable(), 'test', Level::Error, 'test message'));
42+
self::assertTrue($process->hasBeenCalled());
43+
}
44+
45+
public function testIsNotCalledWhenLevelIsTolow(): void
46+
{
47+
$process = new SpyProcessor(LogLevel::ERROR);
48+
$process(new LogRecord(new DateTimeImmutable(), 'test', Level::Warning, 'test message'));
49+
self::assertFalse($process->hasBeenCalled());
50+
}
3351
}

packages/guides/src/Settings/ProjectSettings.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
namespace phpDocumentor\Guides\Settings;
1515

1616
use phpDocumentor\FileSystem\Finder\Exclude;
17+
use Psr\Log\LogLevel;
1718

1819
final class ProjectSettings
1920
{
@@ -32,6 +33,8 @@ final class ProjectSettings
3233
/** @var string[] */
3334
private array $outputFormats = ['html'];
3435
private string $logPath = 'php://stder';
36+
37+
/** @var LogLevel::*|null */
3538
private string|null $failOnError = null;
3639
private bool $showProgressBar = true;
3740
private bool $linksRelative = false;
@@ -135,11 +138,13 @@ public function isFailOnError(): bool
135138
return $this->failOnError !== null;
136139
}
137140

141+
/** @return LogLevel::* */
138142
public function getFailOnError(): string|null
139143
{
140144
return $this->failOnError;
141145
}
142146

147+
/** @param LogLevel::* $logLevel */
143148
public function setFailOnError(string $logLevel): void
144149
{
145150
$this->failOnError = $logLevel;

phpstan-baseline.neon

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ parameters:
2020
count: 6
2121
path: packages/guides-cli/src/Command/Run.php
2222

23-
-
24-
message: "#^Parameter \\#1 \\$callback of method Monolog\\\\Logger\\:\\:pushProcessor\\(\\) expects callable\\(Monolog\\\\LogRecord\\)\\: Monolog\\\\LogRecord, phpDocumentor\\\\Guides\\\\Cli\\\\Logger\\\\SpyProcessor given\\.$#"
25-
count: 1
26-
path: packages/guides-cli/src/Command/Run.php
27-
2823
-
2924
message: "#^Parameter \\#1 \\$documents of class phpDocumentor\\\\Guides\\\\Handlers\\\\CompileDocumentsCommand constructor expects array\\<phpDocumentor\\\\Guides\\\\Nodes\\\\DocumentNode\\>, mixed given\\.$#"
3025
count: 1
@@ -65,21 +60,6 @@ parameters:
6560
count: 1
6661
path: packages/guides-cli/src/DependencyInjection/ContainerFactory.php
6762

68-
-
69-
message: "#^Method phpDocumentor\\\\Guides\\\\Cli\\\\Logger\\\\SpyProcessor\\:\\:__invoke\\(\\) has parameter \\$record with no value type specified in iterable type array\\.$#"
70-
count: 1
71-
path: packages/guides-cli/src/Logger/SpyProcessor.php
72-
73-
-
74-
message: "#^Method phpDocumentor\\\\Guides\\\\Cli\\\\Logger\\\\SpyProcessor\\:\\:__invoke\\(\\) return type has no value type specified in iterable type array\\.$#"
75-
count: 1
76-
path: packages/guides-cli/src/Logger/SpyProcessor.php
77-
78-
-
79-
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\\(\\)$#"
80-
count: 1
81-
path: packages/guides-cli/src/Logger/SpyProcessor.php
82-
8363
-
8464
message: "#^Cannot call method end\\(\\) on Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\NodeParentInterface\\|null\\.$#"
8565
count: 1

0 commit comments

Comments
 (0)