Passed
Pull Request — master (#77)
by Marco
08:43
created

Columnar::resolveField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Firesphere\SolrSearch\Helpers;
4
5
use SilverStripe\Dev\Debug;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\ORM\FieldType\DBField;
8
use SilverStripe\ORM\SS_List;
9
use SilverStripe\View\ArrayData;
10
11
class Columnar
12
{
13
    protected $component;
14
    protected $columns = [];
15
    protected $columnName = '';
16
17
    public function __construct($component, $columns = [])
18
    {
19
        $this->component = $component;
20
        $this->columns = $columns;
21
        $this->columnName = $this->columns ? array_shift($this->columns) : null;
22
    }
23
24
    public static function make($component, $columns)
25
    {
26
        if (!is_array($columns)) {
27
            $columns = array_filter(explode('.', $columns));
28
        }
29
        return new self($component, $columns);
30
    }
31
32
    /**
33
     * @param DataObject|ArrayData|SS_List $obj
34
     * @param mixed $columns
35
     *
36
     * @return null
37
     */
38
    public static function identify($obj, $columns)
39
    {
40
        $instance = self::make($obj, $columns);
41
        if ($obj instanceof DataObject || $obj instanceof ArrayData) {
42
            return $instance->resolveData();
43
        } elseif ($obj instanceof SS_List) {
0 ignored issues
show
introduced by
$obj is always a sub-type of SilverStripe\ORM\SS_List.
Loading history...
44
            return $instance->resolveList();
45
        } elseif ($obj instanceof DBField) {
46
            return $instance->resolveField();
47
        } elseif (is_array($obj)) {
48
            return $obj;
49
        } else {
50
            return null;
51
        }
52
    }
53
54
    public function resolveField()
55
    {
56
        if ($this->columnName) {
57
            $method = $this->columnName;
58
            return $this->component->$method();
59
        }
60
        return $this->component->dataValue();
61
    }
62
63
    public function resolveData()
64
    {
65
        $data = null;
66
        if (empty($this->columnName)) {
67
            return $this->component->toMap();
68
        }
69
        // Inspect component for element $relation
70
        if ($this->component->hasMethod($this->columnName)) {
71
            $relation = $this->columnName;
72
            return self::identify($this->component->$relation(), $this->columns);
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::identify($this->co...tion(), $this->columns) targeting Firesphere\SolrSearch\Helpers\Columnar::identify() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
73
        }
74
        // Inspect component has attribute
75
        if ($this->component->hasField($this->columnName)) {
76
            $data = $this->component->{$this->columnName};
77
            $dbObject = $this->component->dbObject($this->columnName);
78
            if ($dbObject && $this->columns) {
79
                $dbObject->setValue($data);
80
                return self::identify($dbObject, $this->columns);
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::identify($dbObject, $this->columns) targeting Firesphere\SolrSearch\Helpers\Columnar::identify() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
81
            }
82
        }
83
        if (!is_scalar($data)) {
84
            return self::identify($data, $this->columns);
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::identify($data, $this->columns) targeting Firesphere\SolrSearch\Helpers\Columnar::identify() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
        }
86
        return $data;
87
    }
88
89
    public function resolveList()
90
    {
91
        if (empty($this->columnName)) {
92
            return $this->component->toNestedArray();
93
        }
94
        // Inspect $component for element $relation
95
        if ($this->component->hasMethod($this->columnName)) {
96
            $relation = $this->columnName;
97
            return self::identify($this->component->$relation(), $this->columns);
0 ignored issues
show
Bug introduced by
Are you sure the usage of self::identify($this->co...tion(), $this->columns) targeting Firesphere\SolrSearch\Helpers\Columnar::identify() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
98
        }
99
        return null;
100
    }
101
}