Completed
Push — master ( 785bde...8b4d11 )
by Freek
01:29
created

src/Macros/Validate.php (1 issue)

suspicious assignments to closure imports.

Bug Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
 * @return bool
12
 */
13
class Validate
14
{
15
    public function __invoke()
16
    {
17
        return function ($callback): bool {
18
            if (is_string($callback) || is_array($callback)) {
19
                $validationRule = $callback;
20
21
                $callback = function ($item) use ($validationRule) {
22
                    if (! is_array($item)) {
23
                        $item = ['default' => $item];
24
                    }
25
26
                    if (! is_array($validationRule)) {
27
                        $validationRule = ['default' => $validationRule];
0 ignored issues
show
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...
28
                    }
29
30
                    return app('validator')->make($item, $validationRule)->passes();
31
                };
32
            }
33
34
            foreach ($this->items as $item) {
35
                if (! $callback($item)) {
36
                    return false;
37
                }
38
            }
39
40
            return true;
41
        };
42
    }
43
}
44