use of com.kalessil.phpStorm.phpInspectionsEA.inspectors.semanticalAnalysis.classes.lowerAccessLevel.fixers.MakePrivateFixer in project phpinspectionsea by kalessil.
the class PropertyUsedInPrivateContextStrategy method apply.
public static void apply(@NotNull PhpClass clazz, @NotNull ProblemsHolder holder) {
if (!clazz.isFinal() && !clazz.isInterface()) {
final PhpClass parent = OpenapiResolveUtil.resolveSuperClass(clazz);
final Map<String, Field> fields = new HashMap<>();
for (final Field field : clazz.getOwnFields()) {
if (field.getModifier().isProtected()) {
/* ensure overrides are skipped */
final String fieldName = field.getName();
if (parent == null || OpenapiResolveUtil.resolveField(parent, fieldName) == null) {
fields.put(fieldName, field);
}
}
}
if (!fields.isEmpty()) {
/* collect usage contexts: iterate methods */
final Map<String, Set<String>> contextInformation = new HashMap<>();
for (final Method method : clazz.getOwnMethods()) {
final GroupStatement body = method.isAbstract() ? null : ExpressionSemanticUtil.getGroupStatement(method);
if (body == null) {
continue;
}
final boolean isMagicMethod = magicMethods.contains(method.getName());
/* find fields references matching pre-collected names */
for (final FieldReference reference : PsiTreeUtil.findChildrenOfType(body, FieldReference.class)) {
final String referenceName = reference.getName();
if (!fields.containsKey(referenceName)) {
continue;
}
/* store the context information */
final PsiElement resolved = OpenapiResolveUtil.resolveReference(reference);
if (resolved != null && fields.get(referenceName) == resolved) {
final Set<String> usages = contextInformation.computeIfAbsent(referenceName, r -> new HashSet<>());
final PhpModifier.Access access = method.getAccess();
if (isMagicMethod || access.isPrivate()) {
usages.add("private");
}
if (!isMagicMethod) {
if (access.isProtected()) {
usages.add("protected");
} else if (access.isPublic()) {
usages.add("public");
}
}
}
}
}
fields.forEach((fieldName, list) -> {
if (contextInformation.containsKey(fieldName)) {
final Set<String> usages = contextInformation.get(fieldName);
if (usages.size() == 1 && usages.contains("private")) {
final PsiElement modifier = ModifierExtractionUtil.getProtectedModifier(list);
if (modifier != null) {
holder.registerProblem(modifier, message, new MakePrivateFixer(modifier));
}
}
}
});
fields.clear();
contextInformation.clear();
}
}
}
use of com.kalessil.phpStorm.phpInspectionsEA.inspectors.semanticalAnalysis.classes.lowerAccessLevel.fixers.MakePrivateFixer 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));
}
}
}
Aggregations