Skip to content

Commit

Permalink
Merge pull request #28 from Alireza-Moh/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
Alireza-Moh authored Feb 12, 2025
2 parents 4ca4935 + 786a895 commit 828933a
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 77 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "at.alirezamoh.whisperer-for-laravel"
version = "1.2.0"
version = "1.2.1"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ private boolean isViewOrRouteFacadeMethod(MethodReference methodReference, Proje
* @return true or false
*/
private boolean isExpectedFacadeMethod(String methodName, List<PhpClassImpl> resolvedClasses, PhpClass expectedClass, Map<String, Integer> methodMap) {
return methodMap.containsKey(methodName)
return methodName != null
&& methodMap.containsKey(methodName)
&& expectedClass != null
&& resolvedClasses.stream().anyMatch(clazz -> PhpClassUtils.isChildOf(clazz, expectedClass));
}
Expand Down Expand Up @@ -173,6 +174,10 @@ private boolean isViewFunctionParam(FunctionReference functionReference, PsiElem
* @return true or false
*/
private boolean isExpectedParam(PsiElement position, String methodName, Map<String, Integer> methodMap) {
if (methodName == null) {
return false;
}

Integer expectedParamIndex = methodMap.get(methodName);
return expectedParamIndex != null && expectedParamIndex == MethodUtils.findParamIndex(position, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ public List<BladeModule> getBladeFilesInModule() {
*/
private void initParameters(MethodReference method) {
String viewNamespace = PsiElementUtils.getMethodParameterAt(method, 1);
ParameterList parameters = method.getParameterList();
if (viewNamespace == null) {
return;
}

ParameterList parameters = method.getParameterList();
if (parameters == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,11 @@ private boolean isConfigParam(PsiElement reference, PsiElement position) {
? ((MethodReference) reference).getName()
: ((FunctionReference) reference).getName();

Integer expectedParamIndex = CONFIG_METHODS.get(referenceName);
if (referenceName == null) {
return false;
}

Integer expectedParamIndex = CONFIG_METHODS.get(referenceName);
if (expectedParamIndex == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,39 @@ public List<ConfigModule> getConfigFilesInModule() {
*/
private void initParameters(MethodReference method) {
String configKeyIdentifier = PsiElementUtils.getMethodParameterAt(method, 1);
String configFileName = PsiElementUtils.getMethodParameterAt(method, 0);

if (configKeyIdentifier == null || configFileName == null) {
if (configKeyIdentifier == null) {
return;
}

ParameterList parameters = method.getParameterList();
if (parameters != null) {
PsiElement namespaceParameter = parameters.getParameter(0);
if (namespaceParameter instanceof ConcatenationExpressionImpl concatenationExpression) {
PsiFile containingFile = concatenationExpression.getContainingFile();
VirtualFile virtualFile = containingFile.getVirtualFile();
if (parameters == null) {
return;
}

PsiElement namespaceParameter = parameters.getParameter(0);
if (!(namespaceParameter instanceof ConcatenationExpressionImpl concatenationExpression)) {
return;
}

PsiFile containingFile = concatenationExpression.getContainingFile();
VirtualFile virtualFile = containingFile.getVirtualFile();
if (virtualFile == null) {
return;
}

if (virtualFile != null) {
VirtualFile parentDir = virtualFile.getParent();
PsiElement rightOperand = concatenationExpression.getRightOperand();
if (rightOperand instanceof StringLiteralExpression relativePathConfigFilePath && parentDir != null) {
VirtualFile resolvedVirtualFile = parentDir.findFileByRelativePath(
StrUtils.removeQuotes(relativePathConfigFilePath.getText())
);
VirtualFile parentDir = virtualFile.getParent();
PsiElement rightOperand = concatenationExpression.getRightOperand();
if (rightOperand instanceof StringLiteralExpression relativePathConfigFilePath && parentDir != null) {
VirtualFile resolvedVirtualFile = parentDir.findFileByRelativePath(
StrUtils.removeQuotes(relativePathConfigFilePath.getText())
);

if (resolvedVirtualFile != null) {
PsiFile configFile = PsiManager.getInstance(project).findFile(resolvedVirtualFile);
if (configFile != null) {
configFilesInModule.add(
new ConfigModule(configFile, configKeyIdentifier)
);
}
}
}
if (resolvedVirtualFile != null) {
PsiFile configFile = PsiManager.getInstance(project).findFile(resolvedVirtualFile);
if (configFile != null) {
configFilesInModule.add(
new ConfigModule(configFile, configKeyIdentifier)
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ private boolean isTableMethod(MethodReference methodReference) {
* @return true or false
*/
private boolean isTableParam(MethodReference method, PsiElement position) {
int paramIndex = MethodUtils.findParamIndex(position, false);
Integer paramPosition = LaravelPaths.DB_TABLE_METHODS.get(method.getName());

return paramPosition == paramIndex;
if (paramPosition == null) {
return false;
}
return paramPosition == MethodUtils.findParamIndex(position, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ public boolean isGateParam(MethodReference method, PsiElement position) {
if (expectedParamIndex == null) {
return false;
}

return GATE_METHODS.get(method.getName()) == MethodUtils.findParamIndex(position, false);
return expectedParamIndex == MethodUtils.findParamIndex(position, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ private boolean isLaravelRouteMethod(MethodReference methodReference) {

return routeClassReference instanceof ClassReferenceImpl classReferences
&& ROUTE_METHODS.containsKey(methodReference.getName())
&& classReferences.getFQN() != null
&& ROUTE_NAMESPACES.contains(classReferences.getFQN());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ private static boolean isExpectedMethod(
Map<String, Integer> methodMap,
String expectedClassFQN
) {
Integer expectedParamIndex = methodMap.get(methodReference.getName());
String methodName = methodReference.getName();
if (methodName == null) {
return false;
}

Integer expectedParamIndex = methodMap.get(methodName);

if (expectedParamIndex == null) {
return false;
Expand All @@ -124,7 +129,12 @@ private static boolean isInertiaFunction(PsiElement position) {
return false;
}

Integer expectedParamIndex = ROUTE_INERTIA_METHODS.get(functionReference.getName());
String methodName = functionReference.getName();
if (methodName == null) {
return false;
}

Integer expectedParamIndex = ROUTE_INERTIA_METHODS.get(methodName);

if (expectedParamIndex == null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ else if (parentElement instanceof MethodReference methodRef) {
}

private PsiElement resolveParentElement(PsiElement sourceElement) {
PsiElement parent = sourceElement.getParent().getParent().getParent();
if (parent instanceof ArrayAccessExpression arrayAccessExpression) {
PsiElement greatGrandParent = getNthParent(sourceElement, 3);
if (greatGrandParent == null) {
return null;
}

if (greatGrandParent instanceof ArrayAccessExpression arrayAccessExpression) {
return arrayAccessExpression.getValue();
}

parent = sourceElement.getParent();
PsiElement parent = sourceElement.getParent();
if (parent instanceof FieldReferenceImpl fieldReference) {
return fieldReference.getClassReference();
}
Expand Down Expand Up @@ -87,4 +91,15 @@ private PsiElement resolveParentElement(PsiElement sourceElement) {
.map(ArrayHashElement::getKey)
.toArray(PsiElement[]::new);
}

private PsiElement getNthParent(PsiElement element, int n) {
PsiElement current = element;
for (int i = 0; i < n; i++) {
if (current == null) {
return null;
}
current = current.getParent();
}
return current;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static Collection<ArrayHashElement> getRules(PhpClassImpl phpClass, Proje
}

/**
* Extracts validation rules from a method if it references a validation method.
* Analyzes the given method to extract validation rules defined through calls to known validation methods
*
* @param method the method to analyze
* @return a collection of ArrayHashElements representing the validation rules
Expand All @@ -113,7 +113,8 @@ public static Collection<ArrayHashElement> extractValidationRulesFromMethod(Meth

return PsiTreeUtil.findChildrenOfType(method, MethodReference.class).stream()
.filter(methodReference -> {
if (!VALIDATION_METHODS.contains(methodReference.getName())) {
String methodName = methodReference.getName();
if (methodName == null || !VALIDATION_METHODS.contains(methodName)) {
return false;
}

Expand Down Expand Up @@ -235,14 +236,19 @@ public static boolean isInsideCorrectMethod(PsiElement psiElement, Project proje
MethodReference methodReference = MethodUtils.resolveMethodReference(psiElement, 10);

return methodReference != null
&& methodReference.getName() != null
&& PhpClassUtils.isCorrectRelatedClass(methodReference, project, REQUEST_CLASSES)
&& REQUEST_METHODS.containsKey(methodReference.getName())
&& isFieldParam(methodReference, psiElement);
}

public static boolean isFieldParam(MethodReference method, PsiElement position) {
Integer paramPositions = REQUEST_METHODS.get(method.getName());
String methodName = method.getName();
if (methodName == null) {
return false;
}

Integer paramPositions = REQUEST_METHODS.get(methodName);
if (paramPositions == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,11 @@ public static boolean isInsideCorrectMethod(PsiElement psiElement, Project proje
}

private static boolean isInsideRequestMethod(PsiElement psiElement, MethodReference methodReference, Project project) {
String methodName = methodReference.getName();

return PhpClassUtils.isCorrectRelatedClass(methodReference, project, "\\Illuminate\\Http\\Request")
&& RequestFieldUtils.VALIDATION_METHODS.contains(methodReference.getName())
&& methodName != null
&& RequestFieldUtils.VALIDATION_METHODS.contains(methodName)
&& isRuleParam(methodReference, psiElement)
&& isInsideArrayValue(psiElement);
}
Expand Down Expand Up @@ -108,7 +110,6 @@ public static boolean isRuleParam(MethodReference method, PsiElement position) {
if (paramPositions == null) {
return false;
}

return MethodUtils.findParamIndex(position, false) == paramPositions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import com.jetbrains.php.lang.psi.elements.MethodReference;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class RouteNamespaceCompletionContributor extends CompletionContributor {
/**
Expand Down Expand Up @@ -94,7 +97,6 @@ private boolean isNamespaceParam(MethodReference reference, PsiElement position)
if (expectedParamIndex == null) {
return false;
}

return MethodUtils.findParamIndex(position, false) == expectedParamIndex;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ private boolean isRouteActionParam(MethodReference methodReference, PsiElement p
if (expectedParamIndex == null) {
return false;
}

return MethodUtils.findParamIndex(position, false) == expectedParamIndex;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ && isRouteActionParam(method, psiElement)
*/
private boolean isRouteActionParam(MethodReference methodReference, PsiElement position) {
Integer expectedParamIndex = ROUTE_METHODS.get(methodReference.getName());

if (expectedParamIndex == null) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,11 @@ private boolean isRouteParam(PsiElement reference, PsiElement position) {
? ((MethodReference) reference).getName()
: ((FunctionReference) reference).getName();

Integer expectedParamIndex = REDIRECT_AND_URL_METHODS.get(referenceName);
if (referenceName == null) {
return false;
}

Integer expectedParamIndex = REDIRECT_AND_URL_METHODS.get(referenceName);
if (expectedParamIndex == null) {
expectedParamIndex = ROUTE_METHODS.get(referenceName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package at.alirezamoh.whisperer_for_laravel.statusBar;

import at.alirezamoh.whisperer_for_laravel.support.utils.PluginUtils;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.application.ReadAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.StatusBarWidget;
import com.intellij.util.concurrency.AppExecutorUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class LaravelVersionStatusBarWidget implements StatusBarWidget {
private final Project project;
private String versionText = "";

public LaravelVersionStatusBarWidget(Project project) {
this.project = project;
ReadAction.nonBlocking(() -> PluginUtils.laravelVersion(project)).finishOnUiThread(ModalityState.nonModal(), version -> {
if (version != null) {
versionText = "Laravel: " + version;
}
}).submit(AppExecutorUtil.getAppExecutorService());
}

@Override
Expand All @@ -23,7 +30,7 @@ public LaravelVersionStatusBarWidget(Project project) {
return new TextPresentation() {
@Override
public @NotNull String getText() {
return "Laravel: " + PluginUtils.laravelVersion(project);
return versionText;
}

@Override
Expand All @@ -33,7 +40,7 @@ public float getAlignment() {

@Override
public @Nullable String getTooltipText() {
return null;
return "Displays the Laravel framework version for the project";
}
};
}
Expand Down
Loading

0 comments on commit 828933a

Please sign in to comment.