Skip to content

[CodeQuality] Add PHPUNIT_NARROW_ASSERTS set focused on narrowing broad asserts to specific methods#716

Merged
TomasVotruba merged 4 commits into
mainfrom
narrow-asserts-set
Jul 12, 2026
Merged

[CodeQuality] Add PHPUNIT_NARROW_ASSERTS set focused on narrowing broad asserts to specific methods#716
TomasVotruba merged 4 commits into
mainfrom
narrow-asserts-set

Conversation

@TomasVotruba

@TomasVotruba TomasVotruba commented Jul 12, 2026

Copy link
Copy Markdown
Member

Adds a focused PHPUNIT_NARROW_ASSERTS set that groups only the rules narrowing broad asserts into specific, more descriptive PHPUnit methods.

Handy when you want the assert-narrowing cleanups without adopting the full PHPUNIT_CODE_QUALITY set (mock/stub rewrites, type declarations, etc). All rules stay in PHPUNIT_CODE_QUALITY too.

use Rector\Config\RectorConfig;
use Rector\PHPUnit\Set\PHPUnitSetList;

return RectorConfig::configure()
    ->withSets([
        PHPUnitSetList::PHPUNIT_NARROW_ASSERTS,
    ]);

Example changes the set produces:

-$this->assertTrue(isset($array['key']));
+$this->assertArrayHasKey('key', $array);
-$this->assertSame(10, count($items));
+$this->assertCount(10, $items);
-$this->assertEquals('value', $result);
+$this->assertSame('value', $result);
-$this->assertSame(null, $value);
+$this->assertNull($value);

Rules included (13): AssertCompareOnCountableWithMethodToAssertCount, AssertComparisonToSpecificMethod, AssertNotOperator, AssertTrueFalseToSpecificMethod, AssertSameBoolNullToSpecificMethod, AssertFalseStrposToContains, AssertIssetToSpecificMethod, AssertInstanceOfComparison, AssertEmptyNullableObjectToAssertInstanceof, AssertEqualsToSame, AssertSameTrueFalseToAssertTrueFalse, MatchAssertSameExpectedType, AssertArrayCastedObjectToAssertSame.

@TomasVotruba TomasVotruba merged commit 4f779ec into main Jul 12, 2026
8 checks passed
@TomasVotruba TomasVotruba deleted the narrow-asserts-set branch July 12, 2026 10:58
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.

1 participant