Passed
Push — master ( 56fba7...dbe3ef )
by Andreas
03:35
created

midgard_collector   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
dl 0
loc 177
ccs 80
cts 84
cp 0.9524
rs 9.2
c 0
b 0
f 0
wmc 34
lcom 1
cbo 7

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A set_key_property() 0 10 2
A add_value_property() 0 15 4
B build_property_select() 0 21 5
C execute() 0 46 9
A get() 0 8 3
A get_subkey() 0 7 4
A _has_results() 0 5 2
A list_keys() 0 12 3
1
<?php
2
/**
3
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
4
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
6
 */
7
8
use midgard\portable\api\error\exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, exception.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use midgard\portable\storage\connection;
10
11
class midgard_collector extends midgard_query_builder
12
{
13
    /**
14
     * the results determined by execute
15
     *
16
     * @var array
17
     */
18
    private $_results = null;
19
20
    /**
21
     *
22
     * @var string
23
     */
24
    private $key_property = "guid";
25
26
    private $value_properties = array();
27
28 11
    public function __construct($class, $field = null, $value = null)
29
    {
30 11
        parent::__construct($class);
31 11
        if ($field) {
32 11
            $this->add_constraint($field, '=', $value);
33 11
        }
34 11
    }
35
36 5
    public function set_key_property($property)
37
    {
38
        // after execute there is no sense in changing the key property
39 5
        if ($this->_results !== null) {
40
            return false;
41
        }
42 5
        $this->key_property = $property;
43
44 5
        return true;
45
    }
46
47 6
    public function add_value_property($property)
48
    {
49 6
        if ($this->_results !== null) {
50
            return false;
51
        }
52
53 6
        if (!isset($this->value_properties[$property])) {
54
            try {
55 6
                $this->value_properties[$property] = $this->build_property_select($property);
56 6
            } catch (exception $e) {
57 1
                return false;
58
            }
59 5
        }
60 5
        return true;
61
    }
62
63 10
    protected function build_property_select($property)
64
    {
65 10
        $parsed = $this->parse_constraint_name($property);
66
67
        // for properties like up.name
68 9
        if (   strpos($property, ".") !== false
69 9
            && !(strpos($property, "metadata") === 0)) {
70 1
            return $parsed['name'] . " as " . str_replace(".", "_", $property);
71
        }
72
73 9
        $cm = connection::get_em()->getClassMetadata($this->classname);
74 9
        if (array_key_exists($property, $cm->midgard['field_aliases'])) {
0 ignored issues
show
Bug introduced by
The property midgard does not seem to exist in Doctrine\ORM\Mapping\ClassMetadata.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
75 1
            return $parsed['name'] . " as " . str_replace(".", "_", $property);
76
        }
77
78 9
        if ($cm->hasAssociation($property)) {
79 1
            return 'IDENTITY(' . $parsed['name'] . ") as " . $property;
80
        }
81
82 9
        return $parsed['name'];
83
    }
84
85 8
    public function execute()
86
    {
87 8
        if ($this->_results !== null) {
88
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method midgard_query_builder::execute of type midgard\portable\api\object[].

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...
89
        }
90 8
        $this->check_groups();
91 8
        $properties = $this->value_properties;
92 8
        if (!isset($this->value_properties[$this->key_property])) {
93
            try {
94 7
                $properties[] = $this->build_property_select($this->key_property);
95 7
            } catch (exception $e) {
96
                throw new exception('Property "' . $this->key_property . '" not found in "' . $this->classname . '"', exception::INVALID_PROPERTY);
97
            }
98 7
        }
99
100 8
        $this->qb->addSelect(implode(", ", $properties));
101 8
        $this->pre_execution();
102 8
        $results = $this->qb->getQuery()->getArrayResult();
103 8
        $this->post_execution();
104
105 8
        $cm = connection::get_em()->getClassMetadata($this->classname);
106
        // map results by current key property
107 8
        $results_map = array();
108 8
        foreach ($results as $result) {
109 8
            foreach ($result as $key => &$value) {
110
                // for metadata fields remove the "metadata_" prefix
111 8
                if (strpos($key, "metadata_") !== false) {
112 1
                    $result[str_replace("metadata_", "", $key)] = $value;
113 1
                    unset($result[$key]);
114 1
                }
115
                // TODO: find out why Doctrine doesn't do this on its own
116 8
                if ($cm->hasAssociation($key)) {
117 1
                    $value = (int) $value;
118 1
                }
119 8
            }
120 8
            $key = $result[$this->key_property];
121 8
            if (!isset($this->value_properties[$this->key_property])) {
122 7
                unset($result[$this->key_property]);
123 7
            }
124
125 8
            $results_map[$key] = $result;
126 8
        }
127
128 8
        $this->_results = $results_map;
129 8
        return true;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return true; (boolean) is incompatible with the return type of the parent method midgard_query_builder::execute of type midgard\portable\api\object[].

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...
130
    }
131
132
    /**
133
     *
134
     * @param string $key
135
     * @return array
136
     */
137 2
    public function get($key)
138
    {
139 2
        if (   !$this->_has_results()
140 2
            || !isset($this->_results[$key])) {
141 1
            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 midgard_collector::get of type array.

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...
142
        }
143 2
        return $this->_results[$key];
144
    }
145
146
    /**
147
     *
148
     * @param string $key
149
     * @param string $property
150
     */
151 2
    public function get_subkey($key, $property)
152
    {
153 2
        if (!$this->_has_results() || !isset($this->_results[$key]) || !isset($this->_results[$key][$property])) {
154 1
            return false;
155
        }
156 2
        return $this->_results[$key][$property];
157
    }
158
159
    /**
160
     * check whether we got any results to work on
161
     *
162
     * @return boolean
163
     */
164 8
    private function _has_results()
165
    {
166
        // execute was not called or we got an empty resultset
167 8
        return !($this->_results === null || count($this->_results) == 0);
168
    }
169
170
    /**
171
     *
172
     *
173
     * @return array
174
     */
175 8
    public function list_keys()
176
    {
177 8
        if (!$this->_has_results()) {
178 1
            return array();
179
        }
180
181 8
        $keys = array();
182 8
        foreach ($this->_results as $key => $result) {
183 8
            $keys[$key] = '';
184 8
        }
185 8
        return $keys;
186
    }
187
}
188