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

Expand AssertSameWithCountRule to support sizeof #8

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ It also contains this strict framework-specific rules (can be enabled separately
* Check that you are not using `assertSame()` with `true` as expected value. `assertTrue()` should be used instead.
* Check that you are not using `assertSame()` with `false` as expected value. `assertFalse()` should be used instead.
* Check that you are not using `assertSame()` with `null` as expected value. `assertNull()` should be used instead.
* Check that you are not using `assertSame()` with `count($variable)` as second parameter. `assertCount($variable)` should be used instead.
* Check that you are not using `assertSame()` with `count($variable)`, or its alias `sizeof($variable)`, as second parameter. `assertCount($variable)` should be used instead.

## How to document mock objects in phpDocs?

Expand Down
7 changes: 5 additions & 2 deletions src/Rules/PHPUnit/AssertSameWithCountRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ public function processNode(Node $node, Scope $scope): array
if (
$right instanceof Node\Expr\FuncCall
&& $right->name instanceof Node\Name
&& strtolower($right->name->toString()) === 'count'
&& in_array(strtolower($right->name->toString()), ['count', 'sizeof'], true)
) {
return [
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
sprintf(
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, %s($variable)).',
strtolower($right->name->toString())
),
];
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/PHPUnit/AssertSameWithCountRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public function testRule(): void
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, count($variable)).',
10,
],
[
'You should use assertCount($expectedCount, $variable) instead of assertSame($expectedCount, sizeof($variable)).',
20,
],
]);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Rules/PHPUnit/data/assert-same-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ public function testAssertSameWithCountMethodIsOK()
$this->assertSame(5, $this->count()); // OK
}

public function testAssertSameWithSizeOf()
{
$this->assertSame(5, sizeof([1, 2, 3]));
}

public function testAssertSameWithSizeOfMethodIsOK()
{
$this->assertSame(5, $this->sizeof()); // OK
}
}