Skip to content

[CodeQuality] Fix MatchAssertSameExpectedTypeRector rewriting a passing assert into a failing one - #771

Merged
TomasVotruba merged 3 commits into
rectorphp:mainfrom
MizouziE:fix-MatchAssertSameExpectedType-refactor
Aug 20, 2026
Merged

[CodeQuality] Fix MatchAssertSameExpectedTypeRector rewriting a passing assert into a failing one#771
TomasVotruba merged 3 commits into
rectorphp:mainfrom
MizouziE:fix-MatchAssertSameExpectedType-refactor

Conversation

@MizouziE

Copy link
Copy Markdown
Contributor

MatchAssertSameExpectedTypeRector can rewrite a passing assertSame() into a failing one.

Mechanism

The rule picks the expected literal's new type from NodeTypeResolver::getNativeType(), which returns PHPStan's scope-narrowed type at that program point. A narrowed type is sound to report on, but not to rewrite a literal against: it describes the expression where the narrowing was established, while the rewrite lands elsewhere.

Given Criteria::getType(): int|string|null:

$this->assertSame('Any', $criteria->getType());  // phpstan-phpunit narrows getType() to string

$criteria->setTypeId(100);                       // mutates the receiver
$this->assertSame(100, $criteria->getType());    // rewritten to '100' and fails

@TomasVotruba

Copy link
Copy Markdown
Member

Looks good, thank you 👍

@TomasVotruba
TomasVotruba marked this pull request as ready for review August 20, 2026 16:01
@TomasVotruba
TomasVotruba merged commit e9265f2 into rectorphp:main Aug 20, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants