ToResolveToExpectation   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 37
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B run() 0 23 6
1
<?php
2
3
namespace Overwatch\ServiceBundle\Expectation;
4
5
use Overwatch\ExpectationBundle\Exception as Result;
6
use Overwatch\ExpectationBundle\Expectation\ExpectationInterface;
7
8
/**
9
 * ToResolveToExpectation
10
 * Expectation classes are the actual runners of tests.
11
 * This is the runner for the "toResolveTo" expectation.
12
 */
13
class ToResolveToExpectation implements ExpectationInterface
14
{
15
    private $config;
16
    
17 29
    public function __construct($config)
18
    {
19 29
        $this->config = $config;
20 29
    }
21
    
22
    /**
23
     * @param string $actual
24
     * @param string|null $expected
25
     */
26 2
    public function run($actual, $expected = null)
27
    {
28 2
        $dnsRecords = dns_get_record($actual);
29 2
        $found = '';
30
        
31 2
        foreach ($dnsRecords as $dnsRecord) {
32 2
            if (!in_array($dnsRecord['type'], $this->config['record_types'])) {
33 2
                continue;
34
            }
35
            
36 2
            foreach (['mname', 'txt', 'target', 'ipv6', 'ip'] as $destination) {
37 2
                if (array_key_exists($destination, $dnsRecord)) {
38 2
                    $found = $dnsRecord[$destination];
39 2
                }
40 2
            }
41
            
42 2
            if ($found === $expected) {
43 1
                return $actual . ' has a ' . $dnsRecord['type'] . ' record that resolves to ' . $found;
44
            }
45 1
        }
46
        
47 1
        throw new Result\ExpectationFailedException("Expected $actual to resolve to $expected, actually resolves to $found");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $actual instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $expected instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $found instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
48
    }
49
}
50