|
4 | 4 | import com.intellij.openapi.util.Pair;
|
5 | 5 | import com.intellij.psi.PsiElement;
|
6 | 6 | import com.intellij.psi.PsiFile;
|
7 |
| -import com.intellij.psi.PsiReference; |
8 | 7 | import com.intellij.psi.search.FilenameIndex;
|
9 | 8 | import com.intellij.psi.search.GlobalSearchScope;
|
10 | 9 | import com.intellij.psi.tree.IElementType;
|
11 | 10 | import com.intellij.psi.util.PsiTreeUtil;
|
12 |
| -import com.intellij.util.CommonProcessors; |
13 | 11 | import com.jetbrains.php.PhpIndex;
|
14 | 12 | import com.jetbrains.php.lang.lexer.PhpTokenTypes;
|
15 | 13 | import com.jetbrains.php.lang.parser.PhpElementTypes;
|
16 |
| -import com.jetbrains.php.lang.psi.PhpPsiUtil; |
17 | 14 | import com.jetbrains.php.lang.psi.elements.*;
|
18 | 15 | import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
|
19 | 16 | import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
|
@@ -123,11 +120,9 @@ private static void visitAttribute(@NotNull Method method, @NotNull Consumer<Pai
|
123 | 120 | return;
|
124 | 121 | }
|
125 | 122 |
|
126 |
| - PhpPsiUtil.hasReferencesInSearchScope( |
127 |
| - method.getUseScope(), |
128 |
| - parameters[0], |
129 |
| - new MyAttributePsiReferenceFindProcessor(consumer) |
130 |
| - ); |
| 123 | + for (Variable variable : PhpElementsUtil.getVariablesInScope(method, parameters[0])) { |
| 124 | + visitVariable(variable, consumer); |
| 125 | + } |
131 | 126 | }
|
132 | 127 |
|
133 | 128 | public static class StringPairConsumer implements Consumer<Pair<String, PsiElement>> {
|
@@ -171,84 +166,71 @@ public Set<PsiElement> getValues() {
|
171 | 166 | /**
|
172 | 167 | * Find security roles on Voter implementation and security roles in Yaml
|
173 | 168 | */
|
174 |
| - private static class MyAttributePsiReferenceFindProcessor extends CommonProcessors.FindProcessor<PsiReference> { |
175 |
| - private final Consumer<Pair<String, PsiElement>> consumer; |
176 |
| - |
177 |
| - MyAttributePsiReferenceFindProcessor(Consumer<Pair<String, PsiElement>> consumer) { |
178 |
| - this.consumer = consumer; |
179 |
| - } |
180 |
| - |
181 |
| - @Override |
182 |
| - protected boolean accept(PsiReference psiReference) { |
183 |
| - PsiElement resolve = psiReference.getElement(); |
184 |
| - |
185 |
| - PsiElement parent = resolve.getParent(); |
186 |
| - if(parent instanceof BinaryExpression) { |
187 |
| - // 'VALUE' == $var |
188 |
| - PsiElement rightElement = PsiTreeUtil.prevVisibleLeaf(resolve); |
189 |
| - if(rightElement != null) { |
190 |
| - IElementType node = rightElement.getNode().getElementType(); |
191 |
| - if(isIfOperand(node)) { |
192 |
| - PsiElement leftOperand = ((BinaryExpression) parent).getLeftOperand(); |
193 |
| - String stringValue = PhpElementsUtil.getStringValue(leftOperand); |
194 |
| - if(StringUtils.isNotBlank(stringValue)) { |
195 |
| - consumer.accept(Pair.create(stringValue, leftOperand)); |
196 |
| - } |
| 169 | + private static void visitVariable(@NotNull Variable resolve, @NotNull Consumer<Pair<String, PsiElement>> consumer) { |
| 170 | + PsiElement parent = resolve.getParent(); |
| 171 | + if(parent instanceof BinaryExpression) { |
| 172 | + // 'VALUE' == $var |
| 173 | + PsiElement rightElement = PsiTreeUtil.prevVisibleLeaf(resolve); |
| 174 | + if(rightElement != null) { |
| 175 | + IElementType node = rightElement.getNode().getElementType(); |
| 176 | + if(isIfOperand(node)) { |
| 177 | + PsiElement leftOperand = ((BinaryExpression) parent).getLeftOperand(); |
| 178 | + String stringValue = PhpElementsUtil.getStringValue(leftOperand); |
| 179 | + if(StringUtils.isNotBlank(stringValue)) { |
| 180 | + consumer.accept(Pair.create(stringValue, leftOperand)); |
197 | 181 | }
|
198 | 182 | }
|
| 183 | + } |
199 | 184 |
|
200 |
| - // $var == 'VALUE' |
201 |
| - PsiElement leftElement = PsiTreeUtil.nextVisibleLeaf(resolve); |
202 |
| - if(leftElement != null) { |
203 |
| - IElementType node = leftElement.getNode().getElementType(); |
204 |
| - if(isIfOperand(node)) { |
205 |
| - PsiElement rightOperand = ((BinaryExpression) parent).getRightOperand(); |
206 |
| - String stringValue = PhpElementsUtil.getStringValue(rightOperand); |
207 |
| - if(StringUtils.isNotBlank(stringValue)) { |
208 |
| - consumer.accept(Pair.create(stringValue, rightOperand)); |
209 |
| - } |
| 185 | + // $var == 'VALUE' |
| 186 | + PsiElement leftElement = PsiTreeUtil.nextVisibleLeaf(resolve); |
| 187 | + if(leftElement != null) { |
| 188 | + IElementType node = leftElement.getNode().getElementType(); |
| 189 | + if(isIfOperand(node)) { |
| 190 | + PsiElement rightOperand = ((BinaryExpression) parent).getRightOperand(); |
| 191 | + String stringValue = PhpElementsUtil.getStringValue(rightOperand); |
| 192 | + if(StringUtils.isNotBlank(stringValue)) { |
| 193 | + consumer.accept(Pair.create(stringValue, rightOperand)); |
210 | 194 | }
|
211 | 195 | }
|
212 |
| - } else if(parent instanceof ParameterList) { |
213 |
| - // in_array($x, ['FOOBAR']) |
214 |
| - PsiElement functionCall = parent.getParent(); |
215 |
| - if(functionCall instanceof FunctionReference && "in_array".equalsIgnoreCase(((FunctionReference) functionCall).getName())) { |
216 |
| - PsiElement[] functionParameter = ((ParameterList) parent).getParameters(); |
217 |
| - if(functionParameter.length > 1 && functionParameter[1] instanceof ArrayCreationExpression) { |
218 |
| - PsiElement[] psiElements = PsiTreeUtil.collectElements(functionParameter[1], psiElement -> psiElement.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE); |
219 |
| - for (PsiElement psiElement : psiElements) { |
220 |
| - PsiElement firstChild = psiElement.getFirstChild(); |
221 |
| - String stringValue = PhpElementsUtil.getStringValue(firstChild); |
222 |
| - if(StringUtils.isNotBlank(stringValue)) { |
223 |
| - consumer.accept(Pair.create(stringValue, firstChild)); |
224 |
| - } |
| 196 | + } |
| 197 | + } else if(parent instanceof ParameterList) { |
| 198 | + // in_array($x, ['FOOBAR']) |
| 199 | + PsiElement functionCall = parent.getParent(); |
| 200 | + if(functionCall instanceof FunctionReference && "in_array".equalsIgnoreCase(((FunctionReference) functionCall).getName())) { |
| 201 | + PsiElement[] functionParameter = ((ParameterList) parent).getParameters(); |
| 202 | + if(functionParameter.length > 1 && functionParameter[1] instanceof ArrayCreationExpression) { |
| 203 | + PsiElement[] psiElements = PsiTreeUtil.collectElements(functionParameter[1], psiElement -> psiElement.getNode().getElementType() == PhpElementTypes.ARRAY_VALUE); |
| 204 | + for (PsiElement psiElement : psiElements) { |
| 205 | + PsiElement firstChild = psiElement.getFirstChild(); |
| 206 | + String stringValue = PhpElementsUtil.getStringValue(firstChild); |
| 207 | + if(StringUtils.isNotBlank(stringValue)) { |
| 208 | + consumer.accept(Pair.create(stringValue, firstChild)); |
225 | 209 | }
|
226 | 210 | }
|
227 | 211 | }
|
228 |
| - } else if(parent instanceof PhpSwitch) { |
229 |
| - // case "foobar": |
230 |
| - for (PhpCase phpCase : ((PhpSwitch) parent).getAllCases()) { |
231 |
| - PhpPsiElement condition = phpCase.getCondition(); |
232 |
| - String stringValue = PhpElementsUtil.getStringValue(condition); |
233 |
| - if(StringUtils.isNotBlank(stringValue)) { |
234 |
| - consumer.accept(Pair.create(stringValue, condition)); |
235 |
| - } |
| 212 | + } |
| 213 | + } else if(parent instanceof PhpSwitch) { |
| 214 | + // case "foobar": |
| 215 | + for (PhpCase phpCase : ((PhpSwitch) parent).getAllCases()) { |
| 216 | + PhpPsiElement condition = phpCase.getCondition(); |
| 217 | + String stringValue = PhpElementsUtil.getStringValue(condition); |
| 218 | + if(StringUtils.isNotBlank(stringValue)) { |
| 219 | + consumer.accept(Pair.create(stringValue, condition)); |
236 | 220 | }
|
237 | 221 | }
|
238 |
| - |
239 |
| - return false; |
240 | 222 | }
|
| 223 | + } |
241 | 224 |
|
242 |
| - /** |
243 |
| - * null == null, null != null, null === null |
244 |
| - */ |
245 |
| - private boolean isIfOperand(@NotNull IElementType node) { |
246 |
| - return |
247 |
| - node == PhpTokenTypes.opIDENTICAL || |
| 225 | + /** |
| 226 | + * null == null, null != null, null === null |
| 227 | + */ |
| 228 | + private static boolean isIfOperand(@NotNull IElementType node) { |
| 229 | + return |
| 230 | + node == PhpTokenTypes.opIDENTICAL || |
248 | 231 | node == PhpTokenTypes.opEQUAL ||
|
249 | 232 | node == PhpTokenTypes.opNOT_EQUAL ||
|
250 | 233 | node == PhpTokenTypes.opNOT_IDENTICAL
|
251 | 234 | ;
|
252 |
| - } |
253 | 235 | }
|
254 | 236 | }
|
0 commit comments