Completed
Push — master ( b0547c...262514 )
by Elf
04:33
created

AttributesVisibility::getVisible()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Support\Traits\Eloquent;
4
5
/**
6
 * Add ability for Eloquent to hide or visible attributes via static methods.
7
 */
8
trait AttributesVisibility
9
{
10
    /**
11
     * The shared attributes that should be hidden for serialization.
12
     *
13
     * @var array
14
     */
15
    public static $sharedHidden;
16
17
    /**
18
     * The shared attributes that should be visible in serialization.
19
     *
20
     * @var array
21
     */
22
    public static $sharedVisible;
23
24
    /**
25
     * Get the shared hidden attributes.
26
     *
27
     * @return array|null
28
     */
29
    public static function getSharedHidden()
30
    {
31
        return static::$sharedHidden;
32
    }
33
34
    /**
35
     * Set the shared hidden attributes.
36
     *
37
     * @param  array|null  $hidden
38
     * @return void
39
     */
40
    public static function setSharedHidden($hidden)
41
    {
42
        static::$sharedHidden = $hidden;
0 ignored issues
show
Documentation Bug introduced by
It seems like $hidden can be null. However, the property $sharedHidden is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
43
    }
44
45
    /**
46
     * Get the shared visible attributes.
47
     *
48
     * @return array|null
49
     */
50
    public static function getSharedVisible()
51
    {
52
        return static::$sharedVisible;
53
    }
54
55
    /**
56
     * Set the shared visible attributes.
57
     *
58
     * @param  array|null  $visible
59
     * @return void
60
     */
61
    public static function setSharedVisible($visible)
62
    {
63
        static::$sharedVisible = $visible;
0 ignored issues
show
Documentation Bug introduced by
It seems like $visible can be null. However, the property $sharedVisible is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
64
    }
65
66
    /**
67
     * Make all attributes be visible.
68
     *
69
     * @return void
70
     */
71
    public static function makeAllVisible()
72
    {
73
        static::$sharedHidden = static::$sharedVisible = [];
74
    }
75
76
    /**
77
     * Restore attributes visibility.
78
     *
79
     * @return void
80
     */
81
    public static function restoreAttributesVisibility()
82
    {
83
        static::$sharedHidden = static::$sharedVisible = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $sharedVisible.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
Documentation Bug introduced by
It seems like static::$sharedVisible = null of type null is incompatible with the declared type array of property $sharedHidden.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
84
    }
85
86
    /**
87
     * Get the hidden attributes for the model.
88
     *
89
     * @return array
90
     */
91
    public function getHidden()
92
    {
93
        if (is_array(static::$sharedHidden)) {
94
            return static::$sharedHidden;
95
        }
96
97
        return parent::getHidden();
98
    }
99
100
    /**
101
     * Get the visible attributes for the model.
102
     *
103
     * @return array
104
     */
105
    public function getVisible()
106
    {
107
        if (is_array(static::$sharedVisible)) {
108
            return static::$sharedVisible;
109
        }
110
111
        return parent::getVisible();
112
    }
113
}
114