Skip to content

Commit b9d00b8

Browse files
authored
Merge pull request #8421 from NReib/BOverrideOnConstructor
[GH 8420] PHP Constructor code completion should not add [#Override]
2 parents 6f2a1ee + ad3d9c6 commit b9d00b8

File tree

8 files changed

+289
-0
lines changed

8 files changed

+289
-0
lines changed

php/php.editor/src/org/netbeans/modules/php/editor/codegen/SinglePropertyMethodCreator.java

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public String create(MethodProperty property) {
7979
}
8080

8181
private String getOverrideAttribute(MethodElement method) {
82+
if (method.isConstructor()) {
83+
return CodeUtils.EMPTY_STRING;
84+
}
8285
if (!method.isMagic()
8386
&& (!method.getType().isTrait() || ElementUtils.isAbstractTraitMethod(method))
8487
&& cgsInfo.getPhpVersion().hasOverrideAttribute()) {

php/php.editor/src/org/netbeans/modules/php/editor/completion/PHPCompletionItem.java

+3
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,9 @@ public String getCustomInsertTemplate() {
14191419

14201420
private String getOverrideAttribute() {
14211421
MethodElement method = (MethodElement) getBaseFunctionElement();
1422+
if (method.isConstructor()) {
1423+
return CodeUtils.EMPTY_STRING;
1424+
}
14221425
TypeElement type = method.getType();
14231426
if (!isMagic()
14241427
&& (!type.isTrait() || ElementUtils.isAbstractTraitMethod(method))

php/php.editor/test/unit/data/testfiles/codegen/testInstanceOverrideMethod/testInstanceOverrideMethod.php

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?php
22

33
class Foo {
4+
5+
function __construct() {
6+
7+
}
8+
49
function myFoo(): Foo;
510
}
611

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
public function __construct(){
2+
parent::__construct();
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
class base {
23+
24+
public function __construct() {
25+
}
26+
27+
public function __call(string $name, array $arguments): mixed {
28+
}
29+
30+
public static function __callStatic(string $name, array $arguments): mixed {
31+
}
32+
33+
public function __clone(): void {
34+
}
35+
36+
public function __debugInfo(): array {
37+
}
38+
39+
public function __destruct() {
40+
}
41+
42+
public function __get(string $name): mixed {
43+
}
44+
45+
public function __invoke(): mixed {
46+
}
47+
48+
public function __isset(string $name): bool {
49+
}
50+
51+
public function __serialize(): array {
52+
}
53+
54+
public function __set(string $name, mixed $value): void {
55+
}
56+
57+
public static function __set_state(array $properties): object {
58+
}
59+
60+
public function __sleep(): array {
61+
}
62+
63+
public function __toString(): string {
64+
return "Test";
65+
}
66+
67+
public function __unserialize(array $data): void {
68+
}
69+
70+
public function __unset(string $name): void {
71+
}
72+
73+
public function __wakeup(): void {
74+
}
75+
}
76+
77+
class TestOverride extends base {
78+
__
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
Name: __call
2+
#[\Override]
3+
public function __call(string $name, array $arguments): mixed {
4+
${cursor}return parent::__call($name, $arguments);
5+
}
6+
7+
Name: __call
8+
public function __call(string $name, array $arguments): mixed {
9+
${cursor};
10+
}
11+
12+
Name: __callStatic
13+
#[\Override]
14+
public static function __callStatic(string $name, array $arguments): mixed {
15+
${cursor}return parent::__callStatic($name, $arguments);
16+
}
17+
18+
Name: __callStatic
19+
public static function __callStatic(string $name, array $arguments): mixed {
20+
${cursor};
21+
}
22+
23+
Name: __clone
24+
#[\Override]
25+
public function __clone(): void {
26+
${cursor};
27+
}
28+
29+
Name: __clone
30+
public function __clone(): void {
31+
${cursor};
32+
}
33+
34+
Name: __construct
35+
public function __construct() {
36+
${cursor}parent::__construct();
37+
}
38+
39+
Name: __construct
40+
public function __construct() {
41+
${cursor};
42+
}
43+
44+
Name: __debugInfo
45+
#[\Override]
46+
public function __debugInfo(): array {
47+
${cursor}return parent::__debugInfo();
48+
}
49+
50+
Name: __debugInfo
51+
public function __debugInfo(): array {
52+
${cursor};
53+
}
54+
55+
Name: __destruct
56+
#[\Override]
57+
public function __destruct() {
58+
${cursor}parent::__destruct();
59+
}
60+
61+
Name: __destruct
62+
public function __destruct() {
63+
${cursor};
64+
}
65+
66+
Name: __get
67+
#[\Override]
68+
public function __get(string $name): mixed {
69+
${cursor}return parent::__get($name);
70+
}
71+
72+
Name: __get
73+
public function __get(string $name): mixed {
74+
${cursor};
75+
}
76+
77+
Name: __invoke
78+
#[\Override]
79+
public function __invoke(): mixed {
80+
${cursor}return parent::__invoke();
81+
}
82+
83+
Name: __invoke
84+
public function __invoke(): mixed {
85+
${cursor};
86+
}
87+
88+
Name: __isset
89+
#[\Override]
90+
public function __isset(string $name): bool {
91+
${cursor}return parent::__isset($name);
92+
}
93+
94+
Name: __isset
95+
public function __isset(string $name): bool {
96+
${cursor};
97+
}
98+
99+
Name: __serialize
100+
#[\Override]
101+
public function __serialize(): array {
102+
${cursor}return parent::__serialize();
103+
}
104+
105+
Name: __serialize
106+
public function __serialize(): array {
107+
${cursor};
108+
}
109+
110+
Name: __set
111+
#[\Override]
112+
public function __set(string $name, mixed $value): void {
113+
${cursor};
114+
}
115+
116+
Name: __set
117+
public function __set(string $name, mixed $value): void {
118+
${cursor};
119+
}
120+
121+
Name: __set_state
122+
#[\Override]
123+
public static function __set_state(array $properties): object {
124+
${cursor}return parent::__set_state($properties);
125+
}
126+
127+
Name: __set_state
128+
public static function __set_state(array $properties): object {
129+
${cursor};
130+
}
131+
132+
Name: __sleep
133+
#[\Override]
134+
public function __sleep(): array {
135+
${cursor}return parent::__sleep();
136+
}
137+
138+
Name: __sleep
139+
public function __sleep(): array {
140+
${cursor};
141+
}
142+
143+
Name: __toString
144+
#[\Override]
145+
public function __toString(): string {
146+
return "base[]";${cursor}
147+
}
148+
149+
Name: __toString
150+
public function __toString(): string {
151+
return "TestOverride[]";${cursor}
152+
}
153+
154+
Name: __unserialize
155+
#[\Override]
156+
public function __unserialize(array $data): void {
157+
${cursor};
158+
}
159+
160+
Name: __unserialize
161+
public function __unserialize(array $data): void {
162+
${cursor};
163+
}
164+
165+
Name: __unset
166+
#[\Override]
167+
public function __unset(string $name): void {
168+
${cursor};
169+
}
170+
171+
Name: __unset
172+
public function __unset(string $name): void {
173+
${cursor};
174+
}
175+
176+
Name: __wakeup
177+
#[\Override]
178+
public function __wakeup(): void {
179+
${cursor};
180+
}
181+
182+
Name: __wakeup
183+
public function __wakeup(): void {
184+
${cursor};
185+
}

php/php.editor/test/unit/src/org/netbeans/modules/php/editor/codegen/SelectedPropertyMethodsCreatorTest.java

+6
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ public void testInstanceOverrideMethod_02() throws Exception {
197197
selectProperties(cgsInfo.getPossibleMethods(), "myFoo"), new SinglePropertyMethodCreator.InheritedMethodCreator(cgsInfo)));
198198
}
199199

200+
public void testInstanceOverrideMethod_03() throws Exception {
201+
CGSInfo cgsInfo = getCgsInfo("class Bar extends Foo {^", PhpVersion.PHP_83);
202+
checkResult(new SelectedPropertyMethodsCreator().create(
203+
selectProperties(cgsInfo.getPossibleMethods(), "__construct"), new SinglePropertyMethodCreator.InheritedMethodCreator(cgsInfo)));
204+
}
205+
200206
// #270237
201207
public void testInstanceOverrideMethodWithNullableType_01() throws Exception {
202208
CGSInfo cgsInfo = getCgsInfo("class Bar extends Foo {^", PhpVersion.PHP_71);

php/php.editor/test/unit/src/org/netbeans/modules/php/editor/completion/PHPCodeCompletionMagicMethodTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ public void testToStringCustomTemplate_02_PHP83() throws Exception {
9494
new DefaultFilter(PhpVersion.PHP_83, "__toString"), true);
9595
}
9696

97+
public void testMagicMethodOverride_01_PHP83() throws Exception {
98+
checkCompletionCustomTemplateResult(getTestPath("Override"), " __^",
99+
new DefaultFilter(PhpVersion.PHP_83, "__"), true);
100+
}
101+
97102
@Override
98103
protected Map<String, ClassPath> createClassPathsForTest() {
99104
return Collections.singletonMap(

0 commit comments

Comments
 (0)