From b7b151dfc7831e76ad7a798c60a89afb1b53028a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Paku=C5=82a?= Date: Fri, 15 Dec 2023 14:34:12 +0100 Subject: [PATCH] Fix #263 - add strict_types to php class/interface/trait wizard --- .../resources/templates/ClassTemplate.tpl.php | 4 +++- .../templates/InterfaceTemplate.tpl.php | 2 ++ .../ui/wizards/types/NewPHPClassPage.java | 15 ++++++++++++--- .../ui/wizards/types/NewPHPClassWizard.java | 1 + .../ui/wizards/types/NewPHPElementData.java | 1 + .../ui/wizards/types/NewPHPInterfacePage.java | 12 +++++++++--- .../wizards/types/NewPHPInterfaceWizard.java | 1 + .../ui/wizards/types/NewPHPTraitPage.java | 11 ++++++++--- .../ui/wizards/types/NewPHPTraitWizard.java | 1 + .../ui/wizards/types/NewPHPTypePage.java | 1 + .../ui/wizards/types/PHPClassTemplate.java | 18 ++++++++++++++---- .../ui/wizards/types/PHPElementTemplate.java | 3 +++ .../ui/wizards/types/PHPInterfaceTemplate.java | 8 ++++++++ .../ui/wizards/types/PHPTraitTemplate.java | 8 ++++++++ 14 files changed, 72 insertions(+), 14 deletions(-) diff --git a/plugins/org.eclipse.php.ui/resources/templates/ClassTemplate.tpl.php b/plugins/org.eclipse.php.ui/resources/templates/ClassTemplate.tpl.php index eff3e92ee1..845a40d713 100644 --- a/plugins/org.eclipse.php.ui/resources/templates/ClassTemplate.tpl.php +++ b/plugins/org.eclipse.php.ui/resources/templates/ClassTemplate.tpl.php @@ -1,4 +1,6 @@ interfacesList = page.getInterfaces(); IType[] interfaces = new IType[interfacesList.size()]; interfacesList.toArray(interfaces); diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/NewPHPTraitPage.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/NewPHPTraitPage.java index 31d22dfa8b..f04488ec6e 100644 --- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/NewPHPTraitPage.java +++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/NewPHPTraitPage.java @@ -35,6 +35,7 @@ public class NewPHPTraitPage extends NewPHPTypePage { private static final String[] TRAIT__CHECKBOXES = new String[] { PHP_DOC_BLOCKS }; + private static final String[] TRAIT__CHECKBOXES7 = new String[] { PHP_DOC_BLOCKS, STRICT_TYPES }; public NewPHPTraitPage() { super(Messages.NewPHPTraitPage_0); @@ -77,7 +78,11 @@ private void createElementSection(Composite container) { addElementNameText(elementSection, Messages.NewPHPTraitPage_3); addNamespaceText(elementSection); - addCheckboxesCreation(elementSection, TRAIT__CHECKBOXES); + if (phpVersion == null || phpVersion.isGreaterThan(PHPVersion.PHP5_4)) { + addCheckboxesCreation(elementSection, TRAIT__CHECKBOXES7); + } else { + addCheckboxesCreation(elementSection, TRAIT__CHECKBOXES); + } } @Override @@ -108,8 +113,8 @@ protected IStatus findMostSevereStatus() { } /** - * This method was overriden to handle cases in which project's PHP version is - * less than 5 + * This method was overriden to handle cases in which project's PHP version + * is less than 5 */ @Override public void setVisible(boolean visible) { diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/NewPHPTraitWizard.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/NewPHPTraitWizard.java index 0864701df5..d4ed823268 100644 --- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/NewPHPTraitWizard.java +++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/NewPHPTraitWizard.java @@ -96,6 +96,7 @@ private NewPHPElementData populatePHPElementData() { NewPHPTraitPage page = (NewPHPTraitPage) this.page; NewPHPElementData data = new NewPHPElementData(); data.isGeneratePHPDoc = page.isCheckboxCreationChecked(NewPHPTypePage.PHP_DOC_BLOCKS); + data.isStrictTypes = page.isCheckboxCreationChecked(NewPHPClassPage.STRICT_TYPES); List interfacesList = page.getInterfaces(); IType[] interfaces = new IType[interfacesList.size()]; interfacesList.toArray(interfaces); diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/NewPHPTypePage.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/NewPHPTypePage.java index b01115482d..5567445dfa 100644 --- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/NewPHPTypePage.java +++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/NewPHPTypePage.java @@ -84,6 +84,7 @@ public abstract class NewPHPTypePage extends BasicPHPWizardPage implements IDial public static final String PHP_DOC_BLOCKS = "PHPDoc Blocks";//$NON-NLS-1$ public static final String DESTRUCTOR = "Destructor";//$NON-NLS-1$ public static final String CONSTRUCTOR = "Constructor";//$NON-NLS-1$ + public static final String STRICT_TYPES = "strict_types";//$NON-NLS-1$ public static final String INHERITED_ABSTRACT_METHODS = "Inherited abstract methods";//$NON-NLS-1$ private static final Pattern PHP_IDENTIFIER_PATTERN = Pattern diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPClassTemplate.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPClassTemplate.java index 4fe0d5a145..ac60f9e03b 100644 --- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPClassTemplate.java +++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPClassTemplate.java @@ -42,6 +42,15 @@ public String getTemplatePath() { @Override public String processTemplate(NewPHPElementData data) { + + extract(INPUT, STRICT_TYPES_STRUCT, STRICT_TYPES_COMPILED); + set(STRICT_TYPES_STRUCT, ""); //$NON-NLS-1$ + // handle strict_types + if (data.isStrictTypes) { + set(STRICT_TYPES_VALUE, "1"); //$NON-NLS-1$ + compile(STRICT_TYPES_COMPILED, STRICT_TYPES_STRUCT, true); + } + // handle class default PHPDOC set(DEFAULT_PHPDOC_VAR, ""); //$NON-NLS-1$ @@ -532,15 +541,16 @@ public String getRequiredPHPs() { } /** - * returns true if the given functions array contains a function by the given - * name This method is used for avoiding multiple declaration of constructors - * and destructors. + * returns true if the given functions array contains a function by the + * given name This method is used for avoiding multiple declaration of + * constructors and destructors. * * @param funcs * the functions array to search in * @param name * the name of the function to look for - * @return true if there is a function by the name "name" in the array "funcs" + * @return true if there is a function by the name "name" in the array + * "funcs" */ private boolean containsFunction(IMethod[] funcs, String name) { for (int i = 0; i < funcs.length; i++) { diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPElementTemplate.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPElementTemplate.java index 513beddcee..c0f88d5520 100644 --- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPElementTemplate.java +++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPElementTemplate.java @@ -91,6 +91,9 @@ public abstract class PHPElementTemplate extends TextTemplate { public static final String USE_STRUCT_COMPILED = "use_compiled"; //$NON-NLS-1$ public static final String USE_IN_FILE_STRUCT = "use_in_file"; //$NON-NLS-1$ public static final String USE_IN_STRUCT_COMPILED = "use_in_file_compiled"; //$NON-NLS-1$ + public static final String STRICT_TYPES_STRUCT = "strict_types"; // $NON-NLS-1 + public static final String STRICT_TYPES_VALUE = "strict_types_value"; // $NON-NLS-1 + public static final String STRICT_TYPES_COMPILED = "strict_types_compiled"; //$NON-NLS-1$ public static final String[] EXCLUDE_PARAM_TYPES_LIST = new String[] { "boolean", "bool", "integer", "int", "float", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ "double", "string", "mixed", "void", "unknown_type", "object" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPInterfaceTemplate.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPInterfaceTemplate.java index 1826b4eea6..80a85479df 100644 --- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPInterfaceTemplate.java +++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPInterfaceTemplate.java @@ -35,6 +35,14 @@ public String processTemplate(NewPHPElementData data) { set(DEFAULT_PHPDOC_VAR, getDefaultPHPDoc()); } + extract(INPUT, STRICT_TYPES_STRUCT, STRICT_TYPES_COMPILED); + set(STRICT_TYPES_STRUCT, ""); //$NON-NLS-1$ + // handle strict_types + if (data.isStrictTypes) { + set(STRICT_TYPES_VALUE, "1"); //$NON-NLS-1$ + compile(STRICT_TYPES_COMPILED, STRICT_TYPES_STRUCT, true); + } + // handle interfaces declaration extract(INPUT, INTERFACES_STRUCT, INTERFACES_STRUCT_COMPILED); set(INTERFACES_STRUCT, ""); //$NON-NLS-1$ diff --git a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPTraitTemplate.java b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPTraitTemplate.java index 12d3693bd2..dcf1103dd9 100644 --- a/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPTraitTemplate.java +++ b/plugins/org.eclipse.php.ui/src/org/eclipse/php/internal/ui/wizards/types/PHPTraitTemplate.java @@ -35,6 +35,14 @@ public String processTemplate(NewPHPElementData data) { set(DEFAULT_PHPDOC_VAR, getDefaultPHPDoc()); } + extract(INPUT, STRICT_TYPES_STRUCT, STRICT_TYPES_COMPILED); + set(STRICT_TYPES_STRUCT, ""); //$NON-NLS-1$ + // handle strict_types + if (data.isStrictTypes) { + set(STRICT_TYPES_VALUE, "1"); //$NON-NLS-1$ + compile(STRICT_TYPES_COMPILED, STRICT_TYPES_STRUCT, true); + } + // handle interfaces declaration extract(INPUT, INTERFACES_STRUCT, INTERFACES_STRUCT_COMPILED); set(INTERFACES_STRUCT, ""); //$NON-NLS-1$