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

Support target directories other than the first one matching a namespace #1399

Open
janopae opened this issue Nov 22, 2023 · 1 comment
Open

Comments

@janopae
Copy link

janopae commented Nov 22, 2023

The Symfony Maker bundle decides the path where it creates files by transforming namespaces (given by the individual makers) to file paths, given the composer autloading configuration. This transformation is not always possible unabigiously.

The code that transforms namespaces to directories currently resides here.

In cases where a namespace can't be resolved to a unique path, it just picks the "first" possible path (whatever this means, as JSON object keys, such as the "autoload" key inside the composer.json, are by definition unordered).

This means that it is not possible to generate files in directories outside this "first match" autoload path.

For example

If you configure your test cases to live in the same namespaces as the classes they test (useful to test classes that are @psalm-internal, and generally to reduce use statements), but in a separate directory (useful for tools like the composer-require-checker), you might have an autoload configuration like the following:

    "autoload": {
        "psr-4": {
            "": "src/"
        },
    },
    "autoload-dev": {
        "psr-4": {
            "": "tests/"
        }
    },

In this case, it is not possible to create files in the tests directory using MakerBundle.

@janopae
Copy link
Author

janopae commented Nov 22, 2023

I could think of the following ways to solve this:

Interactive selection

PHPStorm provides an interactive selection when moving a class to a namespace and the path is ambigious. A maker command can't spawn a window, but it could use an interactive CLI:

Path for /App/AcmeFactory:
  [0] src/App/AcmeFactory.php
  [1] tests/App/AcmeFactory.php
>

It seems like this needs to be explicitly supported by the make commands – either by calling a function, which gets an Output passed and checks the namespace for ambiguosity and does the prompt in case, or by calling that function when handling an exception that AutoloaderUtil would throw, or by passing the Output down to the FileManager/AutoloaderUtil.

Provide a way to hard-overwrite the path

Calling a maker command with --target-directory=tests/App/Acme could ignore the namespace and hardly create the file in tests/App/Acme(maybe throw in error if this doesn't make sense given the namespace).

This would be much harder to use, but easier to implement, as we wouldn't need to pass an Output to the AutoloaderUtil or a similar function.

However, it still needs explicit support by the make commands, as they need to catch that parameter and add its content to the ClassNameDetails.

janopae referenced this issue in zenstruck/foundry Nov 22, 2023
Co-authored-by: Wouter J <wouterj@users.noreply.github.com>
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

No branches or pull requests

1 participant