Asserter::assertArrayContains()   D
last analyzed

Complexity

Conditions 13
Paths 136

Size

Total Lines 42
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 4.8178
c 0
b 0
f 0
cc 13
eloc 28
nc 136
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Knp\FriendlyContexts\Utils;
4
5
use Knp\FriendlyContexts\Table\NodesBuilder;
6
7
class Asserter
8
{
9
    protected $formater;
10
11
    public function __construct(TextFormater $formater)
12
    {
13
        $this->formater = $formater;
14
    }
15
16
    public function assertArrayEquals(array $expected, array $real, $fullText = false)
17
    {
18
        $message = sprintf("The given array\r\n\r\n%s\r\nis not equals to expected\r\n\r\n%s", $this->explode($real), $this->explode($expected));
19
20
        if (false === $fullText) {
21
            return $this->assertEquals(
22
                $expected,
23
                $real,
24
                $message
25
            );
26
        } else {
27
            return $this->assertEquals(
28
                $this->explode($expected),
29
                $this->explode($real),
30
                $message
31
            );
32
        }
33
    }
34
35
    public function assertArrayContains(array $expected, array $real, $message = null)
36
    {
37
        $message = $message ?: sprintf("The given array\r\n\r\n%s\r\ndoes not contain the following rows\r\n\r\n%s", $this->explode($real), $this->explode($expected));
38
        $indexes = [];
0 ignored issues
show
Unused Code introduced by
$indexes 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...
39
40
        foreach ($expected as $row) {
41
            $this->assert(is_array($row), $message);
42
        }
43
44
        foreach ($real as $row) {
45
            $this->assert(is_array($row), $message);
46
        }
47
48
        $nodes = (new NodesBuilder)->build($real);
49
        $nodes = $nodes->search(current(current($expected)));
50
51
        foreach ($nodes as $initial) {
52
            $result    = true;
0 ignored issues
show
Unused Code introduced by
$result 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...
53
            $cells     = $expected;
54
            $lineStart = $initial;
55
            do {
56
                $columns       = array_shift($cells);
57
                $columnElement = $lineStart;
58
                do {
59
                    $content = array_shift($columns);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
60
                    $result = $columnElement
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
61
                        ? $content === $columnElement->getContent()
62
                        : false
63
                    ;
64
                    $columnElement = $columnElement ? $columnElement->getRight() : null;
65
                } while (!empty($columns) && $result);
66
                $lineStart = $lineStart ? $lineStart->getBottom() : null;
67
            } while (!empty($cells) && $result);
68
69
            if ($result) {
70
71
                return true;
72
            }
73
        }
74
75
        $this->assert(false, $message);
76
    }
77
78
    public function assertEquals($expected, $real, $message = "Failing to assert equals.")
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Failing to assert equals. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
79
    {
80
        return $this->assert($expected === $real, $message);
81
    }
82
83
    public function assertNotEquals($expected, $real, $message = "Failing to assert not equals.")
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Failing to assert not equals. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
84
    {
85
        return $this->assert($expected !== $real, $message);
86
    }
87
88
    public function assert($result, $message = "Assert failure")
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Assert failure does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
89
    {
90
        if (false === $result) {
91
            throw new \Exception($message, 1);
92
        }
93
94
        return true;
95
    }
96
97
    private function explode($value)
98
    {
99
        if (!is_array($value)) {
100
            return (string) $value;
101
        } else {
102
            return $this->formater->tableToString($value);
103
        }
104
    }
105
}
106