Completed
Push — master ( 036c01...8ec94e )
by Nils
02:11
created

CountRule::validate()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.439
cc 5
eloc 16
nc 4
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Cookies;
4
5
use phm\HttpWebdriverClient\Http\Response\CookieAwareResponse;
6
use phm\HttpWebdriverClient\Http\Response\TimeoutAwareResponse;
7
use Psr\Http\Message\ResponseInterface;
8
use whm\Smoke\Rules\Attribute;
9
use whm\Smoke\Rules\CheckResult;
10
use whm\Smoke\Rules\Rule;
11
12
/**
13
 * This rule can validate if a http request takes longer than a given max duration.
14
 * A website that is slower than one second is considered as slow.
15
 */
16
class CountRule implements Rule
17
{
18
    private $maxCookies;
19
20
    /**
21
     * @param int $maxDuration The maximum duration a http call is allowed to take (time to first byte)
0 ignored issues
show
Bug introduced by
There is no parameter named $maxDuration. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
22
     */
23
    public function init($maxCookies = 20)
24
    {
25
        $this->maxCookies = $maxCookies;
26
    }
27
28
    public function validate(ResponseInterface $response)
29
    {
30
        if ($response instanceof CookieAwareResponse) {
0 ignored issues
show
Bug introduced by
The class phm\HttpWebdriverClient\...nse\CookieAwareResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
31
32
            if (!$response instanceof TimeoutAwareResponse || !$response->isTimeout()) {
0 ignored issues
show
Bug introduced by
The class phm\HttpWebdriverClient\...se\TimeoutAwareResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
33
34
                $cookieCount = $response->getCookieCount();
35
36
                if ($cookieCount > $this->maxCookies) {
37
                    $result = new CheckResult(
38
                        CheckResult::STATUS_FAILURE,
39
                        $cookieCount . ' cookies were stored (limit was ' . $this->maxCookies . ').',
40
                        $cookieCount);
41
                } else {
42
                    $result = new CheckResult(
43
                        CheckResult::STATUS_SUCCESS,
44
                        $cookieCount . ' cookies were stored (limit was ' . $this->maxCookies . ').',
45
                        $cookieCount);
46
                }
47
48
                $result->addAttribute(new Attribute(' cookies', $response->getCookies(), true));
49
50
                return $result;
51
            }
52
        }
53
    }
54
}
55