Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * PHPUnit
4
 *
5
 * Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.de>.
6
 * All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 *
12
 *   * Redistributions of source code must retain the above copyright
13
 *     notice, this list of conditions and the following disclaimer.
14
 *
15
 *   * Redistributions in binary form must reproduce the above copyright
16
 *     notice, this list of conditions and the following disclaimer in
17
 *     the documentation and/or other materials provided with the
18
 *     distribution.
19
 *
20
 *   * Neither the name of Sebastian Bergmann nor the names of his
21
 *     contributors may be used to endorse or promote products derived
22
 *     from this software without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35
 * POSSIBILITY OF SUCH DAMAGE.
36
 *
37
 * @category   Testing
38
 * @package    PHPUnit
39
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
41
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
42
 * @link       http://www.phpunit.de/
43
 * @since      File available since Release 3.2.0
44
 */
45
 
46
require_once 'PHPUnit/Util/File.php';
47
require_once 'PHPUnit/Util/Filter.php';
48
require_once 'PHPUnit/Util/Metrics.php';
49
 
50
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
51
 
52
/**
53
 * File-Level Metrics.
54
 *
55
 * @category   Testing
56
 * @package    PHPUnit
57
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
58
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
59
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
60
 * @version    Release: 3.4.15
61
 * @link       http://www.phpunit.de/
62
 * @since      Class available since Release 3.2.0
63
 */
64
class PHPUnit_Util_Metrics_File extends PHPUnit_Util_Metrics
65
{
66
    protected $coverage      = 0;
67
    protected $loc           = 0;
68
    protected $cloc          = 0;
69
    protected $ncloc         = 0;
70
    protected $locExecutable = 0;
71
    protected $locExecuted   = 0;
72
 
73
    protected $filename;
74
    protected $classes = array();
75
    protected $functions = array();
76
 
77
    protected static $cache = array();
78
 
79
    /**
80
     * Constructor.
81
     *
82
     * @param  string $filename
83
     * @param  array  $codeCoverage
84
     * @throws RuntimeException
85
     */
86
    protected function __construct($filename, &$codeCoverage = array())
87
    {
88
        if (!file_exists($filename)) {
89
            throw new RuntimeException(
90
              sprintf(
91
                'File "%s" not found.',
92
                $filename
93
              )
94
            );
95
        }
96
 
97
        $this->filename = $filename;
98
 
99
        foreach (PHPUnit_Util_File::countLines($this->filename) as $name => $value) {
100
            $this->$name = $value;
101
        }
102
 
103
        $this->setCoverage($codeCoverage);
104
 
105
        foreach (PHPUnit_Util_File::getClassesInFile($filename) as $className => $class) {
106
            $this->classes[$className] = PHPUnit_Util_Metrics_Class::factory(
107
              new ReflectionClass($className), $codeCoverage
108
            );
109
        }
110
 
111
        foreach (PHPUnit_Util_File::getFunctionsInFile($filename) as $functionName => $function) {
112
            $this->functions[$functionName] = PHPUnit_Util_Metrics_Function::factory(
113
              new ReflectionFunction($functionName), $codeCoverage
114
            );
115
        }
116
    }
117
 
118
    /**
119
     * Factory.
120
     *
121
     * @param  string $filename
122
     * @param  array  $codeCoverage
123
     * @return PHPUnit_Util_Metrics_File
124
     */
125
    public static function factory($filename, &$codeCoverage = array())
126
    {
127
        if (!isset(self::$cache[$filename])) {
128
            self::$cache[$filename] = new PHPUnit_Util_Metrics_File($filename, $codeCoverage);
129
        }
130
 
131
        else if (!empty($codeCoverage) && self::$cache[$filename]->getCoverage() == 0) {
132
            self::$cache[$filename]->setCoverage($codeCoverage);
133
        }
134
 
135
        return self::$cache[$filename];
136
    }
137
 
138
    /**
139
     * @param  array $codeCoverage
140
     */
141
    public function setCoverage(array &$codeCoverage)
142
    {
143
        if (!empty($codeCoverage)) {
144
            $this->calculateCodeCoverage($codeCoverage);
145
 
146
            foreach ($this->classes as $class) {
147
                $class->setCoverage($codeCoverage);
148
            }
149
 
150
            foreach ($this->functions as $function) {
151
                $function->setCoverage($codeCoverage);
152
            }
153
        }
154
    }
155
 
156
    /**
157
     * Returns the path to the file.
158
     *
159
     * @return string
160
     */
161
    public function getPath()
162
    {
163
        return $this->filename;
164
    }
165
 
166
    /**
167
     * Classes.
168
     *
169
     * @return array
170
     */
171
    public function getClasses()
172
    {
173
        return $this->classes;
174
    }
175
 
176
    /**
177
     * A class.
178
     *
179
     * @param  string $className
180
     * @return ReflectionClass
181
     */
182
    public function getClass($className)
183
    {
184
        return $this->classes[$className];
185
    }
186
 
187
    /**
188
     * Functions.
189
     *
190
     * @return array
191
     */
192
    public function getFunctions()
193
    {
194
        return $this->functions;
195
    }
196
 
197
    /**
198
     * A function.
199
     *
200
     * @param  string $functionName
201
     * @return ReflectionClass
202
     */
203
    public function getFunction($functionName)
204
    {
205
        return $this->functions[$functionName];
206
    }
207
 
208
    /**
209
     * Lines.
210
     *
211
     * @return array
212
     */
213
    public function getLines()
214
    {
215
        return file($this->filename);
216
    }
217
 
218
    /**
219
     * Tokens.
220
     *
221
     * @return array
222
     */
223
    public function getTokens()
224
    {
225
        return token_get_all(file_get_contents($this->filename));
226
    }
227
 
228
    /**
229
     * Returns the Code Coverage for the file.
230
     *
231
     * @return float
232
     */
233
    public function getCoverage()
234
    {
235
        return $this->coverage;
236
    }
237
 
238
    /**
239
     * Lines of Code (LOC).
240
     *
241
     * @return int
242
     */
243
    public function getLoc()
244
    {
245
        return $this->loc;
246
    }
247
 
248
    /**
249
     * Executable Lines of Code (ELOC).
250
     *
251
     * @return int
252
     */
253
    public function getLocExecutable()
254
    {
255
        return $this->locExecutable;
256
    }
257
 
258
    /**
259
     * Executed Lines of Code.
260
     *
261
     * @return int
262
     */
263
    public function getLocExecuted()
264
    {
265
        return $this->locExecuted;
266
    }
267
 
268
    /**
269
     * Comment Lines of Code (CLOC).
270
     *
271
     * @return int
272
     */
273
    public function getCloc()
274
    {
275
        return $this->cloc;
276
    }
277
 
278
    /**
279
     * Non-Comment Lines of Code (NCLOC).
280
     *
281
     * @return int
282
     */
283
    public function getNcloc()
284
    {
285
        return $this->ncloc;
286
    }
287
 
288
    /**
289
     * Calculates the Code Coverage for the class.
290
     *
291
     * @param  array $codeCoverage
292
     */
293
    protected function calculateCodeCoverage(&$codeCoverage)
294
    {
295
        $statistics = PHPUnit_Util_CodeCoverage::getStatistics(
296
          $codeCoverage,
297
          $this->filename,
298
          1,
299
          $this->loc
300
        );
301
 
302
        $this->coverage      = $statistics['coverage'];
303
        $this->loc           = $statistics['loc'];
304
        $this->locExecutable = $statistics['locExecutable'];
305
        $this->locExecuted   = $statistics['locExecuted'];
306
    }
307
}
308
?>