Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\Fixture;

class MultiplyFunctionCall
{
public function run(string $a)
{
$b = 1 * strlen($a) * 4;
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\Fixture;

class MultiplyFunctionCall
{
public function run(string $a)
{
$b = strlen($a) * 4;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class WithParentheses
public function run()
{
$a = 1 * (2 + 3) * 4;
$a = 1 * ( 2 + 3 ) * 4;
}
}

Expand All @@ -21,6 +22,7 @@ class WithParentheses
public function run()
{
$a = (2 + 3) * 4;
$a = (2 + 3) * 4;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,36 @@ private function processBinaryPlusAndMinus(Plus | Minus $binaryOp): ?Expr

private function isMulParenthesized(File $file, Mul $mul): bool
{
if (! $mul->right instanceof BinaryOp) {
return false;
}

$oldTokens = $file->getOldTokens();
$endTokenPost = $mul->getEndTokenPos();

return isset($oldTokens[$endTokenPost]) && (string) $oldTokens[$endTokenPost] === ')';
if (isset($oldTokens[$endTokenPost]) && (string) $oldTokens[$endTokenPost] === ')') {
$startTokenPos = $mul->right->getStartTokenPos();
$previousEndTokenPost = $mul->left->getEndTokenPos();

while ($startTokenPos > $previousEndTokenPost) {
--$startTokenPos;

if (! isset($oldTokens[$startTokenPos])) {
return false;
}

// handle space before open parentheses
if (trim((string) $oldTokens[$startTokenPos]) === '') {
continue;
}

return (string) $oldTokens[$startTokenPos] === '(';
}

return false;
}

return false;
}

private function processBinaryMulAndDiv(Mul | Div $binaryOp): ?Expr
Expand Down