2

I have a PHP file with a line that produces a warning in NetBeans. How can I force the IDE to ignore that concrete warning?

Notice that I don't want to disable this type of warning solution-wide.

Here is a sample line of code:

if ($query = db_query("SELECT column FROM {table} WHERE type='some_value'")) { ... }

Here is a text of produced warning: 'Possible accidental assignment, assignments in conditions should be avoided.'


I know how to correct the code, but notice that I've asked completely other question! I need a way to suppress the warning having the same statement in the if clause.

I’m not going to use the @ operator either, because it has a completely other mission.

Below you can see how I suppress the ReSharper warning(s) in C#. I want something like that in PHP for NetBeans:

// ReSharper disable PossibleNullReferenceException
_currentPage = restoredStageSurvey._currentPage;
// ReSharper restore PossibleNullReferenceException
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Igor Soloydenko
  • 11,067
  • 11
  • 47
  • 90
  • I know that I can rewrite this line. The problem is that our team doesn't agree with me about this line. My colleague says that this line is correct, so why shouldn't we suppress the warning in this place? Actually, I'm trying to find the IDE feature. If this feature doesn't exist, we'll get rid of this warning correct way. – Igor Soloydenko Dec 02 '11 at 14:15
  • 2
    This might be too specific for SO. And while Programmers.SE is more suited for IDE feature discussions, it's likely to go unanswered there too. If anything, Netbeans would require some form of `/** @disable-syntax-hint: assignment-in-expression */` docblock decorator or something. Seems highly unlikely to exist. But I would totally bug them on their mailing list... – mario Dec 02 '11 at 14:27
  • @mario. Thank you. What a pity that this feature probably doesn't exist. :) – Igor Soloydenko Dec 02 '11 at 14:33

3 Answers3

4

Maybe there is a way to suppress that warning in NetBeans, I don't know.

However, you could also just heed the warning and change your code - it won't do it any harm. The issue NetBeans complains about isn't anything terrible, but it is good style to separate the query and the condition like so:

$query = db_query("SELECT column FROM {table} WHERE type='some_value'");

if ($query)
 { ... }
else
 { // die and report error }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Thank you, but as I told, the condition of the problem is different. I agree that your code is better. I just expected another kind of answer. – Igor Soloydenko Dec 02 '11 at 14:22
  • @key it's well possible that there is no other answer - as mario says, a specific setting for this specific warning may not exist. I would try to convince the colleague that what he's doing isn't great style, and not changing it has already cost a lot of time (i.e. your time trying to find a way to work around the warning) – Pekka Dec 02 '11 at 14:30
  • @Pekka, Even PHP manual provides examples with assigments in loops/conditions(example: http://php.net/manual/ru/function.mysql-query.php). I don't think that people should be avoiding it as it sometimes allows to write much nicer code. Why do you advice against it? It seems like FUD. – XzKto Dec 02 '11 at 14:52
  • @XzKto there is merit to the warning - using this style makes accidental assignments easier to happen, and it makes the code harder to read. These are all soft arguments, but in the specific case, it's more worthwhile to change the code than waste time trying to change netbeans IMO. – Pekka Dec 02 '11 at 17:21
4

While you can't just disable one warning (look for bug reports like http://netbeans.org/bugzilla/show_bug.cgi?id=97224), there is a common solution for this problem (if you have "Ignore assignments in sub-statements" turned ON):

if(($x=$y)) {}

TLDR: Double brackets = Ignore this type of warning.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
XzKto
  • 2,472
  • 18
  • 18
  • 3
    An when somebody inherits the code, there'll be much mystery about why the predecessor did this. Ugh... But I guess it answers the question asked – Pekka Dec 02 '11 at 17:23
  • @Pekka, I never used it before, but it got me thinging: it looks OK, you see the difference between '=' and '=='(that 1% that you need it instead of '===') much better, you don't need to write ugly code like you suggested, you see the right warnings and it doesn't do anything bad. Why not to make this a standard? – XzKto Dec 02 '11 at 18:26
  • 1
    one man's "ugly" is the other man's "readable for somebody else in three years' time". Code needs to be *understandable* and that is more imporant than having it look nice – Pekka Dec 02 '11 at 18:30
  • @ХзКто, thank you. On Monday I'll try what you have recommended. Hope it will help to solve the problem. At the same time I agree with NetBeans team and other people who finds that kind of code weird. – Igor Soloydenko Dec 02 '11 at 20:37
3

You can control the hints/warnings that NetBeans provides through menu ToolsOptionsEditorHints. You can turn off this specific hint by choosing Language: PHP and unselecting the "Possible accidental assignment, assignments in conditions should be avoided" checkbox.

You should however heed the advise of the other answers and reconsider this style.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jonathan Spooner
  • 7,682
  • 2
  • 34
  • 41