Passed
Push — main ( 46fa35...3ac4cc )
by Michiel
06:19
created

PHPUnitUtil::getClassFromFileName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 6
rs 10
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
use Phing\Phing;
21
22
/**
23
 * Various utility functions
24
 *
25
 * @author  Michiel Rook <[email protected]>
26
 * @package phing.tasks.ext.phpunit
27
 * @since   2.1.0
28
 */
29
class PHPUnitUtil
30
{
31
    protected static $definedClasses = [];
32
33
    /**
34
     * Returns the package of a class as defined in the docblock of the class using {@package}
35
     *
36
     * @param string the name of the class
37
     * @return string the name of the package
38
     * @throws ReflectionException
39
     */
40
    public static function getPackageName($classname)
41
    {
42
        $reflect = new ReflectionClass($classname);
43
44
        if (method_exists($reflect, 'getNamespaceName')) {
45
            $namespace = $reflect->getNamespaceName();
46
47
            if ($namespace != '') {
48
                return $namespace;
49
            }
50
        }
51
52
        if (preg_match('/@package[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches)) {
53
            return $matches[1];
54
        }
55
56
        return "default";
57
    }
58
59
    /**
60
     * Returns the subpackage of a class as defined in the docblock of the class
61
     * using {@subpackage}
62
     *
63
     * @param string $classname the name of the class
64
     *
65
     * @return string|null the name of the subpackage
66
     * @throws ReflectionException
67
     * @author Benjamin Schultz <[email protected]>
68
     */
69
    public static function getSubpackageName($classname)
70
    {
71
        $reflect = new ReflectionClass($classname);
72
73
        if (preg_match('/@subpackage[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches)) {
74
            return $matches[1];
75
        }
76
77
        return null;
78
    }
79
80
    /**
81
     * @param $filename
82
     * @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...
83
     * @throws Exception
84
     * @internal param the $string filename
85
     * @internal param optional $Path classpath
86
     * @return array list of classes defined in the file
87
     */
88 4
    public static function getDefinedClasses($filename, $classpath = null)
89
    {
90 4
        $filename = realpath($filename);
91
92 4
        if (!file_exists($filename)) {
93
            throw new Exception("File '" . $filename . "' does not exist");
94
        }
95
96 4
        if (isset(self::$definedClasses[$filename])) {
97 2
            return self::$definedClasses[$filename];
98
        }
99
100 3
        Phing::importFile($filename, $classpath);
101
102 3
        $declaredClasses = get_declared_classes();
103
104 3
        foreach ($declaredClasses as $classname) {
105 3
            $reflect = new ReflectionClass($classname);
106
107 3
            self::$definedClasses[$reflect->getFilename()][] = $classname;
108
109 3
            if (is_array(self::$definedClasses[$reflect->getFilename()])) {
110 3
                self::$definedClasses[$reflect->getFilename()] = array_unique(
111 3
                    self::$definedClasses[$reflect->getFilename()]
112
                );
113
            }
114
        }
115
116 3
        return self::$definedClasses[$filename] ?? [];
117
    }
118
}
119