Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SlevomatCodingStandard.PHP.UselessParentheses.UselessParentheses - false positive #1674

Open
bkdotcom opened this issue Apr 29, 2024 · 1 comment

Comments

@bkdotcom
Copy link
Contributor

bkdotcom commented Apr 29, 2024

These parens aren't useless... am I doing something wrong?

class Utf8
{
    // Get Unicode code point of utf9-encoded character (without need for mbstring extension)
    public static function ord($char)
    {
        $ord = \ord($char[0]);
        if ($ord < 0x80) {
            return $ord;
        } elseif ($ord < 0xe0) {
            return ($ord - 0xc0 << 6)        //  UselessParentheses (invalid)
                + \ord($char[1]) - 0x80;
        } elseif ($ord < 0xf0) {
            return ($ord - 0xe0 << 12)      // UselessParentheses (invalid)
                + (\ord($char[1]) - 0x80 << 6)   // UselessParentheses (invalid)
                + \ord($char[2]) - 0x80;
        } elseif ($ord < 0xf8) {
            return ($ord - 0xf0 << 18)      // UselessParentheses (invalid)
                + (\ord($char[1]) - 0x80 << 12)  // UselessParentheses (invalid)
                + (\ord($char[2]) - 0x80 << 6)   // UselessParentheses (invalid)
                + \ord($char[3]) - 0x80;
        }
        return false;
    }
}

unit tests:

assertSame(97, Utf8::ord('a'));   // 1-bype
assertSame(169, Utf8::ord('©'));   // 2-byte
assertSame(65049, Utf8::ord('︙'));   // 3-byte
assertSame(128169, Utf8::ord('💩'));  // 4-type

to be clear: without parens, PHP evaluates
$ord - 0xc0 << 6 + \ord($char[1]) - 0x80
as
($ord - 0xc0) << (6 + \ord($char[1]) - 0x80)

so...
($ord - 0xc0 << 6) + \ord($char[1]) - 0x80
the parens are very much necessary
also tried:
($ord - 0xc0 << 6) + (\ord($char[1]) - 0x80)
and
$ord - 0xc0 << 6 + (\ord($char[1]) - 0x80)

@bkdotcom
Copy link
Contributor Author

bkdotcom commented May 1, 2024

Possibly similar to #1672, #1678

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant