Total Complexity | 8 |
Total Lines | 45 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
15 | class InvalidConfigurationException extends InvalidArgumentException |
||
16 | { |
||
17 | public function __construct(string $message = 'Invalid configuration detected.', ?string $namespace = null, ?string $key = null, ?Throwable $previous = null) |
||
18 | { |
||
19 | if ($namespace && $key) { |
||
20 | [$this->file, $this->line] = $this->findConfigLine($namespace, $key); |
||
21 | } |
||
22 | |||
23 | parent::__construct($message, previous: $previous); |
||
24 | } |
||
25 | |||
26 | /** |
||
27 | * @experimental Please report any issues with this method to the authors at https://github.com/hydephp/develop/issues |
||
28 | * |
||
29 | * @return array{string, int} |
||
|
|||
30 | */ |
||
31 | protected function findConfigLine(string $namespace, string $key): array |
||
32 | { |
||
33 | $file = realpath("config/$namespace.php"); |
||
34 | $contents = Filesystem::getContents($file); |
||
35 | $lines = explode("\n", $contents); |
||
36 | |||
37 | foreach ($lines as $line => $content) { |
||
38 | if (str_contains($content, "'$key' =>")) { |
||
39 | break; |
||
40 | } |
||
41 | } |
||
42 | |||
43 | assert($file !== false); |
||
44 | assert(isset($line)); |
||
45 | |||
46 | return [$file, $line + 1]; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @internal |
||
51 | * |
||
52 | * @experimental |
||
53 | */ |
||
54 | public static function try(callable $callback, ?string $message = null): mixed |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 |