Completed
Push — master ( 55b0c5...22df67 )
by vistart
19:36
created

MetaTrait::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.name/
9
 * @copyright Copyright (c) 2016 vistart
10
 * @license https://vistart.name/license/
11
 */
12
13
namespace vistart\Models\traits;
14
15
use Yii;
16
17
/**
18
 * Description of MetaTrait
19
 *
20
 * @version 2.0
21
 * @author vistart <[email protected]>
22
 */
23
trait MetaTrait
24
{
25
26
    /**
27
     * Store the guid of blame.
28
     * @var string 
29
     */
30
    public function behaviors()
31
    {
32
        return array_merge($this->getMetaBehaviors(), parent::behaviors());
33
    }
34
35
    public function getKey()
36
    {
37
        return $this->id;
0 ignored issues
show
Bug introduced by
The property id does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
    }
39
40
    public function getValue()
41
    {
42
        return $this->content;
0 ignored issues
show
Bug introduced by
The property content does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
43
    }
44
45
    /**
46
     * Skip all behaviors of parent class.
47
     * @return array
48
     */
49
    public function getMetaBehaviors()
50
    {
51
        return [];
52
    }
53
54
    /**
55
     * Get meta value by specified key. If key doesn't exist, null will be given.
56
     * @param string $key meta key.
57
     * @return string meta value.
58
     */
59
    public static function get($key)
60
    {
61
        $noInitModel = static::buildNoInitModel();
62
        $model = static::find()->where([$noInitModel->idAttribute => $key])->one();
63
        if ($model) {
64
            return $model->value;
65
        }
66
        return null;
67
    }
68
69
    /**
70
     * Get meta values by specified keys. If one of keys doesn't exists, it will
71
     * not appear in return array.
72
     * @param string[] $keys
73
     */
74
    public static function gets($keys = null)
75
    {
76
        $noInitModel = static::buildNoInitModel();
77
        $query = static::find();
78
        if ($keys == null) {
79
            $models = $query->all();
80
        } elseif (is_array($keys)) {
81
            $array = [];
82
            foreach ($keys as $key) {
83
                if (is_string($key) && strlen($key)) {
84
                    $array[] = $key;
85
                }
86
            }
87
            $models = $query->where([$noInitModel->idAttribute => $array])->all();
88
        }
89
        $result = [];
90
        foreach ($models as $key => $model) {
0 ignored issues
show
Bug introduced by
The variable $models does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
91
            $result[$model->key] = $model->value;
92
        }
93
        return $result;
94
    }
95
96
    public static function set($key, $value = null, $createdBy = null)
97
    {
98
        $noInitModel = static::buildNoInitModel();
99
        $model = static::find()->where([$noInitModel->idAttribute => $key])->one();
100
        if ($value == null && $model) {
101
            return $model->delete();
102
        }
103
        if (!$model) {
104
            if (empty($createdBy) && !Yii::$app->user->isGuest) {
105
                $createdBy = Yii::$app->user->identity->guid;
106
            }
107
            $model = new static([$noInitModel->idAttribute => $key, $noInitModel->createdByAttribute => $createdBy]);
0 ignored issues
show
Unused Code introduced by
The call to MetaTrait::__construct() has too many arguments starting with array($noInitModel->idAt...ttribute => $createdBy).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
108
        }
109
        $model->value = $value;
110
        return $model->save();
111
    }
112
113
    public static function sets($keys, $createdBy = null)
114
    {
115
        if (!is_array($keys)) {
116
            return false;
117
        }
118
        foreach ($keys as $key => $value) {
119
            static::set($key, $value, $createdBy);
120
        }
121
    }
122
123
    public static function remove($key)
124
    {
125
        return static::set($key);
126
    }
127
}
128