Passed
Branchmaster (538d99)
by Thomas
03:15
created

FindByNamespace::buildData()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 12.0071

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 29
ccs 26
cts 27
cp 0.963
rs 5.1612
cc 12
eloc 19
nc 10
nop 1
crap 12.0071

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Created by PhpStorm.
5
 * User: tom
6
 * Date: 18/06/2016
7
 * Time: 16:23
8
 */
9
10
namespace twhiston\twLib\Discovery;
11
12
use RecursiveDirectoryIterator;
13
use RecursiveIteratorIterator;
14
use RegexIterator;
15
16
class FindByNamespace
17
{
18
19
    protected $path;
20
21
    protected $data;
22
23
    /**
24
     * FindByNamespace constructor.
25
     * @param $path
26
     */
27 1
    public function __construct($path = null)
28
    {
29 1
        $this->path = ($path === null) ? __DIR__ : $path;
30 1
        $this->data = [];
31 1
    }
32
33 1
    public function find($needle = null, $rebuild = false)
34
    {
35 1
        $this->buildData($rebuild);
36 1
        return $this->filterData($needle);
37
38
    }
39
40 1
    protected function buildData($rebuild)
41
    {
42 1
        if ($rebuild === true || empty($this->data)) {
43 1
            $allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->path));
44 1
            $phpFiles = new RegexIterator($allFiles, '/\.php$/');
45 1
            foreach ($phpFiles as $phpFile) {
46 1
                $content = file_get_contents($phpFile->getRealPath());
47 1
                $tokens = token_get_all($content);
48 1
                $namespace = '';
49 1
                for ($index = 0; isset($tokens[$index]); $index++) {
50 1
                    if (!isset($tokens[$index][0])) {
51
                        continue;
52
                    }
53 1
                    if (T_NAMESPACE === $tokens[$index][0]) {
54 1
                        $index += 2; // Skip namespace keyword and whitespace
55 1
                        while (isset($tokens[$index]) && is_array($tokens[$index])) {
56 1
                            $namespace .= $tokens[$index++][1];
57 1
                        }
58 1
                    }
59 1
                    if (T_CLASS === $tokens[$index][0]) {
60 1
                        $index += 2; // Skip class keyword and whitespace
61 1
                        if (is_array($tokens[$index]) && array_key_exists(1, $tokens[$index])) {
62 1
                            $this->data[] = $namespace . '\\' . $tokens[$index][1];
63 1
                        }
64 1
                    }
65 1
                }
66 1
            }
67 1
        }
68 1
    }
69
70
    protected function filterData($needle)
71
    {
72 1
        return array_values(array_filter($this->data, function ($var) use ($needle) {
73 1
            if (strpos($var, $needle) !== false) {
74 1
                return true;
75
            }
76 1
            return false;
77 1
        }));
78
    }
79
80
}