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

PhpMatcher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 59.52 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 25
loc 42
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A response_content_should_match() 11 11 2
A response_content_should_not_match() 14 14 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 InvalidArgumentException;
5
6
use Behat\Behat\Context\Context;
7
use Behat\Gherkin\Node\PyStringNode;
8
9
use Coduo\PHPMatcher\Factory;
10
use Coduo\PHPMatcher\Factory\SimpleFactory;
11
12
use Behapi\Tools\HttpHistory;
13
14
class PhpMatcher implements Context
15
{
16
    use ApiTrait;
17
18
    /** @var Factory */
19
    private $factory;
20
21
    public function __construct(HttpHistory $history)
22
    {
23
        $this->history = $history;
24
        $this->factory = new SimpleFactory;
25
    }
26
27
    /** @Then the response content should match: */
28 View Code Duplication
    public function response_content_should_match(PyStringNode $pattern)
29
    {
30
        $matcher = $this->factory->createMatcher();
31
        $content = (string) $this->getResponse()->getBody();
32
33
        if ($matcher->match($content, $pattern->getRaw())) {
34
            return;
35
        }
36
37
        throw new InvalidArgumentException($matcher->getError());
38
    }
39
40
    /** @Then the response content should match: */
41 View Code Duplication
    public function response_content_should_not_match(PyStringNode $pattern)
42
    {
43
        $matcher = $this->factory->createMatcher();
44
        $content = (string) $this->getResponse()->getBody();
45
46
        if (!$matcher->match($content, $pattern->getRaw())) {
47
            return;
48
        }
49
50
        throw new InvalidArgumentException(sprintf(
51
            'The response matches with the pattern "%s"',
52
            $pattern->getRaw()
53
        ));
54
    }
55
}
56