Permanently prevent PHPStorm from removing Use statements when formatting code

How do I permanently and forever prevent PhpStorm from removing Use statements when formatting code? When I first start working on many files, I add a code snippet which contains the Use statements that I anticipate needing in that file. I then go about working on that file. Heaven help me if I forget and format the code to make it more readable. Later, when I try to test the code out in the browser, of course it fails because it cannot find the relevant code that was included in one or more of the Use statements. I am so sick of software that tries to substitute its judgment for mine. At this point, I am ready to uninstall PhpStorm and code everything in Notepad+++ if I can't find a way to get rid of this horrible behavior. If I decide that a Use statement is unnecessary, I and I alone will delete it.

8 comments

Can you provide an example of the code and how the use statement gets removed? 

0

I'm not sure what you're asking for because this is so simplistic. But I'll give it a go. First, I enter the following Use statements at the top of a MVC view file:

use dosamigos\ckeditor\CKEditor;
use kartik\helpers\Html;
use kartik\icons\Icon;
use kartik\tabs\TabsX;
use kartik\widgets\ActiveForm;
use yii\helpers\StringHelper;

Then, I make the mistake of formatting my code. what results is this:
use kartik\helpers\Html;

Virtually all the Use statements are deleted because I haven't entered the code that would be use of the classes specified by the Use statements.

For instance, if I have this code already in the file, the dosamigos Use statement will not be deleted:

<?= $form->field($model, 'description')->widget(CKEditor::className(), [
'options' => ['rows' => 6],
'preset' => 'full',
'clientOptions' => [
],
]); ?>


Thus, I am stuck with having to always remember to get everything into the file before I ever format the code — and before I can read the code that may not be well-formatted. (Keep in mind that I am often working with code created by Yii2 giiant or by lesser skilled programmers.)
 
0

By the way, this forum has among the worst text entry editors that I've ever seen and no way to preview what you're entering.

0

Honestly, I don't know if you can edit the settings for PHPStorm built-in code formatting tool. You could use things like https://packagist.org/packages/friendsofphp/php-cs-fixer where you can define your own rules for code formatting.

I would sincerely try a different approach to creating these files. You mention copying these statements from other files, which sounds like you're repeating yourself in the code. I usually "write" use statements when I am using a class I depend on just by ALT+Enter when the class name is selected and tell PhpStorm to import it.

 

0

How do I permanently and forever prevent PhpStorm from removing Use statements when formatting code?

Please uncheck Optimize imports at Code | Show Reformat File Dialog.

By the way, this forum has among the worst text entry editors that I've ever seen and no way to preview what you're entering.

That's an absolute shame. Moreover, we, JetBrains employees, suffer from that every single day. Unfortunately we rely on ZenDesk here, and can't do much ourselves.

The editor is WYSIWYG, but for some reason it has very few available editing options (even quoting is not there), so I ended up with using a generic online Markdown editor and copying the rendered result into the input field. Woo-hoo.

0

@Morozov, Thank you so much for responding from a base of actual knowledge. I can't stand getting responses from folks who start out by sayin, "I don't know" and go on to suggest that I use an additional tool to add another major layer of complexity and cumbersomeness to the whole thing. I guess they just feel a compelling need to display their incompetency and ignorance.

Are changes made in the Show Reformat File Dialog durable? That is, do they have to be set for each file or do they persist from session to session? I'll have to try it to be sure it will work for us.

As to code editing, I suppose it wouldn't do any good to suggest CKeditor. It's so much better than what you've got. I've been in situations like yours. I can empathize.

@Falnyr. Why are you posting something when you start out by saying that you don't know what your talking about. Precisely where did you see that I was copying anything from file to file, even though that makes sense to do from time to time. For your information, I am inserting code snippets using PhpStorm's Live Template functionality. Also, in our case, we need CKeditor in many different files. The easiest and most straightforward way to do that is via a use statement. If in your wisdom you know a way to incorporate CKeditor on a Web page without repeating any of the code for it on each page, please don't bother to write me; instead share your wisdom with the folks at CKsource who create CKeditor and sell the commercial product. Also be sure to share your insights with the folks at DosAmigos, a major development firm, who develop the code for including CKeditor in Yii2. Oh, and also share your insights with the folks at YiiFramework who create Yii2. Obviously, none of the three have your depth of understanding on how to implement the DRY approach. Among them, you should be able to make hundreds of thousands of dollars with all your great insight. As for me, I'll stick with the approach that the experts provide me.

0

@Larrytx Well, so it happened to me before that I was seeking an answer to the question and there were good people telling me that my approach was not exactly efficient, so I listened to these folks and learned. My mistake, I wasn't aware that I am dealing with a person who has the best opinion on all of the related subjects, but then again I am willing to take that risk, after all, PHP is an open source product and what shapes it is a community and people trying to help each other.

I was merely guessing that you, as a modern developer, are using CI pipeline to run tests and standard checks and tool such as PHP-CS-Fixer would come in handy to run in your CI containers as well as locally. Again, my mistake. 

Anyways, I am sure that all of these businesses that you mentioned are doing a marvelous job. It's just been 3 years since the version of PHP that powers Yii2 became unsupported. 

Don't worry, I won't bug you with any more messages, but please provide me with your expert opinion on the subject.

0

Are changes made in the Show Reformat File Dialog durable? That is, do they have to be set for each file or do they persist from session to session? I'll have to try it to be sure it will work for us.

They are IDE-wide, common for all file types and all projects.

0

Please sign in to leave a comment.