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

DisallowedEmptyRuleFixerRector misbehaves, it converts !empty(string|null $foo) to $foo !== null #6724

Closed
janedbal opened this issue Oct 5, 2021 · 3 comments · Fixed by rectorphp/rector-src#956
Labels

Comments

@janedbal
Copy link

janedbal commented Oct 5, 2021

Bug Report

Subject Details
Rector version dev-main

DisallowedEmptyRuleFixerRector is making dangerous non-equivalent changes. See snippet below.

Minimal PHP Code Causing Issue

class Bug {
	public function get(): ?string {
		return null;
	}

	public function example(): void {
		if (!empty($this->get())) {
			
		}
	}
}
// gets converted to
class Bug {
	public function get(): ?string {
		return null;
	}

	public function example(): void {
		if ($this->get() !== null) {
			
		}
	}
}

Expected Behaviour

// should be converted to
class Bug {
	public function get(): ?string {
		return null;
	}

	public function example(): void {
		if ($this->get() !== null && $this->get() !== '') {
			
		}
	}
}
@janedbal janedbal added the bug label Oct 5, 2021
@janedbal
Copy link
Author

janedbal commented Oct 5, 2021

Other strict rules have similar issue. E.g.

public function example(?string $foo): void {
    if ($foo) {} // gets converted to $foo !== null
}

@TomasVotruba
Copy link
Member

Issue is better, thanks 👍

Ref https://twitter.com/janedbal/status/1445342334036725762
Makes sense!

@TomasVotruba
Copy link
Member

Fixed with opt-in empty scalars in rectorphp/rector-src#956

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

Successfully merging a pull request may close this issue.

2 participants