Skip to content

Commit 1722908

Browse files
authored
Merge pull request #722 from hypervel/feature/improve-doctor
Fix false passes and stale results in Doctor diagnostics
2 parents e7a38c8 + b9f2a49 commit 1722908

13 files changed

Lines changed: 345 additions & 27 deletions

‎src/doctor/src/Diagnostics/ComposerAutoloadIsValid.php‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
namespace Hypervel\Doctor\Diagnostics;
66

7+
use Hypervel\Contracts\Process\ProcessResult;
78
use Hypervel\Doctor\Contracts\Fixable;
89
use Hypervel\Doctor\Diagnostic;
910
use Hypervel\Doctor\Results\DiagnosticResult;
1011
use Hypervel\Doctor\Results\FixResult;
1112
use Hypervel\Doctor\Results\Message;
1213
use Hypervel\Doctor\Support\Composer;
14+
use Hypervel\Doctor\Support\Details;
1315
use Hypervel\Foundation\PackageManifest;
1416
use Hypervel\Support\Composer as ComposerSupport;
1517
use Hypervel\Support\Facades\Process;
@@ -73,7 +75,7 @@ public function check(): DiagnosticResult
7375

7476
return $this->fail('invalid')
7577
->fixable()
76-
->withDetails(trim($process->errorOutput() !== '' ? $process->errorOutput() : $process->output()));
78+
->withDetails($this->failureDetails($process));
7779
}
7880

7981
/**
@@ -92,12 +94,28 @@ public function fix(DiagnosticResult $result, ?string $option = null): FixResult
9294

9395
if (! $process->successful()) {
9496
return $this->fixFailed('regeneration-failed')
95-
->withDetails(trim($process->errorOutput() !== '' ? $process->errorOutput() : $process->output()));
97+
->withDetails($this->failureDetails($process));
9698
}
9799

98100
return $this->fixed('regenerated');
99101
}
100102

103+
/**
104+
* Get the failure details for a Composer process.
105+
*/
106+
private function failureDetails(ProcessResult $process): string
107+
{
108+
$exitCode = $process->exitCode();
109+
110+
return Details::processOutput(
111+
$process->output(),
112+
$process->errorOutput(),
113+
$exitCode === null
114+
? 'Composer failed without output or an exit code.'
115+
: "Composer exited with code {$exitCode} without output.",
116+
);
117+
}
118+
101119
/**
102120
* Get the optimization option that preserves the current autoloader mode.
103121
*/

‎src/doctor/src/Diagnostics/QueueConnectionIsReachable.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ private function probeRedisQueue(array $configuration): void
139139
{
140140
$connection = $configuration['connection'] ?? 'default';
141141

142-
Redis::connection($connection)->ping();
142+
// phpredis returns false instead of throwing for ERR replies such as "max number of clients reached".
143+
if (Redis::connection($connection)->ping() === false) {
144+
throw new RuntimeException("Redis connection [{$connection}] returned an error for PING.");
145+
}
143146
}
144147

145148
/**

‎src/doctor/src/Diagnostics/RedisConnectionsAreReachable.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ public function check(): DiagnosticResult
5252

5353
foreach ($connections as $connection) {
5454
try {
55-
Redis::connection($connection)->ping();
55+
// phpredis returns false instead of throwing for ERR replies such as "max number of clients reached".
56+
if (Redis::connection($connection)->ping() === false) {
57+
$failures[$connection] = 'The Redis server returned an error for PING.';
58+
}
5659
} catch (Throwable $e) {
5760
if (Coroutine::causedByCancellation($e)) {
5861
throw $e;

‎src/doctor/src/Diagnostics/RequiredConfigurationValuesAreSet.php‎

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,37 @@ private function missingValues(): array
6060
$required = $this->requiredValues();
6161

6262
return [
63+
...$this->missingDefaultValues(),
6364
...array_intersect_key($required, array_flip(Configured::missing(array_keys($required)))),
6465
...$this->missingMercureValues(),
6566
...$this->missingMailValues(),
6667
];
6768
}
6869

70+
/**
71+
* Get default driver keys without a usable value, mapped to the feature that needs them.
72+
*
73+
* The managers read these keys with typed getters, so a blank or null
74+
* value fails at runtime instead of falling back to a default driver.
75+
*
76+
* @return array<string, string>
77+
*/
78+
private function missingDefaultValues(): array
79+
{
80+
$defaults = [
81+
'broadcasting.default' => 'default broadcaster',
82+
'filesystems.default' => 'default filesystem disk',
83+
'mail.default' => 'default mailer',
84+
'queue.default' => 'default queue connection',
85+
];
86+
87+
return array_filter(
88+
$defaults,
89+
static fn (string $key): bool => Configured::string($key) === null,
90+
ARRAY_FILTER_USE_KEY,
91+
);
92+
}
93+
6994
/**
7095
* Get configuration keys required by the active drivers, mapped to the feature that needs them.
7196
*
@@ -88,7 +113,12 @@ private function requiredValues(): array
88113
*/
89114
private function broadcastingValues(): array
90115
{
91-
$connection = Configured::string('broadcasting.default', 'null');
116+
$connection = Configured::string('broadcasting.default');
117+
118+
if ($connection === null) {
119+
return [];
120+
}
121+
92122
$driver = Configured::string("broadcasting.connections.{$connection}.driver", $connection);
93123

94124
$keys = match ($driver) {
@@ -107,7 +137,12 @@ private function broadcastingValues(): array
107137
*/
108138
private function missingMercureValues(): array
109139
{
110-
$connection = Configured::string('broadcasting.default', 'null');
140+
$connection = Configured::string('broadcasting.default');
141+
142+
if ($connection === null) {
143+
return [];
144+
}
145+
111146
$prefix = "broadcasting.connections.{$connection}";
112147

113148
if (Configured::string("{$prefix}.driver", $connection) !== 'mercure') {
@@ -142,9 +177,9 @@ private function missingMercureValues(): array
142177
*/
143178
private function filesystemValues(): array
144179
{
145-
$disk = Configured::string('filesystems.default', 'local');
180+
$disk = Configured::string('filesystems.default');
146181

147-
if (Configured::string("filesystems.disks.{$disk}.driver", $disk) !== 's3') {
182+
if ($disk === null || Configured::string("filesystems.disks.{$disk}.driver", $disk) !== 's3') {
148183
return [];
149184
}
150185

@@ -158,9 +193,16 @@ private function filesystemValues(): array
158193
*/
159194
private function loggingValues(): array
160195
{
196+
$default = Configured::string('logging.default');
197+
198+
// The default log channel is optional; without one, LogManager writes to its emergency logger.
199+
if ($default === null) {
200+
return [];
201+
}
202+
161203
$values = [];
162204

163-
foreach (ActiveDrivers::logChannels(Configured::string('logging.default', 'stack')) as $channel) {
205+
foreach (ActiveDrivers::logChannels($default) as $channel) {
164206
$values = [...$values, ...$this->logChannelValues($channel)];
165207
}
166208

@@ -206,9 +248,15 @@ private function logChannelValues(string $channel): array
206248
*/
207249
private function missingMailValues(): array
208250
{
251+
$default = Configured::string('mail.default');
252+
253+
if ($default === null) {
254+
return [];
255+
}
256+
209257
$values = [];
210258

211-
foreach (ActiveDrivers::mailers(Configured::string('mail.default', 'log')) as $mailer) {
259+
foreach (ActiveDrivers::mailers($default) as $mailer) {
212260
$values = [...$values, ...$this->missingMailerValues($mailer)];
213261
}
214262

@@ -266,7 +314,12 @@ private function missingMailerValues(string $mailer): array
266314
*/
267315
private function queueValues(): array
268316
{
269-
$connection = Configured::string('queue.default', 'database');
317+
$connection = Configured::string('queue.default');
318+
319+
if ($connection === null) {
320+
return [];
321+
}
322+
270323
$values = [];
271324

272325
foreach (ActiveDrivers::queueConnections($connection) as $name) {

‎src/doctor/src/Diagnostics/SessionDriverIsReachable.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ private function probeDatabaseSessions(): void
116116
*/
117117
private function probeRedisSessions(): void
118118
{
119-
Redis::connection(Configured::string('session.connection', SessionManager::DEFAULT_REDIS_CONNECTION))->ping();
119+
$connection = Configured::string('session.connection', SessionManager::DEFAULT_REDIS_CONNECTION);
120+
121+
// phpredis returns false instead of throwing for ERR replies such as "max number of clients reached".
122+
if (Redis::connection($connection)->ping() === false) {
123+
throw new RuntimeException("Redis connection [{$connection}] returned an error for PING.");
124+
}
120125
}
121126

122127
/**

‎src/doctor/src/Support/ComposerJson.php‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99

1010
class ComposerJson
1111
{
12-
/**
13-
* The decoded composer.json payload.
14-
*
15-
* @var null|array<string, mixed>
16-
*/
17-
private ?array $contents = null;
18-
1912
/**
2013
* Determine whether the application has a composer.json file.
2114
*/
@@ -41,9 +34,11 @@ public function phpConstraint(): ?string
4134
*/
4235
public function requiredExtensions(): array
4336
{
37+
$contents = $this->contents() ?? [];
38+
4439
return $this->extensionsFrom([
45-
...$this->packageNames(Arr::get($this->contents() ?? [], 'require', [])),
46-
...$this->packageNames(Arr::get($this->contents() ?? [], 'require-dev', [])),
40+
...$this->packageNames(Arr::get($contents, 'require', [])),
41+
...$this->packageNames(Arr::get($contents, 'require-dev', [])),
4742
]);
4843
}
4944

@@ -62,14 +57,13 @@ public function suggestedExtensions(): array
6257
/**
6358
* Get the decoded composer.json contents.
6459
*
60+
* Read on every call because this class is auto-singletoned for the worker
61+
* lifetime, so a memoized copy would go stale between Doctor runs.
62+
*
6563
* @return null|array<string, mixed>
6664
*/
6765
public function contents(): ?array
6866
{
69-
if ($this->contents !== null) {
70-
return $this->contents;
71-
}
72-
7367
if (! $this->exists()) {
7468
return null;
7569
}
@@ -81,7 +75,7 @@ public function contents(): ?array
8175
}
8276

8377
/** @var array<string, mixed> $decoded */
84-
return $this->contents = $decoded;
78+
return $decoded;
8579
}
8680

8781
/**

‎src/doctor/src/Support/Details.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public static function failures(array $failures): string
3535
*/
3636
public static function processOutput(string $output, string $errorOutput, string $fallback): string
3737
{
38-
$output = trim($errorOutput !== '' ? $errorOutput : $output);
38+
$errorOutput = trim($errorOutput);
39+
$output = $errorOutput !== '' ? $errorOutput : trim($output);
3940

4041
return $output === '' ? $fallback : $output;
4142
}

0 commit comments

Comments
 (0)