Tests/Unit/Utils/RestrictorTest.php
The method getStorageManagerMock has a boolean flag argument $hasItem, which is a certain sign of a Single Responsibility Principle violation. Open
Open
private function getStorageManagerMock($item = 'userId', $hasItem = false)
- Read upRead up
- Exclude checks
BooleanArgumentFlag
Since: 1.4.0
A boolean flag argument is a reliable indicator for a violation of the Single Responsibility Principle (SRP). You can fix this problem by extracting the logic in the boolean flag into its own class or method.
Example
class Foo {
public function bar($flag = true) {
}
}
Source https://phpmd.org/rules/cleancode.html#booleanargumentflag
Define a constant instead of duplicating this literal "userId" 5 times. Open
Open
$result = $restrictor->blockBy('userId');
- Read upRead up
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
function run() { prepare('action1'); // Non-Compliant - 'action1' is duplicated 3 times execute('action1'); release('action1'); }
Compliant Solution
ACTION_1 = 'action1'; function run() { prepare(ACTION_1); execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Call to undeclared method \NW\RequestLimitBundle\Tests\Unit\Utils\RestrictorTest::assertEquals
Open
Open
$this->assertEquals('userId', $result);
- Exclude checks
Call to method expects
from undeclared class \PHPUnit_Framework_MockObject_MockObject
Open
Open
$storageManager->expects($this->never())->method('getItem');
- Exclude checks
Call to method expects
from undeclared class \PHPUnit_Framework_MockObject_MockObject
Open
Open
$storageManager->expects($this->never())->method('setItem');
- Exclude checks
Call to undeclared method \NW\RequestLimitBundle\Tests\Unit\Utils\RestrictorTest::never
Open
Open
$storageManager->expects($this->never())->method('getItem');
- Exclude checks
Class extends undeclared class \PHPUnit_Framework_TestCase
Open
Open
class RestrictorTest extends \PHPUnit_Framework_TestCase
- Exclude checks
Call to undeclared method \NW\RequestLimitBundle\Tests\Unit\Utils\RestrictorTest::once
Open
Open
$storageManager->expects($this->once())
- Exclude checks
Call to undeclared method \NW\RequestLimitBundle\Tests\Unit\Utils\RestrictorTest::createMock
Open
Open
$storageManager = $this->createMock(
- Exclude checks
Call to undeclared method \NW\RequestLimitBundle\Tests\Unit\Utils\RestrictorTest::once
Open
Open
$storageManager->expects($this->once())
- Exclude checks
Argument 2 (hasItem)
is true
but \NW\RequestLimitBundle\Tests\Unit\Utils\RestrictorTest::getStorageManagerMock()
takes false
defined at /code/Tests/Unit/Utils/RestrictorTest.php:45
Open
Open
$storageManager = $this->getStorageManagerMock('userId', true);
- Exclude checks
Call to undeclared method \NW\RequestLimitBundle\Tests\Unit\Utils\RestrictorTest::setExpectedException
Open
Open
$this->setExpectedException(RequestLimitReachedException::class);
- Exclude checks
Call to undeclared method \NW\RequestLimitBundle\Tests\Unit\Utils\RestrictorTest::once
Open
Open
$storageManager->expects($this->once())
- Exclude checks
Return type of getStorageManagerMock()
is undeclared type \PHPUnit_Framework_MockObject_MockObject
Open
Open
private function getStorageManagerMock($item = 'userId', $hasItem = false)
- Exclude checks
Call to undeclared method \NW\RequestLimitBundle\Tests\Unit\Utils\RestrictorTest::never
Open
Open
$storageManager->expects($this->never())->method('setItem');
- Exclude checks