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?

评论操作 固定链接

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

请先登录再写评论。