| Conditions | 4 |
| Paths | 14 |
| Total Lines | 124 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 95 | public function testCall() { |
||
| 96 | |||
| 97 | $obj = new CURLGetRequest($this->configuration, self::RESOURCE_PATH); |
||
| 98 | |||
| 99 | /* === Allow encoding ============================================== */ |
||
| 100 | $obj->getConfiguration()->setAllowEncoding(true); |
||
| 101 | |||
| 102 | $resAE = $obj->call(); |
||
| 103 | |||
| 104 | $this->assertEquals(CURLGetRequest::METHOD_GET, json_decode($resAE->getResponseBody(), true)["method"]); |
||
| 105 | $this->assertEquals(200, $resAE->getResponseInfo()["http_code"]); |
||
| 106 | |||
| 107 | $obj->getConfiguration()->setAllowEncoding(false); |
||
| 108 | |||
| 109 | /* === Connect timeout ============================================= */ |
||
| 110 | $obj->getConfiguration()->setConnectTimeout(30); |
||
| 111 | |||
| 112 | $resCT = $obj->call(); |
||
| 113 | |||
| 114 | $this->assertEquals(CURLGetRequest::METHOD_GET, json_decode($resCT->getResponseBody(), true)["method"]); |
||
| 115 | $this->assertEquals(200, $resCT->getResponseInfo()["http_code"]); |
||
| 116 | |||
| 117 | $obj->getConfiguration()->setConnectTimeout(0); |
||
| 118 | |||
| 119 | /* === Debug ======================================================= */ |
||
| 120 | $obj->getConfiguration()->setDebug(true); |
||
| 121 | |||
| 122 | $resD = $obj->call(); |
||
| 123 | |||
| 124 | $this->assertEquals(CURLGetRequest::METHOD_GET, json_decode($resD->getResponseBody(), true)["method"]); |
||
| 125 | $this->assertEquals(200, $resD->getResponseInfo()["http_code"]); |
||
| 126 | |||
| 127 | $obj->getConfiguration()->setDebug(false); |
||
| 128 | |||
| 129 | /* === Headers ===================================================== */ |
||
| 130 | $obj->addHeader("h", "v"); |
||
| 131 | |||
| 132 | $resH = $obj->call(); |
||
| 133 | |||
| 134 | $this->assertContains("h: v", $resH->getRequestHeader()); |
||
| 135 | $this->assertEquals(CURLGetRequest::METHOD_GET, json_decode($resH->getResponseBody(), true)["method"]); |
||
| 136 | $this->assertEquals(200, $resH->getResponseInfo()["http_code"]); |
||
| 137 | |||
| 138 | $obj->removeHeader("h"); |
||
| 139 | |||
| 140 | /* === JSON ======================================================== */ |
||
| 141 | $obj->addHeader("Content-Type", "application/json"); |
||
| 142 | $obj->addPostData("name", "value"); |
||
| 143 | |||
| 144 | $resJSON = $obj->call(); |
||
| 145 | |||
| 146 | $this->assertContains("Content-Type: application/json", $resJSON->getRequestHeader()); |
||
| 147 | $this->assertContains('{"name":"value"}', $resJSON->getRequestBody()); |
||
| 148 | $this->assertEquals(CURLGetRequest::METHOD_GET, json_decode($resH->getResponseBody(), true)["method"]); |
||
| 149 | $this->assertEquals(200, $resH->getResponseInfo()["http_code"]); |
||
| 150 | |||
| 151 | $obj->removeHeader("Content-Type"); |
||
| 152 | $obj->removePostData("name"); |
||
| 153 | |||
| 154 | /* === Request timeout ============================================= */ |
||
| 155 | $obj->getConfiguration()->setRequestTimeout(30); |
||
| 156 | |||
| 157 | $resRT = $obj->call(); |
||
| 158 | |||
| 159 | $this->assertEquals(CURLGetRequest::METHOD_GET, json_decode($resRT->getResponseBody(), true)["method"]); |
||
| 160 | $this->assertEquals(200, $resRT->getResponseInfo()["http_code"]); |
||
| 161 | |||
| 162 | $obj->getConfiguration()->setRequestTimeout(0); |
||
| 163 | |||
| 164 | /* === SSL verification ============================================ */ |
||
| 165 | $obj->getConfiguration()->setSslVerification(false); |
||
| 166 | |||
| 167 | $resSSL = $obj->call(); |
||
| 168 | |||
| 169 | $this->assertEquals(CURLGetRequest::METHOD_GET, json_decode($resSSL->getResponseBody(), true)["method"]); |
||
| 170 | $this->assertEquals(200, $resSSL->getResponseInfo()["http_code"]); |
||
| 171 | |||
| 172 | $obj->getConfiguration()->setSslVerification(true); |
||
| 173 | |||
| 174 | /* === Verbose ===================================================== */ |
||
| 175 | $obj->getConfiguration()->setVerbose(true); |
||
| 176 | |||
| 177 | $resV = $obj->call(); |
||
| 178 | |||
| 179 | $this->assertEquals(CURLGetRequest::METHOD_GET, json_decode($resV->getResponseBody(), true)["method"]); |
||
| 180 | $this->assertEquals(200, $resV->getResponseInfo()["http_code"]); |
||
| 181 | |||
| 182 | $obj->getConfiguration()->setVerbose(false); |
||
| 183 | /* === HTTP code 0 ================================================= */ |
||
| 184 | $obj->getConfiguration()->setRequestTimeout(10); |
||
| 185 | $obj->addQueryData("sleep", 60); |
||
| 186 | |||
| 187 | try { |
||
| 188 | $obj->call(); |
||
| 189 | } catch (Exception $ex) { |
||
| 190 | |||
| 191 | $this->assertInstanceOf(CURLRequestCallException::class, $ex); |
||
| 192 | $this->assertContains("Call to ", $ex->getMessage()); |
||
| 193 | $this->assertEquals(0, $ex->getResponse()->getResponseInfo()["http_code"]); |
||
|
1 ignored issue
–
show
|
|||
| 194 | } |
||
| 195 | |||
| 196 | $obj->getConfiguration()->setRequestTimeout(0); |
||
| 197 | $obj->removeQueryData("sleep"); |
||
| 198 | |||
| 199 | /* === HTTP codes ================================================== */ |
||
| 200 | foreach (HTTPUtility::getCodes() as $code) { |
||
| 201 | try { |
||
| 202 | |||
| 203 | $obj->addQueryData("code", $code); |
||
| 204 | |||
| 205 | $rslt = $obj->call(); |
||
| 206 | |||
| 207 | $this->assertEquals($code, $rslt->getResponseInfo()["http_code"], "The method getResponseInfo() does not return the expected value with code " . $code); |
||
| 208 | $this->assertGreaterThanOrEqual(200, $rslt->getResponseInfo()["http_code"]); |
||
| 209 | $this->assertLessThanOrEqual(299, $rslt->getResponseInfo()["http_code"]); |
||
| 210 | } catch (Exception $ex) { |
||
| 211 | |||
| 212 | $this->assertInstanceOf(CURLRequestCallException::class, $ex); |
||
| 213 | $this->assertEquals($code, $ex->getResponse()->getResponseInfo()["http_code"], "The method getResponseInfo() does not return the expected value with code " . $code); |
||
| 214 | } |
||
| 215 | } |
||
| 216 | |||
| 217 | /* ================================================================= */ |
||
| 218 | } |
||
| 219 | |||
| 291 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: