Issues (3)

Security Analysis    not enabled

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.

src/Validable.php (3 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
 * Copyright (c) Padosoft.com 2018.
4
 */
5
6
namespace Padosoft\Laravel\Validable;
7
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Facades\App;
10
use Illuminate\Validation\Factory as ValidatorFactory;
11
12
/**
13
 * Trait Validable
14
 * @package Padosoft\Laravel\Validable
15
 * @property Array $rules Validation rules
16
 * @property Array $messages Validation messages
17
 */
18
trait Validable
19
{
20
    /**
21
     * Error message bag
22
     *
23
     * @var Illuminate\Support\MessageBag
24
     */
25
    protected $errors;
26
    /**
27
     * Validator instance
28
     *
29
     * @var Illuminate\Validation\Factory
30
     */
31
    protected $validator = null;
32
33
    protected static function bootValidable()
34
    {
35
        static::saving(function (Model $model) {
36
            if (!$model->validate()){
37
				return false;
38
			}
39
        });
40
    }
41
42
    public function setValidator(ValidatorFactory $validator)
43
    {
44
        $this->validator = $validator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $validator of type object<Illuminate\Validation\Factory> is incompatible with the declared type object<Padosoft\Laravel\...ate\Validation\Factory> of property $validator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
45
    }
46
47
    public function hasValidator()
48
    {
49
        return $this->validator !== null;
50
    }
51
52
    /**
53
     * Validates current attributes against rules
54
     */
55
    public function validate()
56
    {
57
        if (!$this->hasValidator()) {
58
            $this->setValidator(App::make('validator'));
59
        }
60
61
        $v = $this->validator->make($this->attributes,
0 ignored issues
show
The property attributes 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...
62
            $this->exists ? static::getUpdatingRules($this) : static::getCreatingRules(), static::getMessages());
0 ignored issues
show
The property exists 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...
63
        if ($v->passes()) {
64
            return true;
65
        }
66
        $this->setErrors($v->messages());
67
68
        return false;
69
    }
70
71
    /**
72
     * Set error message bag
73
     *
74
     * @var Illuminate\Support\MessageBag
75
     */
76
    protected function setErrors($errors)
77
    {
78
        $this->errors = $errors;
79
    }
80
81
    /**
82
     * Retrieve error message bag
83
     */
84
    public function getErrors()
85
    {
86
        return $this->errors !== null ? $this->errors : [];
87
    }
88
89
    /**
90
     * Inverse of wasSaved
91
     */
92
    public function hasErrors()
93
    {
94
        return !empty($this->errors);
95
    }
96
97
    /**
98
     * Return true if the validation is passed and the model was saved on db
99
     * @return bool
100
     */
101
    public function wasSaved()
102
    {
103
        return empty($this->errors);
104
    }
105
106
107
    public static function getRules()
108
    {
109
        if (isset(static::$rules)) {
110
            return static::$rules;
111
        }
112
113
        return [];
114
    }
115
116
    public static function getCreatingRules()
117
    {
118
        if (isset(static::$rules)) {
119
            return static::$rules;
120
        }
121
122
        return [];
123
    }
124
125
    protected static function replacePlaceholders(Model $model, $rules)
126
    {
127
        $replaced = [];
128
        foreach ($rules as $key => $rule) {
129
            foreach ($model->attributes as $attr => $val) {
130
				if(strpos($rule,$attr)!==false && is_scalar($val)){
131
					$rule = str_replace('{' . $attr . '}', $val, $rule);
132
				}
133
            }
134
            $replaced[$key] = $rule;
135
        }
136
137
        return $replaced;
138
    }
139
140
    public static function getUpdatingRules(Model $model)
141
    {
142
        $rules = [];
143
144
        if (isset(static::$rules)) {
145
            $rules = static::$rules;
146
        }
147
148
        if (isset(static::$updating_rules)) {
149
            $rules = static::$updating_rules;
150
        }
151
152
        return static::replacePlaceholders($model, $rules);
153
    }
154
155
    public static function getMessages()
156
    {
157
        if (isset(static::$messages)) {
158
            return static::$messages;
159
        }
160
161
        return [];
162
    }
163
}
164