Improve Your Code With New Hints

Hi all! After few weeks of inactivity we are here again and we want to show you some new hints which will try to improve your code in NetBeans 7.4 (at least we hope so ;-). So here they are:

Too Many Lines

So what do you think that it does? Yes, exactly :-) It checks the number of lines of class, interface, trait and method (function) declarations. If the number is exceeded, than IDE warns you. So your elements will be more readable and pretty thin :-)

Do you think that the default value is too strict? Don't worry, you can simply adjust it ;-)

Superglobals

Everyone knows superglobals, but not everyone knows that superglobal arrays shouldn't be accessed directly. Never ever trust to user inputs. Every user input should be somehow filtered/validated. And that's why this hint was created :-)

Braces

As you certainly know, one can use conditional statements and control structures without curly braces (if block doesn't contain more than one statement). But it makes code less readable, so we suggest you to use braces everytime. This hint works for Do-While, While, For and Foreach loops and for If-Else-ElseIf statements.

Empty Statement

Empty statement is just an empty statement so why to pollute your code by stuff which does nothing?

Error Control Operator Misused

Error control operator is pure evil (we talk about an "at" sign). It hides much more errors than you think. So don't use it. But we allow to use it in some special cases like fopen() and such.

Nested Blocks in Functions

Just another hint which tries to make your code more readable. Too many nested blocks make your code ugly and it also tends to violate single responsibility principle. So it tries to check the depth of your blocks and warns you if it seems suspicious.

And you can set your custom depth number of course ;-)

Parent Constructor Call

Constructor of parent class should be called if exists. It ensures the right initialization of instantiated object. So if you override parent constructor we try to check whether you should call parent constructor or not.

Unnecessary Closing Delimiter

Using PHP closing delimiter at the end of file is unnecessary when there is no text after it. It is just a cause of Headers already sent error and such. So if it finds such an unnecessary delimiter, IDE will warn you.

Unreachable Statement

What are unreachable statements? E.g. statements after return statement. Just snippets which will never be executed. They just pollute your code. Hint tries to detect them and warn you.

Please, don't forget, that all hints are just our suggestions. If you don't want, you don't have to use them. You can simply disable every hint which you don't like in: Tools -> Options -> Editor -> Hints -> PHP. Just uncheck it in the list.

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:

I didn't get the closing ?> delimiter. Does it cause a "headers already sent" error?

Posted by Fernando Basso on August 13, 2013 at 05:33 PM CEST #

Thank you! It looks to be very practical. Good job.

Posted by guest on August 14, 2013 at 10:08 PM CEST #

@Fernando - If you accidentally place a space after the ?> tag, it can cause a "header's already sent error". This is pretty frustrating for new programmers, especially.

Thank you NB core, for helping make us better coders! Loving the updates.

Posted by Eli McMakin on August 14, 2013 at 10:10 PM CEST #

Netbeans is leading the way on static PHP checking!

Is it too challenging for Netbeans's parser to suppress hints?
For example, most of the time, I want to be warned about unused variables. Sometimes, I must hack suppressions to please Netbeans. I already filed this feature request:

foreach ($myArray as $unused => $value) { //would be nice to suppress $unused
echo $value;
}
unset($unused); //sugar to suppress Netbeans hint

$neverUsed = "This has a real warning that can be fixed";

Netbeans Java has @suppress, and PhpCodesniffer can suppress blocks

Posted by guest on August 16, 2013 at 09:04 PM CEST #

guest: it works properly it 7.4.

Posted by Ondrej Brejla on August 17, 2013 at 12:38 AM CEST #

I think the "Superglobals" hint should also warn about using $_REQUEST. :)

Posted by guest on August 20, 2013 at 02:48 PM CEST #

It warns in the dev build ;)

Posted by Ondrej Brejla on August 20, 2013 at 02:51 PM CEST #

Plz make a setting for the psr guidelines to it will be really helpfull, and an autoformat to if can be :)

Posted by chris on August 20, 2013 at 09:42 PM CEST #

I prepare some code hints for PSR-0 and PSR-1 for next NB version.

Posted by Ondrej Brejla on August 20, 2013 at 09:59 PM CEST #

I think this is pretty good information which you shared with us. I always follow your blog for good stuff which I think the good decision of mine. :)

Posted by guest on August 21, 2013 at 10:26 AM CEST #

Ondrej: Netbeans has 2 Codesniffer plugins. PHP_Codesniffer supports PSR-0, PSR-1 and more.

Posted by Bruce Ingalls on August 21, 2013 at 06:51 PM CEST #

I really know that ;) But those are external CLI tools which have to be executed somehow, I talk about native Hints support.

Posted by Ondrej Brejla on August 21, 2013 at 06:54 PM CEST #

For other readers, who have not used the Netbeans CodeSniffer plugin, it does require you to call `pear install php_Codesniffer`, just as Netbeans requires you to install php, and other php tools.
The codesniffer plugin does have a native feel inside Netbeans, just like all the php tools in the options menu. However, like php itself, Codesniffer also runs as a CLI program.

Netbeans is missing a PSR-? code formatting style, which can be saved into XML, and a good place to start. This also means that every time you automatically re-indent your code, you also must fix your code afterwards, to make it PSR compliant.

I recommend everyone use the Codesniffer plugin (for now), available at http://plugins.netbeans.org/plugin/40282/phpmd-php-codesniffer-plugin

Posted by Bruce Ingalls on August 21, 2013 at 07:30 PM CEST #

And just one note from me, CodeSniffer and Mess Detector tools will have a native integration in NB 7.4 as well ;) But you will have to install them too, of course ;)

Posted by Ondrej Brejla on August 21, 2013 at 07:57 PM CEST #

That will be great :) will there also be an psr format option? Currently it's not really possible to set this up correctly in netbeans.

Posted by chris on August 21, 2013 at 08:30 PM CEST #

Not sure if I'll make it in a time to 8.0 but I have it in my todo list. It's a lot of work.

Posted by Ondrej Brejla on August 21, 2013 at 08:40 PM CEST #

It would be cool if CodeSniffer/MD which is new in 7.4 beta could be added to the "Hints" feature so that checks are made on-the-fly.

I have opened a bug with that request:

https://netbeans.org/bugzilla/show_bug.cgi?id=235540

Please support my request :)

Posted by lordnelson on September 05, 2013 at 07:20 AM CEST #

I could not find a way, to disable the "multiple assignment" hint.
Sometimes it helps, but more often than not, I wish to ignore warnings, for code, like:

$stmt = $db->prepare('...');
//...
$stmt = null; //another assignment; mark for garbage collection
I realize that alternative syntax sugar exists, such as unset(), or creating a null-setting function (not necessarily improving code).

Posted by guest on September 05, 2013 at 02:56 PM CEST #

guest: Hint is called "Immutable variables"

Posted by Ondrej Brejla on September 05, 2013 at 03:02 PM CEST #

Is there a way to know which projects are currently being worked on by the Netbeans team?

For example I want to know what features are currently being developed and the road map for the pending features if that's possible.

Posted by Sherif Sabry on September 06, 2013 at 06:54 AM CEST #

I'd like to put up my hand and ask for Fixer to go with sniffer and MD. If you have ever had to deal with something really big and horribly written, like say WordPress, you will want to have Fixer to whip the files you decide to work on into shape before you start. Otherwise there will be so many sniffer finds on the page you will hardly be able to read it. And of course it is nice to work with clean, grown up code instead of the armature hour stuff that is in the plugins and templates.

I know it can't be hard to integrate because I have a plugin handling it in Sublime-Text right now. For those who are not familiar, it is kind of the PHP lovechild of HTML Tidy and Rebase. Just be careful with it. Doc and Sniffer will go out and find your code smells, and Fixer will go take care of the fact your code smells, not the WHY it smells. It is only designed so that people who work in shops that have very tight coding style guidelines can get their stuff checked into git.

Like I said, great for PHP in WordPress projects. It will fix the 100 ugly things wrong with the code in somebody's functions.php and leave the half dozen really broken things exposed for fixing.

Cheers!

Posted by headless_monkeyman on September 22, 2013 at 08:56 AM CEST #

Thanks for these hints, I generally find them helpful.
Just a small criticism on a couple of them where I think more careful use of English would be appropriate:-
It says here 'Please, don't forget, that all hints are just our suggestions'. If this is true then it is not good English to use words in the Hints like "Must", "Allowed" or "Do not", which imply that the code won't work: ("If-Else Statements Must Use Braces", "Method Length Is 11 Lines (10 allowed)", "Do Not Access Superglobals..."). This is very confusing. Better English would be "Should", "Recommended", "Should not", or "Preferred", "Suggested", etc.
Thanks again, and keep up the good work!

Posted by guest on February 21, 2014 at 11:17 AM CET #

It's rather unhelpful to flag references to dollar_SERVER as requiring filtering. Many of the fields provided do not ever need filtering.

Posted by guest on April 01, 2014 at 09:34 AM CEST #

Error suppression is not always evil. Sometimes it's perfectly reasonable, especially when applied to a simple variable. The only error that can arise through referring to a simple variable is that it is unset, so preceding it with the at sign recognises that there are good reasons why the variable may not be set and that null is acceptable. For example, suppose a loop that conditionally sets array elements, but may not always set any element, resulting in the array being unset, and assume the processing is in a method that returns the array. This is neatly achieved by return (array) @$resultarray; and gives the desirable degenerate case of an empty array.

Posted by guest on April 01, 2014 at 09:39 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