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 |
||
| 34 | class AdminTestBaseOLD extends TestCase |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | protected static $isDatabaseCreated = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var ContainerInterface |
||
| 43 | */ |
||
| 44 | protected $container; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Client |
||
| 48 | */ |
||
| 49 | protected $client; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Initialize an application with a container and a client. Create database if required. |
||
| 53 | * |
||
| 54 | * @param string $url |
||
| 55 | * @param null $method |
||
| 56 | * @param array $parameters |
||
| 57 | */ |
||
| 58 | public function initApplication($url = '/', $method = null, $parameters = []) |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Assert that an exception is raised in the given code. |
||
| 87 | * |
||
| 88 | * @param $exceptionClass |
||
| 89 | * @param Closure $closure |
||
| 90 | */ |
||
| 91 | View Code Duplication | protected function assertExceptionRaised($exceptionClass, Closure $closure) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param ApplicationConfiguration $applicationConfiguration |
||
| 115 | * @param array $configuration |
||
| 116 | * @return AdminConfiguration |
||
| 117 | */ |
||
| 118 | protected function createAdminConfiguration(ApplicationConfiguration $applicationConfiguration, array $configuration = []) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return MenuFactory |
||
| 130 | */ |
||
| 131 | protected function createKnpMenuFactory() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Return Admin configurations samples |
||
| 138 | * |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | protected function getAdminsConfiguration() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @return KernelInterface | PHPUnit_Framework_MockObject_MockObject |
||
| 165 | */ |
||
| 166 | protected function mockKernel() |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param $name |
||
| 176 | * @return ActionInterface | PHPUnit_Framework_MockObject_MockObject |
||
| 177 | */ |
||
| 178 | protected function mockAction($name) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return ActionConfiguration | PHPUnit_Framework_MockObject_MockObject |
||
| 193 | */ |
||
| 194 | protected function mockActionConfiguration() |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @return Session | PHPUnit_Framework_MockObject_MockObject |
||
| 212 | */ |
||
| 213 | View Code Duplication | protected function mockSession() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @return Logger | PHPUnit_Framework_MockObject_MockObject |
||
| 230 | */ |
||
| 231 | View Code Duplication | protected function mockLogger() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Return a mock of an entity repository |
||
| 248 | * |
||
| 249 | * @return RepositoryInterface | PHPUnit_Framework_MockObject_MockObject |
||
| 250 | */ |
||
| 251 | protected function mockEntityRepository() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return EntityManager | PHPUnit_Framework_MockObject_MockObject |
||
| 260 | */ |
||
| 261 | protected function mockEntityManager() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @return Registry | PHPUnit_Framework_MockObject_MockObject |
||
| 282 | * |
||
| 283 | * @deprecated |
||
| 284 | */ |
||
| 285 | protected function mockDoctrine() |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @return ActionFactory | PHPUnit_Framework_MockObject_MockObject |
||
| 300 | */ |
||
| 301 | protected function mockActionFactory() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @return TokenStorageInterface | PHPUnit_Framework_MockObject_MockObject |
||
| 316 | */ |
||
| 317 | protected function mockTokenStorage() |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return ApplicationConfiguration|PHPUnit_Framework_MockObject_MockObject |
||
| 326 | */ |
||
| 327 | protected function mockApplicationConfiguration() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return MessageHandlerInterface | PHPUnit_Framework_MockObject_MockObject |
||
| 342 | */ |
||
| 343 | protected function mockMessageHandler() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @return TranslatorInterface | PHPUnit_Framework_MockObject_MockObject |
||
| 355 | */ |
||
| 356 | protected function mockTranslator() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param array $entities |
||
| 365 | * @return DataProviderInterface|PHPUnit_Framework_MockObject_MockObject |
||
| 366 | */ |
||
| 367 | protected function mockDataProvider($entities = []) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @return FieldFactory|PHPUnit_Framework_MockObject_MockObject |
||
| 381 | */ |
||
| 382 | protected function mockFieldFactory() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param $class |
||
| 394 | * |
||
| 395 | * @return PHPUnit_Framework_MockObject_MockObject|mixed |
||
| 396 | */ |
||
| 397 | protected function getMockWithoutConstructor($class) |
||
| 405 | |||
| 406 | View Code Duplication | protected function setPrivateProperty($object, $property, $value) |
|
| 414 | |||
| 415 | View Code Duplication | protected function getPrivateProperty($object, $property) |
|
| 424 | } |
||
| 425 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.