Skip to content

Rector removes lines in switch-case that is not dead #2730

@escopecz

Description

@escopecz
Subject Details
Rector version https://github.com/rectorphp/rector-prefixed/releases/tag/v0.6.13
PHP version 7.2.26
Full Command mautic (3.x) $ bin/rector process app/bundles/AssetBundle --set dead-code --dry-run1
Demo link https://getrector.org/demo/4db04e11-6f12-407c-9b4d-97133acff941
rector.yaml No config

Current Behaviour

Rector works for Mautic!!! 🎉 I tried to run Rector on AssetBundle and noticed following removed lines which I don't think is correct.

We use a static method to convert upload size to bytes in Mautic, but it was borrowed from Symfony:

Rector suggests to remove these lines:

         switch (strtolower(substr($size, -1))) {
             case 't':
-                $max *= 1024;
                 // no break
             case 'g':
-                $max *= 1024;
                 // no break
             case 'm':
-                $max *= 1024;
                 // no break
             case 'k':
                 $max *= 1024;
    ----------- end diff -----------

Minimal PHP Code Causing Issue

<?php declare(strict_types=1);

final class DemoFile
{
    public static function convertSizeToBytes($size)
    {
        if ('' === $size) {
            return PHP_INT_MAX;
        }

        $max = ltrim($size, '+');
        if (0 === strpos($max, '0x')) {
            $max = intval($max, 16);
        } elseif (0 === strpos($max, '0')) {
            $max = intval($max, 8);
        } else {
            $max = intval($max);
        }

        switch (strtolower(substr($size, -1))) {
            case 't':
                $max *= 1024;
                // no break
            case 'g':
                $max *= 1024;
                // no break
            case 'm':
                $max *= 1024;
                // no break
            case 'k':
                $max *= 1024;
        }

        return $max;
    }
}

Expected Behaviour

I don't think it should remove the lines.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions