use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project phpinspectionsea by kalessil.
the class MustReturnSpecifiedTypeStrategy method apply.
public static void apply(@NotNull PhpType allowedTypes, @NotNull Method method, @NotNull ProblemsHolder holder) {
final PsiElement returnType = OpenapiElementsUtil.getReturnType(method);
if (returnType != null) {
/* case: return type has ben specified */
final PhpType resolved = OpenapiResolveUtil.resolveType((PhpTypedElement) returnType, holder.getProject());
if (resolved != null) {
final PhpType normalizedType = new PhpType();
resolved.filterUnknown().getTypes().forEach(t -> normalizedType.add(Types.getType(t)));
if (!normalizedType.isEmpty() && !PhpType.isSubType(normalizedType, allowedTypes)) {
final PsiElement nameNode = NamedElementUtil.getNameIdentifier(method);
if (nameNode != null) {
final PhpType withoutStatic = allowedTypes.filter((new PhpType()).add(Types.strStatic));
holder.registerProblem(nameNode, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), method.getName(), withoutStatic.toString(), normalizedType.toString()));
}
}
}
} else {
final Collection<PhpReturn> returns = PsiTreeUtil.findChildrenOfType(method, PhpReturn.class);
if (returns.isEmpty()) {
/* case: the method doesn't return anything */
final PsiElement nameNode = NamedElementUtil.getNameIdentifier(method);
if (nameNode != null) {
final PhpType withoutStatic = allowedTypes.filter((new PhpType()).add(Types.strStatic));
holder.registerProblem(nameNode, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), method.getName(), withoutStatic.toString(), ""));
}
} else {
/* case: method returns non-compatible type */
final PhpType withoutStatic = allowedTypes.filter((new PhpType()).add(Types.strStatic));
final Project project = holder.getProject();
for (final PhpReturn expression : returns) {
final PhpType normalizedType = new PhpType();
final PsiElement returnValue = ExpressionSemanticUtil.getExpressionTroughParenthesis(ExpressionSemanticUtil.getReturnValue(expression));
if (returnValue instanceof PhpTypedElement) {
/* previously we had an issue with https://youtrack.jetbrains.com/issue/WI-31249 here */
final PhpType resolved = OpenapiResolveUtil.resolveType((PhpTypedElement) returnValue, project);
if (resolved != null) {
resolved.filterUnknown().getTypes().forEach(t -> normalizedType.add(Types.getType(t)));
/* case: resolve has failed or resolved types are compatible */
if (normalizedType.isEmpty() || PhpType.isSubType(normalizedType, allowedTypes)) {
continue;
} else /* case: closure or anonymous class */
if (method != ExpressionSemanticUtil.getScope(expression)) {
continue;
}
}
}
holder.registerProblem(expression, String.format(MessagesPresentationUtil.prefixWithEa(messagePattern), method.getName(), withoutStatic.toString(), normalizedType.toString()));
}
returns.clear();
}
}
}
use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project phpinspectionsea by kalessil.
the class UnsupportedStringOffsetOperationsInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpArrayAccessExpression(@NotNull ArrayAccessExpression expression) {
final Project project = holder.getProject();
if (PhpLanguageLevel.get(project).atLeast(PhpLanguageLevel.PHP710)) {
PsiElement target = null;
String message = null;
boolean isTargetContext = false;
/* context identification phase */
final PsiElement candidate = expression.getValue();
if (candidate instanceof Variable || candidate instanceof FieldReference || candidate instanceof ArrayAccessExpression) {
/* false-positives: pushing to pre-defined globals */
PsiElement possiblyValue = candidate;
while (possiblyValue instanceof ArrayAccessExpression) {
possiblyValue = ((ArrayAccessExpression) possiblyValue).getValue();
}
if (possiblyValue instanceof Variable) {
final String variableName = ((Variable) possiblyValue).getName();
if (ExpressionCostEstimateUtil.predefinedVars.contains(variableName)) {
return;
}
}
final PsiElement parent = expression.getParent();
/* case 1: unsupported casting to array */
if (parent instanceof ArrayAccessExpression) {
message = messageOffset;
target = parent;
while (target.getParent() instanceof ArrayAccessExpression) {
target = target.getParent();
}
PsiElement context = target.getParent();
if (context instanceof AssignmentExpression) {
isTargetContext = ((AssignmentExpression) context).getValue() != target;
} else if (OpenapiTypesUtil.is(context, PhpElementTypes.ARRAY_VALUE)) {
final PsiElement array = context.getParent();
if ((context = array.getParent()) instanceof AssignmentExpression) {
isTargetContext = ((AssignmentExpression) context).getValue() != array;
}
}
} else /* case 2: array push operator is not supported by strings */
{
final ArrayIndex index = expression.getIndex();
if (index == null || index.getValue() == null) {
final PsiElement context = expression.getParent();
if (context instanceof AssignmentExpression) {
message = messagePush;
target = expression;
isTargetContext = ((AssignmentExpression) context).getValue() != expression;
}
}
}
}
/* type verification and reporting phase */
if (isTargetContext && ExpressionSemanticUtil.getScope(target) != null) {
final PhpType resolved = OpenapiResolveUtil.resolveType((PhpTypedElement) candidate, project);
if (resolved != null) {
final boolean isTarget = resolved.filterUnknown().getTypes().stream().anyMatch(type -> Types.getType(type).equals(Types.strString));
if (isTarget) {
holder.registerProblem(target, MessagesPresentationUtil.prefixWithEa(message), ProblemHighlightType.GENERIC_ERROR);
}
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project phpinspectionsea by kalessil.
the class CallableParameterUseCaseInTypeContextInspection method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpMethod(@NotNull Method method) {
if (!this.isTestContext(method)) {
this.inspectUsages(method.getParameters(), method);
}
}
@Override
public void visitPhpFunction(@NotNull Function function) {
if (!this.isTestContext(function)) {
this.inspectUsages(function.getParameters(), function);
}
}
private void inspectUsages(@NotNull Parameter[] parameters, @NotNull PhpScopeHolder scopeHolder) {
final Project project = holder.getProject();
final PhpIndex index = PhpIndex.getInstance(project);
final PhpEntryPointInstruction entryPoint = scopeHolder.getControlFlow().getEntryPoint();
for (final Parameter parameter : parameters) {
/* normalize parameter types, skip analysis when mixed or object appears */
final Set<String> paramTypes = new HashSet<>();
final PhpType parameterType = OpenapiResolveUtil.resolveType(parameter, project);
if (parameterType != null) {
label: for (final String type : parameterType.filterUnknown().getTypes()) {
final String typeNormalized = Types.getType(type);
switch(typeNormalized) {
case Types.strMixed:
case Types.strObject:
paramTypes.clear();
break label;
case Types.strCallable:
paramTypes.add(Types.strArray);
paramTypes.add(Types.strString);
paramTypes.add("\\Closure");
break;
case Types.strIterable:
paramTypes.add(Types.strArray);
paramTypes.add("\\Traversable");
break;
}
paramTypes.add(typeNormalized);
}
}
if (paramTypes.isEmpty()) {
continue;
} else {
/* in some case PhpStorm is not recognizing default value as parameter type */
final PsiElement defaultValue = parameter.getDefaultValue();
if (defaultValue instanceof PhpTypedElement) {
final PhpType defaultType = OpenapiResolveUtil.resolveType((PhpTypedElement) defaultValue, project);
if (defaultType != null) {
defaultType.filterUnknown().getTypes().forEach(t -> paramTypes.add(Types.getType(t)));
}
}
}
/* false-positive: type is not resolved correctly, default null is taken */
if (paramTypes.size() == 1 && paramTypes.contains(Types.strNull)) {
final PsiElement defaultValue = parameter.getDefaultValue();
if (PhpLanguageUtil.isNull(defaultValue)) {
continue;
}
}
/* now find instructions operating on the parameter and perform analysis */
final String parameterName = parameter.getName();
for (final PhpAccessVariableInstruction instruction : OpenapiControlFlowUtil.getFollowingVariableAccessInstructions(entryPoint, parameterName)) {
final PsiElement parent = instruction.getAnchor().getParent();
final PsiElement callCandidate = null == parent ? null : parent.getParent();
/* Case 1: check if is_* functions being used according to definitions */
if (OpenapiTypesUtil.isFunctionReference(callCandidate)) {
final FunctionReference functionCall = (FunctionReference) callCandidate;
final String functionName = functionCall.getName();
if (functionName == null) {
continue;
}
/* we expect that aliases usage has been fixed already */
final boolean isTypeAnnounced;
switch(functionName) {
case "is_array":
isTypeAnnounced = paramTypes.contains(Types.strArray) || paramTypes.contains(Types.strIterable);
break;
case "is_string":
isTypeAnnounced = paramTypes.contains(Types.strString);
break;
case "is_bool":
isTypeAnnounced = paramTypes.contains(Types.strBoolean);
break;
case "is_int":
isTypeAnnounced = paramTypes.contains(Types.strInteger) || paramTypes.contains(Types.strNumber);
break;
case "is_float":
isTypeAnnounced = paramTypes.contains(Types.strFloat) || paramTypes.contains(Types.strNumber);
break;
case "is_resource":
isTypeAnnounced = paramTypes.contains(Types.strResource);
break;
case "is_numeric":
if (paramTypes.contains(Types.strString)) {
continue;
}
isTypeAnnounced = paramTypes.contains(Types.strNumber) || paramTypes.contains(Types.strFloat) || paramTypes.contains(Types.strInteger);
break;
case "is_callable":
isTypeAnnounced = paramTypes.contains(Types.strCallable) || paramTypes.contains(Types.strArray) || paramTypes.contains(Types.strString) || paramTypes.contains("\\Closure");
break;
case "is_object":
isTypeAnnounced = paramTypes.contains(Types.strObject) || paramTypes.contains(Types.strCallable) || paramTypes.stream().anyMatch(t -> classReferences.contains(t) || (t.startsWith("\\") && !t.equals("\\Closure")));
break;
case "is_a":
isTypeAnnounced = paramTypes.contains(Types.strObject) || paramTypes.contains(Types.strString) || paramTypes.stream().anyMatch(t -> (t.startsWith("\\") && !t.equals("\\Closure")) || classReferences.contains(t));
break;
default:
continue;
}
/* cases: call makes no sense, violation of defined types set */
if (!isTypeAnnounced) {
final PsiElement callParent = functionCall.getParent();
boolean isReversedCheck = false;
if (callParent instanceof UnaryExpression) {
final PsiElement operation = ((UnaryExpression) callParent).getOperation();
isReversedCheck = OpenapiTypesUtil.is(operation, PhpTokenTypes.opNOT);
}
holder.registerProblem(functionCall, MessagesPresentationUtil.prefixWithEa(isReversedCheck ? messageNoSense : messageViolationInCheck));
}
continue;
}
/* Case 2: assignments violating parameter definition */
if (OpenapiTypesUtil.isAssignment(parent)) {
final AssignmentExpression assignment = (AssignmentExpression) parent;
final PhpPsiElement variable = assignment.getVariable();
final PhpPsiElement value = assignment.getValue();
if (variable instanceof Variable && value instanceof PhpTypedElement) {
final String variableName = variable.getName();
if (variableName != null && variableName.equals(parameterName)) {
final PhpType resolvedType = OpenapiResolveUtil.resolveType((PhpTypedElement) value, project);
final Set<String> resolved = new HashSet<>();
if (resolvedType != null) {
resolvedType.filterUnknown().getTypes().forEach(t -> resolved.add(Types.getType(t)));
}
if (resolved.size() >= 2) {
/* false-positives: core functions returning string|array & false|null */
if (resolved.contains(Types.strString) || resolved.contains(Types.strArray)) {
if (resolved.contains(Types.strBoolean)) {
final boolean isFunctionCall = OpenapiTypesUtil.isFunctionReference(value);
if (isFunctionCall) {
resolved.remove(Types.strBoolean);
}
} else if (resolved.contains(Types.strNull)) {
final boolean isFunctionCall = OpenapiTypesUtil.isFunctionReference(value);
if (isFunctionCall) {
resolved.remove(Types.strNull);
}
}
} else /* false-positives: nullable objects */
if (resolved.contains(Types.strNull)) {
final boolean isNullableObject = paramTypes.stream().anyMatch(t -> classReferences.contains(t) || t.startsWith("\\") && !t.equals("\\Closure"));
if (isNullableObject) {
resolved.remove(Types.strNull);
}
}
}
resolved.remove(Types.strMixed);
for (String type : resolved) {
/* translate static/self into FQNs */
if (classReferences.contains(type)) {
PsiElement valueExtract = value;
/* ` = <whatever> ?? <method call>` support */
if (valueExtract instanceof BinaryExpression) {
final BinaryExpression binary = (BinaryExpression) valueExtract;
if (binary.getOperationType() == PhpTokenTypes.opCOALESCE) {
final PsiElement left = binary.getLeftOperand();
if (left != null && OpenapiEquivalenceUtil.areEqual(variable, left)) {
final PsiElement right = binary.getRightOperand();
if (right != null) {
valueExtract = right;
}
}
}
}
/* method call lookup */
if (valueExtract instanceof MethodReference) {
final PsiElement base = valueExtract.getFirstChild();
if (base instanceof ClassReference) {
final PsiElement resolvedClass = OpenapiResolveUtil.resolveReference((ClassReference) base);
if (resolvedClass instanceof PhpClass) {
type = ((PhpClass) resolvedClass).getFQN();
}
} else if (base instanceof PhpTypedElement) {
final PhpType clazzTypes = OpenapiResolveUtil.resolveType((PhpTypedElement) base, project);
if (clazzTypes != null) {
final Set<String> filteredTypes = clazzTypes.filterUnknown().getTypes().stream().map(Types::getType).filter(t -> t.startsWith("\\")).collect(Collectors.toSet());
final int filteredTypesCount = filteredTypes.size();
/* clear resolved class or interface + class */
if (filteredTypesCount == 1 || filteredTypesCount == 2) {
type = filteredTypes.iterator().next();
}
filteredTypes.clear();
}
}
}
/* translate static/self into FQNs didn't work, skip */
if (classReferences.contains(type)) {
continue;
}
}
final boolean isViolation = !this.isTypeCompatibleWith(type, paramTypes, index);
if (isViolation) {
holder.registerProblem(value, String.format(MessagesPresentationUtil.prefixWithEa(patternViolationInAssignment), type));
break;
}
}
resolved.clear();
}
}
}
}
paramTypes.clear();
}
}
private boolean isTypeCompatibleWith(@NotNull String type, @NotNull Collection<String> allowedTypes, @NotNull PhpIndex index) {
/* first case: implicit match */
if (allowedTypes.contains(type)) {
return true;
}
/* second case: inherited classes/interfaces */
final Set<String> possibleTypes = new HashSet<>();
if (type.startsWith("\\")) {
index.getAnyByFQN(type).forEach(clazz -> InterfacesExtractUtil.getCrawlInheritanceTree(clazz, true).forEach(c -> possibleTypes.add(c.getFQN())));
}
return !possibleTypes.isEmpty() && allowedTypes.stream().anyMatch(possibleTypes::contains);
}
};
}
use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project phpinspectionsea by kalessil.
the class OffsetOperationsInspector method isContainerSupportsArrayAccess.
private boolean isContainerSupportsArrayAccess(@NotNull Project project, @NotNull ArrayAccessExpression expression, @NotNull Set<String> indexTypesSupported) {
// ok JB parses `$var[]= ...` always as array, lets make it working properly and report them later
final PsiElement container = expression.getValue();
if (null == container) {
return false;
}
final Set<String> containerTypes = new HashSet<>();
if (container instanceof PhpTypedElement) {
final PhpType type = OpenapiResolveUtil.resolveType((PhpTypedElement) container, project);
if (type != null && !type.hasUnknown()) {
type.getTypes().forEach(t -> containerTypes.add(Types.getType(t)));
Stream.of(Types.strSelf, Types.strStatic).forEach(containerTypes::remove);
}
}
/* === cleanup resolved types === */
if (containerTypes.contains(Types.strMixed)) {
// mixed are not analyzable
containerTypes.clear();
return true;
}
if (containerTypes.size() == 2 && containerTypes.contains(Types.strString)) {
final boolean isForeachKeyType = containerTypes.contains(Types.strInteger);
final boolean isCoreApiStringType = containerTypes.contains(Types.strBoolean);
if (isForeachKeyType || isCoreApiStringType) {
containerTypes.clear();
return true;
}
}
if (containerTypes.contains(Types.strCallable)) {
// treat callable as array
containerTypes.remove(Types.strCallable);
containerTypes.add(Types.strArray);
containerTypes.add(Types.strString);
}
// don't process nulls
containerTypes.remove(Types.strNull);
// don't process generalized objects
containerTypes.remove(Types.strObject);
// don't process mysterious empty set type
containerTypes.remove(Types.strEmptySet);
/* === if we could not resolve container, do nothing === */
if (containerTypes.isEmpty()) {
return true;
}
final PhpIndex index = PhpIndex.getInstance(project);
boolean supportsOffsets = false;
for (final String typeToCheck : containerTypes) {
// commonly used case: string and array
if (typeToCheck.equals(Types.strArray) || typeToCheck.equals(Types.strString)) {
supportsOffsets = true;
/* here we state which regular index types we want to promote */
indexTypesSupported.add(Types.strString);
indexTypesSupported.add(Types.strInteger);
continue;
}
// some of possible types are scalars, what's wrong
if (!typeToCheck.isEmpty() && typeToCheck.charAt(0) != '\\') {
supportsOffsets = false;
break;
}
// now we are at point when analyzing classes only
final List<PhpClass> classes = OpenapiResolveUtil.resolveClassesAndInterfacesByFQN(typeToCheck, index);
for (final PhpClass clazz : classes) {
/* custom offsets management, follow annotated types */
for (final String methodName : Arrays.asList("offsetGet", "offsetSet", "__get", "__set")) {
final Method method = OpenapiResolveUtil.resolveMethod(clazz, methodName);
if (method != null) {
/* regular array index types can be applied */
if (methodName.startsWith("__")) {
indexTypesSupported.add(Types.strString);
indexTypesSupported.add(Types.strInteger);
} else /* user-defined index types can be applied */
{
final Parameter[] parameters = method.getParameters();
if (parameters.length > 0) {
final PhpType type = OpenapiResolveUtil.resolveType(parameters[0], project);
if (type != null) {
type.filterUnknown().getTypes().forEach(t -> indexTypesSupported.add(Types.getType(t)));
}
}
}
supportsOffsets = true;
}
}
}
classes.clear();
}
// when might not support offset access, reuse types container to report back why
if (!supportsOffsets) {
indexTypesSupported.clear();
indexTypesSupported.addAll(containerTypes);
}
containerTypes.clear();
return supportsOffsets;
}
use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project phpinspectionsea by kalessil.
the class OffsetOperationsInspector method buildVisitor.
@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
return new BasePhpElementVisitor() {
@Override
public void visitPhpArrayAccessExpression(@NotNull ArrayAccessExpression expression) {
final PsiElement bracketNode = expression.getLastChild();
if (null == bracketNode || null == expression.getValue()) {
return;
}
// ensure offsets operations are supported, do nothing if no types were resolved
final Set<String> allowedIndexTypes = new HashSet<>();
if (!isContainerSupportsArrayAccess(holder.getProject(), expression, allowedIndexTypes) && !allowedIndexTypes.isEmpty()) {
holder.registerProblem(expression, String.format(MessagesPresentationUtil.prefixWithEa(patternNoOffsetSupport), expression.getValue().getText(), allowedIndexTypes.toString()));
allowedIndexTypes.clear();
return;
}
// TODO: hash-elements e.g. array initialization
if (!allowedIndexTypes.isEmpty() && expression.getIndex() != null) {
final PhpPsiElement indexValue = expression.getIndex().getValue();
if (indexValue instanceof PhpTypedElement) {
final PhpType resolved = OpenapiResolveUtil.resolveType((PhpTypedElement) indexValue, holder.getProject());
if (resolved != null) {
final Set<String> indexTypes = new HashSet<>();
resolved.filterUnknown().getTypes().forEach(t -> indexTypes.add(Types.getType(t)));
if (!indexTypes.isEmpty()) {
filterPossibleTypesWhichAreNotAllowed(indexTypes, allowedIndexTypes);
if (!indexTypes.isEmpty()) {
holder.registerProblem(indexValue, String.format(MessagesPresentationUtil.prefixWithEa(patternInvalidIndex), indexTypes.toString(), allowedIndexTypes.toString()));
indexTypes.clear();
}
}
}
}
}
// clear valid types collection
allowedIndexTypes.clear();
}
};
}
Aggregations