Completed
Push — master ( ff70ce...f7d289 )
by Stefan
02:30
created

AbstractModel::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace BecosoftApi\Model;
4
5
use Illuminate\Support\Collection;
6
7
/**
8
 * Class AbstractModel
9
 * @package BecosoftApi\Model
10
 */
11
abstract class AbstractModel
12
{
13
    /**
14
     * @return array
15
     * @todo Refactor this
16
     */
17
    public function toArray()
18
    {
19
        $values = call_user_func('get_object_vars', $this);
20
21
        foreach($values as $key => $value) {
22
            if (!$value instanceof Collection) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Support\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
23
                continue;
24
            }
25
26
            foreach($value as $subKey => $subValue) {
27
                if (!is_array($subValue)) {
28
                    $subValue = $subValue->toArray();
29
                }
30
31
                $values[$key][$subKey] = $subValue;
32
            }
33
        }
34
35
        return array_filter($values, function ($item) {
36
            if ($item instanceof Collection) {
0 ignored issues
show
Bug introduced by
The class Illuminate\Support\Collection does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
37
                return !$item->isEmpty();
38
            }
39
40
            return !is_null($item);
41
        });
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function toJson()
48
    {
49
        return json_encode($this->toArray());
50
    }
51
}
52