Passed
Branch master (0f077f)
by Henri
01:20
created

MagicsTrait::getVars()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace HnrAzevedo\Datamanager;
4
5
trait MagicsTrait{
6
7
    public function __set(string $prop,$value)
8
    {
9
        if(is_array($value)){
10
            $attr = array_keys($value)[0];
11
            $this->data[$prop][$attr] = $value[$attr];
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
12
            return $this;
13
        }
14
15
        if($this->full){
0 ignored issues
show
Bug Best Practice introduced by
The property full does not exist on HnrAzevedo\Datamanager\MagicsTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
16
            switch($this->data[$prop]['type']){
17
                case 'date':
18
                    $value = (date_format( date_create_from_format(DATAMANAGER_CONFIG['dateformat'],$value) , 'Y-m-d'));
19
                    break;
20
            }
21
        }
22
23
        $this->isSettable($prop);
0 ignored issues
show
Bug introduced by
It seems like isSettable() 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

23
        $this->/** @scrutinizer ignore-call */ 
24
               isSettable($prop);
Loading history...
24
25
        $this->data[$prop]['changed'] = ($prop === $this->primary) ? false : true;
0 ignored issues
show
Bug Best Practice introduced by
The property primary does not exist on HnrAzevedo\Datamanager\MagicsTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
26
        $this->data[$prop]['value'] = $value;
27
        
28
        return $this;
29
    }
30
31
    public function getVars(): array
32
    {
33
        $vars = [];
34
        foreach($this->data as $var => $value){
35
            $vars[$var] = null;
36
        }
37
        return $vars;
38
    }
39
40
    public function __get(string $field)
41
    {
42
        $this->isSettable($field);
43
44
        if($this->full){
0 ignored issues
show
Bug Best Practice introduced by
The property full does not exist on HnrAzevedo\Datamanager\MagicsTrait. Since you implemented __get, consider adding a @property annotation.
Loading history...
45
            switch($this->data[$field]['type']){
46
                case 'date': 
47
                    return (!empty($this->data[$field]['value'])) ? (@date_format( @date_create_from_format('Y-m-d' , $this->data[$field]['value'] ) , DATAMANAGER_CONFIG['dateformat'])) : null ;
48
                    break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
49
                case 'datetime': 
50
                    return (!empty($this->data[$field]['value'])) ? (@date_format( @date_create_from_format('Y-m-d H:i:s' , $this->data[$field]['value'] ) , DATAMANAGER_CONFIG['datetimeformat'])) : null ;
51
                    break;
52
            }
53
        }
54
55
        return $this->data[$field]['value'];
56
    }
57
}