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 | /** |
||
4 | * This file is part of the mo4-coding-standard (phpcs standard) |
||
5 | * |
||
6 | * PHP version 5 |
||
7 | * |
||
8 | * @category PHP |
||
9 | * @package PHP_CodeSniffer-MO4 |
||
10 | * @author Xaver Loppenstedt <[email protected]> |
||
11 | * @license http://spdx.org/licenses/MIT MIT License |
||
12 | * @version GIT: master |
||
13 | * @link https://github.com/Mayflower/mo4-coding-standard |
||
14 | */ |
||
15 | |||
16 | namespace MO4\Sniffs\Strings; |
||
17 | |||
18 | use PHP_CodeSniffer\Sniffs\Sniff; |
||
19 | use PHP_CodeSniffer\Files\File; |
||
20 | |||
21 | /** |
||
22 | * Variable in Double Quoted String sniff. |
||
23 | * |
||
24 | * Variables in double quoted strings must be surrounded by { } |
||
25 | * |
||
26 | * @category PHP |
||
27 | * @package PHP_CodeSniffer-MO4 |
||
28 | * @author Xaver Loppenstedt <[email protected]> |
||
29 | * @copyright 2013 Xaver Loppenstedt, some rights reserved. |
||
30 | * @license http://spdx.org/licenses/MIT MIT License |
||
31 | * @link https://github.com/Mayflower/mo4-coding-standard |
||
32 | */ |
||
33 | class VariableInDoubleQuotedStringSniff implements Sniff |
||
34 | { |
||
35 | /** |
||
36 | * The PHP_CodeSniffer object controlling this run. |
||
37 | * |
||
38 | * @var File |
||
39 | */ |
||
40 | private $phpCsFile = null; |
||
41 | |||
42 | |||
43 | /** |
||
44 | * Registers the tokens that this sniff wants to listen for. |
||
45 | * |
||
46 | * @return array(int) |
||
0 ignored issues
–
show
|
|||
47 | * @see Tokens.php |
||
48 | */ |
||
49 | public function register() |
||
50 | { |
||
51 | return array(T_DOUBLE_QUOTED_STRING); |
||
52 | |||
53 | }//end register() |
||
54 | |||
55 | |||
56 | /** |
||
57 | * Called when one of the token types that this sniff is listening for |
||
58 | * is found. |
||
59 | * |
||
60 | * @param File $phpcsFile The PHP_CodeSniffer file where the |
||
61 | * token was found. |
||
62 | * @param int $stackPtr The position in the PHP_CodeSniffer |
||
63 | * file's token stack where the token |
||
64 | * was found. |
||
65 | * |
||
66 | * @return void |
||
67 | */ |
||
68 | public function process(File $phpcsFile, $stackPtr) |
||
69 | { |
||
70 | $this->phpCsFile = $phpcsFile; |
||
71 | |||
72 | $varRegExp = '/\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/'; |
||
73 | |||
74 | $tokens = $phpcsFile->getTokens(); |
||
75 | $content = $tokens[$stackPtr]['content']; |
||
76 | |||
77 | $matches = array(); |
||
78 | |||
79 | preg_match_all($varRegExp, $content, $matches, PREG_OFFSET_CAPTURE); |
||
80 | |||
81 | foreach ($matches as $match) { |
||
0 ignored issues
–
show
The expression
$matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?
There are different options of fixing this problem.
![]() |
|||
82 | foreach ($match as $info) { |
||
83 | list($var, $pos) = $info; |
||
84 | |||
85 | if ($pos === 1 || $content[($pos - 1)] !== '{') { |
||
86 | if (strpos(substr($content, 0, $pos), '{') > 0 |
||
87 | && strpos(substr($content, 0, $pos), '}') === false |
||
88 | ) { |
||
89 | continue; |
||
90 | } |
||
91 | |||
92 | $lastOpeningBrace = strrpos(substr($content, 0, $pos), '{'); |
||
93 | if ($lastOpeningBrace !== false |
||
94 | && $content[($lastOpeningBrace + 1)] === '$' |
||
95 | ) { |
||
96 | $lastClosingBrace = strrpos(substr($content, 0, $pos), '}'); |
||
97 | |||
98 | if ($lastClosingBrace !== false |
||
99 | && $lastClosingBrace < $lastOpeningBrace |
||
100 | ) { |
||
101 | continue; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | $fix = $this->phpCsFile->addFixableError( |
||
106 | sprintf( |
||
107 | 'must surround variable %s with {Â }', |
||
108 | $var |
||
109 | ), |
||
110 | $stackPtr, |
||
111 | 'NotSurroundedWithBraces' |
||
112 | ); |
||
113 | |||
114 | if ($fix === true) { |
||
115 | $correctVariable = $this->surroundVariableWithBraces( |
||
116 | $content, |
||
117 | $pos, |
||
118 | $var |
||
119 | ); |
||
120 | $this->fixPhpCsFile($stackPtr, $correctVariable); |
||
121 | } |
||
122 | }//end if |
||
123 | }//end foreach |
||
124 | }//end foreach |
||
125 | |||
126 | }//end process() |
||
127 | |||
128 | |||
129 | /** |
||
130 | * Surrounds a variable with curly brackets |
||
131 | * |
||
132 | * @param string $content content |
||
133 | * @param int $pos position |
||
134 | * @param string $var variable |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | private function surroundVariableWithBraces($content, $pos, $var) |
||
139 | { |
||
140 | $before = substr($content, 0, $pos); |
||
141 | $after = substr($content, ($pos + strlen($var))); |
||
142 | |||
143 | return $before.'{'.$var.'}'.$after; |
||
144 | |||
145 | }//end surroundVariableWithBraces() |
||
146 | |||
147 | |||
148 | /** |
||
149 | * Fixes the file |
||
150 | * |
||
151 | * @param int $stackPtr stack pointer |
||
152 | * @param string $correctVariable correct variable |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | private function fixPhpCsFile($stackPtr, $correctVariable) |
||
157 | { |
||
158 | $phpCsFile = $this->phpCsFile; |
||
159 | |||
160 | $phpCsFile->fixer->beginChangeset(); |
||
161 | $phpCsFile->fixer->replaceToken($stackPtr, $correctVariable); |
||
162 | $phpCsFile->fixer->endChangeset(); |
||
163 | |||
164 | }//end fixPhpCsFile() |
||
165 | |||
166 | |||
167 | }//end class |
||
168 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.