Escaper::escape()   A
last analyzed

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
use Illuminate\Support\Arr;
8
9
trait Escaper
10
{
11
    /**
12
     * Escape all values.
13
     *
14
     * @param array $data
15
     *
16
     * @return array
17
     */
18
    protected function escape(array $data): array
19
    {
20
        $arrayDot = array_filter(Arr::dot($data));
0 ignored issues
show
Documentation introduced by
$data is of type array, but the function expects a object<Illuminate\Support\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
21
22
        foreach ($arrayDot as $key => $value) {
23
            if (is_string($value)) {
24
                $arrayDot[$key] = e($value);
25
            }
26
        }
27
28
        foreach ($arrayDot as $key => $value) {
29
            Arr::set($data, $key, $value);
30
        }
31
32
        return $data;
33
    }
34
35
    /**
36
     * Configure the validator instance.
37
     *
38
     * @param \Illuminate\Validation\Validator $validator
39
     *
40
     * @return void
41
     */
42
    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...
43
    {
44
        // Sanitize input data before submission
45
        $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...
46
    }
47
}
48