Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 3.7.2

Bug #19775 False positive in NonExecutableCode sniff when not using curly braces
Submitted: 2013-01-05 02:12 UTC
From: dioubernardo Assigned: squiz
Status: Closed Package: PHP_CodeSniffer (version 1.4.3)
PHP Version: 5.3.6 OS: All
Roadmaps: (Not assigned)    
Subscription  


 [2013-01-05 02:12 UTC] dioubernardo (Bernardo Silva)
Description: ------------ The rule identifies problems below if no {}, I believe it is because you are inside switch. There's something in this latest version corrected about that, I had noticed that in the earlier version just had not reported. In the function "teste" is identified a problem in the first break, after putting in {} in if "teste2" the error is not identified. Test script: --------------- function teste($a, $b){ switch($a){ case 1: if (empty($b)) return 0; break; default: return 2; } return 1; } function teste2($a, $b){ switch($a){ case 1: if (empty($b)){ return 0; } break; default: return 2; } return 1; }

Comments

 [2013-01-05 02:21 UTC] dioubernardo (Bernardo Silva)
There is also no problems in else without {} <?php function teste($a){ if (empty($a)) echo '1'; else return 0; echo "oi"; } ?>
 [2013-01-09 07:53 UTC] squiz (Greg Sherwood)
-Assigned To: +Assigned To: squiz
Can't people just use braces... :) I'll see if there is anything I can do to detect this properly in the sniff.
 [2013-01-09 08:18 UTC] squiz (Greg Sherwood)
-Summary: Problems Squiz.PHP.NonExecutableCode.Unreachable rule when unused {} +Summary: False positive in NonExecutableCode sniff when not using curly braces
 [2013-01-09 08:21 UTC] squiz (Greg Sherwood)
-Status: Assigned +Status: Closed
Fixed in github repo: https://github.com/squizlabs/PHP_CodeSniffer/commit/01754d9c1a622def2f7d04747a b7ebcce767aef4
 [2013-01-11 22:48 UTC] dioubernardo (Bernardo Silva)
The throw also generates the problem function fun($a, $b){ switch ($a){ case 1: if ($b == 0) throw new Exception("zero"); echo 'oi'; break; } } fun(1, 1);
 [2013-01-14 01:43 UTC] squiz (Greg Sherwood)
I have fixed this generically so the THROW statement no longer throws non executable code errors either.
 [2013-01-15 21:19 UTC] dioubernardo (Bernardo Silva)
The bug continues, sample code: class classFragment { function aplicaValidacao($texto){ if (empty($this->validacao)) return $texto; switch ($this->validacao){ case "STRORMOEDA": if (is_numeric($texto)) return Conversao::converter(DINHEIRO, $texto, TELA); return $texto; case "DATA_EXTENSO": return Conversao::dataExtenso($texto); default: return Conversao::converter($this->validacao, $texto, TELA); } } }
 [2013-01-16 02:21 UTC] squiz (Greg Sherwood)
This new issue is unrelated to the one in this report. Turns out it is actually a core tokenizing issue where the RETURN in the IF statement is being assigned as the closer of the first CASE statement. Needs a core fix, not a sniff fix, in this case. The following code can be used to replicate: switch ($foo) { case 'foo': if ($foo) return $foo; return $bar; default: return $bar; }
 [2013-01-16 02:51 UTC] squiz (Greg Sherwood)
Problem was caused by fix for bug 19755 and fix committed to github repo here: https://github.com/squizlabs/PHP_CodeSniffer/commit/6f77d5a5bf0669eb302073d86d a91abec7e0ae23