Error "Cannot perform refactoring. Cannot modify library or SDK code" when trying to refactor JS class

I am developing a site with a team using WordPress.

Our main theme folder, where all the PHP code should go, is configured as a project root (say, `/path/to/wp/wp-content/themes/my-theme`).

Then there is a PHP include path to the WordPress root (`/path/to/wp`), so that you get all the nice features like autocomplete, quick navigation to WordPress source code and etc.

The reason it is done like so is to store PhpStorm project configuration (`.idea` folder) inside VCS (Git), so that all team members share the same config for code style, for example.

The problem is when I try to refactor some JavaScript class in a `.js` file, which is located inside the theme, I get a red popup saying "Cannot perform refactoring. Cannot modify library or SDK code", because PhpStorm thinks it is a library code:

If I remove the PHP include path to WordPress root, then refactoring works fine.

 

Steps to reproduce:

  1. Create a new folder for a test project somewhere on your machine (for example, `/var/www/test`).
  2. Create a subfolder inside a new folder (`/var/www/test/subfolder`)
  3. Open a subfolder with PhpStorm.
  4. Add a PHP include path of the root folder: in the Project view (Alt+1) right click on "External libraries" > "Configure PHP Include Paths" (F4), then click on a "+" and add a path to the root folder (`/var/www/test`)
  5. In the subfolder, create a new JS file (`index.js`) and add a class declaration inside it like so:
    class MyClass {
    }
  6. Place the cursor on the name of a newly created class and press F6 (or "Refactor" > "Move...")
  7. Receive an error message inside a red popup like on the screenshot above.

PhpStorm version: 2021.3.2

 

How can I fix this problem? Maybe we need another way to make PhpStorm aware of WordPress code?

0

Try to open whole WP installation folder as a project instead (/path/to/wp/ in your case). I'm afraid that's the only option.

0

Dmitry Tronin, in that case all the project configuration will not be available, as it is stored inside `.idea` folder in `/path/to/wp/wp-content/themes/my-theme`, and not inside WordPress root `/path/to/wp`. For the same reason modifications to the project-level settings will not be shared (unless I manually copy my changed settings inside VCS root and tell everybody to copy changes from VCS root inside WordPress root :/).

For this model to work properly we need to move our VCS root to the root folder of WordPress, which is considered a bad practice. It is somewhat a rule of thumb that when you develop a new site with WordPress you create a new theme, locate all your code there and make it a VCS root.

0

Should I create a bug report in PhpStorm tracker? Is it considered a bug?

0

This isn't considered a bug, sorry. 

I would suggest to add WP installation folders one-by-one, excluding the plugin directory, like this:

That way you would get all the required code completion features + won't face the originally reported problem.

0

Dmitry Tronin, thanks! Didn't thought of this workaround. Will try that and see how it works

1

Returning with an update. This workaround definitely works.

However, you have PHP files under the root WordPress directory which are still not visible for PhpStorm, as only WordPress child directories are added to include paths.

Also, sometimes PhpStorm adds the root WordPress directory to include paths by itself, so you have to modify this setting again from time to time.

The workaround you may also try is to move WordPress core into it's own subdirectory and have your app code in another directory. By the way, this is handled automatically by Bedrock. This product is designed to solve this kind of problems and most of well-known problems inherited from WordPress legacy in general. Haven't yet tried it in production, but it's definitely worth checking out.

Would be happy to hear what else can I do to solve the problem.

0

Perhaps it's time to redesign this part on our end. If have a spare minute or two - it would be great if you could submit this to our tracker at https://youtrack.jetbrains.com/newIssue describing the issues you've faced during this style of WP development. If you don't - I can do that for you a bit later.

1

> Also, sometimes PhpStorm adds the root WordPress directory to include paths by itself, so you have to modify this setting again from time to time.

Noticed that this behaviour is reproduced when using Composer from the command line (`composer require`, `composer remove`). Wonder if this happens every time when PhpStorm updates PHP include paths to add/remove/change some entries.

0

Yeah, this is controlled by a Settings (Preferences on macOS) | PHP | Composer : Add packages as libraries checkbox.

1

Thanks for the tip!

Adding `/vendor` directory to include paths and deleting all individual paths for libraries works. So I will have that checkbox turned off and be prepared next time for this sort of issue :)

1

请先登录再写评论。