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

Multi line use statements are fixed wrong by ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName #1581

Closed
klausi opened this issue Jun 9, 2023 · 6 comments
Labels

Comments

@klausi
Copy link

klausi commented Jun 9, 2023

Use statements can be grouped and span multiple lines, example:

<?php

/**
 * @file
 * Example.
 */

use Test\MultiLine as MultiLineAlias,
    Test\MultiLineSecond;

$a = new MultiLineAlias();
$b = new MultiLineSecond();

Reported error: Constant Test\MultiLineSecond should not be referenced via a fully qualified name, but via a use statement. (SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName)

That is misleading, no constant is defined here.

Furthermore the file is fixed wrong and results in broken code:

<?php

/**
 * @file
 * Example.
 */

$a = new MultiLineAlias();
$b = new \MultiLineSecond();

The use statement is completely deleted.

I saw that there was some discussion that Slevomat does not support multiline use statements in #1547 and #1554, but then the fixer should at least ignore those and not generate broken PHP files.

Background: trying to replace Drupal Coder's custom use statement sniff with your upstream code to avoid maintaining it in pfrenssen/coder#199

@klausi klausi changed the title Multi line use statemtns are fixed wrong by ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName Multi line use statements are fixed wrong by ReferenceUsedNamesOnly.ReferenceViaFullyQualifiedName Jun 9, 2023
@kukulich
Copy link
Contributor

Yes, we don't support group uses.

@jonathan1055
Copy link
Contributor

@klausi said

the fixer should at least ignore those and not generate broken PHP files.

Does the fixer still generate broken PHP?

@kukulich
Copy link
Contributor

Does the fixer still generate broken PHP?

Yes, because we don't have support for parsing group uses.

@jonathan1055
Copy link
Contributor

The fixer could be prevented from breaking the php by simply detecting if there is a group use, and then not doing any fixing, just report the error. I did this in #1555
I could investigate to see if this could be done here too

@kukulich
Copy link
Contributor

Yes, it could be solved this way.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants