From 627351e7e2566cc8b39deb3f3ba1333ca65d5174 Mon Sep 17 00:00:00 2001 From: Damiano Improta Date: Sun, 14 Jun 2026 22:10:55 +0200 Subject: [PATCH] Return type for fluent setters changed to for PHP >= 8.0 add logic fluent setter and test cases fix typo comment --- .../netbeans/modules/php/api/PhpVersion.java | 22 +++++ .../codegen/SinglePropertyMethodCreator.java | 6 ++ .../editor/completion/PHPCodeCompletion.java | 5 +- ...tTypedPropertiesSetter_PHP70Fluent.codegen | 36 ++++---- ...tTypedPropertiesSetter_PHP71Fluent.codegen | 36 ++++---- ...tTypedPropertiesSetter_PHP74Fluent.codegen | 90 +++++++++++++++++++ ...hp.testTypedPropertiesSetter_PHP80.codegen | 72 +++++++++++++++ ...tTypedPropertiesSetter_PHP80Fluent.codegen | 90 +++++++++++++++++++ .../SelectedPropertyMethodsCreatorTest.java | 20 +++++ 9 files changed, 340 insertions(+), 37 deletions(-) mode change 100644 => 100755 php/php.api.phpmodule/src/org/netbeans/modules/php/api/PhpVersion.java create mode 100644 php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP74Fluent.codegen create mode 100644 php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP80.codegen create mode 100644 php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP80Fluent.codegen diff --git a/php/php.api.phpmodule/src/org/netbeans/modules/php/api/PhpVersion.java b/php/php.api.phpmodule/src/org/netbeans/modules/php/api/PhpVersion.java old mode 100644 new mode 100755 index 7212c96ab5fa..0e8221793614 --- a/php/php.api.phpmodule/src/org/netbeans/modules/php/api/PhpVersion.java +++ b/php/php.api.phpmodule/src/org/netbeans/modules/php/api/PhpVersion.java @@ -245,6 +245,28 @@ public boolean hasMixedType() { return this.compareTo(PhpVersion.PHP_80) >= 0; } + /** + * Check whether this version supports the static return type. (as of PHP 8.0) + * + * @return {@code true} if this version supports static return type, + * {@code false} otherwise + * @since 2.107 + */ + public boolean hasStaticReturnType() { + return this.compareTo(PhpVersion.PHP_80) >= 0; + } + + /** + * Check whether this version supports the self return type. (as of PHP 7.0) + * + * @return {@code true} if this version supports self return type, + * {@code false} otherwise + * @since 2.107 + */ + public boolean hasSelfReturnType() { + return this.compareTo(PhpVersion.PHP_70) >= 0; + } + /** * Check whether this version supports the never type. * diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/codegen/SinglePropertyMethodCreator.java b/php/php.editor/src/org/netbeans/modules/php/editor/codegen/SinglePropertyMethodCreator.java index f43105519146..917bc3094923 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/codegen/SinglePropertyMethodCreator.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/codegen/SinglePropertyMethodCreator.java @@ -191,6 +191,12 @@ public String create(Property property) { } private String getReturnType() { + if (cgsInfo.isFluentSetter() && cgsInfo.getPhpVersion().hasStaticReturnType()) { + return String.format(": %s ", Type.STATIC); // NOI18N + } + if (cgsInfo.isFluentSetter() && cgsInfo.getPhpVersion().hasSelfReturnType()) { + return String.format(": %s ", Type.SELF); // NOI18N + } if (!cgsInfo.isFluentSetter() && cgsInfo.getPhpVersion().hasVoidReturnType()) { return String.format(": %s ", Type.VOID); // NOI18N } diff --git a/php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletion.java b/php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletion.java index df5233fcdcd1..91badc7ad550 100644 --- a/php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletion.java +++ b/php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletion.java @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -2331,7 +2332,9 @@ private void autoCompleteExpression(final PHPCompletionResult completionResult, Model model = request.result.getModel(); Set aliasedNames = ModelUtils.getAliasedNames(model, request.anchor); - for (final PhpElement element : request.index.getTopLevelElements(prefix, aliasedNames, Trait.ALIAS)) { + List topLevelElements = new ArrayList<>(request.index.getTopLevelElements(prefix, aliasedNames, Trait.ALIAS)); + topLevelElements.sort(Comparator.comparing(PhpElement::getName, String.CASE_INSENSITIVE_ORDER)); + for (final PhpElement element : topLevelElements) { if (CancelSupport.getDefault().isCancelled()) { return; } diff --git a/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP70Fluent.codegen b/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP70Fluent.codegen index 21f2abb76f3b..55d2709d8546 100644 --- a/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP70Fluent.codegen +++ b/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP70Fluent.codegen @@ -1,89 +1,89 @@ -public function setInstanceArray(array $$instanceArray) { +public function setInstanceArray(array $$instanceArray): self { $$this->instanceArray = $instanceArray; return $$this; } -public function setInstanceInt(int $$instanceInt) { +public function setInstanceInt(int $$instanceInt): self { $$this->instanceInt = $instanceInt; return $$this; } -public function setInstanceString(string $$instanceString) { +public function setInstanceString(string $$instanceString): self { $$this->instanceString = $instanceString; return $$this; } -public function setInstanceBool(bool $$instanceBool) { +public function setInstanceBool(bool $$instanceBool): self { $$this->instanceBool = $instanceBool; return $$this; } -public function setInstanceNull($$instanceNull) { +public function setInstanceNull($$instanceNull): self { $$this->instanceNull = $instanceNull; return $$this; } -public function setInstanceNullable(string $$instanceNullable) { +public function setInstanceNullable(string $$instanceNullable): self { $$this->instanceNullable = $instanceNullable; return $$this; } -public function setInstanceArrayNullable(array $$instanceArrayNullable) { +public function setInstanceArrayNullable(array $$instanceArrayNullable): self { $$this->instanceArrayNullable = $instanceArrayNullable; return $$this; } -public function setInstanceClass(Test53 $$instanceClass) { +public function setInstanceClass(Test53 $$instanceClass): self { $$this->instanceClass = $instanceClass; return $$this; } -public static function setStaticInt(int $$staticInt) { +public static function setStaticInt(int $$staticInt): self { self::$$staticInt = $staticInt; return self; } -public static function setStaticNullable(int $$staticNullable) { +public static function setStaticNullable(int $$staticNullable): self { self::$$staticNullable = $staticNullable; return self; } -public function setInstanceInt74($$instanceInt74) { +public function setInstanceInt74($$instanceInt74): self { $$this->instanceInt74 = $instanceInt74; return $$this; } -public function setInstanceString74($$instanceString74) { +public function setInstanceString74($$instanceString74): self { $$this->instanceString74 = $instanceString74; return $$this; } -public function setInstanceBool74($$instanceBool74) { +public function setInstanceBool74($$instanceBool74): self { $$this->instanceBool74 = $instanceBool74; return $$this; } -public function setInstanceNullable74($$instanceNullable74) { +public function setInstanceNullable74($$instanceNullable74): self { $$this->instanceNullable74 = $instanceNullable74; return $$this; } -public static function setStaticInt74($$staticInt74) { +public static function setStaticInt74($$staticInt74): self { self::$$staticInt74 = $staticInt74; return self; } -public static function setStaticString74($$staticString74) { +public static function setStaticString74($$staticString74): self { self::$$staticString74 = $staticString74; return self; } -public static function setStaticBool74($$staticBool74) { +public static function setStaticBool74($$staticBool74): self { self::$$staticBool74 = $staticBool74; return self; } -public static function setStaticNullable74($$staticNullable74) { +public static function setStaticNullable74($$staticNullable74): self { self::$$staticNullable74 = $staticNullable74; return self; } diff --git a/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP71Fluent.codegen b/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP71Fluent.codegen index 9584b85b14e2..a54a37d5c858 100644 --- a/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP71Fluent.codegen +++ b/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP71Fluent.codegen @@ -1,89 +1,89 @@ -public function setInstanceArray(array $$instanceArray) { +public function setInstanceArray(array $$instanceArray): self { $$this->instanceArray = $instanceArray; return $$this; } -public function setInstanceInt(int $$instanceInt) { +public function setInstanceInt(int $$instanceInt): self { $$this->instanceInt = $instanceInt; return $$this; } -public function setInstanceString(string $$instanceString) { +public function setInstanceString(string $$instanceString): self { $$this->instanceString = $instanceString; return $$this; } -public function setInstanceBool(bool $$instanceBool) { +public function setInstanceBool(bool $$instanceBool): self { $$this->instanceBool = $instanceBool; return $$this; } -public function setInstanceNull($$instanceNull) { +public function setInstanceNull($$instanceNull): self { $$this->instanceNull = $instanceNull; return $$this; } -public function setInstanceNullable(?string $$instanceNullable) { +public function setInstanceNullable(?string $$instanceNullable): self { $$this->instanceNullable = $instanceNullable; return $$this; } -public function setInstanceArrayNullable(?array $$instanceArrayNullable) { +public function setInstanceArrayNullable(?array $$instanceArrayNullable): self { $$this->instanceArrayNullable = $instanceArrayNullable; return $$this; } -public function setInstanceClass(Test53 $$instanceClass) { +public function setInstanceClass(Test53 $$instanceClass): self { $$this->instanceClass = $instanceClass; return $$this; } -public static function setStaticInt(int $$staticInt) { +public static function setStaticInt(int $$staticInt): self { self::$$staticInt = $staticInt; return self; } -public static function setStaticNullable(?int $$staticNullable) { +public static function setStaticNullable(?int $$staticNullable): self { self::$$staticNullable = $staticNullable; return self; } -public function setInstanceInt74($$instanceInt74) { +public function setInstanceInt74($$instanceInt74): self { $$this->instanceInt74 = $instanceInt74; return $$this; } -public function setInstanceString74($$instanceString74) { +public function setInstanceString74($$instanceString74): self { $$this->instanceString74 = $instanceString74; return $$this; } -public function setInstanceBool74($$instanceBool74) { +public function setInstanceBool74($$instanceBool74): self { $$this->instanceBool74 = $instanceBool74; return $$this; } -public function setInstanceNullable74($$instanceNullable74) { +public function setInstanceNullable74($$instanceNullable74): self { $$this->instanceNullable74 = $instanceNullable74; return $$this; } -public static function setStaticInt74($$staticInt74) { +public static function setStaticInt74($$staticInt74): self { self::$$staticInt74 = $staticInt74; return self; } -public static function setStaticString74($$staticString74) { +public static function setStaticString74($$staticString74): self { self::$$staticString74 = $staticString74; return self; } -public static function setStaticBool74($$staticBool74) { +public static function setStaticBool74($$staticBool74): self { self::$$staticBool74 = $staticBool74; return self; } -public static function setStaticNullable74($$staticNullable74) { +public static function setStaticNullable74($$staticNullable74): self { self::$$staticNullable74 = $staticNullable74; return self; } diff --git a/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP74Fluent.codegen b/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP74Fluent.codegen new file mode 100644 index 000000000000..8c4ae215a226 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP74Fluent.codegen @@ -0,0 +1,90 @@ +public function setInstanceArray(array $$instanceArray): self { +$$this->instanceArray = $instanceArray; +return $$this; +} + +public function setInstanceInt(int $$instanceInt): self { +$$this->instanceInt = $instanceInt; +return $$this; +} + +public function setInstanceString(string $$instanceString): self { +$$this->instanceString = $instanceString; +return $$this; +} + +public function setInstanceBool(bool $$instanceBool): self { +$$this->instanceBool = $instanceBool; +return $$this; +} + +public function setInstanceNull($$instanceNull): self { +$$this->instanceNull = $instanceNull; +return $$this; +} + +public function setInstanceNullable(?string $$instanceNullable): self { +$$this->instanceNullable = $instanceNullable; +return $$this; +} + +public function setInstanceArrayNullable(?array $$instanceArrayNullable): self { +$$this->instanceArrayNullable = $instanceArrayNullable; +return $$this; +} + +public function setInstanceClass(Test53 $$instanceClass): self { +$$this->instanceClass = $instanceClass; +return $$this; +} + +public static function setStaticInt(int $$staticInt): self { +self::$$staticInt = $staticInt; +return self; +} + +public static function setStaticNullable(?int $$staticNullable): self { +self::$$staticNullable = $staticNullable; +return self; +} + +public function setInstanceInt74(int $$instanceInt74): self { +$$this->instanceInt74 = $instanceInt74; +return $$this; +} + +public function setInstanceString74(string $$instanceString74): self { +$$this->instanceString74 = $instanceString74; +return $$this; +} + +public function setInstanceBool74(bool $$instanceBool74): self { +$$this->instanceBool74 = $instanceBool74; +return $$this; +} + +public function setInstanceNullable74(?string $$instanceNullable74): self { +$$this->instanceNullable74 = $instanceNullable74; +return $$this; +} + +public static function setStaticInt74(int $$staticInt74): self { +self::$$staticInt74 = $staticInt74; +return self; +} + +public static function setStaticString74(string $$staticString74): self { +self::$$staticString74 = $staticString74; +return self; +} + +public static function setStaticBool74(bool $$staticBool74): self { +self::$$staticBool74 = $staticBool74; +return self; +} + +public static function setStaticNullable74(?string $$staticNullable74): self { +self::$$staticNullable74 = $staticNullable74; +return self; +} + diff --git a/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP80.codegen b/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP80.codegen new file mode 100644 index 000000000000..e71ac09abf92 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP80.codegen @@ -0,0 +1,72 @@ +public function setInstanceArray(array $$instanceArray): void { +$$this->instanceArray = $instanceArray; +} + +public function setInstanceInt(int $$instanceInt): void { +$$this->instanceInt = $instanceInt; +} + +public function setInstanceString(string $$instanceString): void { +$$this->instanceString = $instanceString; +} + +public function setInstanceBool(bool $$instanceBool): void { +$$this->instanceBool = $instanceBool; +} + +public function setInstanceNull($$instanceNull): void { +$$this->instanceNull = $instanceNull; +} + +public function setInstanceNullable(?string $$instanceNullable): void { +$$this->instanceNullable = $instanceNullable; +} + +public function setInstanceArrayNullable(?array $$instanceArrayNullable): void { +$$this->instanceArrayNullable = $instanceArrayNullable; +} + +public function setInstanceClass(Test53 $$instanceClass): void { +$$this->instanceClass = $instanceClass; +} + +public static function setStaticInt(int $$staticInt): void { +self::$$staticInt = $staticInt; +} + +public static function setStaticNullable(?int $$staticNullable): void { +self::$$staticNullable = $staticNullable; +} + +public function setInstanceInt74(int $$instanceInt74): void { +$$this->instanceInt74 = $instanceInt74; +} + +public function setInstanceString74(string $$instanceString74): void { +$$this->instanceString74 = $instanceString74; +} + +public function setInstanceBool74(bool $$instanceBool74): void { +$$this->instanceBool74 = $instanceBool74; +} + +public function setInstanceNullable74(?string $$instanceNullable74): void { +$$this->instanceNullable74 = $instanceNullable74; +} + +public static function setStaticInt74(int $$staticInt74): void { +self::$$staticInt74 = $staticInt74; +} + +public static function setStaticString74(string $$staticString74): void { +self::$$staticString74 = $staticString74; +} + +public static function setStaticBool74(bool $$staticBool74): void { +self::$$staticBool74 = $staticBool74; +} + +public static function setStaticNullable74(?string $$staticNullable74): void { +self::$$staticNullable74 = $staticNullable74; +} + diff --git a/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP80Fluent.codegen b/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP80Fluent.codegen new file mode 100644 index 000000000000..bc8f3a8f6260 --- /dev/null +++ b/php/php.editor/test/unit/data/testfiles/codegen/testTypedPropertiesSetter/testTypedPropertiesSetter.php.testTypedPropertiesSetter_PHP80Fluent.codegen @@ -0,0 +1,90 @@ +public function setInstanceArray(array $$instanceArray): static { +$$this->instanceArray = $instanceArray; +return $$this; +} + +public function setInstanceInt(int $$instanceInt): static { +$$this->instanceInt = $instanceInt; +return $$this; +} + +public function setInstanceString(string $$instanceString): static { +$$this->instanceString = $instanceString; +return $$this; +} + +public function setInstanceBool(bool $$instanceBool): static { +$$this->instanceBool = $instanceBool; +return $$this; +} + +public function setInstanceNull($$instanceNull): static { +$$this->instanceNull = $instanceNull; +return $$this; +} + +public function setInstanceNullable(?string $$instanceNullable): static { +$$this->instanceNullable = $instanceNullable; +return $$this; +} + +public function setInstanceArrayNullable(?array $$instanceArrayNullable): static { +$$this->instanceArrayNullable = $instanceArrayNullable; +return $$this; +} + +public function setInstanceClass(Test53 $$instanceClass): static { +$$this->instanceClass = $instanceClass; +return $$this; +} + +public static function setStaticInt(int $$staticInt): static { +self::$$staticInt = $staticInt; +return self; +} + +public static function setStaticNullable(?int $$staticNullable): static { +self::$$staticNullable = $staticNullable; +return self; +} + +public function setInstanceInt74(int $$instanceInt74): static { +$$this->instanceInt74 = $instanceInt74; +return $$this; +} + +public function setInstanceString74(string $$instanceString74): static { +$$this->instanceString74 = $instanceString74; +return $$this; +} + +public function setInstanceBool74(bool $$instanceBool74): static { +$$this->instanceBool74 = $instanceBool74; +return $$this; +} + +public function setInstanceNullable74(?string $$instanceNullable74): static { +$$this->instanceNullable74 = $instanceNullable74; +return $$this; +} + +public static function setStaticInt74(int $$staticInt74): static { +self::$$staticInt74 = $staticInt74; +return self; +} + +public static function setStaticString74(string $$staticString74): static { +self::$$staticString74 = $staticString74; +return self; +} + +public static function setStaticBool74(bool $$staticBool74): static { +self::$$staticBool74 = $staticBool74; +return self; +} + +public static function setStaticNullable74(?string $$staticNullable74): static { +self::$$staticNullable74 = $staticNullable74; +return self; +} + diff --git a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/codegen/SelectedPropertyMethodsCreatorTest.java b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/codegen/SelectedPropertyMethodsCreatorTest.java index f09f4265e653..50eabddd580a 100644 --- a/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/codegen/SelectedPropertyMethodsCreatorTest.java +++ b/php/php.editor/test/unit/src/org/netbeans/modules/php/editor/codegen/SelectedPropertyMethodsCreatorTest.java @@ -358,6 +358,26 @@ public void testTypedPropertiesSetter_PHP74() throws Exception { checkResult(new SelectedPropertyMethodsCreator().create(selectAllProperties(cgsInfo.getPossibleSetters()), new SinglePropertyMethodCreator.SingleSetterCreator(cgsInfo))); } + public void testTypedPropertiesSetter_PHP74Fluent() throws Exception { + CGSInfo cgsInfo = getCgsInfo("^}", PhpVersion.PHP_74); + cgsInfo.setPublicModifier(true); + cgsInfo.setFluentSetter(true); + checkResult(new SelectedPropertyMethodsCreator().create(selectAllProperties(cgsInfo.getPossibleSetters()), new SinglePropertyMethodCreator.SingleSetterCreator(cgsInfo))); + } + + public void testTypedPropertiesSetter_PHP80() throws Exception { + CGSInfo cgsInfo = getCgsInfo("^}", PhpVersion.PHP_80); + cgsInfo.setPublicModifier(true); + checkResult(new SelectedPropertyMethodsCreator().create(selectAllProperties(cgsInfo.getPossibleSetters()), new SinglePropertyMethodCreator.SingleSetterCreator(cgsInfo))); + } + + public void testTypedPropertiesSetter_PHP80Fluent() throws Exception { + CGSInfo cgsInfo = getCgsInfo("^}", PhpVersion.PHP_80); + cgsInfo.setPublicModifier(true); + cgsInfo.setFluentSetter(true); + checkResult(new SelectedPropertyMethodsCreator().create(selectAllProperties(cgsInfo.getPossibleSetters()), new SinglePropertyMethodCreator.SingleSetterCreator(cgsInfo))); + } + // constructor public void testTypedPropertiesConstructor_PHP56() throws Exception { CGSInfo cgsInfo = getCgsInfo("^}", PhpVersion.PHP_56);