Passed
Push — master ( 1a8193...4ba352 )
by Alexander
02:01
created

ScopePreservingFormatter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
eloc 16
c 1
b 0
f 1
dl 0
loc 48
ccs 16
cts 16
cp 1
rs 10

2 Methods

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