Completed
Pull Request — master (#403)
by
unknown
01:14
created

AppendsAttributesToResults::appendLoop()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 4
nc 4
nop 3
1
<?php
2
3
namespace Spatie\QueryBuilder\Concerns;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Spatie\QueryBuilder\Exceptions\InvalidAppendQuery;
8
9
trait AppendsAttributesToResults
10
{
11
    /** @var \Illuminate\Support\Collection */
12
    protected $allowedAppends;
13
14
    public function allowedAppends($appends): self
15
    {
16
        $appends = is_array($appends) ? $appends : func_get_args();
17
18
        $this->allowedAppends = collect($appends);
19
20
        $this->ensureAllAppendsExist();
21
22
        return $this;
23
    }
24
25
    protected function addAppendsToResults(Collection $results)
26
	{
27
	    $appends = $this->request->appends();
0 ignored issues
show
Bug introduced by
The property request 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...
28
	    return $results->each(function($item) use($appends)
29
	    {
30
	        $appends->each(function($append) use($item)
31
	        {
32
	            if(strpos($append, '.'))
33
	            {
34
		        	$subs = collect(explode('.', $append));
35
		        	$relation = $subs->shift();
36
37
		        	$item = $this->appendLoop($item, $relation, $subs);
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $item, 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...
Unused Code introduced by
$item is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
38
	            } else
39
	            {
40
	                $item->append($append);
41
	            }
42
	        });
43
	    });
44
	}
45
46
	private function appendLoop($item, $relation, $subs)
47
	{
48
		if($item->$relation !== null)
49
	    {
50
	    	if($subs->count() === 1)
51
	    	{
52
	    		$sub = $subs->first();
53
		        if($item->$relation instanceof \Illuminate\Database\Eloquent\Collection)
54
		        {
55
		            $item->$relation->each(function($model) use($sub)
56
		            	{
57
		            		$model->append($sub);
58
		            	});
59
		        } else
60
		        {
61
		            $item->$relation->append($sub);
62
	       		}
63
	    	} else
64
	    	{
65
	    		$sub = $subs->shift();
66
	    		$item->$relation = $this->appendLoop($item->$relation, $sub, $subs);
67
	    	}
68
	    }
69
	    return $item;
70
	}
71
72
    protected function ensureAllAppendsExist()
73
    {
74
        $appends = $this->request->appends();
75
76
        $diff = $appends->diff($this->allowedAppends);
77
78
        if ($diff->count()) {
79
            throw InvalidAppendQuery::appendsNotAllowed($diff, $this->allowedAppends);
80
        }
81
    }
82
}
83