Issues (187)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

traits/MetaTrait.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
 * @property string $key
21
 * @property string $value
22
 * @version 2.0
23
 * @author vistart <[email protected]>
24
 */
25
trait MetaTrait
26
{
27
28
    /**
29
     * Store the guid of blame.
30
     * @var string 
31
     */
32 2
    public function behaviors()
33
    {
34 2
        return array_merge($this->getMetaBehaviors(), parent::behaviors());
35
    }
36
37
    public function getKey()
38
    {
39
        return $this->id;
0 ignored issues
show
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...
40
    }
41
42
    public function setKey($key)
43
    {
44
        return $this->id = $key;
45
    }
46
47
    public function getValue()
48
    {
49
        return $this->content;
0 ignored issues
show
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...
50
    }
51
52
    public function setValue($value)
53
    {
54
        return $this->content = $value;
55
    }
56
57
    /**
58
     * Skip all behaviors of parent class.
59
     * @return array
60
     */
61 2
    public function getMetaBehaviors()
62
    {
63 2
        return [];
64
    }
65
66
    /**
67
     * Get meta value by specified key. If key doesn't exist, null will be given.
68
     * @param string $key meta key.
69
     * @return string meta value.
70
     */
71 2
    public static function get($key)
72
    {
73 2
        $noInitModel = static::buildNoInitModel();
74 2
        $model = static::find()->where([$noInitModel->idAttribute => $key])->one();
75 2
        if ($model) {
76 2
            return $model->value;
77
        }
78 2
        return null;
79
    }
80
81
    /**
82
     * Get meta values by specified keys. If one of keys doesn't exists, it will
83
     * not appear in return array.
84
     * @param string[] $keys
85
     * @return array meta key-value pairs.
86
     */
87 2
    public static function gets($keys = null)
88
    {
89 2
        $noInitModel = static::buildNoInitModel();
90 2
        $query = static::find();
91 2
        if ($keys == null) {
92 2
            $models = $query->all();
93 2
        } elseif (is_array($keys)) {
94 2
            $array = [];
95 2
            foreach ($keys as $key) {
96 2
                if (is_string($key) && strlen($key)) {
97 2
                    $array[] = $key;
98 2
                }
99 2
            }
100 2
            $models = $query->where([$noInitModel->idAttribute => $array])->all();
101 2
        }
102 2
        $result = [];
103 2
        foreach ($models as $key => $model) {
0 ignored issues
show
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...
104 2
            $result[$model->key] = $model->value;
105 2
        }
106 2
        return $result;
107
    }
108
109
    /**
110
     * Set value.
111
     * @param string $key
112
     * @param string $value
113
     * @param string $createdBy
114
     * @return int
115
     */
116 2
    public static function set($key, $value = null, $createdBy = null)
117
    {
118 2
        $noInitModel = static::buildNoInitModel();
119 2
        $model = static::find()->where([$noInitModel->idAttribute => $key])->one();
120 2
        if ($value == null && $model) {
0 ignored issues
show
It seems like you are loosely comparing $value of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
121 2
            return $model->delete();
122
        }
123 2
        if (!$model) {
124 2
            if (empty($createdBy) && !Yii::$app->user->isGuest) {
125
                $createdBy = Yii::$app->user->identity->guid;
126
            }
127 2
            $model = new static([$noInitModel->idAttribute => $key, $noInitModel->createdByAttribute => $createdBy]);
0 ignored issues
show
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...
128 2
        }
129 2
        $model->value = $value;
130 2
        return $model->save();
131
    }
132
133
    /**
134
     * Set values in batch.
135
     * @param array $keys meta key-value pairs.
136
     * @param string $createdBy
137
     * @return false if $keys is not an array.
138
     */
139 2
    public static function sets($keys, $createdBy = null)
140
    {
141 2
        if (!is_array($keys)) {
142 2
            return false;
143
        }
144 2
        foreach ($keys as $key => $value) {
145 2
            static::set($key, $value, $createdBy);
146 2
        }
147 2
    }
148
149 2
    public static function remove($key)
150
    {
151 2
        return static::set($key);
152
    }
153
}
154