Completed
Push — master ( a50ce0...3b5091 )
by Sebastian
02:40
created

LimitBodyLineLength::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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\CaptainHook\Git\CommitMessage;
13
14
/**
15
 * Class LimitBodyLineLength
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 LimitBodyLineLength 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 6
    public function __construct($length = 72)
37
    {
38 6
        $this->hint      = 'Body lines should not exceed ' . $length . ' characters';
39 6
        $this->maxLength = $length;
40 6
    }
41
42
    /**
43
     * Check if a body line doesn't exceed the max length limit.
44
     *
45
     * @param  \sebastianfeldmann\CaptainHook\Git\CommitMessage $msg
46
     * @return bool
47
     */
48 5
    public function pass(CommitMessage $msg)
49
    {
50 5
        $lineNr = 1;
51 5
        foreach ($msg->getBodyLines() as $line) {
52 3
            if (strlen($line) > $this->maxLength) {
53 2
                $this->hint .= PHP_EOL . 'Line ' . $lineNr . ' of your body exceeds the max line length';
54 2
                return false;
55
            }
56 2
            $lineNr++;
57
        }
58 3
        return true;
59
    }
60
}
61