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

PhpMatcher   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 78
Duplicated Lines 71.79 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 56
loc 78
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A response_content_should_match() 17 17 2
A response_content_should_not_match() 17 17 2
A response_content_should_match_PyString() 11 11 2
A response_content_should_not_match_PyString() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Behapi\Context;
3
4
use Behat\Behat\Context\Context;
5
use Behat\Gherkin\Node\PyStringNode;
6
7
use Coduo\PHPMatcher\Factory;
8
use Coduo\PHPMatcher\Factory\SimpleFactory;
9
10
use Behapi\Tools\HttpHistory;
11
12
class PhpMatcher implements Context
13
{
14
    use ApiTrait;
15
16
    /** @var Factory */
17
    private $factory;
18
19
    public function __construct(HttpHistory $history)
20
    {
21
        $this->history = $history;
22
23
        $this->factory = new SimpleFactory;
24
    }
25
26
    /** @Then the response content should match :pattern */
27 View Code Duplication
    public function response_content_should_match(string $pattern)
28
    {
29
        $matcher = $this->factory->createMatcher();
30
        $content = (string) $this->getResponse()->getBody();
31
32
        if ($matcher->match($content, $pattern)) {
33
            return;
34
        }
35
36
        throw new InvalidArgumentException(
37
            sprintf(
38
                'The response does not match with the pattern "%s" (error : %s)',
39
                $pattern,
40
                $matcher->getError()
41
            )
42
        );
43
    }
44
45
    /** @Then the response content should not match :pattern */
46 View Code Duplication
    public function response_content_should_not_match(string $pattern)
47
    {
48
        $matcher = $this->factory->createMatcher();
49
        $content = (string) $this->getResponse()->getBody();
50
51
        if (!$matcher->match($content, $pattern)) {
52
            return;
53
        }
54
55
        throw new InvalidArgumentException(
56
            sprintf(
57
                'The response matches with the pattern "%s" (error : %s)',
58
                $pattern,
59
                $matcher->getError()
60
            )
61
        );
62
    }
63
64
    /** @Then the response content should match: */
65 View Code Duplication
    public function response_content_should_match_PyString(PyStringNode $pattern)
66
    {
67
        $matcher = $this->factory->createMatcher();
68
        $content = (string) $this->getResponse()->getBody();
69
70
        if ($matcher->match($content, $pattern)) {
71
            return;
72
        }
73
74
        throw new InvalidArgumentException($matcher->getError());
75
    }
76
77
    /** @Then the response content should match: */
78 View Code Duplication
    public function response_content_should_not_match_PyString(PyStringNode $pattern)
79
    {
80
        $matcher = $this->factory->createMatcher();
81
        $content = (string) $this->getResponse()->getBody();
82
83
        if (!$matcher->match($content, $pattern)) {
84
            return;
85
        }
86
87
        throw new InvalidArgumentException($matcher->getError());
88
    }
89
}
90