Skip to content

Incorrect behavior of DowngradeHashAlgorithmXxHashRector when PHP version is checked #9327

@kkmuffme

Description

@kkmuffme

Bug Report

Subject Details
Rector version last dev-main
Installed as composer dependency

Minimal PHP Code Causing Issue

See https://getrector.com/demo/8a4b4765-e93f-463c-a784-a00d573197a6

<?php

final class DemoFile
{
    public function run()
    {
        return PHP_VERSION_ID >= 80100 ? hash( 'xxh128', $value ) : hash( 'md4', $value );
    }
    
    public function run_v()
    {
        return version_compare( PHP_VERSION, '8.1', '>=' ) ? hash( 'xxh128', $value ) : hash( 'md4', $value );
    }
}

Same also when not used with a ternary but regular if

    public function run_v()
    {
        if ( version_compare( PHP_VERSION, '8.1', '>=' ) ) {
            return hash( 'xxh128', $value );
        }
        
        return hash( 'md4', $value );
    }

Responsible rules

  • DowngradeHashAlgorithmXxHashRector

Expected Behavior

If there is a version check with PHP_VERSION_ID or PHP_VERSION it should not modify the code, since this is correct.

In addition, I'd say that also PHP_MAJOR_VERSION > 8 should not be changed for anything that downgrades below 8.0

I guess this issue not only affects this specific rule, but all downgrade rules

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions