Passed
Push — master ( 69774c...de7969 )
by Sebastian
02:46
created

LimitBodyLineLength::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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