Completed
Pull Request — master (#4)
by Billie
03:15
created

MessageImplementation::getStatuses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PurpleBooth\GitGitHubLint;
6
7
use PurpleBooth\GitGitHubLint\Status\Status;
8
9
/**
10
 * A commit message
11
 *
12
 * @package PurpleBooth\GitGitHubLint
13
 */
14
class MessageImplementation implements Message
15
{
16
    /**
17
     * @var array
18
     */
19
    private $commitMessage;
20
21
    /**
22
     * @var Status[]
23
     */
24
    private $statuses;
25
26
    /**
27
     * Message constructor.
28
     *
29
     * @param string $commitMessage
30
     */
31
    public function __construct(string $commitMessage)
32
    {
33
        $this->commitMessage = explode("\n", $commitMessage);
34
    }
35
36
    /**
37
     * Is the title capitalised
38
     *
39
     * @return bool
40
     */
41
    public function isTitleCapitalised() : bool
42
    {
43
        if ($this->getTitleLength() == 0) {
44
            return false;
45
        }
46
47
        return strtoupper($this->commitMessage[0]){0} === $this->commitMessage[0]{0};
48
    }
49
50
    /**
51
     * Get the title length
52
     *
53
     * @return int
54
     */
55
    public function getTitleLength() : int
56
    {
57
        return strlen($this->commitMessage[0]);
58
    }
59
60
    /**
61
     * Title ends with a full stop
62
     *
63
     * @return bool
64
     */
65
    public function hasTitleAFullStop() : bool
66
    {
67
        if ($this->getTitleLength() == 0) {
68
            return false;
69
        }
70
71
        $lastCharacter = trim($this->commitMessage[0]){$this->getTitleLength() - 1};
72
73
        return $lastCharacter == ".";
74
    }
75
76
    /**
77
     * Has a gap after the title
78
     *
79
     * @return bool
80
     */
81
    public function hasBlankLineAfterTitle() : bool
82
    {
83
        if (count($this->commitMessage) < 2) {
84
            return false;
85
        }
86
87
        return $this->commitMessage[1] == "";
88
    }
89
90
    /**
91
     * Has this message got a body
92
     *
93
     * @return bool
94
     */
95
    public function hasABody(): bool
96
    {
97
        // Has a body
98
        if ($this->getBodyWrapLength() > 0) {
99
            // First line isn't long
100
            return strlen($this->commitMessage[2]) > 0;
101
        }
102
103
        return false;
104
    }
105
106
    /**
107
     * The length at which the message wraps
108
     *
109
     * @return int
110
     */
111
    public function getBodyWrapLength() : int
112
    {
113
        if (count($this->commitMessage) < 3) {
114
            return 0;
115
        }
116
117
        $body              = array_slice($this->commitMessage, 2);
118
        $longestLineLength = 0;
119
120
        foreach ($body as $line) {
121
            if (strlen($line) > $longestLineLength) {
122
                $longestLineLength = strlen($line);
123
            }
124
        }
125
126
        return $longestLineLength;
127
    }
128
129
    /**
130
     * Associate a status with this message
131
     *
132
     * @param Status $status
133
     */
134
    public function addStatus(Status $status)
135
    {
136
        $this->statuses[] = $status;
137
    }
138
139
140
    /**
141
     * Get the status associated with this message
142
     *
143
     * @return Status[]
144
     */
145
    protected function getStatuses()
146
    {
147
        return $this->statuses;
148
    }
149
150
    /**
151
     * Get the number of statuses
152
     *
153
     * @return int
154
     */
155
    public function getStatusCount() : int
156
    {
157
        return count($this->getStatuses());
158
    }
159
}
160