FileResult   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 11
c 3
b 1
f 0
lcom 1
cbo 5
dl 0
loc 92
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 4 1
A getLineResults() 0 4 1
A __construct() 0 12 2
A matchPath() 0 7 2
A matchPaths() 0 11 3
A createLineResults() 0 10 2
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\analyzer\result;
13
14
15
use Eloquent\Pathogen\Factory\PathFactory;
16
17
/**
18
 * Class FileResult
19
 * @package cloak\analyzer\result
20
 */
21
class FileResult
22
{
23
24
    /**
25
     * @var string
26
     */
27
    private $path;
28
29
    /**
30
     * @var \cloak\analyzer\result\LineResult[]
31
     */
32
    private $resultLines;
33
34
35
    /**
36
     * @param string $path
37
     * @param array $resultLines
38
     * @throws \cloak\analyzer\result\FileNotFoundException
39
     */
40
    public function __construct($path, array $resultLines = [])
41
    {
42
        $absolutePath = PathFactory::instance()->create($path);
43
        $filePath = $absolutePath->normalize()->string();
44
45
        if (file_exists($filePath) === false) {
46
            throw new FileNotFoundException("'$path' file does not exist");
47
        }
48
49
        $this->path = $filePath;
50
        $this->resultLines = $this->createLineResults($resultLines);
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getPath()
57
    {
58
        return (string) $this->path;
59
    }
60
61
    /**
62
     * @return \cloak\analyzer\result\LineResult[]
63
     */
64
    public function getLineResults()
65
    {
66
        return $this->resultLines;
67
    }
68
69
    /**
70
     * @param string $path
71
     * @return bool
72
     */
73
    public function matchPath($path)
74
    {
75
        $pathPattern = preg_quote($path, '/');
76
        $result = preg_match("/" . $pathPattern . "/", $this->getPath());
77
78
        return ($result === 0) ? false : true;
79
    }
80
81
    /**
82
     * @param array $paths
83
     * @return bool
84
     */
85
    public function matchPaths(array $paths)
86
    {
87
        foreach ($paths as $path) {
88
            if ($this->matchPath($path) === false) {
89
                continue;
90
            }
91
            return true;
92
        }
93
94
        return false;
95
    }
96
97
    /**
98
     * @param array<integer, integer> $resultLines
99
     * @return \cloak\analyzer\result\LineResult[]
100
     */
101
    private function createLineResults(array $resultLines)
102
    {
103
        $result = [];
104
105
        foreach ($resultLines as $lineNumber => $analyzedResult) {
106
            $result[] = new LineResult($lineNumber, $analyzedResult);
107
        }
108
109
        return $result;
110
    }
111
112
}
113