FileResult::getPath()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
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;
13
14
use cloak\value\LineRange;
15
use cloak\reflection\FileReflection;
16
17
18
/**
19
 * Class FileResult
20
 * @package cloak\result
21
 */
22
class FileResult implements CoverageResultNode
23
{
24
25
    use CoverageResult;
26
27
28
    /**
29
     * @var string
30
     */
31
    private $path;
32
33
    /**
34
     * @var \cloak\result\ResultFactory
35
     */
36
    private $factory;
37
38
    /**
39
     * @var LineRange
40
     */
41
    private $lineRange;
42
43
44
    /**
45
     * @param string $path
46
     * @param LineResultSelectable $selector
47
     */
48
    public function __construct($path, LineResultSelectable $selector)
49
    {
50
        $this->path = $path;
51
        $this->resolveLineRange($selector);
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getPath()
58
    {
59
        return $this->path;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getName()
66
    {
67
        return $this->getPath();
68
    }
69
70
    /**
71
     * @param string $directoryPath
72
     */
73
    public function getRelativePath($directoryPath)
74
    {
75
        $directory = realpath($directoryPath) . "/";
76
77
        return str_replace($directory, "", $this->getPath());
78
    }
79
80
    /**
81
     * @param $value
82
     * @return bool
83
     */
84
    public function matchPath($value)
85
    {
86
        $pathPattern = preg_quote($value, '/');
87
        $result = preg_match("/" . $pathPattern . "/", $this->getPath());
88
89
        return ($result === 0) ? false : true;
90
    }
91
92
    /**
93
     * @return int
94
     */
95
    public function getLineCount()
96
    {
97
        return $this->lineRange->getEndLineNumber();
98
    }
99
100
    /**
101
     * @return \cloak\result\collection\LineResultCollection
102
     */
103
    public function getLineResults()
104
    {
105
        return $this->lineResults;
106
    }
107
108
    /**
109
     * @param FileResult $file
110
     * @return bool
111
     */
112
    public function equals(FileResult $file)
113
    {
114
        return $file->getPath() === $this->getPath();
115
    }
116
117
    /**
118
     * @param LineResultSelectable $selector
119
     */
120
    protected function resolveLineRange(LineResultSelectable $selector)
121
    {
122
        $reflection = new FileReflection($this->getPath());
123
        $this->lineRange = $reflection->getLineRange();
124
125
        $this->factory = new ResultFactory($reflection);
126
        $this->lineResults = $selector->selectByReflection($reflection);
127
    }
128
129
    /**
130
     * @return CoverageResultNodeCollection
131
     */
132
    public function getClassResults()
133
    {
134
        return $this->factory->createClassResults($this->lineResults);
135
    }
136
137
    /**
138
     * @return \cloak\result\collection\CoverageResultCollection
139
     */
140
    public function getTraitResults()
141
    {
142
        return $this->factory->createTraitResults($this->lineResults);
143
    }
144
145
    /**
146
     * @return bool
147
     */
148
    public function hasChildResults()
149
    {
150
        return $this->getChildResults()->isEmpty() === false;
151
    }
152
153
    /**
154
     * @return CoverageResultNodeCollection
155
     */
156
    public function getChildResults()
157
    {
158
        $results = $this->getClassResults();
159
        return $results->merge($this->getTraitResults());
160
    }
161
162
}
163