Ajde_Crud_Options::_select()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 2
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class Ajde_Crud_Options extends Ajde_Object_Standard
4
{
5
    protected $_key;
6
7
    /**
8
     * @var Ajde_Crud_Options
9
     */
10
    protected $_parent = null;
11
12
    /**
13
     * @var array
14
     */
15
    public $_stack = [];
16
17
    public function __construct()
18
    {
19
        $this->_active = $this;
0 ignored issues
show
Bug introduced by
The property _active 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...
20
    }
21
22
    // Protected functions
23
24
    protected function _select($name, $key = null)
25
    {
26
        $key = isset($key) ? $key : $name;
27
        // Get new active object
28
        $className = get_class($this).'_'.ucfirst($name);
29
        /* @var $new Ajde_Crud_Options */
30
        $new = new $className();
31
        $new->_parent = $this;
32
        $new->_key = $key;
33
        if (isset($this->_stack[$key])) {
34
            $new->_stack = $this->_stack[$key];
35
        }
36
37
        return $new;
38
    }
39
40
    protected function _set($key, $value)
41
    {
42
        parent::_set($key, $value);
43
44
        return $this;
45
    }
46
47
    // Public functions
48
49
    /**
50
     * @return Ajde_Crud_Options
51
     */
52
    public function up($obj = false)
53
    {
54
        if (!$obj) {
55
            $obj = $this;
56
        }
57
        if (!isset($obj->_parent)) {
58
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Ajde_Crud_Options::up of type Ajde_Crud_Options.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
59
        }
60
        $obj->_parent->_stack[$obj->_key] = array_merge($obj->_stack, $obj->values());
0 ignored issues
show
Bug introduced by
It seems like $obj is not always an object, but can also be of type boolean. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
61
62
        return $obj->_parent;
63
    }
64
65
    /**
66
     * @return Ajde_Crud_Options
67
     */
68
    public function finished()
69
    {
70
        $test = $this->up();
71
        while ($test) {
72
            $test = $test->up();
73
        }
74
75
        return $this;
76
    }
77
78
    /**
79
     * @return Ajde_Crud_Options
80
     */
81
    public function display()
82
    {
83
        var_dump($this->_stack);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->_stack); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
84
85
        return $this;
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function getArray()
92
    {
93
        return $this->_stack;
94
    }
95
96
    // =========================================================================
97
    // Select functions
98
    // =========================================================================
99
100
    /**
101
     * @return Ajde_Crud_Options_Fields
102
     */
103
    public function selectFields()
104
    {
105
        return $this->_select('fields');
106
    }
107
108
    /**
109
     * @return Ajde_Crud_Options_List
110
     */
111
    public function selectList()
112
    {
113
        return $this->_select('list');
114
    }
115
116
    /**
117
     * @return Ajde_Crud_Options_Edit
118
     */
119
    public function selectEdit()
120
    {
121
        return $this->_select('edit');
122
    }
123
124
    // =========================================================================
125
    // Set functions
126
    // =========================================================================
127
}
128