You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Show error when class name does not match the containing file name #609
Sometimes I rename a class but forget to rename the file. The editor does not warn me about it, but it results in a runtime error, since the class now cannot be autoloaded correctly. PhpStorm has such warning and I miss it after switching to VS Code.
Edit: The error I've described above is for classname–filename discrepancy. Directory–namespace discrepancy should also be validated. In such cases, PhpStorm shows a „namespace doesn't match the PSR-0/PSR-4 project structure” error.
Would also be nice if there were automatic fixes for that: “rename class”/“rename file” and “change namespace to match folder structure”/“move file to correct directory”.
How would you suggest handling classes, that are not supposed to be autoloader?
Note: other IDEs just report that.
For example, the user may have a helper file included manually (I know the file should be listed in "autoload""files", but it isn't in this case). Is it something people may find disrupting when we report that?
After some time with v1.49.15843, I find the new inspection is super useful. I often create a new class by copying a similar file and editing it, so I've already used the fix many times.
I'd suggest that it should be an error (red squiggle instead of blue one). In most cases, it is a serious issue, and I don't think false positives would be as common as you think. In PhpStorm this is an error, and I don't remember seeing any false positives, even when working with legacy projects.
I also find quick fix names a bit confusing:
First action doesn't say what it really does, and the second one has “File” with the uppercase first letter, making me think that it's the class name. I'd suggest renaming them to “Rename class 'Foo' to 'Bar'” and „Rename file 'Bar' to 'Foo'”, or if you want to make it shorter, “Rename class to 'Bar'” and „Rename file 'Foo'”. This is how it looks in PhpStorm and I find it much easier to understand:
I'd also love to see a corresponding inspection for namespaces.
How would you suggest handling classes, that are not supposed to be autoloader?
For example, the user may have a helper file included manually (I know the file should be listed in "autoload""files", but it isn't in this case). Is it something people may find disrupting when we report that?
If you explicitly stated in composer.json that the directory should follow PSR-4, you probably want to see a warning when it doesn't. Composer itself will also yell at you when you run composer dump-autoload -o.
When working with legacy code, it's unavoidable that there will be some false positives, not only with this inspection, but with other ones too. I'd be OK with such tradeoff. It's something that you probably should fix, and it's an easy fix — in most cases you can add the file to autoload.files or move it to a directory that isn't following PSR-4.
Activity
jakubmisek commentedon Jul 29, 2024
Good idea;
Any hint wow do we know, the project is PSR0/PSR4 ?
jrmajor commentedon Jul 29, 2024
You could check
autoloadfield incomposer.json.jakubmisek commentedon Jul 29, 2024
Perfect, makes sense.
jakubmisek commentedon Aug 3, 2024
How would you suggest handling classes, that are not supposed to be autoloader?
Note: other IDEs just report that.
For example, the user may have a helper file included manually (I know the file should be listed in "autoload""files", but it isn't in this case). Is it something people may find disrupting when we report that?
Example:
composer.json:{ "autoload": { "psr-4": { "Vendor\\Package\\": "src/" } } }src/helpers/includes.php:Now, we will report a warning, that class "Helper" should be either renamed to "includes" or the path should be changed.
jrmajor commentedon Aug 10, 2024
After some time with
v1.49.15843, I find the new inspection is super useful. I often create a new class by copying a similar file and editing it, so I've already used the fix many times.I'd suggest that it should be an error (red squiggle instead of blue one). In most cases, it is a serious issue, and I don't think false positives would be as common as you think. In PhpStorm this is an error, and I don't remember seeing any false positives, even when working with legacy projects.
I also find quick fix names a bit confusing:


First action doesn't say what it really does, and the second one has “File” with the uppercase first letter, making me think that it's the class name. I'd suggest renaming them to “Rename class 'Foo' to 'Bar'” and „Rename file 'Bar' to 'Foo'”, or if you want to make it shorter, “Rename class to 'Bar'” and „Rename file 'Foo'”. This is how it looks in PhpStorm and I find it much easier to understand:
I'd also love to see a corresponding inspection for namespaces.
If you explicitly stated in
composer.jsonthat the directory should follow PSR-4, you probably want to see a warning when it doesn't. Composer itself will also yell at you when you runcomposer dump-autoload -o.When working with legacy code, it's unavoidable that there will be some false positives, not only with this inspection, but with other ones too. I'd be OK with such tradeoff. It's something that you probably should fix, and it's an easy fix — in most cases you can add the file to
autoload.filesor move it to a directory that isn't following PSR-4.jakubmisek commentedon Aug 11, 2024
Good point! Code actions title will be adjusted, error severity as well.
We'll taker a look on the namespace quick fix.