Passed
Push — master ( 05edd6...c2f9d5 )
by Caen
03:39 queued 15s
created

Serializable::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 2
b 0
f 0
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
        // Calling the function from a different scope means we only get the public properties.
21
22
        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...
23
    }
24
25
    /** Recursively serialize Arrayables */
26
    public function arraySerialize(): array
27
    {
28
        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

28
        return collect(/** @scrutinizer ignore-type */ $this->toArray())->toArray();
Loading history...
29
    }
30
31
    /** @inheritDoc */
32
    public function jsonSerialize(): array
33
    {
34
        return $this->arraySerialize();
35
    }
36
37
    /** @param  int  $options */
38
    public function toJson($options = 0): string
39
    {
40
        return json_encode($this->jsonSerialize(), $options);
41
    }
42
}
43