Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect unused constants [GH-8244] #8250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -367,14 +367,15 @@ public void visit(ClassDeclaration cldec) {
}
addToPath(cldec);
typeInfo = new TypeDeclarationTypeInfo(cldec);
scan(cldec.getAttributes());
scan(cldec.getSuperClass());
scan(cldec.getInterfaces());
Identifier name = cldec.getName();
addColoringForNode(name, createTypeNameColoring(cldec));
needToScan.put(typeInfo, new ArrayList<>());
if (cldec.getBody() != null) {
cldec.getBody().accept(this);
// GH-8244 scan attributes after constant declarations are scanned
scan(cldec.getAttributes());

// find all usages in the method bodies
scanMethodBodies();
Expand Down Expand Up @@ -513,13 +514,14 @@ public void visit(ClassInstanceCreation node) {
// GH-5551 keep original type info to scan parent blocks
TypeInfo originalTypeInfo = typeInfo;
typeInfo = new ClassInstanceCreationTypeInfo(node);
scan(node.getAttributes());
scan(node.getSuperClass());
scan(node.getInterfaces());
needToScan.put(typeInfo, new ArrayList<>());
Block body = node.getBody();
if (body != null) {
body.accept(this);
// GH-8244 scan attributes after constant declarations are scanned
scan(node.getAttributes());

// find all usages in the method bodies
scanMethodBodies();
Expand Down Expand Up @@ -571,14 +573,15 @@ public void visit(EnumDeclaration node) {
return;
}
addToPath(node);
scan(node.getAttributes());
scan(node.getInterfaces());
typeInfo = new TypeDeclarationTypeInfo(node);
Identifier name = node.getName();
addColoringForNode(name, createTypeNameColoring(node));
needToScan.put(typeInfo, new ArrayList<>());
if (node.getBody() != null) {
node.getBody().accept(this);
// GH-8244 scan attributes after constant declarations are scanned
scan(node.getAttributes());
scanMethodBodies();
addColoringForUnusedPrivateConstants();
}
Expand Down
50 changes: 50 additions & 0 deletions php/php.editor/test/unit/data/testfiles/semantic/gh8244_01.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Test;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class SomeAttribute
{
public function __construct(string $name) {}
}

#[SomeAttribute(name: self::TEST_CLASS)]
class TestClass
{
private const string TEST_CLASS = 'test';
}

#[SomeAttribute(name: self::TEST_ENUM)]
enum TestEnum
{
private const string TEST_ENUM = 'test';
}

#[SomeAttribute(name: self::TEST_TRAIT)]
trait TestTrait
{
private const string TEST_TRAIT = 'test';
}

$anon = new #[SomeAttribute(name: self::TEST_ANON)] class() {
private const string TEST_ANON = 'test';
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Test;

use Attribute;

#[Attribute(Attribute::|>FIELD,STATIC:TARGET_CLASS<|)]
class |>CLASS:SomeAttribute<|
{
public function |>METHOD:__construct<|(string $name) {}
}

#[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_CLASS<|)]
class |>CLASS:TestClass<|
{
private const string |>FIELD,STATIC:TEST_CLASS<| = 'test';
}

#[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_ENUM<|)]
enum |>CLASS:TestEnum<|
{
private const string |>FIELD,STATIC:TEST_ENUM<| = 'test';
}

#[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_TRAIT<|)]
trait |>CLASS:TestTrait<|
{
private const string |>FIELD,STATIC:TEST_TRAIT<| = 'test';
}

$anon = new #[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_ANON<|)] class() {
private const string |>FIELD,STATIC:TEST_ANON<| = 'test';
};
50 changes: 50 additions & 0 deletions php/php.editor/test/unit/data/testfiles/semantic/gh8244_02.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Test;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class SomeAttribute
{
public function __construct(string $name) {}
}

#[SomeAttribute(name: TestClass::TEST_CLASS)]
class TestClass
{
private const string TEST_CLASS = 'test';
}

#[SomeAttribute(name: TestEnum::TEST_ENUM)]
enum TestEnum
{
private const string TEST_ENUM = 'test';
}

#[SomeAttribute(name: TestTrait::TEST_TRAIT)]
trait TestTrait
{
private const string TEST_TRAIT = 'test';
}

$anon = new #[SomeAttribute(name: self::TEST_ANON)] class() {
private const string TEST_ANON = 'test';
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Test;

use Attribute;

#[Attribute(Attribute::|>FIELD,STATIC:TARGET_CLASS<|)]
class |>CLASS:SomeAttribute<|
{
public function |>METHOD:__construct<|(string $name) {}
}

#[SomeAttribute(|>CUSTOM2:name: <|TestClass::|>FIELD,STATIC:TEST_CLASS<|)]
class |>CLASS:TestClass<|
{
private const string |>FIELD,STATIC:TEST_CLASS<| = 'test';
}

#[SomeAttribute(|>CUSTOM2:name: <|TestEnum::|>FIELD,STATIC:TEST_ENUM<|)]
enum |>CLASS:TestEnum<|
{
private const string |>FIELD,STATIC:TEST_ENUM<| = 'test';
}

#[SomeAttribute(|>CUSTOM2:name: <|TestTrait::|>FIELD,STATIC:TEST_TRAIT<|)]
trait |>CLASS:TestTrait<|
{
private const string |>FIELD,STATIC:TEST_TRAIT<| = 'test';
}

$anon = new #[SomeAttribute(|>CUSTOM2:name: <|self::|>FIELD,STATIC:TEST_ANON<|)] class() {
private const string |>FIELD,STATIC:TEST_ANON<| = 'test';
};
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,12 @@ public void testGH5551_02() throws Exception {
public void testDynamicClassConstantFetch_01() throws Exception {
checkSemantic("testfiles/semantic/dynamicClassConstantFetch_01.php");
}

public void testGH8244_01() throws Exception {
checkSemantic("testfiles/semantic/gh8244_01.php");
}

public void testGH8244_02() throws Exception {
checkSemantic("testfiles/semantic/gh8244_02.php");
}
}
Loading