LineResultCollection::getUnusedLineCount()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
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\result\collection;
13
14
use cloak\Collection;
15
use cloak\collection\ElementStackable;
16
use cloak\value\Coverage;
17
use cloak\value\LineRange;
18
use cloak\analyzer\result\LineResult;
19
use cloak\result\CodeCoverageResult;
20
use cloak\result\LineResultSelectable;
21
use cloak\result\LineCountResult;
22
use cloak\reflection\Reflection;
23
use PhpCollection\Sequence;
24
25
26
/**
27
 * Class LineResultCollection
28
 * @package cloak\result\collection
29
 */
30
class LineResultCollection implements CodeCoverageResult, LineResultSelectable, LineCountResult, Collection
31
{
32
33
    use ElementStackable;
34
35
    /**
36
     * @param array $lines
37
     */
38
    public function __construct(array $lines = [])
39
    {
40
        $this->collection = new Sequence($lines);
41
    }
42
43
    /**
44
     * @return int
45
     */
46
    public function getLineCount()
47
    {
48
        return $this->collection->count();
49
    }
50
51
    /**
52
     * @return int
53
     */
54
    public function getDeadLineCount()
55
    {
56
        $lines = $this->selectLines(function(LineResult $line) {
57
            return $line->isDead();
58
        });
59
        return $lines->count();
60
    }
61
62
    /**
63
     * @return int
64
     */
65
    public function getUnusedLineCount()
66
    {
67
        $lines = $this->selectLines(function(LineResult $line) {
68
            return $line->isUnused();
69
        });
70
        return $lines->count();
71
    }
72
73
    /**
74
     * @return int
75
     */
76
    public function getExecutedLineCount()
77
    {
78
        $lines = $this->selectLines(function(LineResult $line) {
79
            return $line->isExecuted();
80
        });
81
        return $lines->count();
82
    }
83
84
    /**
85
     * @return int
86
     */
87
    public function getExecutableLineCount()
88
    {
89
        return $this->getUnusedLineCount() + $this->getExecutedLineCount();
90
    }
91
92
    /**
93
     * @return Coverage The value of code coverage
94
     */
95
    public function getCodeCoverage()
96
    {
97
        return Coverage::fromLineResult($this);
98
    }
99
100
    /**
101
     * @param Coverage $coverage
102
     * @return bool
103
     */
104
    public function isCoverageLessThan(Coverage $coverage)
105
    {
106
        return $this->getCodeCoverage()->lessThan($coverage);
107
    }
108
109
    /**
110
     * @param Coverage $coverage
111
     * @return bool
112
     */
113
    public function isCoverageGreaterEqual(Coverage $coverage)
114
    {
115
        return $this->getCodeCoverage()->greaterEqual($coverage);
116
    }
117
118
    /**
119
     * @param LineRange $lineRange
120
     * @return LineResultCollection
121
     */
122
    public function selectRange(LineRange $lineRange)
123
    {
124
        $lines = $this->selectLines(function(LineResult $line) use ($lineRange) {
125
            $lineNumber = $line->getLineNumber();
126
            return $lineRange->contains($lineNumber);
127
        });
128
129
        return new self($lines->all());
130
    }
131
132
    /**
133
     * @param Reflection $reflection
134
     * @return LineResultCollection
135
     */
136
    public function selectByReflection(Reflection $reflection)
137
    {
138
        $lineRange = $reflection->getLineRange();
139
        return $this->selectRange($lineRange);
140
    }
141
142
    /**
143
     * @param array $analyzeResults
144
     * @return LineResultCollection
145
     */
146
    public static function from(array $analyzeResults)
147
    {
148
//        $results = [];
149
150
//        foreach ($analyzeResults as $lineNumber => $analyzeResult) {
151
  //          $results[] = new LineResult($lineNumber, $analyzeResult);
152
    //    }
153
154
        return new self($analyzeResults);
155
    }
156
157
    /**
158
     * @param callable $filter
159
     * @return \PhpCollection\AbstractSequence
160
     */
161
    public function selectLines(\Closure $filter)
162
    {
163
        $lines = $this->collection->filter($filter);
164
        return $lines;
165
    }
166
167
}
168