Passed
Pull Request — master (#71)
by Cees-Jan
02:05
created

IssueGrouper::generateIssueGroupName()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 2
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ChangelogGenerator;
6
7
use function array_filter;
8
use function array_intersect;
9
use function count;
10
use function implode;
11
use function strpos;
12
13
class IssueGrouper
14
{
15
    /**
16
     * @param Issue[] $issues
17
     *
18
     * @return IssueGroup[]
19
     */
20 4
    public function groupIssues(array $issues, ChangelogConfig $changelogConfig): array
21
    {
22 4
        $this->linkIssues($issues);
23
24 4
        return $this->groupIssuesByLabels($issues, $changelogConfig);
25
    }
26
27
    /**
28
     * @param Issue[] $issues
29
     *
30
     * @return IssueGroup[]
31
     */
32 4
    private function groupIssuesByLabels(array $issues, ChangelogConfig $changelogConfig): array
33
    {
34 4
        $issueGroups = [];
35
36 4
        foreach ($this->getIssuesToGroup($issues) as $issue) {
37 4
            $groupName = $this->generateIssueGroupName($issue, $changelogConfig);
38
39 4
            if (! isset($issueGroups[$groupName])) {
40 4
                $issueGroups[$groupName] = new IssueGroup($groupName);
41
            }
42
43 4
            $issueGroups[$groupName]->addIssue($issue);
44
        }
45
46 4
        return $issueGroups;
47
    }
48
49 4
    private function generateIssueGroupName(Issue $issue, ChangelogConfig $changelogConfig): string
50
    {
51 4
        $labelFilters = $changelogConfig->getLabels();
52
53 4
        if (count($labelFilters) === 0) {
54 2
            $labels = $issue->getLabels();
55
        } else {
56 2
            $labels = array_intersect($issue->getLabels(), $labelFilters);
57
        }
58
59 4
        if ($changelogConfig->getNonGroupedLabel() !== null && count($labels) === 0) {
60 2
            $labels = [$changelogConfig->getNonGroupedLabel()];
61
        }
62
63 4
        return implode(',', $labels);
64
    }
65
66
    /**
67
     * @param Issue[] $issues
68
     *
69
     * @return Issue[]
70
     */
71 4
    private function getIssuesToGroup(array $issues): array
72
    {
73 4
        return array_filter($issues, static function (Issue $issue): bool {
74 4
            return (! $issue->isPullRequest() && $issue->getLinkedPullRequest() !== null) === false;
75 4
        });
76
    }
77
78
    /**
79
     * @param Issue[] $issues
80
     */
81 4
    private function linkIssues(array $issues): void
82
    {
83 4
        foreach ($issues as $issue) {
84 4
            if (! $issue->isPullRequest()) {
85 4
                continue;
86
            }
87
88 4
            foreach ($issues as $i) {
89 4
                if ($issue->getBody() === null) {
90
                    continue;
91
                }
92
93 4
                if ($i->isPullRequest() || strpos($issue->getBody(), '#' . $i->getNumber()) === false) {
94 4
                    continue;
95
                }
96
97 4
                $i->setLinkedPullRequest($issue);
98 4
                $issue->setLinkedIssue($i);
99
            }
100
        }
101 4
    }
102
}
103