SourceHandler   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 156
rs 10
c 0
b 0
f 0
wmc 19

9 Methods

Rating   Name   Duplication   Size   Complexity  
A addSourceFiles() 0 4 2
A addSourceFile() 0 19 4
A getFiles() 0 5 1
A excludeMatchingPCRE() 0 11 3
A getCommonPathPrefix() 0 3 1
A __construct() 0 4 1
A excludeMatchingPattern() 0 11 3
A addPlugin() 0 7 3
A getFilesWithIssues() 0 3 1
1
<?php
2
3
/**
4
 * Source handler
5
 *
6
 * PHP Version 5.3.0
7
 *
8
 * Copyright (c) 2007-2010, Mayflower GmbH
9
 * All rights reserved.
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 *
15
 *   * Redistributions of source code must retain the above copyright
16
 *     notice, this list of conditions and the following disclaimer.
17
 *
18
 *   * Redistributions in binary form must reproduce the above copyright
19
 *     notice, this list of conditions and the following disclaimer in
20
 *     the documentation and/or other materials provided with the
21
 *     distribution.
22
 *
23
 *   * Neither the name of Mayflower GmbH nor the names of his
24
 *     contributors may be used to endorse or promote products derived
25
 *     from this software without specific prior written permission.
26
 *
27
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38
 * POSSIBILITY OF SUCH DAMAGE.
39
 *
40
 * @category PHP_CodeBrowser
41
 *
42
 * @author Elger Thiele <[email protected]>
43
 * @author Michel Hartmann <[email protected]>
44
 * @author Simon Kohlmeyer <[email protected]>
45
 *
46
 * @copyright 2007-2010 Mayflower GmbH
47
 *
48
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
49
 *
50
 * @version SVN: $Id$
51
 *
52
 * @link http://www.phpunit.de/
53
 *
54
 * @since File available since  0.2.0
55
 */
56
57
namespace PHPCodeBrowser;
58
59
use Exception;
60
use Monolog\Logger;
61
use PHPCodeBrowser\Helper\IOHelper;
62
use SplFileInfo;
63
64
/**
65
 * SourceHandler
66
 *
67
 * This class manages lists of source files and their issues.
68
 * For providing these lists the prior generated IssueXML is parsed.
69
 *
70
 * @category PHP_CodeBrowser
71
 *
72
 * @author Elger Thiele <[email protected]>
73
 * @author Christopher Weckerle <[email protected]>
74
 * @author Michel Hartmann <[email protected]>
75
 * @author Simon Kohlmeyer <[email protected]>
76
 *
77
 * @copyright 2007-2010 Mayflower GmbH
78
 *
79
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
80
 *
81
 * @version Release: @package_version@
82
 *
83
 * @link http://www.phpunit.de/
84
 *
85
 * @since Class available since  0.2.0
86
 */
87
class SourceHandler
88
{
89
    /**
90
     * Files to be included in the report
91
     *
92
     * @var array<File>
93
     */
94
    protected $files = [];
95
96
    /**
97
     * Pear Log object where debug output should go to.
98
     *
99
     * @var Logger
100
     */
101
    protected $debugLog;
102
103
    /**
104
     * Default constructor
105
     *
106
     * @param Logger $debugLog
107
     * @param array  $plugins  The plugins to get issues from.
108
     */
109
    public function __construct(Logger $debugLog, array $plugins = [])
110
    {
111
        $this->debugLog = $debugLog;
112
        \array_walk($plugins, [$this, 'addPlugin']);
113
    }
114
115
    /**
116
     * Add a new plugin to the handler.
117
     *
118
     * @param AbstractPlugin $plugin The plugin to add.
119
     */
120
    public function addPlugin(AbstractPlugin $plugin): void
121
    {
122
        foreach ($plugin->getFileList() as $file) {
123
            if (\array_key_exists($file->name(), $this->files)) {
124
                $this->files[$file->name()]->mergeWith($file);
125
            } else {
126
                $this->files[$file->name()] = $file;
127
            }
128
        }
129
    }
130
131
    /**
132
     * Add source files to the list.
133
     *
134
     * @param array<SplFileInfo|string>|\AppendIterator $files The files to add
135
     */
136
    public function addSourceFiles($files): void
137
    {
138
        foreach ($files as $f) {
139
            $this->addSourceFile($f);
140
        }
141
    }
142
143
    /**
144
     * Add a source file.
145
     *
146
     * @param string|SplFileInfo $file The file to add
147
     *
148
     * @throws Exception
149
     */
150
    public function addSourceFile($file): void
151
    {
152
        if (\is_string($file)) {
153
            $filename = $file;
154
            $file     = \realpath($file);
155
        } else {
156
            $filename = $file->getPathname();
157
            $file     = $file->getRealPath();
158
        }
159
160
        if (!$file) {
161
            throw new Exception("{$filename} is no regular file");
162
        }
163
164
        if (\array_key_exists($file, $this->files)) {
165
            return;
166
        }
167
168
        $this->files[$file] = new File($file);
169
    }
170
171
    /**
172
     * Retrieves the parent directory all files have in common.
173
     *
174
     * @return string
175
     */
176
    public function getCommonPathPrefix(): string
177
    {
178
        return IOHelper::getCommonPathPrefix(\array_keys($this->files));
179
    }
180
181
    /**
182
     * Returns a sorted array of the files that should be in the report.
183
     *
184
     * @return array<File>
185
     */
186
    public function getFiles(): array
187
    {
188
        File::sort($this->files);
189
190
        return $this->files;
191
    }
192
193
    /**
194
     * Get a unique list of all file names with issues.
195
     *
196
     * @return array
197
     */
198
    public function getFilesWithIssues(): array
199
    {
200
        return \array_keys($this->files);
201
    }
202
203
    /**
204
     * Remove all files that match the given PCRE.
205
     *
206
     * @param string $expr The PCRE specifying which files to remove.
207
     *
208
     * @return void
209
     */
210
    public function excludeMatchingPCRE(string $expr): void
211
    {
212
        foreach (\array_keys($this->files) as $filename) {
213
            if (!\preg_match($expr, $filename)) {
214
                continue;
215
            }
216
217
            $this->debugLog->debug(
218
                "Excluding {$filename}, it matches PCRE {$expr}"
219
            );
220
            unset($this->files[$filename]);
221
        }
222
    }
223
224
    /**
225
     * Remove all files that match the given shell wildcard pattern
226
     * as accepted by fnmatch().
227
     *
228
     * @param string $pattern The pattern.
229
     *
230
     * @return void
231
     */
232
    public function excludeMatchingPattern(string $pattern): void
233
    {
234
        foreach (\array_keys($this->files) as $filename) {
235
            if (!\fnmatch($pattern, $filename)) {
236
                continue;
237
            }
238
239
            $this->debugLog->debug(
240
                "Excluding {$filename}, it matches pattern {$pattern}"
241
            );
242
            unset($this->files[$filename]);
243
        }
244
    }
245
}
246