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 detection of class usage in double-quoted strings and heredoc #1661

Merged
merged 1 commit into from
Mar 9, 2024

Conversation

c01l
Copy link
Contributor

@c01l c01l commented Feb 6, 2024

I encountered a bug in Sniffs.Namespaces.UnusedUsesSniff that resulted in reporting too many "unused" classes, if a class instantiation occurred inside of a double quoted string or heredoc.

Quick example (HTML composition):

<?php

use SomeNamespace\MessageBox;

$_ = fn($x) => strval($x);
$html .= <<<HTML
<div>
   <div>...</div>
   {$_(new MessageBox("User did something wrong."))}
</div>
HTML
<?php

namespace SomeNamespace;

class MessageBox {
  public function __construct(private string $msg) {}
  public function __toString(): string {
    return "<div class='alert alert-danger'>$this->msg</div>";
  }
}

I added some tests for this case and added logic for detecting T_NEW T_STRING as class references.

@kukulich
Copy link
Contributor

kukulich commented Mar 9, 2024

Thanks.

@kukulich kukulich merged commit 74b296b into slevomat:master Mar 9, 2024
36 checks passed
@kukulich kukulich added the Bug label Mar 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants