AnalyzedResult::fromArray()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 9.4285
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;
13
14
use cloak\analyzer\result\FileResult;
15
use cloak\analyzer\result\FileNotFoundException;
16
use cloak\analyzer\result\collection\FileResultCollection;
17
use Closure;
18
19
20
/**
21
 * Class AnalyzedResult
22
 * @package cloak\analyzer
23
 */
24
class AnalyzedResult
25
{
26
27
    /**
28
     * @var FileResultCollection
29
     */
30
    private $files;
31
32
33
    /**
34
     * @param \cloak\analyzer\result\FileResult[] $files
35
     */
36
    public function __construct(array $files = [])
37
    {
38
        $this->files = new FileResultCollection($files);
39
    }
40
41
    /**
42
     * @param array $results
43
     * @return AnalyzedResult
44
     */
45
    public static function fromArray(array $results)
46
    {
47
        $files = static::parseResult($results);
48
        return new static($files);
49
    }
50
51
    /**
52
     * @param array $results
53
     * @return \cloak\analyzer\result\FileResult[]
54
     */
55
    protected static function parseResult(array $results)
56
    {
57
        $files = [];
58
59
        foreach ($results as $path => $lineResults) {
60
            try {
61
                $file = new FileResult($path, $lineResults);
62
            } catch (FileNotFoundException $exception) {
63
                continue;
64
            }
65
            $key = $file->getPath();
66
            $files[$key] = $file;
67
        }
68
69
        return $files;
70
    }
71
72
    /**
73
     * @param FileResult $file
74
     */
75
    public function addFile(FileResult $file)
76
    {
77
        $this->files->add($file);
78
    }
79
80
    /**
81
     * @return FileResultCollection
82
     */
83
    public function getFiles()
84
    {
85
        return $this->files;
86
    }
87
88
    /**
89
     * @param callable $filter
90
     * @return AnalyzedResult
91
     */
92
    public function includeFile(Closure $filter)
93
    {
94
        $files = $this->files->includeFile($filter);
95
        return $this->createNew($files);
96
    }
97
98
    /**
99
     * @param array $filters
100
     * @return AnalyzedResult
101
     */
102
    public function includeFiles(array $filters)
103
    {
104
        $newResult = $this;
105
106
        foreach ($filters as $filter) {
107
            $newResult = $this->includeFile($filter);
108
        }
109
110
        return $newResult;
111
    }
112
113
    /**
114
     * @param callable $filter
115
     * @return AnalyzedResult
116
     */
117
    public function excludeFile(Closure $filter)
118
    {
119
        $files = $this->files->excludeFile($filter);
120
        return $this->createNew($files);
121
    }
122
123
    /**
124
     * @param array $filters
125
     * @return AnalyzedResult
126
     */
127
    public function excludeFiles(array $filters)
128
    {
129
        $newResult = $this;
130
131
        foreach ($filters as $filter) {
132
            $newResult = $this->excludeFile($filter);
133
        }
134
135
        return $newResult;
136
    }
137
138
    /**
139
     * @return int
140
     */
141
    public function getFileCount()
142
    {
143
        return $this->files->count();
144
    }
145
146
    /**
147
     * @return bool
148
     */
149
    public function isEmpty()
150
    {
151
        return $this->files->isEmpty();
152
    }
153
154
    /**
155
     * @param FileResultCollection $collection
156
     * @return AnalyzedResult
157
     */
158
    private function createNew(FileResultCollection $collection)
159
    {
160
        $files = $collection->toArray();
161
        return new self($files);
162
    }
163
164
}
165