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

DefaultFormatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

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