Skip to content
Open
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,52 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class RemoveDeadIfBlock
{
public function run($condition)
{
if ($value) {
} elseif ($differentValue) {
} else {
}

if ($differentValue) {
echo 'different';
} elseif ($value) {
} else {
}

if ($differentValue) {
} elseif ($value) {
echo 'value';
} else {
}

return $differentValue;
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class RemoveDeadIfBlock
{
public function run($condition)
{
if ($differentValue) {
echo 'different';
}

if (!$differentValue && $value) {
echo 'value';
}

return $differentValue;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class IfElseNoStmt
{
public function run($condition)
{
if ($condition) {
echo "something";
} else {
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class IfElseNoStmt
{
public function run($condition)
{
if ($condition) {
echo "something";
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class IfElseNoStmt
{
public function run($condition)
{
if ($condition) {
echo "something";
} elseif (!$condition) {
} elseif (true) {
echo "global";
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class IfElseNoStmt
{
public function run($condition)
{
if ($condition) {
echo "something";
} elseif (true) {
echo "global";
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class IfNoStmtWithElse
{
public function run($condition)
{
if ($condition) {
} else {
echo "something";
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class IfNoStmtWithElse
{
public function run($condition)
{
if (!$condition) {
echo "something";
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class IfNoStmtWithElseNoStmt
{
public function run($condition)
{
if ($condition) {
} else {
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class IfNoStmtWithElseNoStmt
{
public function run($condition)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class SideEffectChecks
{
public function run()
{
if (10) {
}

if (true) {
}

if (5 + 10) {
}
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class SideEffectChecks
{
public function run()
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class MergeComments
{
public function run($b)
{
// toplevel
if ($b) {
} elseif (!$b) {
// first innner
f();
// second inner
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class SkipPropertyFetch
{
public function run($obj, $condition)
{
if ($condition) {
echo '1';
} elseif ($obj->v) {
echo '2';
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class SkipUsedInNextStmt
{
public function run($resultLen, $result, $decimalSep)
{
for ($i = $resultLen - 1; $i >= 0 && $result[$i] === '0'; $i--) {
;
}

if ($i >= 0 && $result[$i] === $decimalSep) {
$i--;
}

$result = substr($result, 0, $i + 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\Fixture;

class SkipUsedInNextStmt2
{
public function run($resultLen, $result, $decimalSep)
{
foreach ($result as $i => $value) {
;
}

if ($i >= 0 && $result[$i] === $decimalSep) {
$i--;
}

$result = substr($result, 0, $i + 1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveDeadIfBlockRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\If_\RemoveDeadIfBlockRector;

return RectorConfig::configure()
->withRules([RemoveDeadIfBlockRector::class]);
Loading
Loading