Completed
Push — master ( 3ccb4d...f0ec0e )
by Vladimir
05:12
created

CheckPluginFinder::getConfigClass()   B

Complexity

Conditions 10
Paths 9

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 10

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 41
ccs 24
cts 24
cp 1
rs 7.6666
c 0
b 0
f 0
cc 10
nc 9
nop 1
crap 10

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
 * 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 $searchDirs = [__DIR__.'/**', __DIR__.'/**/**'];
22
23 45
    public function __construct(array $dirs = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
24
    {
25 45
        if ($dirs) {
26 2
            $this->searchDirs = array_merge($this->searchDirs, $dirs);
27
        }
28 45
    }
29
30
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
     * @return string[]
32
     */
33 45
    public function find()
34
    {
35 45
        $fs = Finder::create();
36 45
        $files = $fs->in($this->searchDirs)->name('Plugin.php')->files();
37
38 45
        $res = [];
39 45
        foreach ($files as $f) {
40
            /* @var \SplFileInfo $f */
41
42 45
            $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 45
            $class = $this->getConfigClass($code);
44 45
            if (is_subclass_of($class, CheckPluginInterface::class)) {
45 45
                $res[] = $class;
46
            }
47
        }
48
49 45
        return array_unique($res);
50
    }
51
52 45
    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...
53
    {
54 45
        $namespace = [];
55 45
        $class = [];
56
57 45
        $tokens = token_get_all($contents);
58
59
        do {
60 45
            $token = current($tokens);
61
62 45
            if (isset($token[0]) && T_NAMESPACE === $token[0]) {
63 45
                next($tokens);
64
                do {
65 45
                    $token = current($tokens);
66 45
                    if (';' === $token) {
67 45
                        break 1;
68
                    }
69 45
                    $namespace[] = $token[1];
70 45
                } while (next($tokens));
71
72 45
                $namespace = trim(implode('', $namespace));
73
            }
74
75 45
            if (isset($token[0]) && T_CLASS === $token[0]) {
76 45
                next($tokens);
77
                do {
78 45
                    $token = current($tokens);
79
80 45
                    if (T_STRING === $token[0]) {
81 45
                        $class[] = $token[1];
82 45
                        break 1;
83
                    }
84 45
                } while (next($tokens));
85
86 45
                $class = trim(implode('', $class));
87
88 45
                break;
89
            }
90 45
        } while (next($tokens));
91
92 45
        return (string) $namespace.'\\'.(string) $class;
93
    }
94
}
95