Completed
Push — master ( 9c97f7...038df8 )
by Abdelrahman
02:16 queued 01:14
created

Escaper::escape()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Support\Traits;
6
7
trait Escaper
8
{
9
    /**
10
     * Escape all values.
11
     *
12
     * @param array $data
13
     *
14
     * @return array
15
     */
16
    protected function escape(array $data): array
17
    {
18
        $arrayDot = array_filter(array_dot($data));
19
20
        foreach ($arrayDot as $key => $value) {
21
            if (is_string($value)) {
22
                $arrayDot[$key] = e($value);
23
            }
24
        }
25
26
        foreach ($arrayDot as $key => $value) {
27
            array_set($data, $key, $value);
28
        }
29
30
        return $data;
31
    }
32
33
    /**
34
     * Configure the validator instance.
35
     *
36
     * @param \Illuminate\Validation\Validator $validator
37
     *
38
     * @return void
39
     */
40
    public function withValidator($validator): void
0 ignored issues
show
Unused Code introduced by
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        // Sanitize input data before submission
43
        $this->replace($this->escape($this->all()));
0 ignored issues
show
Bug introduced by
It seems like all() 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...
Bug introduced by
It seems like replace() 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...
44
    }
45
}
46