| Conditions | 10 |
| Paths | 25 |
| Total Lines | 40 |
| Code Lines | 24 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 31 | public function validate(ResponseInterface $response) |
||
| 32 | { |
||
| 33 | $uri = $response->getUri(); |
||
|
|
|||
| 34 | |||
| 35 | if ('https' !== $uri->getScheme()) { |
||
| 36 | return; |
||
| 37 | } |
||
| 38 | |||
| 39 | $htmlDocument = new Document((string)$response->getBody()); |
||
| 40 | |||
| 41 | $resources = $htmlDocument->getDependencies($uri, false); |
||
| 42 | |||
| 43 | $unsecures = array(); |
||
| 44 | |||
| 45 | foreach ($resources as $resource) { |
||
| 46 | if ($resource->getScheme() && 'https' !== $resource->getScheme()) { |
||
| 47 | $excluded = false; |
||
| 48 | foreach ($this->excludedFiles as $excludedFile) { |
||
| 49 | if (preg_match('*' . $excludedFile . '*', (string)$resource)) { |
||
| 50 | $excluded = true; |
||
| 51 | break; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | if (!$excluded) { |
||
| 55 | $unsecures[] = $resource; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | if (count($unsecures) > 0) { |
||
| 61 | $message = 'At least one dependency was found on a secure url, that was transfered insecure.<ul>'; |
||
| 62 | foreach ($unsecures as $unsecure) { |
||
| 63 | $message .= '<li>' . (string)$unsecure . '</li>'; |
||
| 64 | } |
||
| 65 | $message .= '</ul>'; |
||
| 66 | return new CheckResult(CheckResult::STATUS_FAILURE, $message, count($unsecures)); |
||
| 67 | } else { |
||
| 68 | return new CheckResult(CheckResult::STATUS_SUCCESS, 'No http element on that https url found.', 0); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | } |
||
| 72 |
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 implementation 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 interface: