Completed
Push — master ( cb3538...cd00cd )
by Julian
14s
created

Parser   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 160
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 23
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 160
ccs 57
cts 60
cp 0.95
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 4
A getClass() 0 11 2
A getCleanReflectionName() 0 4 1
A getMethods() 0 14 4
A getClassName() 0 16 3
B _searchForUnitTestClass() 0 17 6
A classNameMatchesFileName() 0 5 2
A invertSlashes() 0 4 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 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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $class of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
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 = [];
89 28
        $methods = $this->refl->getMethods(\ReflectionMethod::IS_PUBLIC);
90 28
        foreach ($methods as $method) {
91 28
            $hasTestName = preg_match(self::$testName, $method->getName());
92 28
            $hasTestAnnotation = preg_match(self::$testAnnotation, $method->getDocComment());
93 28
            $isTestMethod = $hasTestName || $hasTestAnnotation;
94 28
            if ($isTestMethod) {
95 28
                $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
        $className = $this->_searchForUnitTestClass($newClasses, $filename);
114 30
        if (isset($className)) {
115 12
            return $className;
116
        }
117
118 22
        $className = $this->_searchForUnitTestClass($classes, $filename);
119 22
        if (isset($className)) {
120 20
            return $className;
121
        }
122 2
    }
123
124
    /**
125
     * Search for the name of the unit test
126
     * @param string[] $classes
127
     * @param string $filename
128
     * @return string|null
129
     */
130 30
    private function _searchForUnitTestClass(array $classes, $filename) {
131
        // TODO: After merging this PR or other PR for phpunit 6 support, keep only the applicable subclass name
132 30
        $matchingClassName = null;
133 30
        foreach ($classes as $className) {
134 30
            $class = new \ReflectionClass($className);
135 30
            if ($class->getFileName() == $filename) {
136 28
                if ($class->isSubclassOf('PHPUnit\Framework\TestCase')) {
137 28
                    if ($this->classNameMatchesFileName($filename, $className)) {
138 21
                        return $className;
139 18
                    } else if ($matchingClassName === null) {
140 27
                        $matchingClassName = $className;
141
                    }
142
                }
143
            }
144
        }
145 25
        return $matchingClassName;
146
    }
147
148
    /**
149
     * @param $filename
150
     * @param $className
151
     * @return bool
152
     */
153 28
    private function classNameMatchesFileName($filename, $className)
154
    {
155 28
        return strpos($filename, $className) !== false
156 28
            || strpos($filename, $this->invertSlashes($className)) !== false;
157
    }
158
159 18
    private function invertSlashes($className)
160
    {
161 18
        return str_replace('\\', '/', $className);
162
    }
163
}
164