How To Fix Your Use Statements?
By Ondrej Brejla on Apr 18, 2012
Hi all! Today we would like to introduce you our new feature for NetBeans 7.2 which is called Fix Uses....
Have you ever used Java support in NetBeans and its Fix Imports... action? What does it do. It simply goes through your code and tries to resolve missing import statements. And as you know, Java imports are "something like" PHP use statements. So what that our new Fix Uses... action do? Exactly what you mean. It goes through your code and tries to resolve missing use statements.
Note: We need to say, that this implemetation is really first and it doesn't cover all your use cases. But it does its work (which will be described later) and, hopefully, does it correctly.
So lets describe the work, what this new action does:
How to invoke Fix Uses... action?
It's quite simple. Just click inside a namespace, where you want to resolve missing use statements and press Ctrl + Shift + I, go to Source -> Fix Uses..., or right click in a namespace and select Fix Uses....
Note: If you use namespace declarations without brackets, don't invoke Fix Uses... action at the end of a file on a whitespace! It will not work properly, because these whitespaces doesn't belong to a proper namespace. It's a lexer/parser bug. So when you use this action, invoke it somewhere inside a namespace.
How to select proper types?
When you successfully invoke Fix Uses... action, two things may happen.
- Resolving window occurs, or
- nothing - no resolving window occurs.
When resolving window occurs, than you can see all possible classes (types in general) which you can resolve somehow. It means that you can select a proper type from a combo box which will be used for a new use statement.
When nothing happens, then NetBeans thinks that all types are used properly and nothing has to be done.
There is just one think in a resolving window. There is a check box which allows you to remove all unused statements which occur after confirming a resolving window.
If we find some type, which can't be resolved (no type for this unqualified name exists, or you have some typo in PHPDoc), then it's mentioned in resolving window.
What happens after confirmation of a resolving window?
After confirmation, all resolved (selected) types are inserted into a use statement declaration area which is located right after the namespace declaration and all uses of these types in a code are replaced by their unqualified types. If you wanted to Remove Unused Uses, than they are removed.
What happens when a conflict of type names occurs?
When a conflict occurs, we try to create a use statement with an alias for every conflicting type (except the first one). No other possibilities are available in this first implementation, but we would like to implement some into the next version (e.g. to prefer unqualified names instead of aliases, etc.).
Are there any possibilities, how to customize the default behavior?
Yes. Not too much, but there are three of them. You can find them in Tools -> Options -> Editor -> Formatting -> PHP: Uses
- Prefer Fully Qualified Names over Use of Unqualified Names - if it's checked, then it means, that if you have a fully-qualified name used somewhere in a code, it will not be resolved by default (i.e. combo box in a resolving window will have a default value set to "Don't use type").
- Prefer Multiple Use Statements Combined - if it's checked, it means, that all resolved types will be inserted as a one use statement with a combined types.
- Start Use Statements with a Namespace Separator - if it's checked, then all your resolved types will be prepended by a namespace separator.