ScopePreservingFormatter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
eloc 25
c 1
b 0
f 1
dl 0
loc 70
ccs 27
cts 27
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A __invoke() 0 15 3
A getScopeForCommit() 0 21 6
A setConfig() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Vasoft\VersionIncrement\Changelog;
6
7
use Vasoft\VersionIncrement\Changelog\Interpreter\RegexpScopeInterpreter;
8
use Vasoft\VersionIncrement\Commits\Commit;
9
use Vasoft\VersionIncrement\Commits\CommitCollection;
10
use Vasoft\VersionIncrement\Config;
11
use Vasoft\VersionIncrement\Contract\ChangelogFormatterInterface;
12
use Vasoft\VersionIncrement\Contract\ScopeInterpreterInterface;
13
14
/**
15
 * It generates a changelog while preserving specific scopes passed to the constructor.
16
 * If no scopes are specified, all scopes are preserved.
17
 */
18
class ScopePreservingFormatter implements ChangelogFormatterInterface
19
{
20
    private ?Config $config = null;
21
22
    /**
23
     * Constructs a new ScopePreservingFormatter instance.
24
     *
25
     * @param array<RegexpScopeInterpreter|string> $preservedScopes An optional array of scopes to preserve in the changelog.
26
     *                                                              If empty, all scopes will be included.
27
     */
28 2
    public function __construct(private readonly array $preservedScopes = []) {}
29
30
    /**
31
     * Generates a changelog while preserving specified scopes.
32
     *
33
     * @param CommitCollection $commitCollection a collection of commits grouped into sections
34
     * @param string           $version          the version number for which the changelog is generated
35
     *
36
     * @return string The formatted changelog as a string.
37
     *
38
     * The changelog includes:
39
     * - The version number and date at the top.
40
     * - Sections with their titles.
41
     * - Commits listed under each section, with scopes preserved based on the constructor configuration.
42
     *   If a commit has a scope that matches one of the preserved scopes, it is included in the output.
43
     *   Otherwise, the scope is omitted unless no scopes are specified (all scopes are preserved).
44
     */
45 2
    public function __invoke(CommitCollection $commitCollection, string $version): string
46
    {
47 2
        $date = date('Y-m-d');
48 2
        $changelog = "# {$version} ({$date})\n\n";
49 2
        $sections = $commitCollection->getVisibleSections();
50 2
        foreach ($sections as $section) {
51 2
            $changelog .= sprintf("### %s\n", $section->title);
52 2
            foreach ($section->getCommits() as $commit) {
53 2
                $scope = $this->getScopeForCommit($commit);
54 2
                $changelog .= "- {$scope}{$commit->comment}\n";
55
            }
56 2
            $changelog .= "\n";
57
        }
58
59 2
        return $changelog;
60
    }
61
62 2
    private function getScopeForCommit(Commit $commit): string
63
    {
64 2
        if ('' === $commit->scope) {
65 2
            return '';
66
        }
67
68 2
        foreach ($this->preservedScopes as $scope) {
69 2
            if ($scope instanceof ScopeInterpreterInterface) {
70 1
                $result = $scope->interpret($commit->scope);
71 1
                if (null !== $result) {
72 1
                    return $result;
73
                }
74 2
            } elseif ($commit->scope === $scope) {
75 2
                $scopes = $this->config?->getScopes() ?? [];
76 2
                $scope = $scopes[$commit->scope] ?? $commit->scope;
77
78 2
                return sprintf('%s: ', $scope);
79
            }
80
        }
81
82 2
        return '';
83
    }
84
85 2
    public function setConfig(Config $config): void
86
    {
87 2
        $this->config = $config;
88
    }
89
}
90