Completed
Push — master ( 798367...1cc311 )
by Nils
02:59
created

XmlCheckRule   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 6
dl 0
loc 46
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
B doValidation() 0 32 7
1
<?php
2
3
namespace whm\Smoke\Rules\Xml;
4
5
use phm\HttpWebdriverClient\Http\Response\DomAwareResponse;
6
use phm\HttpWebdriverClient\Http\Response\TimeoutAwareResponse;
7
use Psr\Http\Message\ResponseInterface;
8
use whm\Smoke\Rules\CheckResult;
9
use whm\Smoke\Rules\StandardRule;
10
use whm\Smoke\Rules\ValidationFailedException;
11
12
/**
13
 * This rule checks if the found XML is well-formed.
14
 */
15
class XmlCheckRule extends StandardRule
16
{
17
    protected $contentTypes = array('text/xml', 'application/xml');
18
19
    public function init()
20
    {
21
22
    }
23
24
    /**
25
     * @param ResponseInterface $response
26
     * @throws ValidationFailedException
27
     */
28
    public function doValidation(ResponseInterface $response)
29
    {
30
        if ($response instanceof DomAwareResponse) {
31
            $body = (string)$response->getHtmlBody();
32
        } else {
33
            $body = (string)$response->getBody();
34
        }
35
36
        if ($body == "") {
37
            if ($response instanceof TimeoutAwareResponse) {
38
                if ($response->isTimeout()) {
39
                    return new CheckResult(CheckResult::STATUS_FAILURE, 'The request timed out and produced an empty XML document.');
40
                }
41
            }
42
            return new CheckResult(CheckResult::STATUS_FAILURE, 'The given XML document was empty.');
43
        }
44
45
        $domDocument = new \DOMDocument();
46
        $success = @$domDocument->loadXML($body);
47
48
        if (!$success) {
49
            $lastError = libxml_get_last_error();
50
51
            if ($lastError) {
52
                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\...esponse\BrowserResponse, whm\Smoke\Http\ConnectionRefusedResponse, whm\Smoke\Http\ErrorResponse.

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...
53
                    str_replace("\n", '', $lastError->message) . ').');
54
            } else {
55
                return new CheckResult(CheckResult::STATUS_FAILURE, 'Unknown error occured.');
56
            }
57
58
        }
59
    }
60
}
61