Passed
Push — master ( 351308...878789 )
by Alexander
02:24 queued 10s
created

ScopePreservingFormatter::setConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 2
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\Commits\CommitCollection;
8
use Vasoft\VersionIncrement\Config;
9
use Vasoft\VersionIncrement\Contract\ChangelogFormatterInterface;
10
11
/**
12
 * It generates a changelog while preserving specific scopes passed to the constructor.
13
 * If no scopes are specified, all scopes are preserved.
14
 */
15
class ScopePreservingFormatter implements ChangelogFormatterInterface
16
{
17
    /**
18
     * Constructs a new ScopePreservingFormatter instance.
19
     *
20
     * @param array $preservedScopes An optional array of scopes to preserve in the changelog.
21
     *                               If empty, all scopes will be included.
22
     */
23 1
    public function __construct(private readonly array $preservedScopes = []) {}
24
25
    /**
26
     * Generates a changelog while preserving specified scopes.
27
     *
28
     * @param CommitCollection $commitCollection a collection of commits grouped into sections
29
     * @param string           $version          the version number for which the changelog is generated
30
     *
31
     * @return string The formatted changelog as a string.
32
     *
33
     * The changelog includes:
34
     * - The version number and date at the top.
35
     * - Sections with their titles.
36
     * - Commits listed under each section, with scopes preserved based on the constructor configuration.
37
     *   If a commit has a scope that matches one of the preserved scopes, it is included in the output.
38
     *   Otherwise, the scope is omitted unless no scopes are specified (all scopes are preserved).
39
     */
40 1
    public function __invoke(CommitCollection $commitCollection, string $version): string
41
    {
42 1
        $date = date('Y-m-d');
43 1
        $changelog = "# {$version} ({$date})\n\n";
44 1
        $sections = $commitCollection->getVisibleSections();
45 1
        $allScopes = empty($this->preservedScopes);
46 1
        foreach ($sections as $section) {
47 1
            $changelog .= sprintf("### %s\n", $section->title);
48 1
            foreach ($section->getCommits() as $commit) {
49 1
                if ('' !== $commit->scope
50 1
                    && ($allScopes || in_array($commit->scope, $this->preservedScopes, true))
51
                ) {
52 1
                    $scope = sprintf('%s: ', $commit->scope);
53
                } else {
54 1
                    $scope = '';
55
                }
56
57 1
                $changelog .= "- {$scope}{$commit->comment}\n";
58
            }
59 1
            $changelog .= "\n";
60
        }
61
62 1
        return $changelog;
63
    }
64
65 1
    public function setConfig(Config $config): void
66
    {
67
        // Do nothing
68 1
    }
69
}
70