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

Do not absolutize path when path starts with wildcard #64

Merged
merged 1 commit into from
Mar 3, 2021

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Mar 3, 2021

I'm using allowIn a lot to exclude certain directories from being disallowed.

I noticed that this library absolutizes all inputs making them relative to the current working directory.

That means that if i write:

allowIn:
  - tests

it becomes /my/working/directory/tests. And it also means that if I write

allowIn:
  - **/tests

it becomes /my/working/directory/**/tests.

I think it should not be done when the path starts with a wildcard.

PHPStorm has integration for PHPStan and I noticed that all my allowed paths were ignored by PHPStan when running in PHPStorm. Turns out, PHPStorm doesn't run PHPStan on the actual file, but instead copies the file to a temporary directory and then performs analysis on it.

So when you have tests/MyTest.php it will copy it to /private/var/folders/m4/2_8czq_s1dbcfkm1yf8zjgg80000gn/T/PHPStantemp_folder7840/tests/MyTest.php and then run it.

Because this library makes the allowed paths absolute to the working directory, the allowed path will never match that with the path from the temporary directory.

Current work around that I have is to use the following config:

allowIn:
  - tests/*
  - /private/var/**/PHPStantemp_folder*/tests/*

But by applying this change, I can write it like this:

allowIn:
  - **/tests/*

I checked how PHPStan deals with this. It does not absolutize paths when doing things like this:

parameters:
    ignoreErrors:
        -
            message: '#Dynamic call to static method PHPUnit\\.*#'
            paths:
                - **/tests/*

That means that the above example works fine, also in PHPStorm.

I also reported this to PHPStorm but I'm not sure if this will be fixed.
See https://youtrack.jetbrains.com/issue/WI-58890.

@spaze
Copy link
Owner

spaze commented Mar 3, 2021

Thanks. Interesting, PHPStan indeed does have an exception for wildcards added in phpstan/phpstan-src@beaf375 so I guess it makes sense to do what PHPStan does.

@ruudk
Copy link
Contributor Author

ruudk commented Mar 3, 2021

Great that you found where this is done, I searched the codebase but couldn't find it. :)

src/FileHelper.php Outdated Show resolved Hide resolved
src/FileHelper.php Outdated Show resolved Hide resolved
tests/FileHelperTest.php Show resolved Hide resolved
@ruudk ruudk force-pushed the wildcard branch 2 times, most recently from 31851cd to 1b90165 Compare March 3, 2021 10:12
@ruudk
Copy link
Contributor Author

ruudk commented Mar 3, 2021

@spaze Applied feedback ✅

Copy link
Owner

@spaze spaze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small detail and hopefully the last one.

tests/ClassConstantInvalidUsagesTest.php Outdated Show resolved Hide resolved
I'm using `allowIn` a lot to exclude certain directories from being disallowed.

I noticed that this library absolutizes all inputs making them relative to the current working directory.

That means that if i write:
```neon
allowIn:
  - tests
```
it becomes `/my/working/directory/tests`. And it also means that if I write
```neon
allowIn:
  - **/tests
```
it becomes `/my/working/directory/**/tests`.

I think it should not be done when the path starts with a wildcard.

PHPStorm has integration for PHPStan and I noticed that all my allowed paths were ignored by PHPStan when running in PHPStorm. Turns out, PHPStorm doesn't run PHPStan on the actual file, but instead copies the file to a temporary directory and then performs analysis on it.

So when you have `tests/MyTest.php` it will copy it to `/private/var/folders/m4/2_8czq_s1dbcfkm1yf8zjgg80000gn/T/PHPStantemp_folder7840/tests/MyTest.php` and then run it.

Because this library makes the allowed paths absolute to the working directory, the allowed path will never match that with the path from the temporary directory.

Current work around that I have is to use the following config:
```neon
allowIn:
  - tests/*
  - /private/var/**/PHPStantemp_folder*/tests/*
```

But by applying this change, I can write it like this:
```neon
allowIn:
  - **/tests/*
```

I checked how PHPStan deals with this. It does not absolutize paths when doing things like this:
```neon
parameters:
    ignoreErrors:
        -
            message: '#Dynamic call to static method PHPUnit\\.*#'
            paths:
                - **/tests/*
```

That means that the above example works fine, also in PHPStorm.

I also reported this to PHPStorm but I'm not sure if this will be fixed.
See https://youtrack.jetbrains.com/issue/WI-58890.
@spaze spaze merged commit 146482f into spaze:master Mar 3, 2021
@ruudk ruudk deleted the wildcard branch March 3, 2021 10:43
@spaze
Copy link
Owner

spaze commented Mar 3, 2021

Thanks, just released as v1.4.0.

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