Skip to content

Commit 0e27274

Browse files
committed
[GH 8334] PHP do not display override hint for constructors
The following leads to a compile error in PHP - so we should not display the Add Override hint for constructors. ```php <?php class newPHPClass { public function __construct() { } } class newPHPClass1 extends newPHPClass { #[\Override] public function __construct() { parent::__construct(); } } $anon = new class extends newPHPClass { #[\Override] public function __construct() { parent::__construct(); } }; ```
1 parent 882d1a7 commit 0e27274

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

php/php.editor/src/org/netbeans/modules/php/editor/verification/AddOverrideAttributeHint.java

+4
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ private void addAddOverrideHints(CheckVisitor checkVisitor, List<Hint> hints, Ba
159159
if (CancelSupport.getDefault().isCancelled()) {
160160
return;
161161
}
162+
if (CodeUtils.isConstructor(method)) {
163+
// Override on constructor results in compile Error (checked in PHP 8.4)
164+
return;
165+
}
162166
Identifier methodName = method.getFunction().getFunctionName();
163167
AddOverrideFix fix = new AddOverrideFix(document, method, ts);
164168
hints.add(new Hint(AddOverrideAttributeHint.this,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
class newPHPClass {
21+
22+
public function __construct() {
23+
24+
}
25+
}
26+
27+
class newPHPClass1 extends newPHPClass {
28+
29+
public function __construct() {
30+
parent::__construct();
31+
}
32+
}
33+
34+
$anon = new class extends newPHPClass {
35+
36+
public function __construct() {
37+
parent::__construct();
38+
}
39+
};

php/php.editor/test/unit/data/testfiles/verification/AddOverrideAttributeHint/testNoOverrideHintOnConstructor_01.php.testNoOverrideHintOnConstructor_01.hints

Whitespace-only changes.

php/php.editor/test/unit/src/org/netbeans/modules/php/editor/verification/AddOverrideAttributeHintTest.java

+4
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,10 @@ public void testRemoveOverrideAbstractClass_Fix01() throws Exception {
507507
applyHint("testRemoveOverrideAbstractClass_01.php", " #[\\Overr^ide] // test", "Remove \"#[\\Override]\" Attribute");
508508
}
509509

510+
public void testNoOverrideHintOnConstructor_01() throws Exception{
511+
checkHints("testNoOverrideHintOnConstructor_01.php");
512+
}
513+
510514
private void checkHints(String fileName, PhpVersion phpVersion) throws Exception {
511515
checkHints(new AddOverrideAttributeHintStub(phpVersion), fileName);
512516
}

0 commit comments

Comments
 (0)