Completed
Push — master ( 11a376...8d38b3 )
by Andrii
03:54
created

PrettifyUserLinks::run()   D

Complexity

Conditions 9
Paths 24

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 23.6248

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
ccs 10
cts 23
cp 0.4348
rs 4.909
cc 9
eloc 16
nc 24
nop 1
crap 23.6248
1
<?php
2
/**
3
 * Changelog keeper
4
 *
5
 * @link      https://github.com/hiqdev/chkipper
6
 * @package   chkipper
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\chkipper\modifiers;
12
13
use hiqdev\chkipper\history\History;
14
15
/**
16
 * Modifier that prepares better links to users (committers) when possible.
17
 *
18
 * Git provides emails of commiters.
19
 * If you provide link to committer's personal page this modifier will spread
20
 * it all over the history.
21
 *
22
 * E.g. git provides my email as [email protected] for all my commits.
23
 * So my commits will look like this in history:
24
 * ```
25
 * - [0000000] 2017-01-01 commit comment [[email protected]]
26
 * ```
27
 * I want my commits to link to my github page: https://github.com/hiqsol
28
 * So I add these lines in the links part of the history (at the end of file):
29
 * ```
30
 * [@hiqsol]: https://github.com/hiqsol
31
 * [[email protected]]: https://github.com/hiqsol
32
 * ```
33
 * This modifier will change my commits to look like this:
34
 * ```
35
 * - [0000000] 2017-01-01 commit comment [@hiqsol]
36
 * ```
37
 *
38
 * @author Andrii Vasyliev <[email protected]>
39
 */
40
class PrettifyUserLinks extends AbstractModifier
41
{
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function run(History $history)
46
    {
47 2
        $users = [];
48 2
        $subs = [];
49 2
        foreach ($history->getLinks() as $link => $href) {
50 2
            if ($link[0] === '@') {
51 1
                $users[$href] = $link;
52 2
            } elseif (isset($users[$href])) {
53
                $subs[$link] = $users[$href];
54
            }
55 2
        }
56 2
        if (!$subs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $subs of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
57 2
            return;
58
        }
59
        foreach ($history->getTags() as $tag) {
60
            foreach ($tag->getNotes() as $note) {
61
                foreach ($note->getCommits() as $commit) {
62
                    $author = $commit->getAuthor();
63
                    if (isset($subs[$author])) {
64
                        $commit->setAuthor($subs[$author]);
65
                    }
66
                }
67
            }
68
        }
69
    }
70
}
71