An Oracle blog about NetBeans for PHP

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.

Join the discussion

Comments ( 8 )
  • Zimzat Monday, October 24, 2011

    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.

  • pilotsnipes Monday, October 24, 2011

    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' ) ){



    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!

  • Tom Monday, October 24, 2011

    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!

  • Alex Shinkevich Monday, October 24, 2011

    Great news. My fovourite IDE is getting better and better

  • Ondrej Brejla Tuesday, October 25, 2011

    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.

  • Maghiel Dijksman Tuesday, October 25, 2011

    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

  • Zimzat Tuesday, October 25, 2011

    @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.

  • Ondrej Brejla Wednesday, October 26, 2011

    @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

Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.