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.

HasMeta   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 185
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 38
eloc 71
dl 0
loc 185
c 0
b 0
f 0
rs 9.36

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __get() 0 17 4
A saveMeta() 0 15 4
B __set() 0 37 9
A save() 0 3 3
A push() 0 3 3
A meta() 0 5 1
A getMeta() 0 21 4
A newMeta() 0 19 2
A deleteMeta() 0 10 3
A __isset() 0 8 5
1
<?php 
2
3
namespace Raystech\StarterKit\Traits;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\MorphMany;
9
use Illuminate\Database\Query\JoinClause;
10
11
trait HasMeta
12
{
13
    protected $metaData = NULL;
14
    
15
    protected function newMeta($meta, $extra = array())
0 ignored issues
show
Unused Code introduced by
The parameter $extra is not used and could be removed. ( Ignorable by Annotation )

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

15
    protected function newMeta($meta, /** @scrutinizer ignore-unused */ $extra = array())

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
16
    {
17
        // dd(__class__);
18
        // dd($meta);
19
        $metaModel = new $this->meta_model();
0 ignored issues
show
Bug Best Practice introduced by
The property meta_model does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
20
21
        if($this->meta_global == true) {
0 ignored issues
show
Bug Best Practice introduced by
The property meta_global does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
22
            $metaModel->model_type  = __class__;
23
        }
24
        $metaModel->{$this->meta_key_name}    = $meta[$this->meta_key_name];
0 ignored issues
show
Bug Best Practice introduced by
The property meta_key_name does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
25
        $metaModel->{$this->meta_value_name}  = $meta[$this->meta_value_name];
0 ignored issues
show
Bug Best Practice introduced by
The property meta_value_name does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
26
        $metaModel->{$this->meta_foreign_key} = $this->{$this->meta_primary_key};
0 ignored issues
show
Bug Best Practice introduced by
The property meta_primary_key does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property meta_foreign_key does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
27
28
        // if(!empty($extra)) {
29
        //  foreach($extra as $key => $value) {
30
31
        //  }
32
        // }
33
        return $metaModel;
34
    }
35
36
37
    /**
38
     * Get the data associated with this model
39
     *
40
     * @return hasOne
0 ignored issues
show
Bug introduced by
The type Raystech\StarterKit\Traits\hasOne 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...
41
     */
42
    public function meta()
43
    {
44
        // return $this->morphMany($this->meta_model, $this->meta_foreign_key, $this->primaryKey);
45
        
46
        return $this->hasMany($this->meta_model, $this->meta_foreign_key, $this->primaryKey);
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug introduced by
It seems like hasMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

46
        return $this->/** @scrutinizer ignore-call */ hasMany($this->meta_model, $this->meta_foreign_key, $this->primaryKey);
Loading history...
Bug Best Practice introduced by
The property meta_model does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property meta_foreign_key does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
47
    }
48
49
    /**
50
     * Dynamically retrieve attributes on the model.
51
     *
52
     * @param  string  $key
53
     * @return mixed
54
     */
55
    public function __get($key)
56
    {
57
        $value = $this->getAttribute($key);
0 ignored issues
show
Bug introduced by
It seems like getAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

57
        /** @scrutinizer ignore-call */ 
58
        $value = $this->getAttribute($key);
Loading history...
58
59
        if (is_null($value)) {
60
            try {
61
                if(isset($this->getMeta()->$key)) {
62
                    $value = $this->getMeta()->$key->{$this->meta_value_name};
0 ignored issues
show
Bug Best Practice introduced by
The property meta_value_name does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
63
                } else {
64
                    $value = NULL;
65
                }
66
            } catch (Exception $e) {
0 ignored issues
show
Bug introduced by
The type Raystech\StarterKit\Traits\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
67
                $value = NULL;
68
            }
69
        }
70
71
        return $value;
72
    }
73
74
    /**
75
     * Dynamically set attributes on the model.
76
     *
77
     * @param  string  $key
78
     * @param  mixed   $value
79
     * @return void
80
     */
81
    public function __set($key, $value)
82
    {
83
        if (array_key_exists($key, $this->getAttributes())) {
0 ignored issues
show
Bug introduced by
It seems like getAttributes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

83
        if (array_key_exists($key, $this->/** @scrutinizer ignore-call */ getAttributes())) {
Loading history...
84
            $this->setAttribute($key, $value);
0 ignored issues
show
Bug introduced by
It seems like setAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

84
            $this->/** @scrutinizer ignore-call */ 
85
                   setAttribute($key, $value);
Loading history...
85
            return;
86
        }
87
88
        try {
89
            if (isset($this->getMeta()->$key)) {
90
                $this->getMeta()->$key->{$this->meta_value_name} = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property meta_value_name does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
91
                return;
92
            }
93
        } catch (Exception $e) { }
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
94
95
        if(\Config::get('database.default') === 'sqlite') {
96
            $columns = \DB::select('PRAGMA table_info('.$this->table.')');
0 ignored issues
show
Bug Best Practice introduced by
The property table does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
97
98
            foreach ($columns as $column) {
99
                if ($column->name === $key) {
100
                    $this->setAttribute($key, $value);
101
                    return;
102
                }
103
            }
104
        } else {
105
            $columns = \DB::getSchemaBuilder()->getColumnListing($this->getTable());
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

105
            $columns = \DB::getSchemaBuilder()->getColumnListing($this->/** @scrutinizer ignore-call */ getTable());
Loading history...
106
107
            // $columns = \DB::select('DESCRIBE '.$this->table);
108
109
            // dd($columns);
110
            foreach ($columns as $column) {
111
                if ($column === $key) {
112
                    $this->setAttribute($key, $value);
113
                    return;
114
                }
115
            }
116
        }
117
        $this->getMeta()->$key = $this->newMeta(array($this->meta_key_name => $key, $this->meta_value_name => $value));
0 ignored issues
show
Bug Best Practice introduced by
The property meta_key_name does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
118
    }
119
120
121
    public function __isset($key)
122
    {
123
        if ((isset($this->attributes[$key]) 
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
124
            || isset($this->relations[$key])) 
0 ignored issues
show
Bug Best Practice introduced by
The property relations does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
125
            || ($this->hasGetMutator($key) && ! is_null($this->getAttributeValue($key)))) {
0 ignored issues
show
Bug introduced by
It seems like getAttributeValue() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

125
            || ($this->hasGetMutator($key) && ! is_null($this->/** @scrutinizer ignore-call */ getAttributeValue($key)))) {
Loading history...
Bug introduced by
It seems like hasGetMutator() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

125
            || ($this->/** @scrutinizer ignore-call */ hasGetMutator($key) && ! is_null($this->getAttributeValue($key)))) {
Loading history...
126
            return true;
127
        }
128
        return isset($this->getMeta()->$key->{$this->meta_value_name});
0 ignored issues
show
Bug Best Practice introduced by
The property meta_value_name does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
129
    }
130
131
    public function push() 
132
    {
133
        return parent::push() && $this->deleteMeta() && $this->saveMeta();
134
    }
135
136
    public function save(array $options = array())
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

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

136
    public function save(/** @scrutinizer ignore-unused */ array $options = array())

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
137
    {
138
        return parent::save() && $this->deleteMeta() && $this->saveMeta();
139
    }
140
141
    /**
142
     * Get the data associated with this model in a usable format
143
     *
144
     * @return array
145
     */
146
    protected function getMeta()
147
    {
148
        if (is_null($this->metaData)) {
149
            $primaryKey = $this->primaryKey;
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
Unused Code introduced by
The assignment to $primaryKey is dead and can be removed.
Loading history...
150
151
            if (is_null($this->primaryKey)) {
152
                // If nothing has been set and there is no ID then there will be no data
153
                $this->metaData = (object)[];
154
            } else {
155
                $meta = $this->meta;
0 ignored issues
show
Bug Best Practice introduced by
The property meta does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
156
157
                $niceDataArray = array();
158
159
                foreach ($meta as $data) {
160
                    $niceDataArray[$data->{$this->meta_key_name}] = $data;
0 ignored issues
show
Bug Best Practice introduced by
The property meta_key_name does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
161
                }
162
163
                $this->metaData = (object)$niceDataArray;
164
            }
165
        }
166
        return $this->metaData;
167
    }
168
169
    protected function saveMeta()
170
    {
171
        $primaryKey = $this->primaryKey;
0 ignored issues
show
Bug Best Practice introduced by
The property primaryKey does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
172
        foreach ((array)$this->getMeta() as $data) {
173
            
174
            if (is_null($data->{$this->meta_foreign_key})) {
0 ignored issues
show
Bug Best Practice introduced by
The property meta_foreign_key does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
175
                $data->{$this->meta_foreign_key} = $this->$primaryKey;
176
            }
177
178
            if(!is_null($data->{$this->meta_value_name})) {
0 ignored issues
show
Bug Best Practice introduced by
The property meta_value_name does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
179
                $data->save();
180
            }
181
        }
182
        
183
        return true;
184
    }
185
186
    protected function deleteMeta() 
187
    {
188
        foreach ((array)$this->getMeta() as $data) {
189
            if(is_null($data->{$this->meta_value_name})) {
0 ignored issues
show
Bug Best Practice introduced by
The property meta_value_name does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
190
                $dataID = $this->meta_primary_key;
0 ignored issues
show
Bug Best Practice introduced by
The property meta_primary_key does not exist on Raystech\StarterKit\Traits\HasMeta. Since you implemented __get, consider adding a @property annotation.
Loading history...
191
                $data->destroy($data->$dataID);
192
            }
193
        }
194
        
195
        return true;
196
    }
197
198
199
    
200
}