Completed
Push — master ( 203e38...2cdd54 )
by Gerrit
13:04
created

FailedRDMAssertionException::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 * Copyright (C) 2018 Gerrit Addiks.
4
 * This package (including this file) was released under the terms of the GPL-3.0.
5
 * You should have received a copy of the GNU General Public License along with this program.
6
 * If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy.
7
 * @license GPL-3.0
8
 * @author Gerrit Addiks <[email protected]>
9
 */
10
11
namespace Addiks\RDMBundle\Exception;
12
13
use ErrorException;
14
use ReflectionClass;
15
use Addiks\RDMBundle\Exception\FailedRDMAssertionExceptionInterface;
16
17
final class FailedRDMAssertionException extends ErrorException implements FailedRDMAssertionExceptionInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: __toString, getCode, getFile, getLine, getMessage, getPrevious, getTrace, getTraceAsString
Loading history...
18
{
19
20
    /**
21
     * @var string
22
     */
23
    private $type;
24
25
    /**
26
     * @var array<mixed>
27
     */
28
    private $parameters;
29
30 9
    public function __construct(string $message, string $type, array $parameters)
31
    {
32 9
        parent::__construct($message);
33
34 9
        $this->type = $type;
35 9
        $this->parameters = $parameters;
36 9
    }
37
38 1
    public function getType(): string
39
    {
40 1
        return $this->type;
41
    }
42
43 1
    public function getParameters(): array
44
    {
45 1
        return $this->parameters;
46
    }
47
48
    /**
49
     * @param mixed $expectedService
50
     * @param mixed $actualService
51
     */
52 3
    public static function expectedDifferentService(
53
        string $serviceId,
54
        ReflectionClass $classReflection,
55
        $expectedService,
56
        $actualService
57
    ): FailedRDMAssertionException {
58
        /** @var string $actualDescription */
59 3
        $actualDescription = self::generateDescriptionForValue($actualService);
60
61
        /** @var string $expectedDescription */
62 3
        $expectedDescription = self::generateDescriptionForValue($expectedService);
63
64 3
        return new self(
65 3
            sprintf(
66 3
                "Expected service '%s' (%s) on entity %s, was %s instead!",
67 3
                $serviceId,
68 3
                $expectedDescription,
69 3
                $classReflection->getName(),
0 ignored issues
show
Bug introduced by
Consider using $classReflection->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
70 3
                $actualDescription
71
            ),
72 3
            "EXPECTED_DIFFERENT_SERVICE",
73 3
            [$serviceId, $expectedService, $actualService]
74
        );
75
    }
76
77 2
    public static function expectedInstanceOf(
78
        string $expectedClassName,
79
        string $actualClassName,
80
        string $declarationOrigin
81
    ): FailedRDMAssertionException {
82 2
        return new self(
83 2
            sprintf(
84 2
                "Expected instance of %s instead of %s as specified in %s!",
85 2
                $expectedClassName,
86 2
                $actualClassName,
87 2
                $declarationOrigin
88
            ),
89 2
            "EXPECTED_INSTNACE_OF",
90 2
            [$expectedClassName, $actualClassName, $declarationOrigin]
91
        );
92
    }
93
94
    /**
95
     * @param mixed $actualValue
96
     */
97 2
    public static function expectedArray(
98
        $actualValue,
99
        string $declarationOrigin
100
    ): FailedRDMAssertionException {
101
102
        /** @var string $description */
103 2
        $description = self::generateDescriptionForValue($actualValue);
104
105 2
        return new self(
106 2
            sprintf(
107 2
                "Expected array, got %s as specified in %s!",
108 2
                $description,
109 2
                $declarationOrigin
110
            ),
111 2
            "EXPECTED_INSTNACE_OF",
112 2
            [$actualValue, $declarationOrigin]
113
        );
114
    }
115
116
    /**
117
     * @param mixed $value
118
     */
119 5
    private static function generateDescriptionForValue($value): string
120
    {
121
        /** @var string $description */
122 5
        $description = null;
0 ignored issues
show
Unused Code introduced by
$description 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...
123
124 5
        if (is_object($value)) {
125 3
            $description = get_class($value) . '#' . spl_object_hash($value);
126
127
        } else {
128 3
            $description = gettype($value);
129
        }
130
131 5
        return $description;
132
    }
133
134
}
135