SuiteLoader   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 74
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A load() 0 8 2
A getTests() 0 12 3
A globRecursive() 0 10 2
1
<?php
2
namespace Peridot\Runner;
3
4
/**
5
 * SuiteLoader will recursively load test files given a glob friendly
6
 * pattern.
7
 *
8
 * @package Peridot\Runner
9
 */
10
class SuiteLoader implements SuiteLoaderInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    protected $pattern;
16
17
    /**
18
     * @var Context
19
     */
20
    protected $context;
21
22
    /**
23
     * @param string $pattern
24
     */
25
    public function __construct($pattern)
26
    {
27
        $this->pattern = $pattern;
28
        $this->context = Context::getInstance();
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @param $path
35
     */
36
    public function load($path)
37
    {
38
        $tests = $this->getTests($path);
39
        foreach ($tests as $test) {
40
            $this->context->setFile($test);
41
            include $test;
42
        }
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     *
48
     * @param $path
49
     * @return array
50
     * @throws \RuntimeException
51
     */
52
    public function getTests($path)
53
    {
54
        if (is_file($path)) {
55
            return [$path];
56
        }
57
        if (! file_exists($path)) {
58
            throw new \RuntimeException("Cannot load path $path");
59
        }
60
        $pattern = realpath($path) . '/' . $this->pattern;
61
62
        return $this->globRecursive($pattern);
63
    }
64
65
    /**
66
     * Simple recursive glob
67
     *
68
     * @link http://php.net/manual/en/function.glob.php#106595
69
     * @param $pattern
70
     * @param  int   $flags
71
     * @return array
72
     */
73
    protected function globRecursive($pattern, $flags = 0)
74
    {
75
        $files = glob($pattern, $flags);
76
77
        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
78
            $files = array_merge($files, $this->globRecursive($dir .'/'. basename($pattern), $flags));
79
        }
80
81
        return $files;
82
    }
83
}
84