Passed
Push — master ( 5a9394...ac3c5f )
by Thomas
03:09
created

FindByNamespace   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 89.58%

Importance

Changes 7
Bugs 1 Features 3
Metric Value
wmc 21
c 7
b 1
f 3
lcom 1
cbo 0
dl 0
loc 81
ccs 43
cts 48
cp 0.8958
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setPath() 0 4 1
A find() 0 6 1
A __construct() 0 6 3
B buildData() 0 16 5
B processTokens() 0 21 9
A filterData() 0 9 2
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 2
    public function __construct($path = null)
28
    {
29 2
        $this->path = ($path === null || $path === '') ? __DIR__ : $path;
30 2
        $this->data = [];
31 2
        $this->data[$path] = [];
32 2
    }
33
34
    public function setPath($path)
35
    {
36
        $this->path = $path;
37
    }
38
39 2
    public function find($needle = null, $rebuild = false)
40
    {
41 2
        $this->buildData($rebuild);
42 2
        return $this->filterData($needle);
43
44
    }
45
46 2
    protected function buildData($rebuild)
47
    {
48 2
        if ($rebuild === true || empty($this->data[$this->path])) {
49 2
            $allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->path));
50 2
            $phpFiles = new RegexIterator($allFiles, '/\.php$/');
51 2
            foreach ($phpFiles as $phpFile) {
52
                //tokenize file
53 2
                if (file_exists($phpFile->getRealPath())) {
54 2
                    $content = file_get_contents($phpFile->getRealPath());
55 2
                    $this->processTokens(token_get_all($content));
56 2
                } else {
57
                    $this->data[$this->path] = [];
58
                }
59 2
            }
60 2
        }
61 2
    }
62
63 2
    protected function processTokens(array $tokens)
64
    {
65 2
        $namespace = '';
66 2
        for ($index = 0; isset($tokens[$index]); $index++) {
67 2
            if (!isset($tokens[$index][0])) {
68
                continue;
69
            }
70 2
            if (T_NAMESPACE === $tokens[$index][0]) {
71 2
                $index += 2; // Skip namespace keyword and whitespace
72 2
                while (isset($tokens[$index]) && is_array($tokens[$index])) {
73 2
                    $namespace .= $tokens[$index++][1];
74 2
                }
75 2
            }
76 2
            if (T_CLASS === $tokens[$index][0]) {
77 2
                $index += 2; // Skip class keyword and whitespace
78 2
                if (is_array($tokens[$index]) && array_key_exists(1, $tokens[$index])) {
79 2
                    $this->data[$this->path][] = $namespace . '\\' . $tokens[$index][1];
80 2
                }
81 2
            }
82 2
        }
83 2
    }
84
85
86
    protected function filterData($needle)
87
    {
88 2
        return array_values(array_filter($this->data[$this->path], function ($var) use ($needle) {
89 2
            if (strpos($var, $needle) !== false) {
90 1
                return true;
91
            }
92 2
            return false;
93 2
        }));
94
    }
95
96
}