New Old Hints

Hi all! It's several weeks when we last introduced you some pure PHP improvements, isn't it? But now, it's here again :-) Today we would like to write something about old well-known PHP hints, their new implementation and new improvements. Here they are: Order of Arguments, Uninitialized Variables and Accidental Assignments.

Order of Arguments

This hint was renamed. Its old name was Argument with a default value should not be preceded by argument without a default value. It's really strange (and long :-) name, isn't it? So we renamed it to simpler version - Order of Arguments. It checks the order of arguments in your function/method declaration.

Optional arguments should be on the right side and mandatory arguments on the left. This works equally as its previous version, but it has a new feature now - you can apply hint fix on arguments and reorder them automatically! Just press alt + enter on the line with declaration and select the right hint fix.

When you apply the hint fix, everything will be ok :-)

Uninitialized Variables

The next one is a hint which checks an initialization of your variables, because it's a good practise to initialize every variable before its first use. This hint has been renamed too. Its old name was Variable might have not been initialized. Now it's called simply - Uninitialized Variables.

Fixing this state is really simple, just add an initialization of that variable before its use and the hint warning will disappear.

Accidental Assignments

The last one is Accidental Assignments hint. It was also renamed, its old name was Possible accidental assignment, assignments in conditions should be avoided. It checks assignments in conditional statements such as if, while, etc. Mostly you don't want to use assignments there, but comparisons (equal ==, or identical ===).

If assignment is detected in a conditional statement, you can apply hint fix to repair it! It's another difference between old version and the new one. And you can choose equal fix (==), or identical fix (===), of course.

And when you apply it, it will be fixed :-)

There is just one UI connected with this hint, you can apply some settings on it.

You can enable/disable checking assignments in sub-statements (e.g. if ($a === ($b = 25)) {}) and you can enable/disable checking assignments in while statements, because there are some use cases when assignment in a while statement makes sense.

Do you miss some useful hints? Please, describe them and write it to us! Here as a comment, or into our bugzilla as a new enhancement for Editor component!

And that's all for today and as usual, please test it and if you find something strange, don't hesitate to file a new issue (product php, component Editor). Thanks.

Comments:

Overall I see what you did there and agree with most of it. You've made it more explicit there is an error rather than the possibility of an error. You've also added condition checking for when it's okay to do condition assignment.

A concern I have with the first one, though, is that you've exchanged length of name with a learning curve. It doesn't at all specify what's wrong with the order of the arguments.

One question I have about the new Order of Operation auto-fix is does it invoke the rewrite function? Otherwise it will be changing the function definition without changing the function callers.

Posted by Zimzat on October 24, 2011 at 12:30 PM CEST #

Some nice new additions there. Thank you very much!

I especially like the last one. In a lot of my code I have statements like this:

if ( $cookie = $this->ipsclass->my_getcookie( 'acpeditorprefs' ) ){
//blah
}

And Netbeans used to always flag this as a possible error, hopefully now when I get to use this new update, that will be a thing of the past.

Thanks for the blog, it's much appreciated, please keep it coming!

Posted by pilotsnipes on October 24, 2011 at 06:07 PM CEST #

I actually watched the task in the Bugtracker (even though the tracker has been down for new submissions several times last week) and have been looking forward to see that implemented.

Well done! The coming version of Netbeans is getting better every day and you guys are having a big part in this. Really appreciate your work!

Posted by Tom on October 24, 2011 at 07:05 PM CEST #

Great news. My fovourite IDE is getting better and better

Posted by Alex Shinkevich on October 25, 2011 at 01:41 AM CEST #

Zimzat: Wrong args hint just reorders arguments in function/method declaration...not in invocations. It should be used when you create method/function, NOT when you have it and want to refactor it...it's not a refactoring. It's just for preventing wrong code during its first implemetation.

Posted by Ondrej Brejla on October 25, 2011 at 04:58 AM CEST #

Regarding the Accidental Assignments hint: it might be an idea to suggest changing the order in the statement to prevent accidental assignments.
By example

if ($foo == 'bar') // What I ment to do
if ($foo = 'bar') // Accidental assigment

could be rewritten to:

if ('bar' == $foo) // What I ment to do
if ('bar' = $foo) // Syntax error

Posted by Maghiel Dijksman on October 25, 2011 at 05:06 PM CEST #

@Maghiel Dijksman:

I _hate_ that syntax. It feels so unnatural, and it only works if you're comparing a variable to a static value (nothing can save you from comparing two variables). It would be really annoying if my IDE started telling me what to do.

Posted by Zimzat on October 25, 2011 at 05:35 PM CEST #

@Maghiel Dijksman: You can file a new enhancement issue[1] for editor, thanks for your interrest!

[1] http://netbeans.org/bugzilla/enter_bug.cgi?product=php

Posted by Ondrej Brejla on October 26, 2011 at 05:15 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

This blogs is written by NetBeans developers who contribute to the PHP support mainly.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today