use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project yii2support by nvlad.
the class ViewMissedPhpDocInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
return new PhpElementVisitor() {
@Override
public void visitPhpFile(PhpFile PhpFile) {
Project project = PhpFile.getProject();
ViewResolve resolve = ViewUtil.resolveView(PhpFile.getVirtualFile(), project);
if (resolve == null) {
return;
}
Map<String, String> params = getVariables(PhpFile);
Map<String, String> declaredVariables = new HashMap<>();
Collection<PhpDocVariable> variableCollection = PsiTreeUtil.findChildrenOfType(PhpFile, PhpDocVariable.class);
for (PhpDocVariable variable : variableCollection) {
declaredVariables.put(variable.getName(), variable.getType().toString());
}
Map<String, String> missedVariables = new HashMap<>();
for (String variableName : params.keySet()) {
if (!declaredVariables.containsKey(variableName)) {
missedVariables.put(variableName, params.get(variableName));
}
}
if (missedVariables.isEmpty()) {
return;
}
String problemDescription = "Missed View variable declaration.";
ViewMissedPhpDocLocalQuickFix quickFix = new ViewMissedPhpDocLocalQuickFix(PhpFile, missedVariables);
problemsHolder.registerProblem(PhpFile, problemDescription, quickFix);
}
private Map<String, String> getVariables(PhpFile phpFile) {
Collection<PsiReference> references = ReferencesSearch.search(phpFile).findAll();
Map<String, String> result = new HashMap<>();
Map<String, PhpType> viewArgumentCollection = new LinkedHashMap<>();
for (PsiReference reference : references) {
MethodReference methodReference = PsiTreeUtil.getParentOfType(reference.getElement(), MethodReference.class);
if (methodReference == null) {
continue;
}
Map<String, PhpType> params = RenderUtil.getViewArguments(methodReference);
for (Map.Entry<String, PhpType> entry : params.entrySet()) {
if (viewArgumentCollection.containsKey(entry.getKey())) {
PhpType.PhpTypeBuilder typeBuilder = new PhpType.PhpTypeBuilder();
typeBuilder.add(viewArgumentCollection.get(entry.getKey()));
typeBuilder.add(entry.getValue());
viewArgumentCollection.replace(entry.getKey(), typeBuilder.build());
} else {
viewArgumentCollection.put(entry.getKey(), entry.getValue());
}
}
}
for (String key : viewArgumentCollection.keySet()) {
result.put(key, viewArgumentCollection.get(key).toString());
}
return result;
}
};
}
use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project yii2support by nvlad.
the class MissedViewLocalQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final String projectUrl = YiiApplicationUtils.getYiiRootUrl(project);
final VirtualFileManager virtualFileManager = VirtualFileManager.getInstance();
VirtualFile virtualFile = virtualFileManager.findFileByUrl(projectUrl + myPath);
if (virtualFile != null) {
System.out.println("File " + projectUrl + myPath + " already exist.");
return;
}
VirtualFile yiiRoot = YiiApplicationUtils.getYiiRootVirtualFile(project);
if (yiiRoot == null) {
return;
}
PsiDirectory directory = PsiManager.getInstance(project).findDirectory(yiiRoot);
if (directory == null) {
return;
}
List<String> pathElements = StringUtil.split(StringUtil.trimLeading(myPath, '/'), "/");
if (pathElements.size() == 0) {
return;
}
final String fileName = pathElements.get(pathElements.size() - 1);
pathElements.remove(pathElements.size() - 1);
for (String pathElement : pathElements) {
if (directory.findSubdirectory(pathElement) == null) {
directory.createSubdirectory(pathElement);
}
directory = directory.findSubdirectory(pathElement);
if (directory == null) {
return;
}
}
final PsiFile viewPsiFile = directory.createFile(fileName);
FileType fileType = FileTypeManager.getInstance().getFileTypeByExtension(Files.getFileExtension(myPath));
String templateName = getFileTemplateName(fileType);
if (templateName == null) {
System.out.println("Template for \"" + myPath + "\" not detected.");
return;
}
templateName = templateName.toLowerCase();
FileEditorManager.getInstance(project).openFile(viewPsiFile.getVirtualFile(), true);
final FileTemplate[] templates = FileTemplateManager.getDefaultInstance().getTemplates(FileTemplateManager.DEFAULT_TEMPLATES_CATEGORY);
FileTemplate template = null;
for (FileTemplate fileTemplate : templates) {
if (fileTemplate.isTemplateOfType(fileType) && fileTemplate.getName().toLowerCase().equals(templateName)) {
template = fileTemplate;
break;
}
}
if (template != null && viewPsiFile.getViewProvider().getDocument() != null) {
final Properties properties = FileTemplateManager.getDefaultInstance().getDefaultProperties();
if (myParameters != null) {
Set<String> parameters = new LinkedHashSet<>(myParameters.size());
for (Map.Entry<String, PhpType> parameter : myParameters.entrySet()) {
parameters.add(parameter.getKey() + ' ' + parameter.getValue());
}
properties.setProperty("YII2_VIEW_PARAMETERS", StringUtil.join(parameters, ";"));
}
template.setLiveTemplateEnabled(true);
template.setReformatCode(true);
try {
viewPsiFile.getViewProvider().getDocument().insertString(0, template.getText(properties));
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project idea-php-typo3-plugin by cedricziel.
the class MethodArgumentDroppedMatcherInspection method buildVisitor.
@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder problemsHolder, boolean b) {
return new PhpElementVisitor() {
@Override
public void visitPhpElement(PhpPsiElement element) {
if (!PlatformPatterns.psiElement(PhpElementTypes.METHOD_REFERENCE).accepts(element)) {
return;
}
MethodReference methodReference = (MethodReference) element;
ParameterList parameterList = methodReference.getParameterList();
PhpExpression classReference = methodReference.getClassReference();
if (classReference != null) {
PhpType inferredType = classReference.getInferredType();
String compiledClassMethodKey = inferredType.toString() + "->" + methodReference.getName();
if (ExtensionScannerUtil.classMethodHasDroppedArguments(element.getProject(), compiledClassMethodKey)) {
Integer maximumNumberOfArguments = ExtensionScannerUtil.getMaximumNumberOfArguments(element.getProject(), compiledClassMethodKey);
if (parameterList != null && maximumNumberOfArguments != -1 && parameterList.getParameters().length != maximumNumberOfArguments) {
problemsHolder.registerProblem(element, "Number of arguments changed with TYPO3 9, consider refactoring");
}
}
}
}
};
}
use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project idea-php-typo3-plugin by cedricziel.
the class GeneralUtilityServiceTypeProvider method getType.
@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
if (DumbService.getInstance(psiElement.getProject()).isDumb()) {
return null;
}
if (!(psiElement instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(psiElement, "makeInstanceService")) {
return null;
}
MethodReference methodReference = (MethodReference) psiElement;
if (methodReference.getParameters().length == 0) {
return null;
}
PsiElement firstParam = methodReference.getParameters()[0];
if (firstParam instanceof StringLiteralExpression) {
StringLiteralExpression ref = (StringLiteralExpression) firstParam;
String serviceId = ref.getContents();
return new PhpType().add("#" + this.getKey() + serviceId);
}
return null;
}
use of com.jetbrains.php.lang.psi.resolve.types.PhpType in project idea-php-typo3-plugin by cedricziel.
the class GeneralUtilityTypeProvider method getType.
@Nullable
@Override
public PhpType getType(PsiElement psiElement) {
if (DumbService.getInstance(psiElement.getProject()).isDumb()) {
return null;
}
if (!(psiElement instanceof MethodReference) || !PhpElementsUtil.isMethodWithFirstStringOrFieldReference(psiElement, "makeInstance")) {
return null;
}
MethodReference methodReference = (MethodReference) psiElement;
if (methodReference.getParameters().length == 0) {
return null;
}
PsiElement firstParam = methodReference.getParameters()[0];
if (firstParam instanceof PhpReference) {
PhpReference ref = (PhpReference) firstParam;
if (ref.getText().toLowerCase().contains("::class")) {
return new PhpType().add("#" + this.getKey() + ref.getSignature());
}
}
return null;
}
Aggregations