Validate::__invoke()   B
last analyzed

Complexity

Conditions 7
Paths 1

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5386
c 0
b 0
f 0
cc 7
nc 1
nop 0
1
<?php
2
3
namespace Spatie\CollectionMacros\Macros;
4
5
/**
6
 * Returns true if $callback returns true for every item. If $callback
7
 * is a string or an array, regard it as a validation rule.
8
 *
9
 * @param string|callable $callback
10
 **
11
 * @mixin \Illuminate\Support\Collection
12
 *
13
 * @return bool
14
 */
15
class Validate
16
{
17
    public function __invoke()
18
    {
19
        return function ($callback): bool {
20
            if (is_string($callback) || is_array($callback)) {
21
                $validationRule = $callback;
22
23
                $callback = function ($item) use ($validationRule) {
24
                    if (! is_array($item)) {
25
                        $item = ['default' => $item];
26
                    }
27
28
                    if (! is_array($validationRule)) {
29
                        $validationRule = ['default' => $validationRule];
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $validationRule, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
30
                    }
31
32
                    return app('validator')->make($item, $validationRule)->passes();
33
                };
34
            }
35
36
            foreach ($this->items as $item) {
0 ignored issues
show
Bug introduced by
The property items 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...
37
                if (! $callback($item)) {
38
                    return false;
39
                }
40
            }
41
42
            return true;
43
        };
44
    }
45
}
46