use of com.jetbrains.php.lang.psi.elements.PhpPsiElement in project idea-php-typo3-plugin by cedricziel.
the class ClassConstantMatcherInspection method getDeprecatedClassConstants.
private Set<String> getDeprecatedClassConstants(PhpPsiElement element) {
Set<PsiElement> elements = new HashSet<>();
PsiFile[] classConstantMatcherFiles = FilenameIndex.getFilesByName(element.getProject(), "ClassConstantMatcher.php", GlobalSearchScope.allScope(element.getProject()));
for (PsiFile file : classConstantMatcherFiles) {
Collections.addAll(elements, PsiTreeUtil.collectElements(file, el -> PlatformPatterns.psiElement(StringLiteralExpression.class).withParent(PlatformPatterns.psiElement(PhpElementTypes.ARRAY_KEY).withAncestor(4, PlatformPatterns.psiElement(PhpElementTypes.RETURN))).accepts(el)));
}
return elements.stream().map(stringLiteral -> ((StringLiteralExpression) stringLiteral).getContents()).collect(Collectors.toSet());
}
use of com.jetbrains.php.lang.psi.elements.PhpPsiElement in project idea-php-typo3-plugin by cedricziel.
the class ClassNameMatcherInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
return new PhpElementVisitor() {
@Override
public void visitPhpElement(PhpPsiElement element) {
if (!PlatformPatterns.psiElement(PhpElementTypes.CLASS_REFERENCE).accepts(element)) {
return;
}
Set<String> constants = getDeprecatedClasses(element);
ClassReference classReference = (ClassReference) element;
if (constants.contains(classReference.getFQN())) {
problemsHolder.registerProblem(element, "Class removed with TYPO3 9, consider using an alternative");
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.PhpPsiElement in project idea-php-typo3-plugin by cedricziel.
the class ConstantMatcherInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
return new PhpElementVisitor() {
@Override
public void visitPhpElement(PhpPsiElement element) {
if (!PlatformPatterns.psiElement(PhpElementTypes.CONSTANT_REF).accepts(element)) {
return;
}
ConstantReference constantReference = (ConstantReference) element;
Set<String> constants = getRemovedConstantsFQNs(constantReference);
if (constants.contains(constantReference.getFQN())) {
problemsHolder.registerProblem(element, "Constant removed with TYPO3 9, consider using an alternative");
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.PhpPsiElement in project phpinspectionsea by kalessil.
the class PackedHashtableOptimizationInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
/* TODO: docs, http://blog.jpauli.tech/2016/04/08/hashtables.html#packed-hashtable-optimization */
@Override
public void visitPhpArrayCreationExpression(@NotNull ArrayCreationExpression expression) {
/* requires PHP7 */
if (PhpLanguageLevel.get(holder.getProject()).below(PhpLanguageLevel.PHP700)) {
return;
}
/* requires at least 3 children - let array to grow enough */
final PsiElement[] children = expression.getChildren();
if (children.length < 3) {
return;
}
/* false-positives: test classes */
if (this.isTestContext(expression)) {
return;
}
/* step 1: collect indexes and verify array structure */
final List<PhpPsiElement> indexes = new ArrayList<>();
for (final PsiElement pairCandidate : children) {
if (pairCandidate instanceof ArrayHashElement) {
final PhpPsiElement key = ((ArrayHashElement) pairCandidate).getKey();
if ((key instanceof StringLiteralExpression && key.getFirstPsiChild() == null) || OpenapiTypesUtil.isNumber(key)) {
indexes.add(key);
continue;
}
}
break;
}
if (indexes.size() != children.length) {
indexes.clear();
return;
}
/* step 2: analyze collected indexes */
// if string literal is not numeric => stop
boolean hasStringIndexes = false;
boolean hasIncreasingIndexes = true;
int lastIndex = Integer.MIN_VALUE;
for (PhpPsiElement index : indexes) {
final String numericIndex;
final int integerIndex;
/* extract text representation of the index */
if (index instanceof StringLiteralExpression) {
hasStringIndexes = true;
numericIndex = ((StringLiteralExpression) index).getContents();
/* '01' and etc cases can not be converted */
if (numericIndex.length() > 1 && '0' == numericIndex.charAt(0)) {
indexes.clear();
return;
}
} else {
numericIndex = index.getText().replaceAll("\\s+", "");
}
/* try converting into integer */
try {
integerIndex = Integer.parseInt(numericIndex);
} catch (NumberFormatException error) {
indexes.clear();
return;
}
if (integerIndex < lastIndex) {
hasIncreasingIndexes = false;
}
lastIndex = integerIndex;
}
/* report if criteria are met */
if (!hasIncreasingIndexes) {
holder.registerProblem(expression.getFirstChild(), MessagesPresentationUtil.prefixWithEa(messageReorder));
}
if (hasIncreasingIndexes && hasStringIndexes) {
holder.registerProblem(expression.getFirstChild(), MessagesPresentationUtil.prefixWithEa(messageUseNumericKeys));
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.PhpPsiElement in project phpinspectionsea by kalessil.
the class DropMethodFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement expression = descriptor.getPsiElement().getParent();
if (expression instanceof Method && !project.isDisposed()) {
/* delete preceding PhpDoc */
final PhpPsiElement previous = ((Method) expression).getPrevPsiSibling();
if (previous instanceof PhpDocComment) {
previous.delete();
}
/* delete space after the method */
final PsiElement nextExpression = expression.getNextSibling();
if (nextExpression instanceof PsiWhiteSpace) {
nextExpression.delete();
}
/* delete the method itself */
expression.delete();
}
}
Aggregations