Passed
Push — master ( 6ac744...48d05f )
by Mohammad
07:18
created

Validation::requireErrorMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: shanmaseen
5
 * Date: 26/03/19
6
 * Time: 01:39 م
7
 */
8
9
namespace Shamaseen\Laravel\Ratchet\Traits;
10
11
use Illuminate\Contracts\Validation\Factory;
12
13
trait Validation
14
{
15
    /**
16
     * @param array $required
17
     */
18
    function validateRequired($required)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
19
    {
20
        foreach ($required as $input)
21
        {
22
            if(!in_array($input,(array)$this->request))
23
            {
24
                $this->error($this->request,$this->conn,$this->requireErrorMessage($input));
0 ignored issues
show
Bug introduced by
The property request 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...
Bug introduced by
The property conn 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...
Bug introduced by
It seems like error() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
25
            }
26
        }
27
    }
28
29
    /**
30
     * @param $input
31
     * @return string
32
     */
33
    function requireErrorMessage($input)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
    {
35
        return $input.' input is required.';
36
    }
37
38
    /**
39
     * Validate the given request with the given rules.
40
     *
41
     * @param  object  $request
42
     * @param  array  $rules
43
     * @param  array  $messages
44
     * @param  array  $customAttributes
45
     * @return array
46
     *
47
     * @throws \Illuminate\Validation\ValidationException
48
     */
49
    public function validate($request, array $rules,
50
                             array $messages = [], array $customAttributes = [])
51
    {
52
        return $this->getValidationFactory()->make(
53
            (array) $request, $rules, $messages, $customAttributes
54
        )->validate();
55
    }
56
57
    /**
58
     * Get a validation factory instance.
59
     *
60
     * @return \Illuminate\Contracts\Validation\Factory
61
     */
62
    protected function getValidationFactory()
63
    {
64
        return app(Factory::class);
65
    }
66
}