• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / config / 13066327659

29 Jan 2025 11:27PM UTC coverage: 100.0%. Remained the same
13066327659

push

github

natanfelles
Add debug icon

311 of 311 relevant lines covered (100.0%)

3.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Parsers/Parser.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Config Library.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Framework\Config\Parsers;
11

12
use Closure;
13
use JetBrains\PhpStorm\Pure;
14
use SensitiveParameter;
15

16
/**
17
 * Class Parser.
18
 *
19
 * @package config
20
 */
21
abstract class Parser
22
{
23
    /**
24
     * Parse the config output.
25
     *
26
     * @param mixed $config
27
     *
28
     * @throws ParserException If config is invalid or data can not be parsed
29
     *
30
     * @return array<int|string,mixed> The parsed configs
31
     */
32
    abstract public static function parse(mixed $config) : array;
33

34
    /**
35
     * @param Closure $function
36
     *
37
     * @throws ParserException If config data can not be parsed
38
     *
39
     * @return array<int|string,mixed>
40
     */
41
    protected static function parseOrThrow(Closure $function) : array
42
    {
43
        \set_error_handler(static function ($severity, $message, $file, $line) : void {
11✔
44
            $message = static::class . ': ' . $message;
3✔
45
            throw new ParserException($message, $severity, $severity, $file, $line);
3✔
46
        });
11✔
47
        $result = $function();
11✔
48
        \restore_error_handler();
6✔
49
        return $result;
6✔
50
    }
51

52
    /**
53
     * Check for config issues.
54
     *
55
     * @param mixed $config The parser configuration
56
     *
57
     * @throws ParserException If config is invalid
58
     */
59
    protected static function checkConfig(#[SensitiveParameter] mixed $config) : void
60
    {
61
        if (!\is_string($config)) {
12✔
62
            throw new ParserException(static::class . ' config must be a string');
1✔
63
        }
64
        $file = \realpath($config);
11✔
65
        if ($file === false || !\is_file($file)) {
11✔
66
            throw new ParserException('File not found: ' . $config);
1✔
67
        }
68
        if (!\is_readable($file)) {
10✔
69
            throw new ParserException('File is not readable: ' . $config);
1✔
70
        }
71
    }
72

73
    /**
74
     * Recursively adds childs to an array tree.
75
     *
76
     * @param array<int|string,mixed> $parent The main array, where the childs will be added
77
     * @param array<int|string,mixed> $childs Childs to add
78
     * @param mixed $value The last child value
79
     */
80
    protected static function addChild(array &$parent, array $childs, mixed $value) : void
81
    {
82
        $key = \array_shift($childs);
3✔
83
        $parent[$key] = [];
3✔
84
        if ($childs === []) {
3✔
85
            $parent[$key] = $value;
3✔
86
            return;
3✔
87
        }
88
        static::addChild($parent[$key], $childs, $value);
3✔
89
    }
90

91
    /**
92
     * Interprets a string value and returns it with a PHP type.
93
     *
94
     * @param string $value The input value
95
     *
96
     * @return array<int|string,mixed>|bool|float|int|string|null The output value
97
     */
98
    #[Pure]
99
    protected static function getValue(string $value) : array | bool | float | int | string | null
100
    {
101
        $value = \trim($value);
3✔
102
        $lowerValue = \strtolower($value);
3✔
103
        if ($lowerValue === 'true') {
3✔
104
            return true;
3✔
105
        }
106
        if ($lowerValue === 'false') {
3✔
107
            return false;
3✔
108
        }
109
        if ($lowerValue === 'null') {
3✔
110
            return null;
3✔
111
        }
112
        if (\is_numeric($value) && $value >= \PHP_INT_MIN && $value <= \PHP_INT_MAX) {
3✔
113
            return \str_contains($value, '.') ? (float) $value : (int) $value;
3✔
114
        }
115
        if (\str_starts_with($value, '"') && \str_ends_with($value, '"')) {
3✔
116
            $value = \substr($value, 1, -1);
2✔
117
            return \strtr($value, [
2✔
118
                '\"' => '"',
2✔
119
                '\\\\' => '\\',
2✔
120
            ]);
2✔
121
        }
122
        if (\str_starts_with($value, "'") && \str_ends_with($value, "'")) {
3✔
123
            return \substr($value, 1, -1);
3✔
124
        }
125
        return $value;
3✔
126
    }
127

128
    /**
129
     * Sort arrays by keys recursively.
130
     *
131
     * @param mixed $value The input value
132
     *
133
     * @return mixed The output value (sorted by keys if the $value is an array)
134
     */
135
    protected static function ksortRecursive(mixed $value) : mixed
136
    {
137
        if (!\is_array($value)) {
5✔
138
            return $value;
5✔
139
        }
140
        \ksort($value);
5✔
141
        foreach ($value as &$val) {
5✔
142
            $val = static::ksortRecursive($val);
5✔
143
        }
144
        return $value;
5✔
145
    }
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc