8000 move yaml and php service missing annotator into an inspection to fix… · Koc/idea-php-symfony2-plugin@4f863c2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f863c2

Browse files
committed
move yaml and php service missing annotator into an inspection to fix missing services in Container::get calls should be exposed as an inspection Haehnchen#997
1 parent 40201aa commit 4f863c2

File tree

12 files changed

+153
-162
lines changed

12 files changed

+153
-162
lines changed

META-INF/plugin.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,12 @@
526526
language="PHP"
527527
implementationClass="fr.adrienbrault.idea.symfony2plugin.templating.TwigTemplateMissingInspection"/>
528528

529+
<localInspection groupPath="Symfony" shortName="MissingService" displayName="Missing Service"
530+
groupName="Service"
531+
enabledByDefault="true"
532+
level="WARNING"
533+
implementationClass="fr.adrienbrault.idea.symfony2plugin.dic.inspection.MissingServiceInspection"/>
534+
529535
<intentionAction>
530536
<className>fr.adrienbrault.idea.symfony2plugin.intentions.php.PhpServiceIntention</className>
531537
<category>PHP</category>

src/fr/adrienbrault/idea/symfony2plugin/Settings.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public class Settings implements PersistentStateComponent<Settings> {
7171
public boolean twigAnnotateRoute = true;
7272

7373
public boolean phpAnnotateTemplate = true;
74-
public boolean phpAnnotateService = true;
7574
public boolean phpAnnotateRoute = true;
7675
public boolean phpAnnotateTemplateAnnotation = true;
7776
public boolean phpAnnotateTranslation = true;

src/fr/adrienbrault/idea/symfony2plugin/SettingsForm.form

Lines changed: 12 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@
4444
<rowspec value="center:max(d;4px):noGrow"/>
4545
<rowspec value="top:4dlu:noGrow"/>
4646
<rowspec value="center:max(d;4px):noGrow"/>
47-
<rowspec value="top:4dlu:noGrow"/>
48-
<rowspec value="center:max(d;4px):noGrow"/>
49-
<rowspec value="top:4dlu:noGrow"/>
50-
<rowspec value="center:max(d;4px):noGrow"/>
51-
<rowspec value="top:4dlu:noGrow"/>
52-
<rowspec value="center:max(d;4px):noGrow"/>
5347
<colspec value="fill:max(d;4px):noGrow"/>
5448
<colspec value="left:4dlu:noGrow"/>
5549
<colspec value="fill:d:grow"/>
@@ -180,15 +174,6 @@
180174
<text value="Route"/>
181175
</properties>
182176
</component>
183-
<component id="c9461" class="javax.swing.JCheckBox" binding="phpAnnotateService">
184-
<constraints>
185-
<grid row="32" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
186-
<forms/>
187-
</constraints>
188-
<properties>
189-
<text value="Service"/>
190-
</properties>
191-
</component>
192177
<component id="5a5a0" class="javax.swing.JCheckBox" binding="phpAnnotateTemplate">
193178
<constraints>
194179
<grid row="30" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
@@ -198,24 +183,6 @@
198183
<text value="Template"/>
199184
</properties>
200185
</component>
201-
<component id="93ec7" class="javax.swing.JLabel">
202-
<constraints>
203-
<grid row="38" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
204-
<forms/>
205-
</constraints>
206-
<properties>
207-
<text value="Yaml Annotator"/>
208-
</properties>
209-
</component>
210-
<component id="42ded" class="javax.swing.JCheckBox" binding="yamlAnnotateServiceConfig">
211-
<constraints>
212-
<grid row="40" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
213-
<forms/>
214-
</constraints>
215-
<properties>
216-
<text value="Service DIC"/>
217-
</properties>
218-
</component>
219186
<component id="224de" class="javax.swing.JCheckBox" binding="objectManagerFindTypeProvider">
220187
<constraints>
221188
<grid row="20" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
@@ -378,40 +345,40 @@
378345
<text value="Constant (Twig)"/>
379346
</properties>
380347
</component>
381-
<component id="a28d6" class="javax.swing.JCheckBox" binding="phpAnnotateTemplateAnnotation">
348+
<component id="dedbe" class="javax.swing.JCheckBox" binding="enableSchedulerCheckBox">
382349
<constraints>
383-
<grid row="34" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
350+
<grid row="8" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
384351
<forms/>
385352
</constraints>
386353
<properties>
387-
<text value="Template Annotation"/>
354+
<text value="Enable 5min &amp;scheduler (reopen Project after change)"/>
388355
</properties>
389356
</component>
390-
<component id="9e0bc" class="javax.swing.JCheckBox" binding="phpAnnotateTranslation">
357+
<component id="b004d" class="javax.swing.JLabel">
391358
<constraints>
392-
<grid row="36" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
359+
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
393360
<forms/>
394361
</constraints>
395362
<properties>
< 6D code>396-
<text value="Translation"/>
363+
<text value="Download remote files (exp.)"/>
397364
</properties>
398365
</component>
399-
<component id="dedbe" class="javax.swing.JCheckBox" binding="enableSchedulerCheckBox">
366+
<component id="a28d6" class="javax.swing.JCheckBox" binding="phpAnnotateTemplateAnnotation">
400367
<constraints>
401-
<grid row="8" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
368+
<grid row="32" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
402369
<forms/>
403370
</constraints>
404371
<properties>
405-
<text value="Enable 5min &amp;scheduler (reopen Project after change)"/>
372+
<text value="Template Annotation"/>
406373
</properties>
407374
</component>
408-
<component id="b004d" class="javax.swing.JLabel">
375+
<component id="9e0bc" class="javax.swing.JCheckBox" binding="phpAnnotateTranslation">
409376
<constraints>
410-
<grid row="8" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
377+
<grid row="34" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
411378
<forms/>
412379
</constraints>
413380
<properties>
414-
<text value="Download remote files (exp.)"/>
381+
<text value="Translation"/>
415382
</properties>
416383
</component>
417384
</children>

src/fr/adrienbrault/idea/symfony2plugin/SettingsForm.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,9 @@ public class SettingsForm implements Configurable {
4545
private JCheckBox twigAnnotateAsset;
4646
private JCheckBox twigAnnotateAssetTags;
4747
private JCheckBox phpAnnotateTemplate;
48-
private JCheckBox phpAnnotateService;
4948
private JCheckBox phpAnnotateRoute;
5049
private JCheckBox phpAnnotateTemplateAnnotation;
5150
private JCheckBox pluginEnabled;
52-
private JCheckBox yamlAnnotateServiceConfig;
5351

5452
private JButton directoryToWebReset;
5553
private JLabel directoryToWebLabel;
@@ -142,7 +140,6 @@ public boolean isModified() {
142140
|| !twigAnnotateAssetTags.isSelected() == getSettings().twigAnnotateAssetTags
143141

144142
|| !phpAnnotateTemplate.isSelected() == getSettings().phpAnnotateTemplate
145-
|| !phpAnnotateService.isSelected() == getSettings().phpAnnotateService
146143
|| !phpAnnotateRoute.isSelected() == getSettings().phpAnnotateRoute
147144
|| !phpAnnotateTemplateAnnotation.isSelected() == getSettings().phpAnnotateTemplateAnnotation
148145
|| !phpAnnotateTranslation.isSelected() == getSettings().phpAnnotateTranslation
@@ -154,8 +151,6 @@ public boolean isModified() {
154151
|| !codeFoldingTwigTemplate.isSelected() == getSettings().codeFoldingTwigTemplate
155152
|| !codeFoldingTwigConstant.isSelected() == getSettings().codeFoldingTwigConstant
156153

157-
|| !yamlAnnotateServiceConfig.isSelected() == getSettings().yamlAnnotateServiceConfig
158-
159154
|| !directoryToApp.getText().equals(getSettings().directoryToApp)
160155
|| !directoryToWeb.getText().equals(getSettings().directoryToWeb)
161156
;
@@ -181,7 +176,6 @@ public void apply() throws ConfigurationException {
181176
getSettings().twigAnnotateAssetTags = twigAnnotateAssetTags.isSelected();
182177

183178
getSettings().phpAnnotateTemplate = phpAnnotateTemplate.isSelected();
184-
getSettings().phpAnnotateService = phpAnnotateService.isSelected();
185179
getSettings().phpAnnotateRoute = phpAnnotateRoute.isSelected();
186180
getSettings().phpAnnotateTemplateAnnotation = phpAnnotateTemplateAnnotation.isSelected();
187181
getSettings().phpAnnotateTranslation = phpAnnotateTranslation.isSelected();
@@ -193,8 +187,6 @@ public void apply() throws ConfigurationException {
193187
getSettings().codeFoldingTwigTemplate = codeFoldingTwigTemplate.isSelected();
194188
getSettings().codeFoldingTwigConstant = codeFoldingTwigConstant.isSelected();
195189

196-
getSettings().yamlAnnotateServiceConfig = yamlAnnotateServiceConfig.isSelected();
197-
198190
getSettings().directoryToApp = directoryToApp.getText();
199191
getSettings().directoryToWeb = directoryToWeb.getText();
200192
}
@@ -231,7 +223,6 @@ private void updateUIFromSettings() {
231223
twigAnnotateAssetTags.setSelected(getSettings().twigAnnotateAssetTags);
232224

233225
phpAnnotateTemplate.setSelected(getSettings().phpAnnotateTemplate);
234-
phpAnnotateService.setSelected(getSettings().phpAnnotateService);
235226
phpAnnotateRoute.setSelected(getSettings().phpAnnotateRoute);
236227
phpAnnotateTemplateAnnotation.setSelected(getSettings().phpAnnotateTemplateAnnotation);
237228
phpAnnotateTranslation.setSelected(getSettings().phpAnnotateTranslation);
@@ -243,8 +234,6 @@ private void updateUIFromSettings() {
243234
codeFoldingTwigTemplate.setSelected(getSettings().codeFoldingTwigTemplate);
244235
codeFoldingTwigConstant.setSelected(getSettings().codeFoldingTwigConstant);
245236

246-
yamlAnnotateServiceConfig.setSelected(getSettings().yamlAnnotateServiceConfig);
247-
248237
directoryToApp.setText(getSettings().directoryToApp);
249238
directoryToWeb.setText(getSettings().directoryToWeb);
250239
}

src/fr/adrienbrault/idea/symfony2plugin/config/yaml/YamlAnnotator.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ public class YamlAnnotator implements Annotator {
4444
@Override
4545
public void annotate(@NotNull final PsiElement psiElement, @NotNull AnnotationHolder holder) {
4646

47-
if(!Symfony2ProjectComponent.isEnabled(psiElement.getProject()) || !Settings.getInstance(psiElement.getProject()).yamlAnnotateServiceConfig) {
47+
if(!Symfony2ProjectComponent.isEnabled(psiElement.getProject())) {
4848
return;
4949
}
5050

5151
this.annotateParameter(psiElement, holder);
5252
this.annotateClass(psiElement, holder);
53-
this.annotateService(psiElement, holder);
5453

5554
// only match inside service definitions
5655
if(!YamlElementPatternHelper.getInsideKeyValue("services").accepts(psiElement)) {
@@ -87,25 +86,6 @@ private void annotateParameter(@NotNull final PsiElement psiElement, @NotNull An
8786

8887
}
8988

90-
private void annotateService(@NotNull final PsiElement psiElement, @NotNull AnnotationHolder holder) {
91-
if(!YamlElementPatternHelper.getServiceDefinition().accepts(psiElement) || !YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(psiElement)) {
92-
return;
93-
}
94-
95-
String serviceName = getServiceName(psiElement);
96-
97-
// dont mark "@", "@?", "@@" escaping and expressions
98-
if(serviceName.length() < 2 || serviceName.startsWith("=") || serviceName.startsWith("@")) {
99-
return;
100-
}
101-
102-
if(ContainerCollectionResolver.hasServiceNames(psiElement.getProject(), serviceName)) {
103-
return;
104-
}
105-
106-
holder.createWarningAnnotation(psiElement, "Missing Service");
107-
}
108-
10989
private void annotateClass(@NotNull final PsiElement element, @NotNull AnnotationHolder holder) {
11090

11191
if(!((YamlElementPatternHelper.getSingleLineScalarKey("class", "factory_class").accepts(element) || YamlElementPatternHelper.getParameterClassPattern().accepts(element)) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element))) {

src/fr/adrienbrault/idea/symfony2plugin/dic/PhpServiceAnnotator.java

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package fr.adrienbrault.idea.symfony2plugin.dic.inspection;
2+
3+
import com.intellij.codeInspection.LocalInspectionTool;
4+
import com.intellij.codeInspection.ProblemHighlightType;
5+
import com.intellij.codeInspection.ProblemsHolder;
6+
import com.intellij.psi.PsiElement;
7+
import com.intellij.psi.PsiElementVisitor;
8+
import com.jetbrains.php.lang.PhpLanguage;
9+
import com.jetbrains.php.lang.psi.elements.MethodReference;
10+
import fr.adrienbrault.idea.symfony2plugin.Symfony2InterfacesUtil;
11+
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
12+
import fr.adrienbrault.idea.symfony2plugin.config.yaml.YamlElementPatternHelper;
13+
import fr.adrienbrault.idea.symfony2plugin.stubs.ContainerCollectionResolver;
14+
import fr.adrienbrault.idea.symfony2plugin.util.PsiElementUtils;
15+
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
16+
import org.apache.commons.lang.StringUtils;
17+
import org.jetbrains.annotations.NotNull;
18+
import org.jetbrains.yaml.YAMLLanguage;
19+
20+
/**
21+
* @author Daniel Espendiller <daniel@espendiller.net>
22+
*/
23+
public class MissingServiceInspection extends LocalInspectionTool< F438 /span> {
24+
25+
public static final String INSPECTION_MESSAGE = "Missing Service";
26+
27+
@NotNull
28+
public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
29+
if (!Symfony2ProjectComponent.isEnabled(holder.getProject())) {
30+
return super.buildVisitor(holder, isOnTheFly);
31+
}
32+
33+
return new MyPsiElementVisitor(holder);
34+
}
35+
36+
private static class MyPsiElementVisitor extends PsiElementVisitor {
37+
private final ProblemsHolder holder;
38+
39+
MyPsiElementVisitor(ProblemsHolder holder) {
40+
this.holder = holder;
41+
}
42+
43+
@Override
44+
public void visitElement(PsiElement element) {
45+
if(element.getLanguage() == PhpLanguage.INSTANCE) {
46+
// PHP
47+
48+
MethodReference methodReference = PsiElementUtils.getMethodReferenceWithFirstStringParameter(element);
49+
if (methodReference != null && new Symfony2InterfacesUtil().isContainerGetCall(methodReference)) {
50+
String serviceName = Symfony2InterfacesUtil.getFirstArgumentStringValue(methodReference);
51+
if(serviceName != null && StringUtils.isNotBlank(serviceName)) {
52+
if(!ContainerCollectionResolver.hasServiceNames(element.getProject(), serviceName)) {
53+
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
54+
}
55+
}
56+
}
57+
} else if(element.getLanguage() == YAMLLanguage.INSTANCE) {
58+
// yaml
59+
60+
if(YamlElementPatternHelper.getServiceDefinition().accepts(element) && YamlElementPatternHelper.getInsideServiceKeyPattern().accepts(element)) {
61+
String serviceName = YamlHelper.trimSpecialSyntaxServiceName(PsiElementUtils.getText(element));
62+
63+
// dont mark "@", "@?", "@@" escaping and expressions
64+
if(serviceName.length() > 2 && !serviceName.startsWith("=") && !serviceName.startsWith("@")) {
65+
if(!ContainerCollectionResolver.hasServiceNames(element.getProject(), serviceName)) {
66+
holder.registerProblem(element, INSPECTION_MESSAGE, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
67+
}
68+
}
69+
}
70+
}
71+
72+
super.visitElement(element);
73+
}
74+
}
75+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<html>
2+
<body>
3+
<p>No Symfony service with given id was found. If you not some help to create one <a href="https://symfony.com/doc/current/service_container.html">follow me</a></p>
4+
<!-- tooltip end -->
5+
</body>
6+
</html>

tests/fr/adrienbrault/idea/symfony2plugin/tests/config/yaml/YamlAnnotatorTest.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,6 @@ public void testAnnotatorParameterForEnvMustNotAnnotationAsMissing() {
142142
assertAnnotationNotContains("services.yml", "services:\n '%ENV(FO<caret>O)%'", "Missing Parameter");
143143
}
144144

145-
public void testAnnotatorServiceName() {
146-
assertAnnotationContains("services.yml", "services:\n @args<caret>_unknown", "Missing Service");
147-
assertAnnotationContains("services.yml", "services:\n @Args<caret>_unknown", "Missing Service");
148-
assertAnnotationNotContains("services.yml", "services:\n @args<caret>_foo", "Missing Service");
149-
assertAnnotationNotContains("services.yml", "services:\n @Args<caret>_foo", "Missing Service");
150-
151-
assertAnnotationNotContains("services.yml", "services:\n @@args<caret>_unknown", "Missing Service");
152-
assertAnnotationNotContains("services.yml", "services:\n @=args<caret>_unknown", "Missing Service");
153-
}
154-
155145
public void testAnnotatorClass() {
156146
assertAnnotationContains("services.yml", "services:\n class: Args\\Fo<caret>oBar", "Missing Class");
157147
assertAnnotationContains("services.yml", "services:\n class: 'Args\\Fo<caret>oBar'", "Missing Class");

0 commit comments

Comments
 (0)
0