Completed
Push — master ( aa9657...bb9832 )
by Sebastian
05:21
created

LimitSubjectLength::pass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
c 0
b 0
f 0
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of CaptainHook.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianFeldmann\CaptainHook\Hook\Message\Rule;
11
12
use SebastianFeldmann\Git\CommitMessage;
13
14
/**
15
 * Class LimitSubjectLength
16
 *
17
 * @package CaptainHook
18
 * @author  Sebastian Feldmann <[email protected]>
19
 * @link    https://github.com/sebastianfeldmann/captainhook
20
 * @since   Class available since Release 0.9.0
21
 */
22
class LimitSubjectLength extends Base
23
{
24
    /**
25
     * Length limit
26
     *
27
     * @var int
28
     */
29
    protected $maxLength;
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param int $length
35
     */
36 4
    public function __construct(int $length = 50)
37
    {
38 4
        $this->hint      = 'Subject line should not exceed ' . $length . ' characters';
39 4
        $this->maxLength = $length;
40 4
    }
41
42
    /**
43
     * Check if commit message doesn't exceeed the max length.
44
     *
45
     * @param  \SebastianFeldmann\Git\CommitMessage $msg
46
     * @return bool
47
     */
48 3
    public function pass(CommitMessage $msg) : bool
49
    {
50 3
        $subjectLength = strlen($msg->getSubject());
51 3
        if ($subjectLength > $this->maxLength) {
52 1
            $this->hint .= ' (' . $subjectLength . ')';
53 1
            return false;
54
        }
55 2
        return true;
56
    }
57
}
58