MessageJsonSerializeTrait::jsonSerializeArray()   B
last analyzed

Complexity

Conditions 8
Paths 11

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 8.4444
c 0
b 0
f 0
cc 8
nc 11
nop 1
crap 8
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the zibios/sharep.
7
 *
8
 * (c) Zbigniew Ślązak
9
 */
10
11
namespace App\Slack\MessageBuilder;
12
13
trait MessageJsonSerializeTrait
14
{
15 9
    public function jsonSerialize(): array
16
    {
17 9
        $properties = get_object_vars($this);
18
19 9
        return $this->jsonSerializeArray($properties);
20
    }
21
22 9
    private function jsonSerializeArray(array $properties): array
23
    {
24 9
        $jsonData = [];
25 9
        foreach ($properties as $name => $value) {
26 9
            if (\is_string($name)) {
27 9
                $name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $name));
28
            }
29 9
            if ($value instanceof \JsonSerializable) {
0 ignored issues
show
Bug introduced by
The class JsonSerializable does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
30 8
                $jsonData[$name] = $value->jsonSerialize();
31 9
            } elseif (\is_array($value) && \count($value) > 0) {
32 3
                $jsonData[$name] = $this->jsonSerializeArray($value);
33 9
            } elseif (\is_bool($value)) {
34 8
                $jsonData[$name] = $value;
35 9
            } elseif (!empty($value)) {
36 9
                $jsonData[$name] = $value;
37
            }
38
        }
39
40 9
        return $jsonData;
41
    }
42
}
43