Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.eclipse.core.runtime.CoreException;
import org.eclipse.dltk.annotations.NonNull;
import org.eclipse.dltk.ast.declarations.ModuleDeclaration;
import org.eclipse.dltk.ast.references.ConstantReference;
import org.eclipse.dltk.ast.references.VariableReference;
import org.eclipse.dltk.core.*;
import org.eclipse.dltk.evaluation.types.AmbiguousType;
Expand Down Expand Up @@ -206,6 +207,31 @@ public static IType[] getVariableType(ISourceModule sourceModule, String variabl
return EMPTY_TYPES;
}

/**
* Returns the type of an object-valued constant.
*
* @param sourceModule
* @param constantName
* @param position
* @return the constant value type, or an empty array when it cannot be inferred
*/
public static IType[] getConstantType(ISourceModule sourceModule, String constantName, int position) {
ModuleDeclaration moduleDeclaration = SourceParserUtil.getModuleDeclaration(sourceModule, null);
IContext context = ASTUtils.findContext(sourceModule, moduleDeclaration, position);
if (context != null) {
ConstantReference constantReference = new ConstantReference(position, position + constantName.length(),
constantName);
ExpressionTypeGoal goal = new ExpressionTypeGoal(context, constantReference);
PHPTypeInferencer typeInferencer = new PHPTypeInferencer();
IEvaluatedType evaluatedType = typeInferencer.evaluateType(goal);
IType[] modelElements = getTypes(position, context, evaluatedType);
if (modelElements != null) {
return modelElements;
}
}
return EMPTY_TYPES;
}

private static VariableReference getVariableReference(String variableName, int position) {
String start = ""; //$NON-NLS-1$
int arrayType = 0;
Expand Down Expand Up @@ -687,6 +713,16 @@ private static IType[] innerGetClassName(ISourceModule sourceModule, @NonNull Te
int statementStart = statementText.getOriginalOffset(classNameStart);
return getVariableType(sourceModule, className, statementStart);
}
// Object-valued constants are valid receivers since PHP 8.1. Unlike
// variables and function calls, a bare constant has no syntactic marker,
// so resolve the identifier through normal expression type inference.
if (!isClassTriger && classNameStart >= 0 && className != null && className.length() > 0) {
int statementStart = statementText.getOriginalOffset(classNameStart);
IType[] constantTypes = getConstantType(sourceModule, className, statementStart);
if (ArrayUtils.isNotEmpty(constantTypes)) {
return constantTypes;
}
}
boolean arrayReference = false;
if (propertyEndPosition > 0 && statementText.charAt(propertyEndPosition - 1) == ']'
&& phpVersion.isGreaterThan(PHPVersion.PHP5_3)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ private IModelElement[] internalASTResolve(ISourceModule sourceModule, IModelAcc
}

end = PHPTextSequenceUtilities.readIdentifierEndIndex(source, end, true);
IModelElement[] constantMethod = resolveObjectConstantMethod(sourceModule, cache, source, offset);
if (constantMethod != null) {
return constantMethod;
}

ModuleDeclaration parsedUnit = SourceParserUtil.getModuleDeclaration(sourceModule, null);

Expand Down Expand Up @@ -527,6 +531,64 @@ else if ((node instanceof TypeDeclaration || node instanceof MethodDeclaration)
return null;
}

private IModelElement[] resolveObjectConstantMethod(ISourceModule sourceModule, IModelAccessCache cache,
String source, int methodStart) {
int operatorEnd = methodStart;
while (operatorEnd > 0 && Character.isWhitespace(source.charAt(operatorEnd - 1))) {
operatorEnd--;
}
if (operatorEnd < 2 || source.charAt(operatorEnd - 2) != '-' || source.charAt(operatorEnd - 1) != '>') {
return null;
}

int constantEnd = operatorEnd - 2;
while (constantEnd > 0 && Character.isWhitespace(source.charAt(constantEnd - 1))) {
constantEnd--;
}
int constantStart = constantEnd;
while (constantStart > 0 && Character.isJavaIdentifierPart(source.charAt(constantStart - 1))) {
constantStart--;
}
if (constantStart == constantEnd
|| constantStart > 0 && source.charAt(constantStart - 1) == '$') {
return null;
}

int methodEnd = methodStart;
while (methodEnd < source.length() && Character.isJavaIdentifierPart(source.charAt(methodEnd))) {
methodEnd++;
}
if (methodStart == methodEnd) {
return null;
}
int openParenthesis = methodEnd;
while (openParenthesis < source.length() && Character.isWhitespace(source.charAt(openParenthesis))) {
openParenthesis++;
}
if (openParenthesis >= source.length() || source.charAt(openParenthesis) != '(') {
return null;
}

String constantName = source.substring(constantStart, constantEnd);
IType[] types = CodeAssistUtils.getConstantType(sourceModule, constantName, constantStart);
if (types == null || types.length == 0) {
return null;
}

String methodName = source.substring(methodStart, methodEnd);
List<IModelElement> methods = new LinkedList<>();
for (IType type : types) {
try {
ITypeHierarchy hierarchy = cache.getSuperTypeHierarchy(type, null);
methods.addAll(Arrays.asList(
PHPModelUtils.getFirstTypeHierarchyMethod(type, hierarchy, methodName, true, null)));
} catch (CoreException e) {
PHPCorePlugin.log(e);
}
}
return methods.toArray(new IModelElement[methods.size()]);
}

private IModelElement[] lookForMatchingElements(PHPDocTag[] tags, ISourceModule sourceModule,
ModuleDeclaration parsedUnit, int offset, int end, boolean isMethodOrFunction, IModelAccessCache cache)
throws ModelException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public int compare(ISourceRange o1, ISourceRange o2) {
ConstantDeclarationSearcher searcher = new ConstantDeclarationSearcher(fileOffsets, constantName);
try {
moduleDeclaration.traverse(searcher);
for (Scalar scalar : searcher.getDeclarations()) {
subGoals.add(new ExpressionTypeGoal(goal.getContext(), scalar));
for (Expression expression : searcher.getDeclarations()) {
subGoals.add(new ExpressionTypeGoal(goal.getContext(), expression));
}
} catch (Exception e) {
PHPCorePlugin.log(e);
Expand Down Expand Up @@ -135,15 +135,15 @@ static class ConstantDeclarationSearcher extends ASTVisitor {
private int currentStart;
private int currentEnd;
private boolean stopProcessing;
private List<Scalar> declarations = new LinkedList<>();
private List<Expression> declarations = new LinkedList<>();

public ConstantDeclarationSearcher(SortedSet<ISourceRange> offsets, String constantName) {
this.constantName = constantName;
offsetsIt = offsets.iterator();
setNextRange();
}

public List<Scalar> getDeclarations() {
public List<Expression> getDeclarations() {
return declarations;
}

Expand All @@ -168,14 +168,13 @@ public boolean visit(CallExpression node) throws Exception {
if ("define".equalsIgnoreCase(node.getName())) { //$NON-NLS-1$
// report global constant:
List<ASTNode> args = node.getArgs().getChilds();
if (args.size() == 2) {
if (args.size() >= 2) {
ASTNode firstArg = args.get(0);
ASTNode secondArg = args.get(1);
if (firstArg instanceof Scalar && secondArg instanceof Scalar) {
if (firstArg instanceof Scalar && secondArg instanceof Expression) {
Scalar constantName = (Scalar) firstArg;
Scalar constantValue = (Scalar) secondArg;
if (this.constantName.equals(stripQuotes(constantName.getValue()))) {
declarations.add(constantValue);
declarations.add((Expression) secondArg);
}
}
}
Expand All @@ -189,7 +188,7 @@ public boolean visit(ConstantDeclaration node) throws Exception {
}
Expression value = node.getConstantValue();
if (value instanceof Scalar) {
declarations.add((Scalar) value);
declarations.add(value);
}
return visitGeneral(node);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2026 Karel Kuhpast and others.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.php.internal.core.typeinference.evaluators;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.dltk.ast.references.ConstantReference;
import org.eclipse.dltk.ti.GoalState;
import org.eclipse.dltk.ti.IContext;
import org.eclipse.dltk.ti.goals.GoalEvaluator;
import org.eclipse.dltk.ti.goals.IGoal;
import org.eclipse.dltk.ti.types.IEvaluatedType;
import org.eclipse.php.internal.core.typeinference.PHPTypeInferenceUtils;
import org.eclipse.php.internal.core.typeinference.context.INamespaceContext;
import org.eclipse.php.internal.core.typeinference.goals.ConstantDeclarationGoal;

/**
* Resolves a bare PHP constant reference to its declaration so its value can
* participate in normal expression type inference.
*/
public class ConstantReferenceEvaluator extends GoalEvaluator {

private final ConstantReference constantReference;
private final List<IEvaluatedType> evaluatedTypes = new LinkedList<>();

public ConstantReferenceEvaluator(IGoal goal, ConstantReference constantReference) {
super(goal);
this.constantReference = constantReference;
}

@Override
public IGoal[] init() {
IContext context = goal.getContext();
String constantName = constantReference.getName();
String namespace = null;
if (context instanceof INamespaceContext) {
namespace = ((INamespaceContext) context).getNamespace();
}

List<IGoal> subGoals = new LinkedList<>();
if (namespace != null && !namespace.isEmpty()) {
subGoals.add(new ConstantDeclarationGoal(context, constantName, namespace));
}
subGoals.add(new ConstantDeclarationGoal(context, constantName, null));
return subGoals.toArray(new IGoal[subGoals.size()]);
}

@Override
public Object produceResult() {
return PHPTypeInferenceUtils.combineTypes(evaluatedTypes);
}

@Override
public IGoal[] subGoalDone(IGoal subgoal, Object result, GoalState state) {
if (state != GoalState.RECURSIVE && result instanceof IEvaluatedType) {
evaluatedTypes.add((IEvaluatedType) result);
}
return IGoal.NO_GOALS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.eclipse.dltk.ast.ASTNode;
import org.eclipse.dltk.ast.declarations.TypeDeclaration;
import org.eclipse.dltk.ast.references.ConstantReference;
import org.eclipse.dltk.ast.references.TypeReference;
import org.eclipse.dltk.ast.references.VariableReference;
import org.eclipse.dltk.ti.IGoalEvaluatorFactory;
Expand Down Expand Up @@ -89,6 +90,9 @@ private GoalEvaluator createExpressionEvaluator(ExpressionTypeGoal exprGoal) {
Scalar scalar = (Scalar) expression;
return new ScalarEvaluator(exprGoal, scalar);
}
if (expressionClass == ConstantReference.class) {
return new ConstantReferenceEvaluator(exprGoal, (ConstantReference) expression);
}
if (expressionClass == TypeReference.class || expressionClass == FullyQualifiedReference.class) {
TypeReference type = (TypeReference) expression;
return new TypeReferenceEvaluator(exprGoal, type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,17 @@ public class SelectionEngineTests {
new String[] { "/workspace/selection/php5", "/workspace/selection/php53", "/workspace/selection/php54",
"/workspace/selection/php55", "/workspace/selection/php56", "/workspace/selection/php7",
"/workspace/selection/php71", "/workspace/selection/php72", "/workspace/selection/php73",
"/workspace/selection/php80" });
"/workspace/selection/php80", "/workspace/selection/php81" });
TESTS.put(PHPVersion.PHP8_2,
new String[] { "/workspace/selection/php5", "/workspace/selection/php53", "/workspace/selection/php54",
"/workspace/selection/php55", "/workspace/selection/php56", "/workspace/selection/php7",
"/workspace/selection/php71", "/workspace/selection/php72", "/workspace/selection/php73",
"/workspace/selection/php80" });
"/workspace/selection/php80", "/workspace/selection/php81" });
TESTS.put(PHPVersion.PHP8_3,
new String[] { "/workspace/selection/php5", "/workspace/selection/php53", "/workspace/selection/php54",
"/workspace/selection/php55", "/workspace/selection/php56", "/workspace/selection/php7",
"/workspace/selection/php71", "/workspace/selection/php72", "/workspace/selection/php73",
"/workspace/selection/php80" });
"/workspace/selection/php80", "/workspace/selection/php81" });
};

protected IProject project;
Expand Down Expand Up @@ -266,4 +266,4 @@ private static String getCursor(PdttFile pdttFile) {
return config.get("cursor");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Object-valued global constant member completion
--FILE--
<?php
class Database
{
public function query(): mixed
{
return null;
}
}

define('DATABASE', new Database());

DATABASE->|
?>
--EXPECT--
method(query)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
Select a method through an object-valued global constant
--FILE--
<?php
class Database
{
public function query(): mixed
{
return null;
}
}

define('DATABASE', new Database());

DATABASE->|query|();
?>
--EXPECT--
method(query)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
Select a property through an object-valued global constant
--FILE--
<?php
class Database
{
public mixed $error = null;
}

define('DATABASE', new Database());

DATABASE->|error|;
?>
--EXPECT--
field($error)