Test Setup Failed
Pull Request — master (#62)
by Alex
03:03
created

ModelSerialiser::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace AlgoWeb\PODataLaravel\Serialisers;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class ModelSerialiser
8
{
9
    // take a supplied Eloquent model with metadata trait and serialise it in bulk.
10
    // Upstream POData implementation has an N+1 lookup problem that interacts badly with how
11
    // Eloquent handles property accesses
12
13
    private static $mutatorCache = [];
14
    private static $metadataCache = [];
15
16
    public function __construct()
17
    {
18
19
    }
20
21
    /**
22
     * Serialise needed bits of supplied model, taking fast path where possible
23
     *
24
     * @param $model
25
     * @return mixed
26
     */
27
    public function bulkSerialise($model)
28
    {
29
        $class = get_class($model);
30
        assert($model instanceof Model, $class);
31
        // dig up metadata
32
        if (!isset(static::$metadataCache[$class])) {
0 ignored issues
show
Bug introduced by
Since $metadataCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $metadataCache to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
33
            static::$metadataCache[$class] = $model->metadata();
0 ignored issues
show
Bug introduced by
Since $metadataCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $metadataCache to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
34
        }
35
        $meta = static::$metadataCache[$class];
0 ignored issues
show
Bug introduced by
Since $metadataCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $metadataCache to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
36
        $keys = array_keys($meta);
37
        // dig up getter list - we only care about the mutators that end up in metadata
38
        if (!isset(static::$mutatorCache[$class])) {
0 ignored issues
show
Bug introduced by
Since $mutatorCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $mutatorCache to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
39
            $getterz = [];
40
            $datez = $model->getDates();
41
            $castz = $model->getCasts();
42
            foreach ($keys as $key) {
43
                if ($model->hasGetMutator($key) || in_array($key, $datez) || array_key_exists($key, $castz)) {
44
                    $getterz[] = $key;
45
                }
46
            }
47
            static::$mutatorCache[$class] = $getterz;
0 ignored issues
show
Bug introduced by
Since $mutatorCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $mutatorCache to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
48
        }
49
        $getterz = static::$mutatorCache[$class];
0 ignored issues
show
Bug introduced by
Since $mutatorCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $mutatorCache to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
50
        $result = array_intersect_key($model->getAttributes(), $meta);
51
        foreach ($keys as $key) {
52
            if (!isset($result[$key])) {
53
                $result[$key] = null;
54
            }
55
        }
56
        foreach ($getterz as $getter) {
57
            $result[$getter] = $model->$getter;
58
        }
59
60
        return $result;
61
    }
62
63
    public function reset()
64
    {
65
        static::$mutatorCache = [];
0 ignored issues
show
Bug introduced by
Since $mutatorCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $mutatorCache to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
66
        static::$metadataCache = [];
0 ignored issues
show
Bug introduced by
Since $metadataCache is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $metadataCache to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
67
    }
68
}
69