Jsonable::toJson()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace Atog\Api;
2
3
/**
4
 * Trait Jsonable implements JsonSerializable interface
5
 * @package Atog\Api
6
 */
7
trait Jsonable
8
{
9
    /**
10
     * @return array
11
     */
12 3
    public function jsonSerialize()
13
    {
14 3
        return $this->toArray();
15
    }
16
    
17
    /**
18
     * @return array
19
     */
20 16
    public function toArray()
21
    {
22 16
        return $this->attributes;
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
23
    }
24
    
25
    /**
26
     * @return string
27
     */
28 3
    public function __toString()
29
    {
30 3
        return $this->toJson();
31
    }
32
    
33
    /**
34
     * @return string
35
     */
36 6
    public function toJson()
37
    {
38 6
        return json_encode($this->toArray(), true);
39
    }
40
}
41