Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 23 | class CsrfListenerTest extends EventListenerTest |
||
| 24 | { |
||
| 25 | const VALID_TOKEN = 'valid'; |
||
| 26 | const INVALID_TOKEN = 'invalid'; |
||
| 27 | const INTENTION = 'rest'; |
||
| 28 | |||
| 29 | /** @var EventDispatcherInterface */ |
||
| 30 | protected $eventDispatcherMock; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * If set to null before initializing mocks, Request::getSession() is expected not to be called. |
||
| 34 | * |
||
| 35 | * @var \Symfony\Component\HttpFoundation\Session\SessionInterface |
||
| 36 | */ |
||
| 37 | protected $sessionMock; |
||
| 38 | |||
| 39 | protected $sessionIsStarted = true; |
||
| 40 | |||
| 41 | protected $csrfTokenHeaderValue = self::VALID_TOKEN; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Route returned by Request::get( '_route' ) |
||
| 45 | * If set to false, get( '_route' ) is expected not to be called. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $route = 'ezpublish_rest_something'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * If set to false, Request::getRequestMethod() is expected not to be called. |
||
| 53 | */ |
||
| 54 | protected $requestMethod = 'POST'; |
||
| 55 | |||
| 56 | public function provideExpectedSubscribedEventTypes() |
||
| 62 | |||
| 63 | public function testIsNotRestRequest() |
||
| 75 | |||
| 76 | View Code Duplication | public function testCsrfDisabled() |
|
| 85 | |||
| 86 | View Code Duplication | public function testNoSessionStarted() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Tests that method CSRF check don't apply to are indeed ignored. |
||
| 99 | * |
||
| 100 | * @param string $ignoredMethod |
||
| 101 | * @dataProvider getIgnoredRequestMethods |
||
| 102 | */ |
||
| 103 | public function testIgnoredRequestMethods($ignoredMethod) |
||
| 111 | |||
| 112 | public function getIgnoredRequestMethods() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @dataProvider provideSessionRoutes |
||
| 123 | */ |
||
| 124 | public function testSessionRequests($route) |
||
| 131 | |||
| 132 | public static function provideSessionRoutes() |
||
| 133 | { |
||
| 134 | return [ |
||
| 135 | ['ezpublish_rest_createSession'], |
||
| 136 | ['ezpublish_rest_refreshSession'], |
||
| 137 | ['ezpublish_rest_deleteSession'], |
||
| 138 | ]; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
| 143 | */ |
||
| 144 | public function testNoHeader() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @expectedException \eZ\Publish\Core\Base\Exceptions\UnauthorizedException |
||
| 153 | */ |
||
| 154 | public function testInvalidToken() |
||
| 160 | |||
| 161 | public function testValidToken() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return CsrfProviderInterface|PHPUnit_Framework_MockObject_MockObject |
||
| 172 | */ |
||
| 173 | protected function getCsrfProviderMock() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @return PHPUnit_Framework_MockObject_MockObject|GetResponseEvent |
||
| 195 | */ |
||
| 196 | View Code Duplication | protected function getEventMock($class = null) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @return \Symfony\Component\HttpFoundation\Session\SessionInterface|PHPUnit_Framework_MockObject_MockObject |
||
| 212 | */ |
||
| 213 | protected function getSessionMock() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @return ParameterBag|PHPUnit_Framework_MockObject_MockObject |
||
| 228 | */ |
||
| 229 | protected function getRequestHeadersMock() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @return PHPUnit_Framework_MockObject_MockObject|Request |
||
| 262 | */ |
||
| 263 | protected function getRequestMock() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @return PHPUnit_Framework_MockObject_MockObject|EventDispatcherInterface |
||
| 297 | */ |
||
| 298 | protected function getEventDispatcherMock() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param bool $csrfEnabled |
||
| 311 | * |
||
| 312 | * @return CsrfListener |
||
| 313 | */ |
||
| 314 | protected function getEventListener($csrfEnabled = true) |
||
| 331 | } |
||
| 332 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.