Completed
Push — master ( 3a6c0f...94b851 )
by Kirill
03:45
created

Jsonable::jsonSerialize()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 11
cp 0
rs 9.2568
c 0
b 0
f 0
cc 5
nc 5
nop 0
crap 30
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\Document;
14
15
/**
16
 * Trait Jsonable
17
 */
18
trait Jsonable
19
{
20
    /**
21
     * @return array
22
     */
23
    public function jsonSerialize(): array
24
    {
25
        $result = [];
26
27
        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...
28
            if ($value instanceof Readable) {
29
                continue;
30
            }
31
32
            if ($value instanceof Document) {
33
                continue;
34
            }
35
36
            if ($field === 'parent') {
37
                continue;
38
            }
39
40
            $result[$field] = $value;
41
        }
42
43
        return $result;
44
    }
45
}
46