Completed
Push — master ( 9ad1a6...571f4d )
by Nils
02:31
created

BigContentRule::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Html;
4
5
use phm\HttpWebdriverClient\Http\Response\ResourcesAwareResponse;
6
use Psr\Http\Message\ResponseInterface;
7
use whm\Smoke\Rules\Attribute;
8
use whm\Smoke\Rules\CheckResult;
9
use whm\Smoke\Rules\StandardRule;
10
11
/**
12
 * This rule calculates the size of a html document. If the document is bigger than a given value
13
 * the test will fail.
14
 */
15
class BigContentRule extends StandardRule
16
{
17
    private $maxSize;
18
19
    protected $contentTypes = array('text/html');
20
21
    public function init($maxSize = 400)
22
    {
23
        $this->maxSize = $maxSize;
24
    }
25
26
    protected function doValidation(ResponseInterface $response)
27
    {
28
        $totalSize = 0;
29
30
        if ($response instanceof ResourcesAwareResponse) {
31
            foreach ($response->getResources() as $resource) {
32
                $resourceSize = round($resource['transferSize'] / 1000);
33
34
                $totalSize += $resourceSize;
35
            }
36
        }
37
38
        if ($totalSize > $this->maxSize) {
39
            $message = 'The total size of the file (and all assets) was ' . $totalSize . ' KB (max: ' . $this->maxSize . ' KB).';
40
            $result = new CheckResult(CheckResult::STATUS_FAILURE, $message, $totalSize);
41
            $result->addAttribute(new Attribute('resources', $response->getResources(), true));
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 getResources() does only exist in the following implementations of said interface: phm\HttpWebdriverClient\...t\Chrome\ChromeResponse.

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...
42
            return $result;
43
        } else {
44
            $message = 'The total size of the file (and all assets) was ' . $totalSize . ' KB (max: ' . $this->maxSize . ' KB).';
45
            $result = new CheckResult(CheckResult::STATUS_SUCCESS, $message, $totalSize);
46
            $result->addAttribute(new Attribute('resources', $response->getResources(), true));
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 getResources() does only exist in the following implementations of said interface: phm\HttpWebdriverClient\...t\Chrome\ChromeResponse.

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...
47
            return $result;
48
        }
49
    }
50
}
51