Completed
Push — master ( 6c3b36...3a7f48 )
by wen
02:41
created

Column::getRelationValue()   D

Complexity

Conditions 9
Paths 18

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 4.909
c 0
b 0
f 0
cc 9
eloc 22
nc 18
nop 0
1
<?php
2
3
4
namespace Sco\Admin\Column;
5
6
use Carbon\Carbon;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Relations\BelongsTo;
9
use Illuminate\Support\Collection;
10
use Sco\Attributes\HasOriginalAndAttributesTrait;
11
12
13
abstract class Column
14
{
15
    use HasOriginalAndAttributesTrait;
16
17
    protected $defaultsAttributes = [
18
        'title'    => '',
19
        'sortable' => false,
20
        'width'    => 0,
21
    ];
22
23
    protected $name;
24
25
    protected $defaults = [];
26
27
    protected $configFactory;
28
29
    protected $model;
30
31
    public function __construct($name, $attributes)
32
    {
33
        //$this->configFactory = $factory;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
34
        $this->name = $name;
35
        $this->setAttribute(array_merge($this->getDefaults(), $attributes));
36
    }
37
38
    protected function getDefaults()
39
    {
40
        return array_merge($this->defaultsAttributes, $this->defaults);
41
    }
42
43
    public function getName()
44
    {
45
        return $this->name;
46
    }
47
48
    public function getModel()
49
    {
50
        return $this->model;
51
    }
52
53
    public function setModel(Model $model)
54
    {
55
        $this->model = $model;
56
        return $this;
57
    }
58
59
    public function toArray()
60
    {
61
        return [
62
            'key'      => $this->name,
63
            'title'    => $this->getAttribute('title'),
64
            'sortable' => $this->getAttribute('sortable'),
65
            'width'    => $this->getAttribute('width'),
66
        ];
67
    }
68
69
    protected function isRelationship()
70
    {
71
        return $this->hasAttribute('relationship');
72
    }
73
74
    protected function getRelationValue()
75
    {
76
        $model = $this->getModel();
77
        $relation = $model->{$this->getAttribute('relationship')}();
78
        $fields = $this->getAttribute('fields', $this->name);
79
        $results = $relation->getResults();
80
        if (strpos($fields, ',') === false) {
81
            if ($results instanceof Model) {
82
                return $results->{$fields};
83
            }
84
            if ($results instanceof Collection) {
85
                return $results->pluck($fields);
86
            }
87
        }
88
89
        $values = collect();
90
        $fields = is_array($fields) ? $fields : explode(',', $fields);
91
92
        if ($results instanceof Model) {
93
            foreach ($fields as $field) {
94
                $values->put($field, $results->{$field});
95
            }
96
        }
97
98
        if ($results instanceof Collection) {
99
            $values = $results->map(function ($item) use ($fields) {
100
                $map = collect();
101
                foreach ($fields as $field) {
102
                    $map->put($field, $item->{$field});
103
                }
104
                return $map;
105
            });
106
        }
107
        return $values;
108
    }
109
110
    public function render()
111
    {
112
        if (isset($this->getModel()->{$this->name})) {
113
            $value = $this->getModel()->{$this->name};
114
        }
115
116
        if ($this->isRelationship()) {
117
            $value = $this->getRelationValue();
118
        }
119
120
        if ($value instanceof Carbon) {
121
            if ($this->hasAttribute('format')) {
122
                if ($this->getAttribute('format') == 'humans') {
123
                    $value = $value->diffForHumans();
0 ignored issues
show
Bug introduced by
The variable $value does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
124
                } else {
125
                    $value = $value->format($this->getAttribute('format'));
126
                }
127
            }
128
        }
129
130
        return $value;
131
    }
132
133
    public function __toString()
134
    {
135
        return $this->toJson();
136
    }
137
}
138