Completed
Pull Request — master (#216)
by
unknown
04:14
created

Parser::getCleanReflectionName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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