Conditions | 12 |
Paths | 9 |
Total Lines | 57 |
Code Lines | 32 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
61 | public function process(File $phpcsFile, $stackPtr): void |
||
62 | { |
||
63 | $varRegExp = '/\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/'; |
||
64 | |||
65 | $tokens = $phpcsFile->getTokens(); |
||
66 | $content = $tokens[$stackPtr]['content']; |
||
67 | |||
68 | $matches = []; |
||
69 | |||
70 | \preg_match_all($varRegExp, $content, $matches, PREG_OFFSET_CAPTURE); |
||
71 | |||
72 | foreach ($matches as $match) { |
||
73 | foreach ($match as [$var, $pos]) { |
||
74 | if (1 !== $pos && '{' === $content[($pos - 1)]) { |
||
75 | continue; |
||
76 | } |
||
77 | |||
78 | if (\strpos(\substr($content, 0, $pos), '{') > 0 |
||
79 | && false === \strpos(\substr($content, 0, $pos), '}') |
||
80 | ) { |
||
81 | continue; |
||
82 | } |
||
83 | |||
84 | $lastOpeningBrace = \strrpos(\substr($content, 0, $pos), '{'); |
||
85 | |||
86 | if (false !== $lastOpeningBrace |
||
87 | && '$' === $content[($lastOpeningBrace + 1)] |
||
88 | ) { |
||
89 | $lastClosingBrace = \strrpos(\substr($content, 0, $pos), '}'); |
||
90 | |||
91 | if (false !== $lastClosingBrace |
||
92 | && $lastClosingBrace < $lastOpeningBrace |
||
93 | ) { |
||
94 | continue; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | $fix = $phpcsFile->addFixableError( |
||
99 | \sprintf( |
||
100 | 'must surround variable %s with { }', |
||
101 | $var |
||
102 | ), |
||
103 | $stackPtr, |
||
104 | 'NotSurroundedWithBraces' |
||
105 | ); |
||
106 | |||
107 | if (true !== $fix) { |
||
108 | continue; |
||
109 | } |
||
110 | |||
111 | $correctVariable = $this->surroundVariableWithBraces( |
||
112 | $content, |
||
113 | $pos, |
||
114 | $var |
||
115 | ); |
||
116 | |||
117 | $this->fixPhpCsFile($stackPtr, $correctVariable, $phpcsFile); |
||
118 | } |
||
155 |