Passed
Pull Request — master (#32)
by Baptiste
02:27
created

JsonMatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
namespace Behapi\Context;
3
4
use InvalidArgumentException;
5
6
use Behat\Behat\Context\Context;
7
use Behat\Gherkin\Node\PyStringNode;
8
9
use Symfony\Component\PropertyAccessor\PropertyAccess;
10
11
use Behapi\Tools\HttpHistory;
12
use Behapi\Tools\BehapiFactory;
13
14
class JsonMatcher extends Json
15
{
16
    /** @var JsonFactory */
17
    private $factory;
18
19
    public function __construct(HttpHistory $history)
20
    {
21
        parent::__construct($history);
22
23
        $this->factory = new BehapiFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Behapi\Tools\BehapiFactory() of type object<Behapi\Tools\BehapiFactory> is incompatible with the declared type object<Behapi\Context\JsonFactory> of property $factory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
24
    }
25
26
    /** @Then the root should match: */
27 View Code Duplication
    public function root_should_match(PyStringNode $pattern)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
28
    {
29
        $matcher = $this->factory->createMatcher();
30
        $body = (string) $this->getResponse()->getBody();
31
32
        if ($matcher->match($body, $pattern->getRaw())) {
33
            return;
34
        }
35
36
        throw new InvalidArgumentException(
37
            sprintf(
38
                'The json root does not match with the given pattern (error : %s)',
39
                $matcher->getError()
40
            )
41
        );
42
    }
43
44
    /** @Then in the json, :path should match: */
45 View Code Duplication
    public function path_should_match(string $path, PyStringNode $pattern)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
46
    {
47
        $value = $this->getValue($path);
48
        $matcher = $this->factory->createMatcher();
49
50
        $json = json_encode($value);
0 ignored issues
show
Unused Code introduced by
$json is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
52
        if ($matcher->match($value, $pattern->getRaw())) {
53
            return;
54
        }
55
56
        throw new InvalidArgumentException(
57
            sprintf(
58
                'The json path "%s" does not match with the given pattern (error : %s)',
59
                $path,
60
                $matcher->getError()
61
            )
62
        );
63
    }
64
65
    /** @Then in the json, :path should not match: */
66 View Code Duplication
    public function path_should_not_match(string $path, PyStringNode $pattern)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
67
    {
68
        $value = $this->getValue($path);
69
        $matcher = $this->factory->createMatcher();
70
71
        $json = json_encode($value);
0 ignored issues
show
Unused Code introduced by
$json is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
72
73
        if (!$matcher->match($value, $pattern->getRaw())) {
74
            return;
75
        }
76
77
        throw new InvalidArgumentException(
78
            sprintf(
79
                'The json path "%s" matches with the given pattern (error : %s)',
80
                $path,
81
                $matcher->getError()
82
            )
83
        );
84
    }
85
}
86