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 |
||
| 16 | abstract class AbstractCacheHandlerTest extends AbstractBaseHandlerTest |
||
| 17 | { |
||
| 18 | abstract public function getHandlerMethodName(): string; |
||
| 19 | |||
| 20 | abstract public function getHandlerClassName(): string; |
||
| 21 | |||
| 22 | abstract public function providerForUnCachedMethods(): array; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @dataProvider providerForUnCachedMethods |
||
| 26 | * |
||
| 27 | * @param string $method |
||
| 28 | * @param array $arguments |
||
| 29 | * @param array|null $tags |
||
| 30 | * @param string|null $key |
||
| 31 | * @param mixed $returnValue |
||
| 32 | */ |
||
| 33 | final public function testUnCachedMethods(string $method, array $arguments, array $tags = null, string $key = null, $returnValue = null) |
||
| 34 | { |
||
| 35 | $handlerMethodName = $this->getHandlerMethodName(); |
||
| 36 | |||
| 37 | $this->loggerMock->expects($this->once())->method('logCall'); |
||
| 38 | |||
| 39 | $innerHandler = $this->createMock($this->getHandlerClassName()); |
||
| 40 | $this->persistenceHandlerMock |
||
| 41 | ->expects($this->once()) |
||
| 42 | ->method($handlerMethodName) |
||
| 43 | ->will($this->returnValue($innerHandler)); |
||
| 44 | |||
| 45 | $innerHandler |
||
| 46 | ->expects($this->once()) |
||
| 47 | ->method($method) |
||
| 48 | ->with(...$arguments) |
||
| 49 | ->will($this->returnValue($returnValue)); |
||
| 50 | |||
| 51 | if ($tags || $key) { |
||
|
|
|||
| 52 | $this->cacheMock |
||
| 53 | ->expects(!empty($tags) ? $this->once() : $this->never()) |
||
| 54 | ->method('invalidateTags') |
||
| 55 | ->with($tags); |
||
| 56 | |||
| 57 | $this->cacheMock |
||
| 58 | ->expects(!empty($key) ? $this->once() : $this->never()) |
||
| 59 | ->method('deleteItem') |
||
| 60 | ->with($key); |
||
| 61 | } else { |
||
| 62 | $this->cacheMock |
||
| 63 | ->expects($this->never()) |
||
| 64 | ->method($this->anything()); |
||
| 65 | } |
||
| 66 | |||
| 67 | $handler = $this->persistenceCacheHandler->$handlerMethodName(); |
||
| 68 | call_user_func_array(array($handler, $method), $arguments); |
||
| 69 | } |
||
| 70 | |||
| 71 | abstract public function providerForCachedLoadMethods(): array; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @dataProvider providerForCachedLoadMethods |
||
| 75 | * |
||
| 76 | * @param string $method |
||
| 77 | * @param array $arguments |
||
| 78 | * @param string $key |
||
| 79 | * @param mixed $data |
||
| 80 | * @param bool $multi Default false, set to true if method will lookup several cache items. |
||
| 81 | * @param array $additionalCalls Sets of additional calls being made to handlers, with 4 values (0: handler name, 1: handler class, 2: method, 3: return data) |
||
| 82 | * @param bool $logHitMiss To opt in to specify method uses the newer logHit / logMiss logic for call logging. |
||
| 83 | */ |
||
| 84 | final public function testLoadMethodsCacheHit(string $method, array $arguments, string $key, $data = null, bool $multi = false, array $additionalCalls = [], bool $logHitMiss = false) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @dataProvider providerForCachedLoadMethods |
||
| 129 | * |
||
| 130 | * @param string $method |
||
| 131 | * @param array $arguments |
||
| 132 | * @param string $key |
||
| 133 | * @param object $data |
||
| 134 | * @param bool $multi Default false, set to true if method will lookup several cache items. |
||
| 135 | * @param array $additionalCalls Sets of additional calls being made to handlers, with 4 values (0: handler name, 1: handler class, 2: method, 3: return data) |
||
| 136 | * @param bool $logHitMiss To opt in to specify method uses the newer logHit / logMiss logic for call logging. |
||
| 137 | */ |
||
| 138 | final public function testLoadMethodsCacheMiss(string $method, array $arguments, string $key, $data = null, bool $multi = false, array $additionalCalls = [], bool $logHitMiss = false) |
||
| 203 | } |
||
| 204 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: