ScopePreservingFormatter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 0
c 1
b 0
f 1
dl 0
loc 1
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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
    protected ?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 6
    public function __construct(protected 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 6
    public function __invoke(CommitCollection $commitCollection, string $version): string
46
    {
47 6
        $date = date('Y-m-d');
48 6
        $changelog = "# {$version} ({$date})\n\n";
49 6
        $sections = $commitCollection->getVisibleSections();
50 6
        foreach ($sections as $section) {
51 6
            $changelog .= sprintf("### %s\n", $section->title);
52 6
            foreach ($section->getCommits() as $commit) {
53 6
                $scope = '' === $commit->scope ? '' : $this->getScopeForCommit($commit);
54 6
                $changelog .= "- {$scope}{$commit->comment}\n";
55
            }
56 6
            $changelog .= "\n";
57
        }
58
59 6
        return $changelog;
60
    }
61
62 2
    protected function getScopeForCommit(Commit $commit): string
63
    {
64 2
        foreach ($this->preservedScopes as $scope) {
65 2
            if ($scope instanceof ScopeInterpreterInterface) {
66 1
                $result = $scope->interpret($commit->scope);
67 1
                if (null !== $result) {
68 1
                    return $result;
69
                }
70 1
            } elseif ($commit->scope === $scope) {
71 1
                $scopes = $this->config?->getScopes() ?? [];
72 1
                $scope = $scopes[$commit->scope] ?? $commit->scope;
73
74 1
                return sprintf('%s: ', $scope);
75
            }
76
        }
77
78 2
        return '';
79
    }
80
81 6
    public function setConfig(Config $config): void
82
    {
83 6
        $this->config = $config;
84
    }
85
}
86