rector icon indicating copy to clipboard operation
rector copied to clipboard

New rule proposal: incorrect use of parenthesescan can cause unexpected behavior in code

Open palaueb opened this issue 8 months ago • 1 comments

Thanks for all this awesome tool, for me, is one of the best PHP tools ever done. Great job!

Feature Request

I have seen the PHP rule on https://cloud-ci.sgs.com/sonar/coding_rules?open=php%3AS6600&rule_key=php%3AS6600 that states this In PHP, incorrect use of parentheses can cause unexpected behavior in code. Therefore, it is important to avoid the unnecessary usage of parentheses for language constructs. Using constructs with parentheses can be misleading as it will produce a syntax that looks like a normal function call. However those constructs have lower precedence in the evaluation order than some others operators, this can lead to a behavior completely different from what the function syntax would hint. Also, some of the constructs have optional parameters, while modifying the code we may remove a parameter while keeping the parentheses, resulting in invalid code.

I thing this will help refactoring to better code.

Diff

-return(true);
+return true;

-echo("hello");
+echo "hello";

-break(2);
+break 2;

-case(3);
+case 3;

-continue(3);
+continue 3;

-include($file);
+include $file;

-include_once($file);
+include_once $file;

-require($file);
+require $file;

-require_once($file);
+require_once $file;

-print("hello Human");
+print "hello Human";

-throw(new Exception('Exception message'));
+throw new Exception('Exception message');

-yield($i);
+yield $i;

-yield from(funcall());
+yield from funcall();

palaueb avatar Jun 05 '24 17:06 palaueb