use of com.jetbrains.php.lang.psi.elements.PhpModifierList in project phpinspectionsea by kalessil.
the class ModifierExtractionUtil method getProtectedModifier.
@Nullable
public static PsiElement getProtectedModifier(final PhpClassMember subject) {
final PsiElement expression = (subject instanceof Field) ? subject.getParent() : subject;
final PhpModifierList list = PsiTreeUtil.findChildOfType(expression, PhpModifierList.class);
final LeafPsiElement[] modifiers = PsiTreeUtil.getChildrenOfType(list, LeafPsiElement.class);
PsiElement result = null;
if (modifiers != null) {
for (final LeafPsiElement modifier : modifiers) {
if (modifier.getText().equalsIgnoreCase("protected")) {
result = modifier;
break;
}
}
}
return result;
}
use of com.jetbrains.php.lang.psi.elements.PhpModifierList in project phpinspectionsea by kalessil.
the class MisorderedModifiersInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder problemsHolder, final boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpMethod(@NotNull Method method) {
if (method.isStatic() || method.isAbstract() || method.isFinal()) {
final PhpModifierList modifiersNode = PsiTreeUtil.findChildOfType(method, PhpModifierList.class);
final List<LeafPsiElement> modifiers = PsiTreeUtil.findChildrenOfType(modifiersNode, LeafPsiElement.class).stream().filter(element -> !(element instanceof PsiWhiteSpace)).collect(Collectors.toList());
if (modifiersNode != null && modifiers.size() >= 2) {
final String original = this.getOriginalOrder(modifiers);
final String expected = this.getExpectedOrder(original, standardOrder);
if (!original.equals(expected)) {
problemsHolder.registerProblem(modifiersNode, MessagesPresentationUtil.prefixWithEa(message), new TheLocalFix(expected));
}
}
}
}
@NotNull
private String getOriginalOrder(@NotNull Collection<LeafPsiElement> original) {
return original.stream().map(LeafElement::getText).collect(Collectors.joining(" ")).toLowerCase();
}
@NotNull
private String getExpectedOrder(@NotNull String original, @NotNull Collection<String> expected) {
return expected.stream().filter(original::contains).collect(Collectors.joining(" "));
}
};
}
use of com.jetbrains.php.lang.psi.elements.PhpModifierList in project phpinspectionsea by kalessil.
the class AccessModifierPresentedInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpClass(@NotNull PhpClass clazz) {
/* community request: interfaces have only public methods, what is default access levels */
if (!ANALYZE_INTERFACES && clazz.isInterface()) {
return;
}
/* inspect methods */
for (final Method method : clazz.getOwnMethods()) {
final PsiElement methodName = NamedElementUtil.getNameIdentifier(method);
if (methodName != null && method.getAccess().isPublic()) {
final PhpModifierList modifiers = PsiTreeUtil.findChildOfType(method, PhpModifierList.class);
if (modifiers != null && !modifiers.getText().toLowerCase().contains("public")) {
final String message = String.format(messagePattern, method.getName());
holder.registerProblem(methodName, message, new MemberVisibilityFix(modifiers));
}
}
}
/* inspect fields */
final PhpLanguageLevel phpVersion = PhpProjectConfigurationFacade.getInstance(holder.getProject()).getLanguageLevel();
final boolean checkConstantVisibility = phpVersion.compareTo(PhpLanguageLevel.PHP710) >= 0;
for (final Field field : clazz.getOwnFields()) {
final PsiElement fieldName = NamedElementUtil.getNameIdentifier(field);
if (fieldName != null && field.getModifier().isPublic()) {
if (field.isConstant()) {
/* {const}.isPublic() always returns true, even if visibility is not declared */
if (ANALYZE_CONSTANTS && checkConstantVisibility && field.getPrevPsiSibling() == null) {
final String message = String.format(messagePattern, field.getName());
holder.registerProblem(fieldName, message, new ConstantVisibilityFix(field));
}
} else {
final PhpModifierList modifiers = PsiTreeUtil.findChildOfType(field.getParent(), PhpModifierList.class);
if (modifiers != null && !modifiers.getText().toLowerCase().contains("public")) {
final String message = String.format(messagePattern, field.getName());
holder.registerProblem(fieldName, message, new MemberVisibilityFix(modifiers));
}
}
}
}
}
};
}
Aggregations