BigFilesRule   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 4 1
B doValidation() 0 25 6
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 BigFilesRule extends StandardRule
16
{
17
    private $maxElementSize;
18
19
    protected $contentTypes = array('text/html');
20
21
    public function init($maxSize = 400)
22
    {
23
        $this->maxElementSize = $maxSize;
24
    }
25
26
    protected function doValidation(ResponseInterface $response)
27
    {
28
        $bigFiles = [];
29
30
        if ($response instanceof ResourcesAwareResponse) {
31
            foreach ($response->getResources() as $resource) {
32
                $resourceSize = round($resource['transferSize'] / 1000);
33
34
                if ($resourceSize > $this->maxElementSize) {
35
                    $bigFiles[] = ['name' => $resource['name'], 'size' => $resourceSize];
36
                }
37
            }
38
        }
39
40
        if (count($bigFiles) > 0) {
41
            $message = "Some files were found that are too big (max: " . $this->maxElementSize . " KB):<ul>";
42
            foreach ($bigFiles as $bigFile) {
43
                $message .= '<li>File: ' . $bigFile['name'] . ', Size: ' . $bigFile['size'] . ' KB</li>';
44
            }
45
            $message .= "</ul>";
46
            $result = new CheckResult(CheckResult::STATUS_FAILURE, $message, count($bigFiles));
47
            $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, phm\HttpWebdriverClient\...t\Guzzle\GuzzleResponse, 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...
48
            return $result;
49
        }
50
    }
51
}
52