Passed
Push — master ( e0f7b0...4354d2 )
by Vladimir
05:39 queued 20s
created

CheckFinder   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 84
rs 10
c 0
b 0
f 0
ccs 40
cts 42
cp 0.9524
wmc 16

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A find() 0 19 3
B getConfigClass() 0 49 11
1
<?php
2
/**
3
 * This file is part of the `tvi/monitor-bundle` project.
4
 *
5
 * (c) https://github.com/turnaev/monitor-bundle/graphs/contributors
6
 *
7
 * For the full copyright and license information, please view the LICENSE.md
8
 * file that was distributed with this source code.
9
 */
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...
10
11
namespace Tvi\MonitorBundle\Check;
12
13
use Symfony\Component\Finder\Finder;
14
15
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
16
 * @author Vladimir Turnaev <[email protected]>
17
 */
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...
18
class CheckFinder
19
{
20
    protected $searchDirs = [__DIR__.'/**', __DIR__.'/**/**'];
21
22 41
    public function __construct(array $dirs = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
23
    {
24 41
        if($dirs) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
25
            $this->searchDirs = array_merge($this->searchDirs, $dirs);
26
        }
27 41
    }
28
29
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
30
     * @return string[]
31
     */
32 41
    public function find()
33
    {
34 41
        $fs = Finder::create();
35 41
        $files = $fs->in($this->searchDirs)->name('*.php')->files();
36
37 41
        $res = [];
38 41
        foreach ($files as $f) {
39
            /* @var \SplFileInfo $f */
40
41 41
            $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

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