| Conditions | 1 |
| Paths | 1 |
| Total Lines | 90 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 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 |
||
| 102 | public function testProcessSwagger() |
||
| 103 | { |
||
| 104 | $content = file_get_contents(dirname(__FILE__).'/../../../resources/simple-swagger.json'); |
||
| 105 | $swagger = json_decode($content); |
||
| 106 | $orderPath = '/order/{orderId}'; |
||
| 107 | |||
| 108 | $swaggerParserMock = $this->getMockBuilder('\Swagger\Document') |
||
| 109 | ->disableOriginalConstructor() |
||
| 110 | ->setMethods(['getBasePath', 'setDocument', 'getOperationsById']) |
||
| 111 | ->getMock(); |
||
| 112 | |||
| 113 | $response = $swagger->paths->$orderPath->get->responses; |
||
|
|
|||
| 114 | |||
| 115 | $responseMock = $this->getMockBuilder('\Swagger\Object\Responses') |
||
| 116 | ->disableOriginalConstructor() |
||
| 117 | ->setMethods(['getHttpStatusCode']) |
||
| 118 | ->getMock(); |
||
| 119 | $responseMock->expects($this->once()) |
||
| 120 | ->method('getHttpStatusCode'); |
||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | $operationMock = $this->getMockBuilder('\Swagger\Object\Operation') |
||
| 125 | ->disableOriginalConstructor() |
||
| 126 | ->setMethods(['getDocumentObjectProperty', 'getResponses']) |
||
| 127 | ->getMock(); |
||
| 128 | $operationMock->expects($this->once()) |
||
| 129 | ->method('getDocumentObjectProperty'); |
||
| 130 | $operationMock->expects($this->once()) |
||
| 131 | ->method('getResponses') |
||
| 132 | ->willReturn($responseMock); |
||
| 133 | |||
| 134 | |||
| 135 | $serviceMock = $this->getMockBuilder('\Swagger\OperationReference') |
||
| 136 | ->disableOriginalConstructor() |
||
| 137 | ->setMethods(['getPath', 'getMethod', 'getOperation']) |
||
| 138 | ->getMock(); |
||
| 139 | $serviceMock->expects($this->exactly(3)) |
||
| 140 | ->method('getPath') |
||
| 141 | ->will($this->onConsecutiveCalls('/user', '/user', '/order/{orderId}')); |
||
| 142 | $serviceMock->expects($this->exactly(5)) |
||
| 143 | ->method('getMethod') |
||
| 144 | ->will($this->onConsecutiveCalls('POST', 'DELETE', 'GET', 'POST', 'GET')); |
||
| 145 | $serviceMock->expects($this->exactly(2)) |
||
| 146 | ->method('getOperation') |
||
| 147 | ->willReturn($operationMock); |
||
| 148 | |||
| 149 | |||
| 150 | $operations = [$serviceMock, $serviceMock, $serviceMock]; |
||
| 151 | |||
| 152 | $swaggerParserMock |
||
| 153 | ->expects($this->once()) |
||
| 154 | ->method('getOperationsById') |
||
| 155 | ->willReturn($operations); |
||
| 156 | $this->sut = new SwaggerStrategy($swaggerParserMock); |
||
| 157 | |||
| 158 | $this->sut->process($content, array('host' => 'localhost')); |
||
| 159 | /*$this->callProcessMethod(0); |
||
| 160 | |||
| 161 | $schema = array(); |
||
| 162 | $schema['$ref'] = '#/definitions/Person'; |
||
| 163 | |||
| 164 | $customer = array(); |
||
| 165 | $otherParam = array('in' => 'blub'); |
||
| 166 | $customer['post']['parameters'][] = $otherParam; |
||
| 167 | $customer['get']['responses']['200']['schema'] = $schema; |
||
| 168 | $customerPath = '/person/customer'; |
||
| 169 | $this->swagger->paths->$customerPath = (object) $customer; |
||
| 170 | |||
| 171 | $bodyParam = array('in' => 'body', 'schema' => $schema); |
||
| 172 | $consultant = array(); |
||
| 173 | $consultant['get']['responses']['400'] = new \stdClass(); |
||
| 174 | $consultant['post']['parameters'][] = $bodyParam; |
||
| 175 | $consultantPath = '/person/consultant'; |
||
| 176 | $consultantPathWithId = '/person/consultant/{id}'; |
||
| 177 | $this->swagger->paths->$consultantPath = (object) $consultant; |
||
| 178 | $this->swagger->paths->$consultantPathWithId = (object) $consultant; |
||
| 179 | |||
| 180 | $person = new \stdClass(); |
||
| 181 | $person->type = "object"; |
||
| 182 | $person->properties = new \stdClass(); |
||
| 183 | $person->properties->id = new \stdClass(); |
||
| 184 | $person->properties->name = new \stdClass(); |
||
| 185 | $this->swagger->definitions->Person = $person; |
||
| 186 | |||
| 187 | $apiDefinition = $this->callProcessMethod(2); |
||
| 188 | foreach ($apiDefinition->getEndpoints(false) as $endpoint) { |
||
| 189 | $this->assertEquals($person, $apiDefinition->getSchema($endpoint)); |
||
| 190 | }*/ |
||
| 191 | } |
||
| 192 | |||
| 238 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.