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: Match short namespace symbols #91

Merged

Conversation

eliashaeussler
Copy link
Contributor

Problem

When using short namespaces instead of full namespaces, the symbol should properly match in order to detect usages of a required package.

Example

A package with PSR-4 namespace Cocur\Slugify\ is required. The project code base contains the following class:

<?php

use Cocur\Slugify;

final class MyBeautifulClass
{
    public function __construct(
        private readonly Slugify\Slugify $slugify,
    ) {
    }
}

In this example, the namespace symbol would not match, because the registered PSR-4 namespace contains a trailing backslash whereas the imported namespace in the class does not.

Solution

To resolve incorrect behavior in such cases, a trailing backslash is now always appended to the constructed namespace symbols.

All Submissions:

New Feature Submissions:

  1. Does your submission pass tests?
  2. Have you lint your code locally prior to submission?

Changes to Core Features:

  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your core changes, as applicable?
  • Have you successfully ran tests with your changes locally?

When using short namespaces instead of full namespaces, the symbol
should properly match in order to detect usages of a required package.

Example:

    A package with PSR-4 namespace "Cocur\Slugify\" is required. The
    project code base contains the following class:

    <?php

    use Cocur\Slugify;

    final class MyBeautifulClass
    {
        public function __construct(
            private readonly Slugify\Slugify $slugify,
        ) {
        }
    }

In this example, the namespace symbol would not match, because the
registered PSR-4 namespace contains a trailing backslash whereas
the imported namespace in the class does not.

To resolve incorrect behavior in such cases, a trailing backslash is
now always appended to the constructed namespace symbols.
@icanhazstring
Copy link
Member

Makes total sense. Thanks for the addition :)

@icanhazstring icanhazstring merged commit 7d95ce3 into composer-unused:main Mar 16, 2023
@icanhazstring
Copy link
Member

Tagged with 0.1.13. Will be in next composer-unused release :)

@eliashaeussler eliashaeussler deleted the fix/short-namespaces branch March 16, 2023 09:54
@eliashaeussler
Copy link
Contributor Author

Thanks for the quick merge @icanhazstring!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants