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

Request #13570 Enhancement of DisallowCountInLoopsSniff
Submitted: 2008-04-03 06:14 UTC
From: jotango Assigned: squiz
Status: Closed Package: PHP_CodeSniffer (version 1.0.1)
PHP Version: Irrelevant OS: Any
Roadmaps: 1.1.0    
Subscription  


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 8 + 23 = ?

 
 [2008-04-03 06:14 UTC] jotango (Jan Miczaika)
Description: ------------ As I mailed earlier I modified your DisallowCountInLoopsSniff to disallow frequently used count-ish functions in loop conditions. If someone writes for ( $i = 0; $i < strlen($string); $i++ ) { strlen is computed for every loop iteration, leading to longer run times. This sniff issues a warning and suggests assigning the variable beforehand. Test script: --------------- $string = 'hello'; for ( $i = 0; $i < strlen($string); $i++ ) { print $i; } Expected result: ---------------- See a warning from the sniff Actual result: -------------- The use of sizeof,strlen inside a loop condition reduces performance. Assign the return value of the function to a variable and use the variable in the loop condition instead.

Comments

 [2008-04-07 01:15 UTC] squiz (Greg Sherwood)
Thanks for the sniff. I've replace the existing DisallowCountInLoops sniff with this new one and added count() to the list of disallowed functions.
 [2008-04-07 01:15 UTC] squiz (Greg Sherwood)
This bug has been fixed in CVS. If this was a documentation problem, the fix will appear on pear.php.net by the end of next Sunday (CET). If this was a problem with the pear.php.net website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better.