Issues (2921)

src/Check/CheckPluginFinder.php (21 issues)

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
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
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
Missing short description in doc comment
Loading history...
17
 * @author Vladimir Turnaev <[email protected]>
18
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @package tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
19
class CheckPluginFinder
20
{
21
    protected $searchExps = [__DIR__.'/**', __DIR__.'/**/**'];
22
23 57
    public function __construct(array $exps = null)
0 ignored issues
show
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
Missing doc comment for function addCheckPluginFinderPath()
Loading history...
The type Tvi\MonitorBundle\Check\CheckPluginFinderPath was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
    {
32
        foreach ($checkPluginFinderPath->getPathes() as $path) {
33
            $this->addSearchExps($path);
34
        }
35
    }
36
37 10
    public function addSearchExps($exps)
0 ignored issues
show
Missing doc comment for function addSearchExps()
Loading history...
38
    {
39 10
        $exps = \is_string($exps) ? [$exps] : $exps;
40
41 10
        $this->searchExps = array_merge($this->searchExps, $exps);
42 10
        $this->searchExps = array_unique($this->searchExps);
43 10
    }
44
45
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
46
     * @return string[]
47
     */
48 57
    public function find()
49
    {
50 57
        $fs = Finder::create();
51 57
        $res = [];
52 57
        foreach ($this->searchExps as $searchExp) {
53
            try {
54 57
                $files = $fs->in($searchExp)->name('Plugin.php')->files();
55
56 57
                foreach ($files as $f) {
57
                    /* @var \SplFileInfo $f */
58
59 57
                    $code = $f->getContents();
0 ignored issues
show
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

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