Passed
Branchmaster (721fdb)
by Thomas
04:23
created

FindByNamespace::buildData()   D

Complexity

Conditions 10
Paths 8

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10.0064

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
ccs 24
cts 25
cp 0.96
rs 4.8196
cc 10
eloc 18
nc 8
nop 1
crap 10.0064

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
                        $this->data[] = $namespace . '\\' . $tokens[$index][1];
62 1
                    }
63 1
                }
64 1
            }
65 1
        }
66 1
    }
67
68
    protected function filterData($needle)
69
    {
70 1
        return array_values(array_filter($this->data, function ($var) use ($needle) {
71 1
            if (strpos($var, $needle) !== false) {
72 1
                return true;
73
            }
74 1
            return false;
75 1
        }));
76
    }
77
78
}