Validation::requireErrorMessage()   A
last analyzed

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
use Illuminate\Support\Collection;
13
14
trait Validation
15
{
16
    /**
17
     * @param array $required
18
     */
19
    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...
20
    {
21
        foreach ($required as $input)
22
        {
23
            if(!in_array($input,(array)$this->request))
24
            {
25
                $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...
26
            }
27
        }
28
    }
29
30
    /**
31
     * @param $input
32
     * @return string
33
     */
34
    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...
35
    {
36
        return $input.' input is required.';
37
    }
38
39
    /**
40
     * Validate the given request with the given rules.
41
     *
42
     * @param  object  $request
43
     * @param  array  $rules
44
     * @param  array  $messages
45
     * @param  array  $customAttributes
46
     * @return array
47
     *
48
     * @throws \Illuminate\Validation\ValidationException
49
     */
50
    public function validate($request, array $rules,
51
                             array $messages = [], array $customAttributes = [])
52
    {
53
        if($request instanceof Collection)
54
            $request = $request->toArray();
55
56
        return $this->getValidationFactory()->make(
57
            (array) $request, $rules, $messages, $customAttributes
58
        )->validate();
59
    }
60
61
    /**
62
     * Get a validation factory instance.
63
     *
64
     * @return \Illuminate\Contracts\Validation\Factory
65
     */
66
    protected function getValidationFactory()
67
    {
68
        return app(Factory::class);
69
    }
70
}
71