GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 125e00...775331 )
by Toby
10:03 queued 12s
created

HasAdditionalProperties   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
eloc 42
c 2
b 0
f 0
dl 0
loc 169
ccs 52
cts 52
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdditionalAttribute() 0 4 2
A setAdditionalAttribute() 0 3 1
A initializeHasAdditionalProperties() 0 8 2
A getAdditionalAttributesAttribute() 0 9 3
A saveAdditionalAttribute() 0 4 1
A addProperty() 0 3 1
A getColumnName() 0 3 1
A setAttribute() 0 6 2
A __call() 0 23 3
A getAdditionalAttributes() 0 3 1
A getAttribute() 0 6 2
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace BristolSU\ControlDB\AdditionalProperties;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Str;
8
9
/**
10
 * Allow a model to have any arbitrary properties, set at runtime
11
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
12
trait HasAdditionalProperties
13
{
14
15
    /**
16
     * Initialise the trait
17
     *
18
     * - Add all additional properties to the appends array
19
     * - Add additional_properties column to hidden
20
     * - Cast the additional_properties column to an array
21
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
22 279
    public function initializeHasAdditionalProperties()
23
    {
24 279
        if (is_subclass_of($this, Model::class)) {
25 278
            $this->append(static::getAdditionalAttributes());
0 ignored issues
show
Bug introduced by
The method append() does not exist on BristolSU\ControlDB\Addi...HasAdditionalProperties. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

25
            $this->/** @scrutinizer ignore-call */ 
26
                   append(static::getAdditionalAttributes());
Loading history...
26 278
            $this->addHidden($this->getColumnName());
0 ignored issues
show
Bug introduced by
The method addHidden() does not exist on BristolSU\ControlDB\Addi...HasAdditionalProperties. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

26
            $this->/** @scrutinizer ignore-call */ 
27
                   addHidden($this->getColumnName());
Loading history...
27 278
            $this->casts[] = $this->getColumnName();
0 ignored issues
show
Bug Best Practice introduced by
The property casts does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
        } else {
29 1
            throw new Exception('The HasAdditionalProperties trait must only be used in an Eloquent model');
30
        }
31 278
    }
32
33
    /**
34
     * Add an additional property
35
     *
36
     * @param $key
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
37
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
38 36
    public static function addProperty(string $key): void
39
    {
40 36
        app(AdditionalPropertyStore::class)->addProperty(static::class, $key);
41 36
    }
42
43
    /**
44
     * Get all additional attributes the model is using
45
     * 
46
     * @return array
47
     */
48 279
    public static function getAdditionalAttributes(): array
49
    {
50 279
        return (app(AdditionalPropertyStore::class)->getProperties(static::class) ?? []);
51
    }
52
    
53
    /**
54
     * Retrieve an additional attribute value
55
     * 
56
     * @param string $key Key of the attribute
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
57
     * @return mixed Value of the attribute
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
58
     */
59 25
    public function getAdditionalAttribute(string $key)
60
    {
61 25
        return (array_key_exists($key, ($this->{$this->getColumnName()}??[])) 
62 25
            ? $this->{$this->getColumnName()}[$key] : null);
63
    }
64
65
    /**
66
     * Set an additional attribute value
67
     * 
68
     * @param string $key Key of the attribute
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
69
     * @param mixed $value Value of the attribute
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
70
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
71 27
    public function setAdditionalAttribute(string $key, $value)
72
    {
73 27
        $this->attributes[$this->getColumnName()] = array_merge(($this->{$this->getColumnName()}??[]), [$key => $value]);
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
74 27
    }
75
76
    /**
77
     * Save an additional attribute value
78
     *
79
     * @param string $key Key of the attribute
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
80
     * @param mixed $value Value of the attribute
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
81
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
82 9
    public function saveAdditionalAttribute(string $key, $value)
83
    {
84 9
        $this->setAdditionalAttribute($key, $value);
85 9
        $this->save();
0 ignored issues
show
Bug introduced by
The method save() does not exist on BristolSU\ControlDB\Addi...HasAdditionalProperties. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

85
        $this->/** @scrutinizer ignore-call */ 
86
               save();
Loading history...
86 9
    }
87
88
    /**
89
     * Override the default getAttribute function
90
     * 
91
     * Any time an attribute is retrieved, we will check if it's an additional property and return it if so
92
     * 
93
     * @param string $key Key of the property to retrieve
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
94
     * @return mixed Value of the property
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
95
     */
96 253
    public function getAttribute($key)
97
    {
98 253
        if (in_array($key, static::getAdditionalAttributes())) {
99 5
            return $this->getAdditionalAttribute($key);
100
        }
101 253
        return parent::getAttribute($key);
102
    }
103
104
    /**
105
     * Override the default getAttribute function
106
     *
107
     * Any time an attribute is set, we will check if it's an additional property and set it correspondingly if so
108
     *
109
     * @param string $key Key of the property to retrieve
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
110
     * @param mixed $value Value of the attribute
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
111
     * @return mixed Value of the property
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
112
     */
113 264
    public function setAttribute($key, $value)
114
    {
115 264
        if (in_array($key, static::getAdditionalAttributes())) {
116 14
            $this->setAdditionalAttribute($key, $value);
117
        } else {
118 264
            parent::setAttribute($key, $value);
119
        }
120 264
    }
121
122
    /**
123
     * Cast the additional attributes column to an array, or return an empty array by default.
124
     * 
125
     * @return array|mixed
126
     */
127 34
    public function getAdditionalAttributesAttribute()
128
    {
129 34
        if(!array_key_exists($this->getColumnName(), $this->attributes)) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
130 24
            return [];
131
        }
132 29
        if(is_string($this->attributes[$this->getColumnName()])) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
133 14
            return (json_decode($this->attributes[$this->getColumnName()], true) ?? []);
134
        }
135 15
        return $this->attributes[$this->getColumnName()];
136
    }
137
138
    /**
139
     * Get the name of the additional attributes column
140
     * 
141
     * @return string Name of the additional attributes column
142
     */
143 278
    private function getColumnName()
0 ignored issues
show
Coding Style introduced by
Private method name "HasAdditionalProperties::getColumnName" must be prefixed with an underscore
Loading history...
144
    {
145 278
        return 'additional_attributes';
146
    }
147
148
    /**
149
     * Dynamically define accessors for the appended properties.
150
     * 
151
     * By catching all method calls, we can check if they are calls for an accessor or mutator to an attribute and
152
     * carry out the corresponding action.
153
     * 
154
     * @param string $method Method of the call
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
155
     * @param array $args Arguments for the call
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
156
     * @return mixed
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
157
     */
158 78
    public function __call($method, $args)
159
    {
160
        // Check if the call was an accessor
161
        $additionalAccessors = array_map(function($propertyKey) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
162 14
            return 'get'.Str::studly($propertyKey).'Attribute';
163 78
        }, static::getAdditionalAttributes());
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
164 78
        if(in_array($method, $additionalAccessors)) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
165 14
            return $this->getAdditionalAttribute(
166 14
                Str::snake(Str::substr(Str::substr($method, 3), 0, -9))
167
            );
168
        }
169
        
170
        // Check if the call was a mutator
171
        $additionalAccessors = array_map(function($propertyKey) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
172 10
            return 'set'.Str::studly($propertyKey).'Attribute';
173 74
        }, static::getAdditionalAttributes());
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
174 74
        if(in_array($method, $additionalAccessors)) {
0 ignored issues
show
Coding Style introduced by
Expected "if (...) {\n"; found "if(...) {\n"
Loading history...
175 1
            $this->setAdditionalAttribute(
176 1
                Str::snake(Str::substr(Str::substr($method, 3), 0, -9)), $args[0]
177
            );
178
        } else {
179 73
            return parent::__call($method,
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
180 73
                $args);
0 ignored issues
show
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 12 spaces, but found 16.
Loading history...
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
181
        }
182
        
183
        
184 1
    }
185
}
186