Completed
Push — master ( 5ebb18...910b8e )
by Iman
01:39
created

Pipeline::through()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3
1
<?php
2
3
namespace Imanghafoori\Middlewarize;
4
5
use Illuminate\Pipeline\Pipeline as CorePipe;
6
7
class Pipeline extends CorePipe
8
{
9
    /**
10
     * Set the array of pipes.
11
     *
12
     * @param  callable|array|string  $pipes
13
     * @return \Illuminate\Pipeline\Pipeline
14
     */
15 11
    public function through($pipes)
16
    {
17 11
        $pipes = is_callable($pipes) ? [$pipes] : $pipes;
18 11
        $this->pipes = is_array($pipes) ? $pipes : func_get_args();
19
20 11
        return $this;
21
    }
22
23
    /**
24
     * Get a Closure that represents a slice of the application onion.
25
     *
26
     * @return \Closure
27
     */
28 11
    protected function carry()
29
    {
30
        return function ($stack, $pipe) {
31
            return function ($passable) use ($stack, $pipe) {
32 11
                if (is_callable($pipe)) {
33
                    // If the pipe is an instance of a Closure, we will just call it directly but
34
                    // otherwise we'll resolve the pipes out of the container and call it with
35
                    // the appropriate method and arguments, returning the results back out.
36 3
                    return $pipe($passable, $stack);
37 9
                } elseif (! is_object($pipe)) {
38 8
                    [$name, $parameters] = $this->parsePipeString($pipe);
0 ignored issues
show
Bug introduced by
The variable $name 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...
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...
39
40
                    // If the pipe is a string we will parse the string and resolve the class out
41
                    // of the dependency injection container. We can then build a callable and
42
                    // execute the pipe function giving in the parameters that are required.
43 8
                    $name = explode('@', $name);
0 ignored issues
show
Bug introduced by
The variable $name 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...
44 8
                    $pipe = $this->getContainer()->make($name[0]);
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...
45
46 8
                    $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...
47
                } else {
48
                    // If the pipe is already an object we'll just make a callable and pass it to
49
                    // the pipe as-is. There is no need to do any extra parsing and formatting
50
                    // since the object we're given was already a fully instantiated object.
51 1
                    $parameters = [$passable, $stack];
52
                }
53
54 9
                $method = $name[1] ?? $this->method;
55
56 9
                return method_exists($pipe, $method)
57 9
                    ? $pipe->{$method}(...$parameters)
58 9
                    : $pipe(...$parameters);
59 11
            };
60 11
        };
61
    }
62
}
63