Skip to content

Commit 6f2a1ee

Browse files
authored
Merge pull request #8250 from junichi11/gh-8244-incorrect-unused-constants
Fix incorrect unused constants [GH-8244]
2 parents 82a46a5 + 45332c9 commit 6f2a1ee

File tree

6 files changed

+214
-3
lines changed

6 files changed

+214
-3
lines changed

php/php.editor/src/org/netbeans/modules/php/editor/csl/SemanticAnalysis.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,15 @@ public void visit(ClassDeclaration cldec) {
367367
}
368368
addToPath(cldec);
369369
typeInfo = new TypeDeclarationTypeInfo(cldec);
370-
scan(cldec.getAttributes());
371370
scan(cldec.getSuperClass());
372371
scan(cldec.getInterfaces());
373372
Identifier name = cldec.getName();
374373
addColoringForNode(name, createTypeNameColoring(cldec));
375374
needToScan.put(typeInfo, new ArrayList<>());
376375
if (cldec.getBody() != null) {
377376
cldec.getBody().accept(this);
377+
// GH-8244 scan attributes after constant declarations are scanned
378+
scan(cldec.getAttributes());
378379

379380
// find all usages in the method bodies
380381
scanMethodBodies();
@@ -513,13 +514,14 @@ public void visit(ClassInstanceCreation node) {
513514
// GH-5551 keep original type info to scan parent blocks
514515
TypeInfo originalTypeInfo = typeInfo;
515516
typeInfo = new ClassInstanceCreationTypeInfo(node);
516-
scan(node.getAttributes());
517517
scan(node.getSuperClass());
518518
scan(node.getInterfaces());
519519
needToScan.put(typeInfo, new ArrayList<>());
520520
Block body = node.getBody();
521521
if (body != null) {
522522
body.accept(this);
523+
// GH-8244 scan attributes after constant declarations are scanned
524+
scan(node.getAttributes());
523525

524526
// find all usages in the method bodies
525527
scanMethodBodies();
@@ -571,14 +573,15 @@ public void visit(EnumDeclaration node) {
571573
return;
572574
}
573575
addToPath(node);
574-
scan(node.getAttributes());
575576
scan(node.getInterfaces());
576577
typeInfo = new TypeDeclarationTypeInfo(node);
577578
Identifier name = node.getName();
578579
addColoringForNode(name, createTypeNameColoring(node));
579580
needToScan.put(typeInfo, new ArrayList<>());
580581
if (node.getBody() != null) {
581582
node.getBody().accept(this);
583+
// GH-8244 scan attributes after constant declarations are scanned
584+
scan(node.getAttributes());
582585
scanMethodBodies();
583586
addColoringForUnusedPrivateConstants();
584587
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
namespace Test;
21+
22+
use Attribute;
23+
24+
#[Attribute(Attribute::TARGET_CLASS)]
25+
class SomeAttribute
26+
{
27+
public function __construct(string $name) {}
28+
}
29+
30+
#[SomeAttribute(name: self::TEST_CLASS)]
31+
class TestClass
32+
{
33+
private const string TEST_CLASS = 'test';
34+
}
35+
36+
#[SomeAttribute(name: self::TEST_ENUM)]
37+
enum TestEnum
38+
{
39+
private const string TEST_ENUM = 'test';
40+
}
41+
42+
#[SomeAttribute(name: self::TEST_TRAIT)]
43+
trait TestTrait
44+
{
45+
private const string TEST_TRAIT = 'test';
46+
}
47+
48+
$anon = new #[SomeAttribute(name: self::TEST_ANON)] class() {
49+
private const string TEST_ANON = 'test';
50+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
namespace Test;
21+
22+
use Attribute;
23+
24+
#[Attribute(Attribute::|>FIELD,STATIC:TARGET_CLASS<|)]
25+
class |>CLASS:SomeAttribute<|
26+
{
27+
public function |>METHOD:__construct<|(string $name) {}
28+
}
29+
30+
#[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_CLASS<|)]
31+
class |>CLASS:TestClass<|
32+
{
33+
private const string |>FIELD,STATIC:TEST_CLASS<| = 'test';
34+
}
35+
36+
#[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_ENUM<|)]
37+
enum |>CLASS:TestEnum<|
38+
{
39+
private const string |>FIELD,STATIC:TEST_ENUM<| = 'test';
40+
}
41+
42+
#[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_TRAIT<|)]
43+
trait |>CLASS:TestTrait<|
44+
{
45+
private const string |>FIELD,STATIC:TEST_TRAIT<| = 'test';
46+
}
47+
48+
$anon = new #[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_ANON<|)] class() {
49+
private const string |>FIELD,STATIC:TEST_ANON<| = 'test';
50+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
namespace Test;
21+
22+
use Attribute;
23+
24+
#[Attribute(Attribute::TARGET_CLASS)]
25+
class SomeAttribute
26+
{
27+
public function __construct(string $name) {}
28+
}
29+
30+
#[SomeAttribute(name: TestClass::TEST_CLASS)]
31+
class TestClass
32+
{
33+
private const string TEST_CLASS = 'test';
34+
}
35+
36+
#[SomeAttribute(name: TestEnum::TEST_ENUM)]
37+
enum TestEnum
38+
{
39+
private const string TEST_ENUM = 'test';
40+
}
41+
42+
#[SomeAttribute(name: TestTrait::TEST_TRAIT)]
43+
trait TestTrait
44+
{
45+
private const string TEST_TRAIT = 'test';
46+
}
47+
48+
$anon = new #[SomeAttribute(name: self::TEST_ANON)] class() {
49+
private const string TEST_ANON = 'test';
50+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
namespace Test;
21+
22+
use Attribute;
23+
24+
#[Attribute(Attribute::|>FIELD,STATIC:TARGET_CLASS<|)]
25+
class |>CLASS:SomeAttribute<|
26+
{
27+
public function |>METHOD:__construct<|(string $name) {}
28+
}
29+
30+
#[SomeAttribute(|>CUSTOM2:name: <|TestClass::|>FIELD,STATIC:TEST_CLASS<|)]
31+
class |>CLASS:TestClass<|
32+
{
33+
private const string |>FIELD,STATIC:TEST_CLASS<| = 'test';
34+
}
35+
36+
#[SomeAttribute(|>CUSTOM2:name: <|TestEnum::|>FIELD,STATIC:TEST_ENUM<|)]
37+
enum |>CLASS:TestEnum<|
38+
{
39+
private const string |>FIELD,STATIC:TEST_ENUM<| = 'test';
40+
}
41+
42+
#[SomeAttribute(|>CUSTOM2:name: <|TestTrait::|>FIELD,STATIC:TEST_TRAIT<|)]
43+
trait |>CLASS:TestTrait<|
44+
{
45+
private const string |>FIELD,STATIC:TEST_TRAIT<| = 'test';
46+
}
47+
48+
$anon = new #[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_ANON<|)] class() {
49+
private const string |>FIELD,STATIC:TEST_ANON<| = 'test';
50+
};

php/php.editor/test/unit/src/org/netbeans/modules/php/editor/csl/SemanticAnalyzerTest.java

+8
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,12 @@ public void testGH5551_02() throws Exception {
233233
public void testDynamicClassConstantFetch_01() throws Exception {
234234
checkSemantic("testfiles/semantic/dynamicClassConstantFetch_01.php");
235235
}
236+
237+
public void testGH8244_01() throws Exception {
238+
checkSemantic("testfiles/semantic/gh8244_01.php");
239+
}
240+
241+
public void testGH8244_02() throws Exception {
242+
checkSemantic("testfiles/semantic/gh8244_02.php");
243+
}
236244
}

0 commit comments

Comments
 (0)