| Conditions | 9 |
| Paths | 24 |
| Total Lines | 53 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 80 | public function testLoggerWillCreateConfiguredHandlers(array $handlerConfigs = []): void |
||
| 81 | { |
||
| 82 | /** @var LoggerFactory&MockObject $factory */ |
||
| 83 | $factory = $this->getMockBuilder(LoggerFactory::class) |
||
| 84 | ->onlyMethods(['getService', 'buildService']) |
||
| 85 | ->getMock(); |
||
| 86 | |||
| 87 | $serviceName = 'FooLogger'; |
||
| 88 | |||
| 89 | $handlers = []; |
||
| 90 | $getArgs = $buildArgs = []; |
||
| 91 | $getReturns = $buildReturns = []; |
||
| 92 | foreach ($handlerConfigs as $handlerName => $handler) { |
||
| 93 | if (is_object($handler)) { |
||
| 94 | $handlers[] = $handler; |
||
| 95 | continue; |
||
| 96 | } |
||
| 97 | if (!is_array($handler) && !is_string($handler)) { |
||
| 98 | continue; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** @var HandlerInterface&MockObject $handlerObject */ |
||
| 102 | $handlerObject = $this->createMock(HandlerInterface::class); |
||
| 103 | |||
| 104 | if (is_string($handler)) { |
||
| 105 | $getArgs[] = [$this->container, $handler, $serviceName]; |
||
| 106 | $getReturns[] = $handlerObject; |
||
| 107 | } elseif (is_array($handler)) { |
||
| 108 | /** @var HandlerInterface&MockObject $handlerObject */ |
||
| 109 | $handlerObject = $this->createMock(HandlerInterface::class); |
||
| 110 | $buildArgs[] = [$this->container, $handlerName, $handler, $serviceName]; |
||
| 111 | $buildReturns[] = $handlerObject; |
||
| 112 | } |
||
| 113 | $handlers[] = $handlerObject; |
||
| 114 | } |
||
| 115 | |||
| 116 | if (!empty($getArgs)) { |
||
| 117 | $factory->expects($this->exactly(count($getArgs))) |
||
|
|
|||
| 118 | ->method('getService') |
||
| 119 | ->withConsecutive(...$getArgs) |
||
| 120 | ->willReturnOnConsecutiveCalls(...$getReturns); |
||
| 121 | } |
||
| 122 | |||
| 123 | if (!empty($buildArgs)) { |
||
| 124 | $factory->expects($this->exactly(count($buildArgs))) |
||
| 125 | ->method('buildService') |
||
| 126 | ->withConsecutive(...$buildArgs) |
||
| 127 | ->willReturnOnConsecutiveCalls(...$buildReturns); |
||
| 128 | } |
||
| 129 | |||
| 130 | $logger = $factory($this->container, $serviceName, ['handlers' => $handlerConfigs]); |
||
| 131 | |||
| 132 | $this->assertSame($handlers, $logger->getHandlers()); |
||
| 133 | } |
||
| 169 |