Test Failed
Push — master ( f3c0e1...0438c7 )
by Iman
01:53
created

Pipeline::_carry()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 1
nop 0
dl 0
loc 33
rs 9.392
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\Middlewarize;
4
5
use Illuminate\Pipeline\Pipeline as BasePipeline;
6
7
class Pipeline extends BasePipeline
8
{
9
    protected function _carry()
10
    {
11
        return function ($stack, $pipe) {
12
            return function ($passable) use ($stack, $pipe) {
13
                if (is_callable($pipe)) {
14
                    // If the pipe is an instance of a Closure, we will just call it directly but
15
                    // otherwise we'll resolve the pipes out of the container and call it with
16
                    // the appropriate method and arguments, returning the results back out.
17
                    return $pipe($passable, $stack);
18
                } elseif (! is_object($pipe)) {
19
                    [$name, $parameters] = $this->parsePipeString($pipe);
0 ignored issues
show
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $parameters seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
20
21
                    // If the pipe is a string we will parse the string and resolve the class out
22
                    // of the dependency injection container. We can then build a callable and
23
                    // execute the pipe function giving in the parameters that are required.
24
                    $pipe = $this->getContainer()->make($name);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $pipe, 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...
25
26
                    $parameters = array_merge([$passable, $stack], $parameters);
0 ignored issues
show
Bug introduced by
The variable $parameters seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
27
                } else {
28
                    // If the pipe is already an object we'll just make a callable and pass it to
29
                    // the pipe as-is. There is no need to do any extra parsing and formatting
30
                    // since the object we're given was already a fully instantiated object.
31
                    $parameters = [$passable, $stack];
32
                }
33
34
                $response = method_exists($pipe, $this->method)
35
                    ? $pipe->{$this->method}(...$parameters)
36
                    : $pipe(...$parameters);
37
38
                return $response;
39
            };
40
        };
41
    }
42
}