ResponseTrait::getMatchers()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 52
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 33
nc 1
nop 0
dl 0
loc 52
rs 9.392
c 0
b 0
f 0

How to fix   Long Method   

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
/*
4
 * This file is part of the doyo/code-coverage project.
5
 *
6
 * (c) Anthonius Munthi <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Spec\Doyo\Bridge\CodeCoverage;
15
16
use Symfony\Component\HttpFoundation\JsonResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\JsonResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Symfony\Component\HttpFoundation\Response;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Webmozart\Assert\Assert;
0 ignored issues
show
Bug introduced by
The type Webmozart\Assert\Assert was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
/**
21
 * Trait ResponseTrait.
22
 */
23
trait ResponseTrait
24
{
25
    public function getMatchers(): array
26
    {
27
        return [
28
            'beInJson' => function ($subject) {
29
                Assert::isInstanceOf($subject, JsonResponse::class);
30
31
                return true;
32
            },
33
            'containJsonKey' => function ($subject, $key) {
34
                /** @var \Symfony\Component\HttpFoundation\JsonResponse $subject */
35
                $json = $subject->getContent();
36
                $json = json_decode($json, true);
37
                Assert::isArray($json);
38
                Assert::keyExists($json, $key);
39
40
                return true;
41
            },
42
            'containJsonKeyWithValue' => function ($subject, $key, $expected) {
43
                /** @var \Symfony\Component\HttpFoundation\JsonResponse $subject */
44
                $json = $subject->getContent();
45
                $json = json_decode($json, true);
46
                Assert::keyExists($json, $key);
47
                Assert::contains($json[$key], $expected);
48
49
                return true;
50
            },
51
            'haveStatusCode' => function ($subject, $expected) {
52
                Assert::eq($subject->getStatusCode(), $expected);
53
54
                return true;
55
            },
56
            'haveContent' => function ($subject, $expected) {
57
                Assert::isInstanceOf($subject, Response::class);
58
                Assert::contains($subject->getContent(), $expected);
59
60
                return true;
61
            },
62
            'beAHttpResponse' => function ($subject) {
63
                Assert::isInstanceOf($subject, Response::class);
64
65
                return true;
66
            },
67
            'beASerializedObject' => function ($subject, $expected) {
68
                /** @var \Symfony\Component\HttpFoundation\Response $subject */
69
                $serialized = unserialize($subject->getContent());
70
                $class = $expected;
71
                if (!\is_string($class)) {
72
                    $class = \get_class($class);
73
                }
74
                Assert::isInstanceOf($serialized, $class);
75
76
                return true;
77
            },
78
        ];
79
    }
80
}
81