|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Vasoft\VersionIncrement\Changelog; |
|
6
|
|
|
|
|
7
|
|
|
use Vasoft\VersionIncrement\Commits\Commit; |
|
8
|
|
|
use Vasoft\VersionIncrement\Commits\CommitCollection; |
|
9
|
|
|
use Vasoft\VersionIncrement\Config; |
|
10
|
|
|
use Vasoft\VersionIncrement\Contract\ChangelogFormatterInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* It generates a changelog while preserving specific scopes passed to the constructor. |
|
14
|
|
|
* If no scopes are specified, all scopes are preserved. |
|
15
|
|
|
*/ |
|
16
|
|
|
class ScopePreservingFormatter implements ChangelogFormatterInterface |
|
17
|
|
|
{ |
|
18
|
|
|
private ?Config $config = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Constructs a new ScopePreservingFormatter instance. |
|
22
|
|
|
* |
|
23
|
|
|
* @param array $preservedScopes An optional array of scopes to preserve in the changelog. |
|
24
|
|
|
* If empty, all scopes will be included. |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function __construct(private readonly array $preservedScopes = []) {} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Generates a changelog while preserving specified scopes. |
|
30
|
|
|
* |
|
31
|
|
|
* @param CommitCollection $commitCollection a collection of commits grouped into sections |
|
32
|
|
|
* @param string $version the version number for which the changelog is generated |
|
33
|
|
|
* |
|
34
|
|
|
* @return string The formatted changelog as a string. |
|
35
|
|
|
* |
|
36
|
|
|
* The changelog includes: |
|
37
|
|
|
* - The version number and date at the top. |
|
38
|
|
|
* - Sections with their titles. |
|
39
|
|
|
* - Commits listed under each section, with scopes preserved based on the constructor configuration. |
|
40
|
|
|
* If a commit has a scope that matches one of the preserved scopes, it is included in the output. |
|
41
|
|
|
* Otherwise, the scope is omitted unless no scopes are specified (all scopes are preserved). |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function __invoke(CommitCollection $commitCollection, string $version): string |
|
44
|
|
|
{ |
|
45
|
2 |
|
$date = date('Y-m-d'); |
|
46
|
2 |
|
$changelog = "# {$version} ({$date})\n\n"; |
|
47
|
2 |
|
$sections = $commitCollection->getVisibleSections(); |
|
48
|
2 |
|
foreach ($sections as $section) { |
|
49
|
2 |
|
$changelog .= sprintf("### %s\n", $section->title); |
|
50
|
2 |
|
foreach ($section->getCommits() as $commit) { |
|
51
|
2 |
|
$scope = $this->getScopeForCommit($commit); |
|
52
|
2 |
|
$changelog .= "- {$scope}{$commit->comment}\n"; |
|
53
|
|
|
} |
|
54
|
2 |
|
$changelog .= "\n"; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
2 |
|
return $changelog; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
private function getScopeForCommit(Commit $commit): string |
|
61
|
|
|
{ |
|
62
|
|
|
if ( |
|
63
|
2 |
|
'' === $commit->scope |
|
64
|
2 |
|
|| (!empty($this->preservedScopes) && !in_array($commit->scope, $this->preservedScopes, true)) |
|
65
|
|
|
) { |
|
66
|
2 |
|
return ''; |
|
67
|
|
|
} |
|
68
|
2 |
|
$scopes = $this->config->getScopes(); |
|
|
|
|
|
|
69
|
2 |
|
$scope = $scopes[$commit->scope] ?? $commit->scope; |
|
70
|
|
|
|
|
71
|
2 |
|
return sprintf('%s: ', $scope); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
2 |
|
public function setConfig(Config $config): void |
|
75
|
|
|
{ |
|
76
|
2 |
|
$this->config = $config; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.