Passed
Push — master ( 3a6c9c...7c86a5 )
by Siad
06:49
created

PHPUnitUtil::getDefinedClasses()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5.0073

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 2
dl 0
loc 29
ccs 14
cts 15
cp 0.9333
crap 5.0073
rs 9.4888
c 0
b 0
f 0
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19
20
/**
21
 * Various utility functions
22
 *
23
 * @author  Michiel Rook <[email protected]>
24
 * @package phing.tasks.ext.phpunit
25
 * @since   2.1.0
26
 */
27
class PHPUnitUtil
28
{
29
    protected static $definedClasses = [];
30
31
    /**
32
     * Returns the package of a class as defined in the docblock of the class using @package
33
     *
34
     * @param  string the name of the class
35
     * @return string the name of the package
36
     */
37
    public static function getPackageName($classname)
38
    {
39
        $reflect = new ReflectionClass($classname);
40
41
        if (method_exists($reflect, 'getNamespaceName')) {
42
            $namespace = $reflect->getNamespaceName();
43
44
            if ($namespace != '') {
45
                return $namespace;
46
            }
47
        }
48
49
        if (preg_match('/@package[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches)) {
50
            return $matches[1];
51
        }
52
53
        return "default";
54
    }
55
56
    /**
57
     * Returns the subpackage of a class as defined in the docblock of the class
58
     * using @subpackage
59
     *
60
     * @param string $classname the name of the class
61
     *
62
     * @author Benjamin Schultz <[email protected]>
63
     * @return string|null the name of the subpackage
64
     */
65
    public static function getSubpackageName($classname)
66
    {
67
        $reflect = new ReflectionClass($classname);
68
69
        if (preg_match('/@subpackage[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches)) {
70
            return $matches[1];
71
        } else {
72
            return null;
73
        }
74
    }
75
76
    /**
77
     * Derives the classname from a filename.
78
     * Assumes that there is only one class defined in that particular file, and that
79
     * the naming follows the dot-path (Java) notation scheme.
80
     *
81
     * @param  string the filename
82
     * @return string the name fo the class
83
     */
84
    public static function getClassFromFileName($filename)
85
    {
86
        $filename = basename($filename);
87
88
        $rpos = strrpos($filename, '.');
89
90
        if ($rpos != -1) {
91
            $filename = substr($filename, 0, $rpos);
92
        }
93
94
        return $filename;
95
    }
96
97
    /**
98
     * @param $filename
99
     * @param null $classpath
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $classpath is correct as it would always require null to be passed?
Loading history...
100
     * @throws Exception
101
     * @internal param the $string filename
102
     * @internal param optional $Path classpath
103
     * @return array list of classes defined in the file
104
     */
105 4
    public static function getDefinedClasses($filename, $classpath = null)
106
    {
107 4
        $filename = realpath($filename);
108
109 4
        if (!file_exists($filename)) {
110
            throw new Exception("File '" . $filename . "' does not exist");
111
        }
112
113 4
        if (isset(self::$definedClasses[$filename])) {
114 2
            return self::$definedClasses[$filename];
115
        }
116
117 3
        Phing::__import($filename, $classpath);
118
119 3
        $declaredClasses = get_declared_classes();
120
121 3
        foreach ($declaredClasses as $classname) {
122 3
            $reflect = new ReflectionClass($classname);
123
124 3
            self::$definedClasses[$reflect->getFilename()][] = $classname;
125
126 3
            if (is_array(self::$definedClasses[$reflect->getFilename()])) {
127 3
                self::$definedClasses[$reflect->getFilename()] = array_unique(
128 3
                    self::$definedClasses[$reflect->getFilename()]
129
                );
130
            }
131
        }
132
133 3
        return self::$definedClasses[$filename] ?? [];
134
    }
135
}
136