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 |
||
| 27 | class ProfileClientTest extends TestCase |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var Collector |
||
| 31 | */ |
||
| 32 | private $collector; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var Stack |
||
| 36 | */ |
||
| 37 | private $activeStack; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var HttpClient|MockObject |
||
| 41 | */ |
||
| 42 | private $client; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var RequestInterface |
||
| 46 | */ |
||
| 47 | private $request; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var Formatter|MockObject |
||
| 51 | */ |
||
| 52 | private $formatter; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var Stopwatch |
||
| 56 | */ |
||
| 57 | private $stopwatch; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var StopwatchEvent |
||
| 61 | */ |
||
| 62 | private $stopwatchEvent; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var ProfileClient |
||
| 66 | */ |
||
| 67 | private $subject; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var ResponseInterface |
||
| 71 | */ |
||
| 72 | private $response; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var Promise |
||
| 76 | */ |
||
| 77 | private $fulfilledPromise; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var \Exception |
||
| 81 | */ |
||
| 82 | private $exception; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var RejectedPromise |
||
| 86 | */ |
||
| 87 | private $rejectedPromise; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var UriInterface |
||
| 91 | */ |
||
| 92 | private $uri; |
||
| 93 | |||
| 94 | public function setUp(): void |
||
| 95 | { |
||
| 96 | $this->collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock(); |
||
|
|
|||
| 97 | $this->activeStack = new Stack('default', 'FormattedRequest'); |
||
| 98 | $this->client = $this->getMockBuilder(ClientInterface::class)->getMock(); |
||
| 99 | $this->uri = new Uri('https://example.com/target'); |
||
| 100 | $this->request = new Request('GET', $this->uri); |
||
| 101 | $this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock(); |
||
| 102 | $this->stopwatch = $this->getMockBuilder(Stopwatch::class)->disableOriginalConstructor()->getMock(); |
||
| 103 | $this->stopwatchEvent = $this->getMockBuilder(StopwatchEvent::class)->disableOriginalConstructor()->getMock(); |
||
| 104 | $this->subject = new ProfileClient($this->client, $this->collector, $this->formatter, $this->stopwatch); |
||
| 105 | $this->response = new Response(); |
||
| 106 | $this->exception = new \Exception(); |
||
| 107 | $this->fulfilledPromise = new FulfilledPromise($this->response); |
||
| 108 | $this->rejectedPromise = new RejectedPromise($this->exception); |
||
| 109 | |||
| 110 | $this->collector->method('getActiveStack')->willReturn($this->activeStack); |
||
| 111 | $this->formatter |
||
| 112 | ->method('formatResponse') |
||
| 113 | ->with($this->response) |
||
| 114 | ->willReturn('FormattedResponse') |
||
| 115 | ; |
||
| 116 | |||
| 117 | $this->stopwatch |
||
| 118 | ->method('start') |
||
| 119 | ->willReturn($this->stopwatchEvent) |
||
| 120 | ; |
||
| 121 | |||
| 122 | $this->stopwatchEvent |
||
| 123 | ->method('getDuration') |
||
| 124 | ->willReturn(42) |
||
| 125 | ; |
||
| 126 | } |
||
| 127 | |||
| 128 | public function testSendRequest(): void |
||
| 129 | { |
||
| 130 | $this->client |
||
| 131 | ->expects($this->once()) |
||
| 132 | ->method('sendRequest') |
||
| 133 | ->with($this->identicalTo($this->request)) |
||
| 134 | ->willReturn($this->response) |
||
| 135 | ; |
||
| 136 | |||
| 137 | $response = $this->subject->sendRequest($this->request); |
||
| 138 | |||
| 139 | $this->assertEquals($this->response, $response); |
||
| 140 | $this->assertEquals('GET', $this->activeStack->getRequestMethod()); |
||
| 141 | $this->assertEquals('/target', $this->activeStack->getRequestTarget()); |
||
| 142 | $this->assertEquals('example.com', $this->activeStack->getRequestHost()); |
||
| 143 | $this->assertEquals('https', $this->activeStack->getRequestScheme()); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function testSendRequestTypeError() |
||
| 147 | { |
||
| 148 | $this->client |
||
| 149 | ->expects($this->once()) |
||
| 150 | ->method('sendRequest') |
||
| 151 | ->willReturnCallback(function () { |
||
| 152 | throw new \Error('You set string to int prop'); |
||
| 153 | }); |
||
| 154 | $this->formatter |
||
| 155 | ->expects($this->once()) |
||
| 156 | ->method('formatException') |
||
| 157 | ->with($this->isInstanceOf(\Error::class)); |
||
| 158 | |||
| 159 | $this->expectException(\Error::class); |
||
| 160 | $this->expectExceptionMessage('You set string to int prop'); |
||
| 161 | $this->subject->sendRequest($this->request); |
||
| 162 | } |
||
| 163 | |||
| 164 | public function testSendAsyncRequest(): void |
||
| 186 | |||
| 187 | View Code Duplication | public function testOnFulfilled(): void |
|
| 211 | |||
| 212 | View Code Duplication | public function testOnRejected(): void |
|
| 241 | } |
||
| 242 | |||
| 243 | interface ClientInterface extends HttpClient, HttpAsyncClient |
||
| 244 | { |
||
| 246 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..