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

Incorrect behavior of DisallowedEmptyRuleFixerRector with uninitialized properties #8306

Closed
nerones opened this issue Nov 14, 2023 · 5 comments
Labels

Comments

@nerones
Copy link

nerones commented Nov 14, 2023

Bug Report

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

Minimal PHP Code Causing Issue

See https://getrector.com/demo/7e222012-b871-4e3c-9b05-3a62017753f7

<?php

final class DemoFile
{
    protected string $test;
    
    public function run()
    {
        return empty($this->test);
    }
}

Responsible rules

  • DisallowedEmptyRuleFixerRector

Expected Behavior

The code must not change.

The problem with this rule is that is not safe to compare against an empty string because the property it's not initialized. The empty function will also check for not initialized properties. The same thing will happen with other types.

@nerones nerones added the bug label Nov 14, 2023
@samsonasik
Copy link
Member

empty() usage is not recommended on strict usage, see https://phpstan.org/r/e0c34868-f3b5-481d-821e-5499e6d0253d

I think the correct usage is to change with isset early on unitialized property:

-return empty($this->test);
+return ! isset($this->test) || $this->test === '':

@nerones
Copy link
Author

nerones commented Nov 20, 2023

You are right, what you propose is a better solution.

@TomasVotruba
Copy link
Member

@samsonasik Could you handle this one? Thanks 👍

@samsonasik
Copy link
Member

I will try

@samsonasik
Copy link
Member

Resolved at PR:

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

No branches or pull requests

3 participants