Completed
Push — master ( b327c3...e0e4ad )
by Vladimir
05:32
created

CheckFinder::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 CheckFinder
20
{
21
    protected $searchDirs = [__DIR__.'/**', __DIR__.'/**/**'];
22
23 43
    public function __construct(array $dirs = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
24
    {
25 43
        if ($dirs) {
26 1
            $this->searchDirs = array_merge($this->searchDirs, $dirs);
27
        }
28 43
    }
29
30
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
     * @return string[]
32
     */
33 43
    public function find()
34
    {
35 43
        $fs = Finder::create();
36 43
        $files = $fs->in($this->searchDirs)->name('Config.php')->files();
37
38 43
        $res = [];
39 43
        foreach ($files as $f) {
40
            /* @var \SplFileInfo $f */
41
42 43
            $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

42
            /** @scrutinizer ignore-call */ 
43
            $code = $f->getContents();
Loading history...
43 43
            $class = $this->getConfigClass($code);
44 43
            if (is_subclass_of($class, CheckConfigInterface::class)) {
45 43
                $res[] = $class;
46
            }
47
        }
48
49 43
        $res = array_unique($res);
50
51 43
        return $res;
52
    }
53
54 43
    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...
55
    {
56 43
        $namespace = [];
57 43
        $class = [];
58
59 43
        $tokens = token_get_all($contents);
60
61
        do {
62 43
            $token = current($tokens);
63
64 43
            if (isset($token[0]) && T_NAMESPACE == $token[0]) {
65 43
                next($tokens);
66
                do {
67 43
                    $token = current($tokens);
68 43
                    if (';' == $token) {
69 43
                        break 1;
70
                    }
71 43
                    $namespace[] = $token[1];
72 43
                } while (next($tokens));
73
74 43
                $namespace = trim(implode('', $namespace));
75
            }
76
77 43
            if (isset($token[0]) && T_CLASS == $token[0]) {
78 43
                next($tokens);
79
                do {
80 43
                    $token = current($tokens);
81
82 43
                    if (T_STRING == $token[0]) {
83 43
                        $class[] = $token[1];
84 43
                        break 1;
85
                    }
86 43
                } while (next($tokens));
87
88 43
                $class = trim(implode('', $class));
89
90 43
                break;
91
            }
92 43
        } while (next($tokens));
93
94 43
        $configClass = (string) $namespace.'\\'.(string) $class;
95
96 43
        return $configClass;
97
    }
98
}
99