RegExExistsRule::doValidation()   B
last analyzed

Complexity

Conditions 7
Paths 15

Size

Total Lines 30

Duplication

Lines 8
Ratio 26.67 %

Importance

Changes 0
Metric Value
dl 8
loc 30
rs 8.5066
c 0
b 0
f 0
cc 7
nc 15
nop 1
1
<?php
2
3
namespace whm\Smoke\Rules\Html;
4
5
use Psr\Http\Message\ResponseInterface;
6
use whm\Smoke\Rules\StandardRule;
7
use whm\Smoke\Rules\ValidationFailedException;
8
9
/**
10
 * This rule will analyze any html document and checks if a given string is contained.
11
 */
12
class RegExExistsRule extends StandardRule
13
{
14
    private $regExs;
15
16
    protected $contentTypes = array('text/html', 'application/json', 'application/xml');
17
18
    /**
19
     * @param int $string The string that the document must contain
0 ignored issues
show
Bug introduced by
There is no parameter named $string. 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...
20
     */
21
    public function init(array $regExs)
22
    {
23
        $regExArray = array();
24
25
        foreach ($regExs as $regEx) {
26
            if (array_key_exists('regex', $regEx)) {
27
                $isRegex = $regEx['isRegex'] == 'on';
28
                $regExArray[] = ['pattern' => $regEx['regex'], 'isRegEx' => $isRegex];
29
            } else {
30
                $regExArray[] = $regEx;
31
            }
32
        }
33
34
        $this->regExs = $regExArray;
35
    }
36
37
    /**
38
     * @param ResponseInterface $response
39
     * @throws ValidationFailedException
40
     */
41
    protected function doValidation(ResponseInterface $response)
42
    {
43
        $errors = [];
44
45
        foreach ($this->regExs as $regEx) {
46
            if ($regEx['isRegEx']) {
47
                $pattern = str_replace('', '\\~', $regEx['pattern']);
48
49 View Code Duplication
                if (preg_match('~' . $pattern . '~', (string)$response->getBody()) === 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
                    $errors[] = 'Regular expression: ' . $regEx['pattern'];
51
                }
52 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
                if (preg_match('^' . preg_quote($regEx['pattern']) . '^', (string)$response->getBody()) === 0) {
54
                    $errors[] = 'Text: ' . $regEx['pattern'];
55
                }
56
            }
57
        }
58
59
        if (count($errors) > 0) {
60
            $errorString = 'The following text elements were not found: <ul>';
61
62
            foreach ($errors as $error) {
63
                $errorString .= '<li>' . $error . '</li>';
64
            }
65
66
            $errorString .= '</ul>';
67
68
            throw new ValidationFailedException($errorString);
69
        }
70
    }
71
}
72