Passed
Push — master ( c2f9d5...d2c74b )
by Caen
03:32 queued 13s
created

Serializable::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Support\Concerns;
6
7
use function json_encode;
8
use function collect;
9
10
/**
11
 * Automatically serializes an Arrayable implementation when JSON is requested.
12
 *
13
 * @see \Hyde\Support\Contracts\SerializableContract
14
 */
15
trait Serializable
16
{
17
    /** Default implementation to dynamically serialize all public properties. Can be overridden for increased control. */
18
    public function toArray(): array
19
    {
20
        return $this->automaticallySerialize();
21
    }
22
23
    /** Recursively serialize Arrayables */
24
    public function arraySerialize(): array
25
    {
26
        return collect($this->toArray())->toArray();
0 ignored issues
show
Bug introduced by
$this->toArray() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
        return collect(/** @scrutinizer ignore-type */ $this->toArray())->toArray();
Loading history...
27
    }
28
29
    /** @inheritDoc */
30
    public function jsonSerialize(): array
31
    {
32
        return $this->arraySerialize();
33
    }
34
35
    /** @param  int  $options */
36
    public function toJson($options = 0): string
37
    {
38
        return json_encode($this->jsonSerialize(), $options);
39
    }
40
41
    /** Automatically serialize all public properties. */
42
    protected function automaticallySerialize(): array
43
    {
44
        // Calling the function from a different scope means we only get the public properties.
45
46
        return get_object_vars(...)->__invoke($this);
0 ignored issues
show
Bug introduced by
The type Hyde\Support\Concerns\get_object_vars 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...
47
    }
48
}
49