Skip to content

Commit

Permalink
Fix #263 - add strict_types to php class/interface/trait wizard
Browse files Browse the repository at this point in the history
  • Loading branch information
zulus committed Dec 15, 2023
1 parent ed44b9b commit b7b151d
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
/*strict_types:*/declare(strict_types=#strict_types_value#);
/*:strict_types*/
/*namespace:*/namespace #namespace_name#;
/*:namespace*/
/*php_content:*/
Expand All @@ -21,7 +23,7 @@
/*functions:*/
/*func_phpdoc:*/
/**
/*func_phpdoc_params:*/*#func_phpdoc_param#
/*func_phpdoc_params:*/*#func_phpdoc_param#
/*:func_phpdoc_params*/ #func_phpdoc_see#*//*:func_phpdoc*/
#func_modifier# #func_static_modifier# function #func_name#(/*func_params:*/#func_param_name#/*:func_params*/) {
#func_parent_call#
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
/*strict_types:*/declare(strict_types=#strict_types_value#);
/*:strict_types*/
/*namespace:*/namespace #namespace_name#;
/*:namespace*/
/*php_content:*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class NewPHPClassPage extends NewPHPTypePage {
private Button browseSuperBtn;
private static final String[] CLASS_CHECKBOXES_PHP5 = new String[] { REQUIRE_ONCE, CONSTRUCTOR, PHP_DOC_BLOCKS,
null, DESTRUCTOR, TODOS, null, INHERITED_ABSTRACT_METHODS };

private static final String[] CLASS_CHECKBOXES_PHP7 = new String[] { REQUIRE_ONCE, STRICT_TYPES, CONSTRUCTOR,
PHP_DOC_BLOCKS, null, DESTRUCTOR, TODOS, null, INHERITED_ABSTRACT_METHODS };
public static final String[] CLASS_MODIFIERS = new String[] { "none", //$NON-NLS-1$
"final", "abstract" }; //$NON-NLS-1$ //$NON-NLS-2$

Expand Down Expand Up @@ -136,7 +139,11 @@ private void createElementSection(final Composite composite) {
createTraitsControls(elementSection);
}

addCheckboxesCreation(elementSection, CLASS_CHECKBOXES_PHP5);
if (phpVersion == null || phpVersion.isGreaterThan(PHPVersion.PHP5_4)) {
addCheckboxesCreation(elementSection, CLASS_CHECKBOXES_PHP7);
} else {
addCheckboxesCreation(elementSection, CLASS_CHECKBOXES_PHP5);
}
}

// add the UI section that handles the Superclass selection
Expand Down Expand Up @@ -296,7 +303,8 @@ public String getSuperclassName() {
}

/**
* Creates a UI component withing this Wizard page for selecting the Superclass
* Creates a UI component withing this Wizard page for selecting the
* Superclass
*
* @return the selected super class
*/
Expand Down Expand Up @@ -340,7 +348,8 @@ public ITypeInfoFilterExtension getFilterExtension() {
}

/**
* Returns true/false - whether the requested modifer's check box is selected
* Returns true/false - whether the requested modifer's check box is
* selected
*
* @param checkBoxIndex
* - the index of the checkbox
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ private NewPHPElementData populatePHPElementData() {
data.isGenerateConstructor = page.isCheckboxCreationChecked(NewPHPClassPage.CONSTRUCTOR);

data.isGenerateDestructor = page.isCheckboxCreationChecked(NewPHPClassPage.DESTRUCTOR);
data.isStrictTypes = page.isCheckboxCreationChecked(NewPHPClassPage.STRICT_TYPES);
data.isFinal = page.isCreateModifierChecked(1);
data.isAbstract = page.isCreateModifierChecked(2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class NewPHPElementData {
public String[] requiredToAdd = new String[0];
public boolean isGenerateConstructor = false;
public boolean isGenerateDestructor = false;
public boolean isStrictTypes = false;
public boolean isGeneratePHPDoc = false;
public boolean isGenerateTODOs = false;
public boolean isInFirstBlock = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.core.runtime.IStatus;
import org.eclipse.dltk.core.DLTKCore;
import org.eclipse.dltk.core.IScriptProject;
import org.eclipse.php.core.PHPVersion;
import org.eclipse.php.internal.ui.util.StatusInfo;
import org.eclipse.php.internal.ui.util.StatusUtil;
import org.eclipse.swt.SWT;
Expand All @@ -32,6 +33,7 @@
public class NewPHPInterfacePage extends NewPHPTypePage {

private static final String[] INTERFACE__CHECKBOXES = new String[] { REQUIRE_ONCE, PHP_DOC_BLOCKS };
private static final String[] INTERFACE__CHECKBOXES7 = new String[] { REQUIRE_ONCE, STRICT_TYPES, PHP_DOC_BLOCKS };

public NewPHPInterfacePage() {
super(Messages.NewPHPInterfacePage_0);
Expand Down Expand Up @@ -75,7 +77,11 @@ private void createElementSection(Composite container) {
addNamespaceText(elementSection);

createSuperInterfacesControls(elementSection);
addCheckboxesCreation(elementSection, INTERFACE__CHECKBOXES);
if (phpVersion == null || phpVersion.isGreaterThan(PHPVersion.PHP5_4)) {
addCheckboxesCreation(elementSection, INTERFACE__CHECKBOXES7);
} else {
addCheckboxesCreation(elementSection, INTERFACE__CHECKBOXES);
}
}

@Override
Expand Down Expand Up @@ -106,8 +112,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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private NewPHPElementData populatePHPElementData() {
NewPHPInterfacePage page = (NewPHPInterfacePage) this.page;
NewPHPElementData data = new NewPHPElementData();
data.isGeneratePHPDoc = page.isCheckboxCreationChecked(NewPHPTypePage.PHP_DOC_BLOCKS);
data.isStrictTypes = page.isCheckboxCreationChecked(NewPHPClassPage.STRICT_TYPES);
List<IType> interfacesList = page.getInterfaces();
IType[] interfaces = new IType[interfacesList.size()];
interfacesList.toArray(interfaces);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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$

Expand Down Expand Up @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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$
Expand Down

0 comments on commit b7b151d

Please sign in to comment.