Passed
Push — master ( 3ea542...55f4cf )
by Caen
13:20 queued 13s
created

BuildWarnings::hasWarnings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 2
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support;
6
7
use Hyde\Facades\Config;
8
use Hyde\Framework\Exceptions\BuildWarning;
9
use Illuminate\Contracts\Debug\ExceptionHandler;
10
use Symfony\Component\Console\Style\OutputStyle;
11
use function app;
12
use function sprintf;
13
14
/**
15
 * @experimental
16
 *
17
 * @see \Hyde\Framework\Testing\Unit\BuildWarningsTest
18
 */
19
class BuildWarnings
20
{
21
    /** @var array<\Hyde\Framework\Exceptions\BuildWarning> */
22
    protected array $warnings = [];
23
24
    public static function getInstance(): static
25
    {
26
        if (! app()->bound(self::class)) {
27
            app()->singleton(self::class);
28
        }
29
30
        return app(self::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return app(self::class) could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the type-hinted return Hyde\Support\BuildWarnings. Consider adding an additional type-check to rule them out.
Loading history...
31
    }
32
33
    public static function report(BuildWarning|string $warning): void
34
    {
35
        static::getInstance()->warnings[] = $warning instanceof BuildWarning ? $warning : new BuildWarning($warning);
36
    }
37
38
    /** @return array<\Hyde\Framework\Exceptions\BuildWarning> */
39
    public static function getWarnings(): array
40
    {
41
        return static::getInstance()->warnings;
42
    }
43
44
    public static function hasWarnings(): bool
45
    {
46
        return count(static::getInstance()->warnings) > 0;
47
    }
48
49
    public static function reportsWarnings(): bool
50
    {
51
        return Config::getBool('hyde.log_warnings', true);
52
    }
53
54
    public static function reportsWarningsAsExceptions(): bool
55
    {
56
        return Config::getBool('hyde.convert_build_warnings_to_exceptions', false);
57
    }
58
59
    public static function writeWarningsToOutput(OutputStyle $output, bool $verbose = false): void
60
    {
61
        if (static::reportsWarningsAsExceptions()) {
62
            self::renderWarningsAsExceptions($output);
63
        } else {
64
            self::renderWarnings($output, $verbose);
65
        }
66
    }
67
68
    protected static function renderWarnings(OutputStyle $output, bool $verbose): void
69
    {
70
        foreach (static::getWarnings() as $number => $warning) {
71
            $output->writeln(sprintf(' %s. <comment>%s</comment>', $number + 1, $warning->getMessage()));
72
            if ($verbose) {
73
                $output->writeln(sprintf('    <fg=gray>%s:%s</>', $warning->getFile(), $warning->getLine()));
74
            }
75
        }
76
    }
77
78
    protected static function renderWarningsAsExceptions(OutputStyle $output): void
79
    {
80
        foreach (static::getWarnings() as $warning) {
81
            app(ExceptionHandler::class)->renderForConsole($output, $warning);
82
        }
83
    }
84
}
85