Completed
Push — master ( 92cc5a...52da16 )
by Nils
11:02
created

XmlCheckRule::doValidation()   C

Complexity

Conditions 7
Paths 12

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 1
Metric Value
dl 0
loc 32
rs 6.7272
c 4
b 1
f 1
cc 7
eloc 19
nc 12
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Xml;
4
5
use phm\HttpWebdriverClient\Http\Request\TimeoutAwareRequest;
6
use phm\HttpWebdriverClient\Http\Response\DomAwareResponse;
7
use phm\HttpWebdriverClient\Http\Response\TimeoutAwareResponse;
8
use Psr\Http\Message\ResponseInterface;
9
use whm\Smoke\Rules\CheckResult;
10
use whm\Smoke\Rules\StandardRule;
11
use whm\Smoke\Rules\ValidationFailedException;
12
13
/**
14
 * This rule checks if the found XML is well-formed.
15
 */
16
class XmlCheckRule extends StandardRule
17
{
18
    protected $contentTypes = array('text/xml', 'application/xml');
19
20
    /**
21
     * @param ResponseInterface $response
22
     * @throws ValidationFailedException
23
     */
24
    public function doValidation(ResponseInterface $response)
25
    {
26
        if ($response instanceof DomAwareResponse) {
27
            $body = (string)$response->getHtmlBody();
28
        } else {
29
            $body = (string)$response->getBody();
30
        }
31
32
        if ($body == "") {
33
            if ($response instanceof TimeoutAwareResponse) {
34
                if ($response->isTimeout()) {
35
                    return new CheckResult(CheckResult::STATUS_FAILURE, 'The request timed out and produced an empty XML document.');
36
                }
37
            }
38
            return new CheckResult(CheckResult::STATUS_FAILURE, 'The given XML document was empty.');
39
        }
40
41
        $domDocument = new \DOMDocument();
42
        $success = @$domDocument->loadXML($body);
43
44
        if (!$success) {
45
            $lastError = libxml_get_last_error();
46
47
            if ($lastError) {
48
                throw new ValidationFailedException('The xml file ' . $response->getUri() . ' is not well formed (last error: ' .
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Http\Message\ResponseInterface as the method getUri() does only exist in the following implementations of said interface: phm\HttpWebdriverClient\...t\Chrome\ChromeResponse, phm\HttpWebdriverClient\...t\Guzzle\GuzzleResponse, phm\HttpWebdriverClient\...\Client\Guzzle\Response, phm\HttpWebdriverClient\...\HeadlessChromeResponse, phm\HttpWebdriverClient\...esponse\BrowserResponse.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
49
                    str_replace("\n", '', $lastError->message) . ').');
50
            } else {
51
                return new CheckResult(CheckResult::STATUS_FAILURE, 'Unknown error occured.');
52
            }
53
54
        }
55
    }
56
}
57