Completed
Push — master ( ca1c7c...1b72cc )
by Kirill
02:54
created

Jsonable::jsonSerialize()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 0
cts 13
cp 0
rs 8.5546
c 0
b 0
f 0
cc 7
nc 16
nop 0
crap 56
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Reflection\Common;
11
12
use Railt\Io\Readable;
13
use Railt\Reflection\Contracts\Definition;
14
use Railt\Reflection\Contracts\Document;
15
16
/**
17
 * Trait Jsonable
18
 */
19
trait Jsonable
20
{
21
    /**
22
     * @return array
23
     */
24
    public function jsonSerialize(): array
25
    {
26
        $result = [];
27
28
        foreach ($this->getObjectFields() as $field => $value) {
0 ignored issues
show
Bug introduced by
It seems like getObjectFields() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
29
            foreach ($this->jsonNonRenderableTypes() as $type) {
30
                if ($value instanceof $type) {
31
                    continue 2;
32
                }
33
            }
34
35
            foreach ($this->jsonNonRenderableFields() as $name) {
36
                if ($field === $name) {
37
                    continue 2;
38
                }
39
            }
40
41
            $result[$field] = $value;
42
        }
43
44
        // Additional information about type
45
        if ($this instanceof Definition) {
46
            $result['__typename'] = $this::getType()->getName();
47
        }
48
49
        return $result;
50
    }
51
52
    /**
53
     * @return iterable|string[]
54
     */
55
    private function jsonNonRenderableTypes(): iterable
56
    {
57
        yield Readable::class;
58
        yield Document::class;
59
    }
60
61
    /**
62
     * @return iterable|string[]
63
     */
64
    private function jsonNonRenderableFields(): iterable
65
    {
66
        // Recursive relation exclusion
67
        yield 'parent';
68
69
        // Dictionary relation exclusion
70
        yield 'dictionary';
71
72
        // File information exclusion
73
        yield 'offset';
74
        yield 'line';
75
        yield 'column';
76
77
        // Reverse superfluous inheritance information
78
        yield 'extendedBy';
79
    }
80
}
81