use of com.jetbrains.php.lang.psi.elements.PhpClass in project phpinspectionsea by kalessil.
the class NormallyCallsParentMethodStrategy method apply.
public static void apply(@NotNull Method method, @NotNull ProblemsHolder holder) {
final PhpClass clazz = method.getContainingClass();
if (clazz != null) {
final String methodName = method.getName();
final PhpClass parentClazz = OpenapiResolveUtil.resolveSuperClass(clazz);
final Method parentMethod = parentClazz == null ? null : OpenapiResolveUtil.resolveMethod(parentClazz, methodName);
final PhpClass parentMethodHolder = parentMethod == null ? null : parentMethod.getContainingClass();
if (parentMethodHolder != null && !parentMethod.isAbstract() && !parentMethod.getAccess().isPrivate()) {
final boolean isUsed = PsiTreeUtil.findChildrenOfType(method, MethodReference.class).stream().anyMatch(call -> methodName.equals(call.getName()));
if (!isUsed) {
final PsiElement nameNode = NamedElementUtil.getNameIdentifier(method);
if (nameNode != null) {
holder.registerProblem(nameNode, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), methodName, parentMethodHolder.getName(), methodName));
}
}
}
}
}
use of com.jetbrains.php.lang.psi.elements.PhpClass 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));
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.elements.PhpClass in project phpinspectionsea by kalessil.
the class ProtectedMembersOfFinalClassStrategy method apply.
public static void apply(@NotNull PhpClassMember subject, @NotNull ProblemsHolder holder) {
final PhpClass clazz = subject.getContainingClass();
final boolean isTargetContext = clazz != null && clazz.isFinal() && subject.getModifier().isProtected();
if (isTargetContext && OpenapiResolveUtil.resolveSuperClass(clazz) == null && !isOverride(subject, clazz)) {
final PsiElement modifier = ModifierExtractionUtil.getProtectedModifier(subject);
if (modifier != null) {
holder.registerProblem(modifier, message, new MakePrivateFixer(modifier));
}
}
}
use of com.jetbrains.php.lang.psi.elements.PhpClass in project phpinspectionsea by kalessil.
the class HasAlsoMethodStrategy method apply.
public static void apply(@NotNull Method method, @NotNull String companion, @NotNull ProblemsHolder holder) {
final PhpClass clazz = method.getContainingClass();
if (clazz != null && OpenapiResolveUtil.resolveMethod(clazz, companion) == null) {
final PsiElement nameNode = NamedElementUtil.getNameIdentifier(method);
if (nameNode != null) {
final String message = String.format(messagePattern, method.getName(), companion);
holder.registerProblem(nameNode, message, ProblemHighlightType.GENERIC_ERROR);
}
}
}
use of com.jetbrains.php.lang.psi.elements.PhpClass in project phpinspectionsea by kalessil.
the class MustNotThrowExceptionsStrategy method apply.
public static void apply(final Method method, final ProblemsHolder holder) {
if (null == method.getNameIdentifier()) {
return;
}
final HashSet<PsiElement> processedRegistry = new HashSet<>();
final HashMap<PhpClass, HashSet<PsiElement>> throwsExceptions = CollectPossibleThrowsUtil.collectNestedAndWorkflowExceptions(method, processedRegistry, holder);
processedRegistry.clear();
if (throwsExceptions.size() > 0) {
for (Map.Entry<PhpClass, HashSet<PsiElement>> pair : throwsExceptions.entrySet()) {
/* extract pairs */
final PhpClass thrown = pair.getKey();
final HashSet<PsiElement> expressions = pair.getValue();
final String message = messagePattern.replace("%c%", thrown.getFQN()).replace("%m%", method.getName());
for (PsiElement blame : expressions) {
holder.registerProblem(blame, message, ProblemHighlightType.GENERIC_ERROR);
}
expressions.clear();
}
throwsExceptions.clear();
}
}
Aggregations