This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* |
||
3 | * This file is part of project-quality-inspector. |
||
4 | * |
||
5 | * (c) Alexandre GESLIN <[email protected]> |
||
6 | * |
||
7 | * For the full copyright and license information, please view the LICENSE |
||
8 | * file that was distributed with this source code. |
||
9 | */ |
||
10 | |||
11 | namespace ProjectQualityInspector\Rule; |
||
12 | |||
13 | use ProjectQualityInspector\Application\ProcessHelper; |
||
14 | use ProjectQualityInspector\Exception\ExpectationFailedException; |
||
15 | |||
16 | /** |
||
17 | * Class FilesRule |
||
18 | * |
||
19 | * @package ProjectQualityInspector\Rule |
||
20 | */ |
||
21 | class FilesRule extends AbstractRule |
||
22 | { |
||
23 | public function __construct(array $config, $baseDir) |
||
24 | { |
||
25 | parent::__construct($config, $baseDir); |
||
26 | } |
||
27 | |||
28 | /** |
||
29 | * @inheritdoc |
||
30 | */ |
||
31 | public function evaluate() |
||
32 | { |
||
33 | $expectationsFailedExceptions = []; |
||
34 | |||
35 | foreach ($this->config as $fileConf) { |
||
36 | try { |
||
37 | $files = $this->expectsFilesGlobExists($fileConf, $this->baseDir); |
||
38 | if (isset($fileConf['grep']) && count($files)) { |
||
39 | $this->expectsFilesGrep($files, $fileConf['grep'], $this->getReason($fileConf)); |
||
0 ignored issues
–
show
|
|||
40 | } |
||
41 | $this->addAssertion($this->getValue($fileConf)); |
||
0 ignored issues
–
show
It seems like
$this->getValue($fileConf) targeting ProjectQualityInspector\...bstractRule::getValue() can also be of type array ; however, ProjectQualityInspector\...actRule::addAssertion() does only seem to accept string , maybe add an additional type check?
This check looks at variables that are passed out again to other methods. If the outgoing method call has stricter type requirements than the method itself, an issue is raised. An additional type check may prevent trouble. ![]() |
|||
42 | } catch (ExpectationFailedException $e) { |
||
43 | $expectationsFailedExceptions[] = $e; |
||
44 | $this->addAssertion($this->getValue($fileConf), [['message' => $e->getMessage() . $e->getReason(), 'type' => 'expectsFilesGlobExists|expectsFilesGrep']]); |
||
0 ignored issues
–
show
It seems like
$this->getValue($fileConf) targeting ProjectQualityInspector\...bstractRule::getValue() can also be of type array ; however, ProjectQualityInspector\...actRule::addAssertion() does only seem to accept string , maybe add an additional type check?
This check looks at variables that are passed out again to other methods. If the outgoing method call has stricter type requirements than the method itself, an issue is raised. An additional type check may prevent trouble. ![]() |
|||
45 | } |
||
46 | } |
||
47 | |||
48 | if (count($expectationsFailedExceptions)) { |
||
49 | $this->throwRuleViolationException($expectationsFailedExceptions); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param $fileConf |
||
55 | * @param $baseDir |
||
56 | * @return array |
||
57 | */ |
||
58 | private function expectsFilesGlobExists($fileConf, $baseDir) |
||
59 | { |
||
60 | $files = []; |
||
61 | $fileName = $this->getValue($fileConf); |
||
62 | $reason = $this->getReason($fileConf); |
||
63 | |||
64 | $filePath = $baseDir . DIRECTORY_SEPARATOR . $fileName; |
||
65 | |||
66 | if ($fileName[0] == '!') { |
||
67 | $fileName = ltrim($fileName, '!'); |
||
68 | $filePath = $baseDir . DIRECTORY_SEPARATOR . $fileName; |
||
69 | $this->globShouldNotFind($filePath, $reason); |
||
0 ignored issues
–
show
It seems like
$reason defined by $this->getReason($fileConf) on line 62 can also be of type array or null ; however, ProjectQualityInspector\...le::globShouldNotFind() does only seem to accept string , maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. ![]() |
|||
70 | } else { |
||
71 | $files = $this->globShouldFind($filePath, $reason); |
||
72 | } |
||
73 | |||
74 | return $files; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param $filePathGlob |
||
79 | * @param $reason |
||
80 | * @return array |
||
81 | * |
||
82 | * @throws ExpectationFailedException |
||
83 | */ |
||
84 | private function globShouldFind($filePathGlob, $reason) |
||
85 | { |
||
86 | $message = sprintf('file <fg=green>"%s"</> should exists', $filePathGlob); |
||
87 | $files = glob($filePathGlob); |
||
88 | if (!count($files)) { |
||
89 | throw new ExpectationFailedException($filePathGlob, $message, $reason); |
||
90 | } |
||
91 | |||
92 | return $files; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @param string $filePathGlob |
||
97 | * @param string $reason |
||
98 | * |
||
99 | * @throws ExpectationFailedException |
||
100 | */ |
||
101 | private function globShouldNotFind($filePathGlob, $reason) |
||
102 | { |
||
103 | $message = sprintf('file <fg=green>"%s"</> should not exists', $filePathGlob); |
||
104 | |||
105 | if (count(glob($filePathGlob))) { |
||
106 | throw new ExpectationFailedException($filePathGlob, $message, $reason); |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param array $files |
||
112 | * @param array|string $greps |
||
113 | * @param string $reason |
||
114 | */ |
||
115 | private function expectsFilesGrep(array $files, $greps, $reason) |
||
116 | { |
||
117 | $greps = (!is_array($greps)) ? [$greps] : $greps; |
||
118 | |||
119 | foreach ($files as $file) { |
||
120 | foreach ($greps as $grep) { |
||
121 | $this->fileShouldGrep($file, $grep, $reason); |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @param string $filePath |
||
128 | * @param string $grep |
||
129 | * @param string $reason |
||
130 | * |
||
131 | * @throws ExpectationFailedException |
||
132 | */ |
||
133 | private function fileShouldGrep($filePath, $grep, $reason) |
||
134 | { |
||
135 | $message = sprintf('file <fg=green>"%s"</> should contain <fg=green>"%s"</> string', $filePath, $grep); |
||
136 | $negation = false; |
||
137 | $recursiveOption = ''; |
||
138 | |||
139 | if ($grep[0] == '!') { |
||
140 | $grep = ltrim($grep, '!'); |
||
141 | $negation = true; |
||
142 | $message = sprintf('file <fg=green>"%s"</> should not contain <fg=green>"%s"</> string', $filePath, $grep); |
||
143 | } |
||
144 | |||
145 | if (is_dir($filePath)) { |
||
146 | $recursiveOption = '-r'; |
||
147 | } |
||
148 | |||
149 | $result = ProcessHelper::execute(sprintf('grep %s %s %s', $recursiveOption, escapeshellarg($grep), $filePath), $this->baseDir, true); |
||
150 | |||
151 | if (($negation && count($result)) || (!$negation && !count($result))) { |
||
152 | throw new ExpectationFailedException($filePath, $message, $reason); |
||
153 | } |
||
154 | } |
||
155 | } |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.