-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Symbol declared but not used for members read by variable name #3167
Comments
Oh, I see PHP 8.1 introduced the class MyClass {
public readonly string $readOnlyMember;
public readonly string $otherReadOnlyMember;
private string $totallyPrivateMember;
public function __construct() {
$this->readOnlyMember = 'a';
$this->otherReadOnlyMember = 'b';
$this->totallyPrivateMember = 'c';
}
public function privateData() : string {
return $this->totallyPrivateMember;
}
} |
You can suppress this on a case by case basis with an annotation above the statement |
Makes sense, thanks! |
My bad. The <?php
class MyClass {
/** @disregard P1003 */
private string $readOnlyMember; // Symbol '$readOnlyMember' is declared but not used
... It would great if you could add a new feature that suppresses P1003 for a member declaration when the class has a Sorry for the confusion. |
Alternatively, I could write the public function __get($name): mixed
{
$allowed = [
'readOnlyMember' => $this->readOnlyMember,
'otherReadOnlyMember' => $this->otherReadOnlyMember,
];
if (isset($allowed[$name])) {
return $allowed[$name];
}
throw new \Exception("Attempt to read private or unknown member $name");
} It's just as readable as in the first block above and there are no P1003 errors. Works for me. |
That looks like a good solution. I'll keep this ticket open to fix the |
Describe the bug
A "symbol is declared but not used" error/warning is given for class members that are accessed in the code by using a variable for the member name where the variable is from a hard-coded array. I often do this in
__get()
methods to make private members readable. So, I get lots of these errors/warnings. Normally, these are very useful for cleaning up code. It would be great if they could be suppressed in this situation.To Reproduce
Expected behavior
No error messages or warnings.
Screenshots
The error messages/warnings shown in the inline comments.
Platform and version
The text was updated successfully, but these errors were encountered: