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 <mrook@php.net>
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
     * @throws ReflectionException
37
     */
38 0
    public static function getPackageName($classname)
39
    {
40 0
        $reflect = new ReflectionClass($classname);
41

42 0
        if (method_exists($reflect, 'getNamespaceName')) {
43 0
            $namespace = $reflect->getNamespaceName();
44

45 0
            if ($namespace != '') {
46 0
                return $namespace;
47
            }
48
        }
49

50 0
        if (preg_match('/@package[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches)) {
51 0
            return $matches[1];
52
        }
53

54 0
        return "default";
55
    }
56

57
    /**
58
     * Returns the subpackage of a class as defined in the docblock of the class
59
     * using {@subpackage}
60
     *
61
     * @param string $classname the name of the class
62
     *
63
     * @return string|null the name of the subpackage
64
     * @throws ReflectionException
65
     * @author Benjamin Schultz <bschultz@proqrent.de>
66
     */
67 0
    public static function getSubpackageName($classname)
68
    {
69 0
        $reflect = new ReflectionClass($classname);
70

71 0
        if (preg_match('/@subpackage[\s]+([\.\w]+)/', $reflect->getDocComment(), $matches)) {
72 0
            return $matches[1];
73
        }
74

75 0
        return null;
76
    }
77

78
    /**
79
     * Derives the classname from a filename.
80
     * Assumes that there is only one class defined in that particular file, and that
81
     * the naming follows the dot-path (Java) notation scheme.
82
     *
83
     * @param  string the filename
84
     * @return string the name fo the class
85
     */
86 0
    public static function getClassFromFileName($filename)
87
    {
88 0
        $filename = basename($filename);
89

90 0
        $rpos = strrpos($filename, '.');
91

92 0
        if ($rpos != -1) {
93 0
            $filename = substr($filename, 0, $rpos);
94
        }
95

96 0
        return $filename;
97
    }
98

99
    /**
100
     * @param $filename
101
     * @param null $classpath
102
     * @throws Exception
103
     * @internal param the $string filename
104
     * @internal param optional $Path classpath
105
     * @return array list of classes defined in the file
106
     */
107 1
    public static function getDefinedClasses($filename, $classpath = null)
108
    {
109 1
        $filename = realpath($filename);
110

111 1
        if (!file_exists($filename)) {
112 0
            throw new Exception("File '" . $filename . "' does not exist");
113
        }
114

115 1
        if (isset(self::$definedClasses[$filename])) {
116 1
            return self::$definedClasses[$filename];
117
        }
118

119 1
        Phing::importFile($filename, $classpath);
120

121 1
        $declaredClasses = get_declared_classes();
122

123 1
        foreach ($declaredClasses as $classname) {
124 1
            $reflect = new ReflectionClass($classname);
125

126 1
            self::$definedClasses[$reflect->getFilename()][] = $classname;
127

128 1
            if (is_array(self::$definedClasses[$reflect->getFilename()])) {
129 1
                self::$definedClasses[$reflect->getFilename()] = array_unique(
130 1
                    self::$definedClasses[$reflect->getFilename()]
131
                );
132
            }
133
        }
134

135 1
        return self::$definedClasses[$filename] ?? [];
136
    }
137
}

Read our documentation on viewing source code .

Loading