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/Filter.php';
47
require_once 'PHPUnit/Util/Metrics.php';
48
 
49
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
50
 
51
/**
52
 * Project-Level Metrics.
53
 *
54
 * @category   Testing
55
 * @package    PHPUnit
56
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
57
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
58
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
59
 * @version    Release: 3.4.15
60
 * @link       http://www.phpunit.de/
61
 * @since      Class available since Release 3.2.0
62
 */
63
class PHPUnit_Util_Metrics_Project extends PHPUnit_Util_Metrics
64
{
65
    protected static $CPD_IGNORE_LIST = array(
66
      T_INLINE_HTML,
67
      T_COMMENT,
68
      T_DOC_COMMENT,
69
      T_OPEN_TAG,
70
      T_OPEN_TAG_WITH_ECHO,
71
      T_CLOSE_TAG,
72
      T_WHITESPACE
73
    );
74
 
75
    protected $classes   = array();
76
    protected $files     = array();
77
    protected $functions = array();
78
 
79
    protected $cls     = 0;
80
    protected $clsa    = 0;
81
    protected $clsc    = 0;
82
    protected $interfs = 0;
83
    protected $roots   = 0;
84
    protected $leafs   = 0;
85
    protected $maxDit  = 0;
86
 
87
    protected $cpdDuplicates = array();
88
    protected $cpdHashes     = array();
89
    protected $dependencies  = array();
90
 
91
    /**
92
     * Constructor.
93
     *
94
     * @param  array   $files
95
     * @param  array   $codeCoverage
96
     * @param  boolean $cpd
97
     * @param  integer $cpdMinLines
98
     * @param  integer $cpdMinMatches
99
     */
100
    public function __construct(Array $files, &$codeCoverage = array(), $cpd = FALSE, $cpdMinLines = 5, $cpdMinMatches = 70)
101
    {
102
        foreach ($files as $file) {
103
            if (file_exists($file)) {
104
                $this->files[$file] = PHPUnit_Util_Metrics_File::factory(
105
                  $file, $codeCoverage
106
                );
107
 
108
                foreach ($this->files[$file]->getFunctions() as $function) {
109
                    $this->functions[$function->getFunction()->getName()] = $function;
110
                }
111
 
112
                foreach ($this->files[$file]->getClasses() as $class) {
113
                    $className = $class->getClass()->getName();
114
                    $package   = $class->getPackage();
115
 
116
                    $this->classes[$className] = $class;
117
 
118
                    if ($class->getClass()->isInterface()) {
119
                        $this->interfs++;
120
                    } else {
121
                        if ($class->getClass()->isAbstract()) {
122
                            $this->clsa++;
123
                        } else {
124
                            $this->clsc++;
125
                        }
126
 
127
                        $this->cls++;
128
                    }
129
                }
130
            }
131
        }
132
 
133
        $numClasses = count($this->classes);
134
 
135
        foreach ($this->classes as $a => $b) {
136
            foreach ($this->classes as $c => $d) {
137
                $this->dependencies[$a][$c] = 0;
138
            }
139
        }
140
 
141
        foreach ($this->classes as $className => $class) {
142
            foreach ($class->getDependencies() as $dependency) {
143
                $this->dependencies[$className][$dependency] = 1;
144
            }
145
 
146
            $class->setProject($this);
147
 
148
            if ($class->getNOC() == 0) {
149
                $this->leafs++;
150
            }
151
 
152
            else if ($class->getClass()->getParentClass() === FALSE) {
153
                $this->roots++;
154
            }
155
 
156
            $this->maxDit = max($this->maxDit, $class->getDit());
157
        }
158
 
159
        if ($cpd) {
160
            $this->copyPasteDetection($cpdMinLines, $cpdMinMatches);
161
        }
162
    }
163
 
164
    /**
165
     * Returns the classes of this project.
166
     *
167
     * @return array
168
     */
169
    public function getClasses()
170
    {
171
        return $this->classes;
172
    }
173
 
174
    /**
175
     * A class.
176
     *
177
     * @param  string $className
178
     * @return ReflectionClass
179
     */
180
    public function getClass($className)
181
    {
182
        return $this->classes[$className];
183
    }
184
 
185
    /**
186
     * Returns the dependencies between the classes of this project.
187
     *
188
     * @return array
189
     */
190
    public function getDependencies()
191
    {
192
        return $this->dependencies;
193
    }
194
 
195
    /**
196
     * Returns the dependencies between the classes of this project
197
     * as GraphViz/DOT markup.
198
     *
199
     * @return Image_GraphViz
200
     * @since  Method available since Release 3.2.2
201
     */
202
    public function getDependenciesAsDOT()
203
    {
204
        if (PHPUnit_Util_Filesystem::fileExistsInIncludePath('Image/GraphViz.php')) {
205
            require_once 'Image/GraphViz.php';
206
        } else {
207
            throw new RuntimeException('Image_GraphViz is not available.');
208
        }
209
 
210
        $graph = new Image_GraphViz(
211
          TRUE,
212
          array(
213
            'overlap'  => 'scale',
214
            'splines'  => 'true',
215
            'sep'      => '.1',
216
            'fontsize' => '8'
217
          )
218
        );
219
 
220
        foreach (array_keys($this->dependencies) as $className) {
221
            $graph->addNode($className);
222
        }
223
 
224
        foreach ($this->dependencies as $from => $dependencies) {
225
            foreach ($dependencies as $to => $flag) {
226
                if ($flag === 1) {
227
                    $graph->addEdge(array($from => $to));
228
                }
229
            }
230
        }
231
 
232
        return $graph;
233
    }
234
 
235
    /**
236
     * Returns the duplicates found by the Copy & Paste Detection (CPD).
237
     *
238
     * @return array
239
     */
240
    public function getDuplicates()
241
    {
242
        return $this->cpdDuplicates;
243
    }
244
 
245
    /**
246
     * Returns the files of this project.
247
     *
248
     * @return array
249
     */
250
    public function getFiles()
251
    {
252
        return $this->files;
253
    }
254
 
255
    /**
256
     * A file.
257
     *
258
     * @param  string $className
259
     * @return ReflectionClass
260
     */
261
    public function getFile($filename)
262
    {
263
        return $this->files[$filename];
264
    }
265
 
266
    /**
267
     * Functions.
268
     *
269
     * @return array
270
     */
271
    public function getFunctions()
272
    {
273
        return $this->functions;
274
    }
275
 
276
    /**
277
     * A function.
278
     *
279
     * @param  string $functionName
280
     * @return ReflectionClass
281
     */
282
    public function getFunction($functionName)
283
    {
284
        return $this->functions[$functionName];
285
    }
286
 
287
    /**
288
     * Returns the Number of Classes (CLS) for the project.
289
     *
290
     * @return integer
291
     * @see    http://www.aivosto.com/project/help/pm-oo-misc.html
292
     */
293
    public function getCLS()
294
    {
295
        return $this->cls;
296
    }
297
 
298
    /**
299
     * Returns the Number of Abstract Classes (CLSa) for the project.
300
     *
301
     * @return integer
302
     * @see    http://www.aivosto.com/project/help/pm-oo-misc.html
303
     */
304
    public function getCLSa()
305
    {
306
        return $this->clsa;
307
    }
308
 
309
    /**
310
     * Returns the Number of Concrete Classes (CLSc) for the project.
311
     *
312
     * @return integer
313
     * @see    http://www.aivosto.com/project/help/pm-oo-misc.html
314
     */
315
    public function getCLSc()
316
    {
317
        return $this->clsc;
318
    }
319
 
320
    /**
321
     * Returns the Number of Root Classes (ROOTS) for the project.
322
     *
323
     * @return integer
324
     * @see    http://www.aivosto.com/project/help/pm-oo-misc.html
325
     */
326
    public function getRoots()
327
    {
328
        return $this->roots;
329
    }
330
 
331
    /**
332
     * Returns the Number of Leaf Classes (LEAFS) for the project.
333
     *
334
     * @return integer
335
     * @see    http://www.aivosto.com/project/help/pm-oo-misc.html
336
     */
337
    public function getLeafs()
338
    {
339
        return $this->leafs;
340
    }
341
 
342
    /**
343
     * Returns the Number of Interfaces (INTERFS) for the project.
344
     *
345
     * @return integer
346
     * @see    http://www.aivosto.com/project/help/pm-oo-misc.html
347
     */
348
    public function getInterfs()
349
    {
350
        return $this->interfs;
351
    }
352
 
353
    /**
354
     * Returns the Maximum Depth of Intheritance Tree (maxDIT) for the project.
355
     *
356
     * @return integer
357
     * @see    http://www.aivosto.com/project/help/pm-oo-misc.html
358
     */
359
    public function getMaxDit()
360
    {
361
        return $this->maxDit;
362
    }
363
 
364
    /**
365
     * Copy & Paste Detection (CPD).
366
     *
367
     * @param  integer $minLines
368
     * @param  integer $minMatches
369
     * @author Johann-Peter Hartmann <johann-peter.hartmann@mayflower.de>
370
     */
371
    protected function copyPasteDetection($minLines, $minMatches)
372
    {
373
        foreach ($this->files as $file) {
374
            $currentTokenPositions = array();
375
            $currentSignature      = '';
376
            $tokens                = $file->getTokens();
377
            $tokenNr               = 0;
378
            $line                  = 1;
379
 
380
            foreach (array_keys($tokens) as $key) {
381
                $token = $tokens[$key];
382
 
383
                if (is_string($token)) {
384
                    $line += substr_count($token, "\n");
385
                } else {
386
                    if (!in_array($token[0], self::$CPD_IGNORE_LIST)) {
387
                        $currentTokenPositions[$tokenNr++] = $line;
388
                        $currentSignature .= chr($token[0] & 255) . pack('N*', crc32($token[1]));
389
                    }
390
 
391
                    $line += substr_count($token[1], "\n");
392
                }
393
            }
394
 
395
            $tokenNr   = 0;
396
            $firstLine = 0;
397
            $found     = FALSE;
398
 
399
            if (count($currentTokenPositions) > 0) {
400
                do {
401
                    $line = $currentTokenPositions[$tokenNr];
402
 
403
                    $hash = substr(
404
                      md5(
405
                        substr(
406
                          $currentSignature, $tokenNr * 5,
407
                          $minMatches * 5
408
                        ),
409
                        TRUE
410
                      ),
411
                      0,
412
                      8
413
                    );
414
 
415
                    if (isset($this->cpdHashes[$hash])) {
416
                        $found = TRUE;
417
 
418
                        if ($firstLine === 0) {
419
                            $firstLine  = $line;
420
                            $firstHash  = $hash;
421
                            $firstToken = $tokenNr;
422
                        }
423
                    } else {
424
                        if ($found) {
425
                            $fileA      = $this->cpdHashes[$firstHash][0];
426
                            $firstLineA = $this->cpdHashes[$firstHash][1];
427
 
428
                            if ($line + 1 - $firstLine > $minLines &&
429
                                ($fileA->getPath() != $file->getPath() ||
430
                                 $firstLineA       != $firstLine)) {
431
                                $this->cpdDuplicates[] = array(
432
                                  'fileA'      => $fileA,
433
                                  'firstLineA' => $firstLineA,
434
                                  'fileB'      => $file,
435
                                  'firstLineB' => $firstLine,
436
                                  'numLines'   => $line    + 1 - $firstLine,
437
                                  'numTokens'  => $tokenNr + 1 - $firstToken
438
                                );
439
                            }
440
 
441
                            $found     = FALSE;
442
                            $firstLine = 0;
443
                        }
444
 
445
                        $this->cpdHashes[$hash] = array($file, $line);
446
                    }
447
 
448
                    $tokenNr++;
449
                } while ($tokenNr <= (count($currentTokenPositions) -
450
                         $minMatches )+1);
451
            }
452
 
453
            if ($found) {
454
                $fileA      = $this->cpdHashes[$firstHash][0];
455
                $firstLineA = $this->cpdHashes[$firstHash][1];
456
 
457
                if ($line + 1 - $firstLine > $minLines &&
458
                    ($fileA->getPath() != $file->getPath() ||
459
                     $firstLineA       != $firstLine)) {
460
                    $this->cpdDuplicates[] = array(
461
                      'fileA'      => $fileA,
462
                      'firstLineA' => $firstLineA,
463
                      'fileB'      => $file,
464
                      'firstLineB' => $firstLine,
465
                      'numLines'   => $line    + 1 - $firstLine,
466
                      'numTokens'  => $tokenNr + 1 - $firstToken
467
                    );
468
                }
469
 
470
                $found = FALSE;
471
            }
472
        }
473
    }
474
}
475
?>