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

DefaultFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 10
c 1
b 0
f 1
dl 0
loc 29
ccs 10
cts 10
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 14 3
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 in a default format by iterating through visible sections and their commits.
12
 * This formatter does not preserve or filter scopes; it simply lists all commits under their respective sections.
13
 */
14
final class DefaultFormatter implements ChangelogFormatterInterface
15
{
16
    /**
17
     * Generates a changelog in the default format.
18
     *
19
     * @param CommitCollection $commitCollection a collection of commits grouped into sections
20
     * @param string           $version          the version number for which the changelog is generated
21
     *
22
     * @return string The formatted changelog as a string.
23
     *
24
     * The changelog includes:
25
     * - The version number and date at the top.
26
     * - Sections with their titles.
27
     * - Commits listed under each section without preserving scopes.
28
     */
29 14
    public function __invoke(CommitCollection $commitCollection, string $version): string
30
    {
31 14
        $date = date('Y-m-d');
32 14
        $changelog = "# {$version} ({$date})\n\n";
33 14
        $sections = $commitCollection->getVisibleSections();
34 14
        foreach ($sections as $section) {
35 14
            $changelog .= sprintf("### %s\n", $section->title);
36 14
            foreach ($section->getCommits() as $commit) {
37 14
                $changelog .= "- {$commit->comment}\n";
38
            }
39 14
            $changelog .= "\n";
40
        }
41
42 14
        return $changelog;
43
    }
44
}
45