Passed
Push — master ( f250ce...3ee825 )
by Alexander
12:51
created

SinglePreservedScopeInterpreter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 6
c 1
b 0
f 0
dl 0
loc 43
ccs 8
cts 8
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A interpret() 0 9 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Vasoft\VersionIncrement\Changelog\Interpreter;
6
7
use Vasoft\VersionIncrement\Contract\ScopeInterpreterInterface;
8
use Vasoft\VersionIncrement\Config;
9
10
/**
11
 * Scope interpreter that checks if a single scope is in a predefined list of preserved scopes
12
 * and formats it according to a specified template.
13
 *
14
 * This interpreter is typically used in conjunction with a formatter that handles
15
 * individual scopes (e.g., ScopePreservingFormatter) or processes a list of scopes
16
 * derived from a single commit's scope field (e.g., a future MultipleScopePreservingFormatter).
17
 *
18
 * It supports mapping the raw scope name to a human-readable title using the provided Config.
19
 * If the input scope is not in the list of preserved scopes, or if the mapped title is empty,
20
 * it returns null, indicating the scope should not be included or formatted.
21
 */
22
class SinglePreservedScopeInterpreter implements ScopeInterpreterInterface
23
{
24
    /**
25
     * Constructs a new SinglePreservedScopeInterpreter instance.
26
     *
27
     * @param array<string> $preservedScopes An array of scope names that are allowed to be processed and formatted.
28
     *                                       Only scopes present in this list will be handled.
29
     * @param Config        $config          the configuration object, used to retrieve human-readable titles for scope names
30
     * @param string        $template        The template used to format the scope name. Defaults to '%s :'.
31
     *                                       The '%s' placeholder will be replaced by the processed scope string
32
     *                                       (either the mapped title from config or the raw scope name if no mapping exists).
33
     *                                       Note: The template itself is responsible for adding any desired separators
34
     *                                       (e.g., ':', ' - '). The example '%s :' will result in 'scope_name :'.
35
     */
36 3
    public function __construct(
37
        private readonly array $preservedScopes,
38
        private readonly Config $config,
39
        private readonly string $template = '%s: ',
40 3
    ) {}
41
42
    /**
43
     * Interprets a single scope string.
44
     *
45
     * Checks if the provided scope is in the list of preserved scopes.
46
     * If yes, it attempts to map the scope name to a human-readable title using the config.
47
     * Then, it formats the resulting title using the configured template.
48
     * If the scope is not preserved or the final formatted string would be empty,
49
     * it returns null.
50
     *
51
     * @param string $scope the raw scope string to interpret
52
     *
53
     * @return null|string The formatted scope string (e.g., 'api :') if the scope is preserved and non-empty,
54
     *                     or null if the scope is not in the preserved list or results in an empty formatted string.
55
     */
56 7
    public function interpret(string $scope): ?string
57
    {
58 7
        if (!in_array($scope, $this->preservedScopes, true)) {
59 4
            return null;
60
        }
61 7
        $scopes = $this->config->getScopes();
62 7
        $scopeFormatted = $scopes[$scope] ?? $scope;
63
64 7
        return '' === $scopeFormatted ? '' : sprintf($this->template, $scopeFormatted);
65
    }
66
}
67