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

TestFileLoader::loadFile()   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 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
4
namespace ParaTest\Runners\PHPUnit;
5
6
7
class TestFileLoader
8
{
9
    /**
10
     * The pattern used for grabbing test files. Uses the *Test.php convention
11
     * that PHPUnit defaults to.
12
     */
13
    const TEST_PATTERN = '/.+Test\.php$/';
14
15
    /**
16
     * Matches php files
17
     */
18
    const FILE_PATTERN = '/.+\.php$/';
19
20
    /**
21
     * Used to ignore directory paths '.' and '..'
22
     *
23
     * @var string
24
     */
25
    private static $dotPattern = '/([.]+)$/';
26
27
    /**
28
     * The collection of loaded files for this test suite
29
     *
30
     * @var array
31
     */
32
    protected $files = array();
33
34
    /**
35
     * The collection of excluded files
36
     *
37
     * @var array
38
     */
39
    protected $excludedFiles = array();
40
41
    /**
42
     * When true, the SuiteLoader add the files to excluded files
43
     *
44
     * @var bool
45
     */
46
    protected $excludingFiles = false;
47
48
    /**
49
     * @var Options
50
     */
51
    private $options;
52
53 25
    public function __construct($options = null)
54
    {
55 25
        if ($options && !$options instanceof Options) {
56 1
            throw new \InvalidArgumentException("SuiteLoader options must be null or of type Options");
57
        }
58
59 24
        $this->options = $options;
60 24
    }
61
62
    /**
63
     * Loads a SuitePath and makes sure to
64
     * take into account the excluded directory / files
65
     *
66
     * @param SuitePath $path
67
     * @return string[]
68
     */
69 12
    public function loadSuitePath(SuitePath $path)
70
    {
71
        // First initialize the list of files and excluded files
72 12
        $this->files          = array();
73 12
        $this->excludedFiles  = array();
74 12
        $this->excludingFiles = true;
75 12
        foreach ($path->getExcludedPaths() as $excludedPath) {
76 2
            $this->loadPath($excludedPath, $path->getPattern());
77 12
        }
78
79
        // Load the SuitePath
80 12
        $this->excludingFiles = false;
81 12
        $this->loadPath($path->getPath(), $path->getPattern());
82
83
        // Reinitialise the excluded files
84 12
        $this->excludedFiles = array();
85
86 12
        return $this->files;
87
    }
88
89
    /**
90
     * Loads suites based on a specific path.
91
     * A valid path can be a directory or file
92
     *
93
     * @param $path
94
     * @param $pattern
95
     * @throws \InvalidArgumentException
96
     * @return string[]
97
     */
98 22
    public function loadPath($path, $pattern = null)
99
    {
100 22
        $this->files = array();
101 22
        $path        = $path ?: $this->options->path;
0 ignored issues
show
Documentation introduced by
The property $path is declared protected in ParaTest\Runners\PHPUnit\Options. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
102 22
        $pattern     = is_null($pattern) ? self::TEST_PATTERN : $pattern;
103
104 22
        if (!file_exists($path)) {
105 2
            throw new \InvalidArgumentException("$path is not a valid directory or file");
106
        }
107 20
        if (is_dir($path)) {
108 13
            $this->loadDir($path, $pattern);
109 20
        } elseif (file_exists($path)) {
110 11
            $this->loadFile($path);
111 11
        }
112
113 20
        return $this->files;
114
    }
115
116
    /**
117
     * Loads suites from a directory
118
     *
119
     * @param string $path
120
     * @param string $pattern
121
     */
122 13
    private function loadDir($path, $pattern = self::TEST_PATTERN)
123
    {
124 13
        $files = scandir($path);
125 13
        foreach ($files as $file) {
126 13
            $this->tryLoadTests($path.DIRECTORY_SEPARATOR.$file, $pattern);
127 13
        }
128 13
    }
129
130
    /**
131
     * Load a single suite file
132
     *
133
     * @param $path
134
     */
135 11
    private function loadFile($path)
136
    {
137 11
        $this->tryLoadTests($path, self::FILE_PATTERN);
138 11
    }
139
140
    /**
141
     * Attempts to load suites from a path.
142
     *
143
     * @param string $path
144
     * @param string $pattern regular expression for matching file names
145
     */
146 20
    private function tryLoadTests($path, $pattern = self::TEST_PATTERN)
147
    {
148 20
        if (preg_match($pattern, $path)) {
149 20
            if ($this->excludingFiles) {
150 2
                $this->excludedFiles[$path] = $path;
151 20
            } elseif (!array_key_exists($path, $this->excludedFiles)) {
152 20
                $this->files[] = $path;
153 20
            }
154 20
        }
155
156 20
        if (!preg_match(self::$dotPattern, $path) && is_dir($path)) {
157 8
            $this->loadDir($path, $pattern);
158 8
        }
159 20
    }
160
161
}