Test Failed
Push — master ( f50264...02e3d1 )
by Vladimir
05:15
created

CheckPluginFinder::addCheckPluginFinderPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
ccs 1
cts 1
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * This file is part of the `tvi/monitor-bundle` project.
5
 *
6
 * (c) https://github.com/turnaev/monitor-bundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
11
12
namespace Tvi\MonitorBundle\Check;
13
14
use Symfony\Component\Finder\Finder;
15
16
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
17
 * @author Vladimir Turnaev <[email protected]>
18
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
19
class CheckPluginFinder
20
{
21
    protected $searchExps = [];
22
23 57
    public function __construct(array $exps = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
24
    {
25 57
        if ($exps) {
26 10
            $this->addSearchExps($exps);
27
        }
28 57
    }
29
30
    public function addCheckPluginFinderPath(CheckPluginFinderPath $checkPluginFinderPath)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addCheckPluginFinderPath()
Loading history...
31
    {
32
        foreach ($checkPluginFinderPath->getPathes() as $path) {
33 57
            $this->addSearchExps($path);
34
        }
35 57
    }
36 57
37
    public function addSearchExps($exps)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function addSearchExps()
Loading history...
38 57
    {
39 57
        $exps = \is_string($exps) ? [$exps] : $exps;
40
41
        $this->searchExps = array_merge($this->searchExps, $exps);
42 57
        $this->searchExps = array_unique($this->searchExps);
43 57
    }
44
45 57
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46 57
     * @return string[]
47
     */
48
    public function find()
49
    {
50 57
        $fs = Finder::create();
51
        $files = $fs->in($this->searchExps)->name('Plugin.php')->files();
52
53 57
        $res = [];
54
        foreach ($files as $f) {
55 57
            /* @var \SplFileInfo $f */
56 57
57
            $code = $f->getContents();
0 ignored issues
show
Bug introduced by
The method getContents() does not exist on SplFileInfo. It seems like you code against a sub-type of SplFileInfo such as Symfony\Component\Finder\SplFileInfo or Symfony\Component\Finder...terator\MockSplFileInfo. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
            /** @scrutinizer ignore-call */ 
58
            $code = $f->getContents();
Loading history...
58 57
            $class = $this->getConfigClass($code);
59
60
            if (is_subclass_of($class, CheckPluginInterface::class)) {
61 57
                $res[] = $class;
62
            }
63 57
        }
64 57
65
        return array_unique($res);
66 57
    }
67 57
68 57
    private function getConfigClass($contents)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getConfigClass()
Loading history...
Coding Style introduced by
Private method name "CheckPluginFinder::getConfigClass" must be prefixed with an underscore
Loading history...
69
    {
70 57
        $namespace = [];
71 57
        $class = [];
72
73 57
        $tokens = token_get_all($contents);
74
75
        do {
76 57
            $token = current($tokens);
77 57
78
            if (isset($token[0]) && T_NAMESPACE === $token[0]) {
79 57
                next($tokens);
80
                do {
81 57
                    $token = current($tokens);
82 57
                    if (';' === $token) {
83 57
                        break 1;
84
                    }
85 57
                    $namespace[] = $token[1];
86
                } while (next($tokens));
87 57
88
                $namespace = trim(implode('', $namespace));
89 57
            }
90
91 57
            if (isset($token[0]) && T_CLASS === $token[0]) {
92
                next($tokens);
93 57
                do {
94
                    $token = current($tokens);
95
96
                    if (T_STRING === $token[0]) {
97
                        $class[] = $token[1];
98
                        break 1;
99
                    }
100
                } while (next($tokens));
101
102
                $class = trim(implode('', $class));
103
104
                break;
105
            }
106
        } while (next($tokens));
107
108
        return (string) $namespace.'\\'.(string) $class;
109
    }
110
}
111