Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overwrite XML injection with plain text for Console output methods #1357

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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 @@ -40,17 +40,29 @@ public class ParameterLanguageInjector implements MultiHostInjector {
new MethodMatcher.CallToSignature("\\Doctrine\\ORM\\Query", "setDQL"),
};

private static final MethodMatcher.CallToSignature[] RESET_INJECTION_SIGNATURES = {
new MethodMatcher.CallToSignature("\\Symfony\\Component\\Console\\Output\\OutputInterface", "write"),
new MethodMatcher.CallToSignature("\\Symfony\\Component\\Console\\Output\\OutputInterface", "writeln"),
new MethodMatcher.CallToSignature("\\Symfony\\Component\\Console\\Output\\Output", "write"),
new MethodMatcher.CallToSignature("\\Symfony\\Component\\Console\\Output\\Output", "writeln"),
new MethodMatcher.CallToSignature("\\Symfony\\Component\\Console\\Formatter\\OutputFormatter", "escape"),
new MethodMatcher.CallToSignature("\\Symfony\\Component\\Console\\Formatter\\OutputFormatter", "escapeTrailingBackslash"),
new MethodMatcher.CallToSignature("\\Symfony\\Component\\Console\\Formatter\\OutputFormatterInterface", "format"),
};

private final MethodLanguageInjection[] LANGUAGE_INJECTIONS = {
new MethodLanguageInjection(LANGUAGE_ID_CSS, "@media all { ", " }", CSS_SELECTOR_SIGNATURES),
new MethodLanguageInjection(LANGUAGE_ID_XPATH, null, null, XPATH_SIGNATURES),
new MethodLanguageInjection(LANGUAGE_ID_JSON, null, null, JSON_SIGNATURES),
new MethodLanguageInjection(LANGUAGE_ID_DQL, null, null, DQL_SIGNATURES),
new MethodLanguageInjection(LANGUAGE_ID_TEXT, null, null, RESET_INJECTION_SIGNATURES),
};

public static final String LANGUAGE_ID_CSS = "CSS";
public static final String LANGUAGE_ID_XPATH = "XPath";
public static final String LANGUAGE_ID_JSON = "JSON";
public static final String LANGUAGE_ID_DQL = "DQL";
public static final String LANGUAGE_ID_TEXT = "TEXT";

private static final String DQL_VARIABLE_NAME = "dql";

Expand Down Expand Up @@ -97,6 +109,10 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull
// JsonResponse::fromJsonString('...')
if (parent instanceof MethodReference) {
if (PhpElementsUtil.isMethodReferenceInstanceOf((MethodReference) parent, languageInjection.getSignatures())) {
// Only "overwrite" language injection to "TEXT" when XML-like characters in literal
if (LANGUAGE_ID_TEXT.equals(language.getID()) && !expr.getContents().contains("<")) {
return;
}
injectLanguage(registrar, expr, language, languageInjection);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
<codeInsight.parameterNameHints language="XML" implementationClass="fr.adrienbrault.idea.symfony2plugin.dic.ServiceArgumentParameterHintsProvider"/>
<codeInsight.parameterNameHints language="yaml" implementationClass="fr.adrienbrault.idea.symfony2plugin.dic.ServiceArgumentParameterHintsProvider"/>

<multiHostInjector implementation="fr.adrienbrault.idea.symfony2plugin.lang.ParameterLanguageInjector"/>
<multiHostInjector implementation="fr.adrienbrault.idea.symfony2plugin.lang.ParameterLanguageInjector" order="first"/>

<localInspection groupPath="Symfony" shortName="PhpRouteMissingInspection" displayName="Route Missing"
groupName="Route"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public void testDqlLanguageInjections() {
assertInjectedLangAtCaret(PhpFileType.INSTANCE, "<?php $dql = \"<caret>\");", LANGUAGE_ID_DQL);
}

public void testTextLanguageInjections() {
String base = "<?php $output = new \\Symfony\\Component\\Console\\Output\\Output();\n";
assertInjectedLangAtCaret(PhpFileType.INSTANCE, base + "$output->write('<info>foo<caret></info>');", LANGUAGE_ID_TEXT);

String base2 = "<?php /** @var $output \\Symfony\\Component\\Console\\Output\\OutputInterface */\n";
assertInjectedLangAtCaret(PhpFileType.INSTANCE, base2 + "$output->writeln('<info>foo<caret></info>');", LANGUAGE_ID_TEXT);

assertInjectedLangAtCaret(PhpFileType.INSTANCE, "\\Symfony\\Component\\Console\\Formatter\\OutputFormatter::escape('<info>foo<caret></info>');", LANGUAGE_ID_TEXT);
}

private void assertInjectedLangAtCaret(LanguageFileType fileType, String configureByText, String lang) {
myFixture.configureByText(fileType, configureByText);
injectionTestFixture.assertInjectedLangAtCaret(lang);
Expand Down