IssueGrouper   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 96.88%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 82
ccs 31
cts 32
cp 0.9688
rs 10
c 1
b 0
f 0
wmc 15

5 Methods

Rating   Name   Duplication   Size   Complexity  
A groupIssuesByLabels() 0 15 3
A generateIssueGroupName() 0 11 2
A groupIssues() 0 5 1
B linkIssues() 0 18 7
A getIssuesToGroup() 0 4 2
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 2
    public function groupIssues(array $issues, ChangelogConfig $changelogConfig): array
21
    {
22 2
        $this->linkIssues($issues);
23
24 2
        return $this->groupIssuesByLabels($issues, $changelogConfig);
25
    }
26
27
    /**
28
     * @param Issue[] $issues
29
     *
30
     * @return IssueGroup[]
31
     */
32 2
    private function groupIssuesByLabels(array $issues, ChangelogConfig $changelogConfig): array
33
    {
34 2
        $issueGroups = [];
35
36 2
        foreach ($this->getIssuesToGroup($issues) as $issue) {
37 2
            $groupName = $this->generateIssueGroupName($issue, $changelogConfig);
38
39 2
            if (! isset($issueGroups[$groupName])) {
40 2
                $issueGroups[$groupName] = new IssueGroup($groupName);
41
            }
42
43 2
            $issueGroups[$groupName]->addIssue($issue);
44
        }
45
46 2
        return $issueGroups;
47
    }
48
49 2
    private function generateIssueGroupName(Issue $issue, ChangelogConfig $changelogConfig): string
50
    {
51 2
        $labelFilters = $changelogConfig->getLabels();
52
53 2
        if (count($labelFilters) === 0) {
54 1
            $labels = $issue->getLabels();
55
        } else {
56 1
            $labels = array_intersect($issue->getLabels(), $labelFilters);
57
        }
58
59 2
        return implode(',', $labels);
60
    }
61
62
    /**
63
     * @param Issue[] $issues
64
     *
65
     * @return Issue[]
66
     */
67 2
    private function getIssuesToGroup(array $issues): array
68
    {
69 2
        return array_filter($issues, static function (Issue $issue): bool {
70 2
            return (! $issue->isPullRequest() && $issue->getLinkedPullRequest() !== null) === false;
71 2
        });
72
    }
73
74
    /**
75
     * @param Issue[] $issues
76
     */
77 2
    private function linkIssues(array $issues): void
78
    {
79 2
        foreach ($issues as $issue) {
80 2
            if (! $issue->isPullRequest()) {
81 2
                continue;
82
            }
83
84 2
            foreach ($issues as $i) {
85 2
                if ($issue->getBody() === null) {
86
                    continue;
87
                }
88
89 2
                if ($i->isPullRequest() || strpos($issue->getBody(), '#' . $i->getNumber()) === false) {
90 2
                    continue;
91
                }
92
93 2
                $i->setLinkedPullRequest($issue);
94 2
                $issue->setLinkedIssue($i);
95
            }
96
        }
97 2
    }
98
}
99