Completed
Pull Request — master (#237)
by
unknown
02:22
created

Parser::getClassName()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 24
ccs 14
cts 14
cp 1
rs 8.5125
c 1
b 0
f 0
cc 6
eloc 13
nc 10
nop 2
crap 6
1
<?php
2
namespace ParaTest\Parser;
3
4
class Parser
5
{
6
    /**
7
     * The path to the source file to parse
8
     *
9
     * @var string
10
     */
11
    private $path;
12
13
    /**
14
     * @var \ReflectionClass
15
     */
16
    private $refl;
17
18
    /**
19
     * Matches a test method beginning with the conventional "test"
20
     * word
21
     *
22
     * @var string
23
     */
24
    private static $testName = '/^test/';
25
26
    /**
27
     * A pattern for matching test methods that use the @test annotation
28
     *
29
     * @var string
30
     */
31
    private static $testAnnotation = '/@test\b/';
32
33 31
    public function __construct($srcPath)
34
    {
35 31
        if (!file_exists($srcPath)) {
36 1
            throw new \InvalidArgumentException("file not found: " . $srcPath);
37
        }
38
39 30
        $this->path = $srcPath;
40 30
        $declaredClasses = get_declared_classes();
41 30
        require_once($this->path);
42 30
        $class = $this->getClassName($this->path, $declaredClasses);
43 30
        if (!$class) {
44 2
            throw new NoClassInFileException();
45
        }
46
        try {
47 28
            $this->refl = new \ReflectionClass($class);
48
        } catch (\ReflectionException $e) {
49
            throw new \InvalidArgumentException("Unable to instantiate ReflectionClass. " . $class . " not found in: " . $srcPath);
50
        }
51 28
    }
52
53
    /**
54
     * Returns the fully constructed class
55
     * with methods or null if the class is abstract
56
     *
57
     * @return null|ParsedClass
58
     */
59 28
    public function getClass()
60
    {
61 28
        return ($this->refl->isAbstract())
62
            ? null
63 28
            : new ParsedClass(
64 28
                $this->refl->getDocComment(),
65 28
                $this->getCleanReflectionName(),
66 28
                $this->refl->getNamespaceName(),
67 28
                $this->getMethods()
68
            );
69
    }
70
71
    /**
72
     * Return reflection name with null bytes stripped
73
     *
74
     * @return string
75
     */
76 28
    private function getCleanReflectionName()
77
    {
78 28
        return str_replace("\x00", '', $this->refl->getName());
79
    }
80
81
    /**
82
     * Return all test methods present in the file
83
     *
84
     * @return array
85
     */
86 28
    private function getMethods()
87
    {
88 28
        $tests = array();
89 28
        $methods = $this->refl->getMethods(\ReflectionMethod::IS_PUBLIC);
90 28
        foreach ($methods as $method) {
91 26
            $hasTestName = preg_match(self::$testName, $method->getName());
92 26
            $hasTestAnnotation = preg_match(self::$testAnnotation, $method->getDocComment());
93 26
            $isTestMethod = $hasTestName || $hasTestAnnotation;
94 26
            if ($isTestMethod) {
95 26
                $tests[] = new ParsedFunction($method->getDocComment(), 'public', $method->getName());
96
            }
97
        }
98 28
        return $tests;
99
    }
100
101
    /**
102
     * Return the class name of the class contained
103
     * in the file
104
     *
105
     * @return string
106
     */
107 30
    private function getClassName($filename, $previousDeclaredClasses)
108
    {
109 30
        $filename = realpath($filename);
110 30
        $classes = get_declared_classes();
111 30
        $newClasses = array_values(array_diff($classes, $previousDeclaredClasses));
112
113 30
        foreach ($newClasses as $className) {
114 12
            $class = new \ReflectionClass($className);
115 12
            if ($class->getFileName() == $filename) {
116 12
                if ($this->classNameMatchesFileName($filename, $className)) {
117 12
                    return $className;
118
                }
119
            }
120
        }
121
122
        // Test class was loaded before somehow
123
        // (referenced from other test class, explicitly loaded, or filename does not match classname)
124 25
        foreach ($classes as $className) {
125 25
            $class = new \ReflectionClass($className);
126 25
            if ($class->getFileName() == $filename) {
127 25
                return $className;
128
            }
129
        }
130 2
    }
131
132
    /**
133
     * @param $filename
134
     * @param $className
135
     * @return bool
136
     */
137 12
    private function classNameMatchesFileName($filename, $className)
138
    {
139 12
        return strpos($filename, $className) !== false
140 12
            || strpos($filename, $this->invertSlashes($className)) !== false;
141
    }
142
143 5
    private function invertSlashes($className)
144
    {
145 5
        return str_replace('\\', '/', $className);
146
    }
147
}
148