LineRange::validateLineRange()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 15
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
3
/**
4
 * This file is part of cloak.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace cloak\value;
13
14
/***
15
 * Class LineRange
16
 * @package cloak\value
17
 */
18
class LineRange
19
{
20
21
    /**
22
     * @var int
23
     */
24
    private $startLineNumber;
25
26
    /**
27
     * @var int
28
     */
29
    private $endLineNumber;
30
31
    /**
32
     * @param int $startLineNumber
33
     * @param int $endLineNumber
34
     */
35
    public function __construct($startLineNumber, $endLineNumber)
36
    {
37
        $this->startLineNumber = $startLineNumber;
38
        $this->endLineNumber = $endLineNumber;
39
        $this->validate();
40
    }
41
42
    /**
43
     * @return int
44
     */
45
    public function getStartLineNumber()
46
    {
47
        return $this->startLineNumber;
48
    }
49
50
    /**
51
     * @return int
52
     */
53
    public function getEndLineNumber()
54
    {
55
        return $this->endLineNumber;
56
    }
57
58
    /**
59
     * @param int $lineNumber
60
     * @return bool
61
     */
62
    public function contains($lineNumber)
63
    {
64
        $result =  $this->getStartLineNumber() <= $lineNumber
65
            && $this->getEndLineNumber() >= $lineNumber;
66
67
        return $result;
68
    }
69
70
    /**
71
     * @throws UnexpectedLineNumberException
72
     * @throws LessThanLineNumberException
73
     */
74
    private function validate()
75
    {
76
        $this->validateLineNumber();
77
        $this->validateLineRange();
78
    }
79
80
    /**
81
     * @throws UnexpectedLineNumberException
82
     */
83
    private function validateLineNumber()
84
    {
85
        if ($this->getStartLineNumber() < 1) {
86
            throw new UnexpectedLineNumberException(sprintf(
87
                'Start line is less than 1, the start line has been specified %d.',
88
                $this->getStartLineNumber())
89
            );
90
        }
91
92
        if ($this->getEndLineNumber() < 1) {
93
            throw new UnexpectedLineNumberException(sprintf(
94
                'End line is less than 1, the start line has been specified %d.',
95
                $this->getEndLineNumber())
96
            );
97
        }
98
    }
99
100
    /**
101
     * @throws LessThanLineNumberException
102
     */
103
    private function validateLineRange()
104
    {
105
        $result = $this->getStartLineNumber() <= $this->getEndLineNumber();
106
107
        if ($result === true) {
108
            return;
109
        }
110
111
        throw new LessThanLineNumberException(sprintf(
112
            'The start line is larger than the end line, the start line is %d, the end line is %d.',
113
                $this->getStartLineNumber(),
114
                $this->getEndLineNumber()
115
            )
116
        );
117
    }
118
119
}
120