Completed
Push — master ( 9d4592...64b389 )
by Andreas
05:50
created

object::__debugInfo()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 2
nop 0
dl 0
loc 19
ccs 0
cts 15
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
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
namespace midgard\portable\api;
8
9
use midgard\portable\storage\connection;
10
use midgard\portable\storage\objectmanager;
11
use midgard\portable\storage\collection;
12
use midgard\portable\storage\metadata\entity as metadata_interface;
13
use midgard\portable\mgdschema\translator;
14
use midgard\portable\api\error\exception;
15
use Doctrine\ORM\Query;
16
use midgard_connection;
17
use Doctrine\Common\Persistence\Proxy;
18
19
abstract class object extends dbobject
20
{
21
    public $action = ''; // <== does this need to do anything?
22
23
    private $collections = array();
24
25
    /**
26
     *
27
     * @param mixed $id ID or GUID
28
     */
29 118
    public function __construct($id = null)
30
    {
31 118
        if ($id !== null)
32 118
        {
33 46
            if (is_int($id))
34 46
            {
35 56
                $this->get_by_id($id);
36 37
            }
37 12
            else if (is_string($id))
38 12
            {
39 12
                $this->get_by_guid($id);
40 11
            }
41 44
        }
42 116
    }
43
44
    /**
45
     *
46
     * @param string $classname
47
     * @return collection
48
     */
49 16
    private function get_collection($classname)
50
    {
51 16
        if (!array_key_exists($classname, $this->collections))
52 16
        {
53 16
            $this->collections[$classname] = new collection($classname);
54 16
        }
55 16
        return $this->collections[$classname];
56
    }
57
58
    public function __debugInfo()
59
    {
60
        $ret = parent::__debugInfo();
61
        if (property_exists($this, 'metadata'))
62
        {
63
            $metadata = new \stdClass;
64
            foreach ($this->cm->getFieldNames() as $name)
65
            {
66
                if (strpos($name, 'metadata_') !== false)
67
                {
68
                    $fieldname = str_replace('metadata_', '', $name);
69
                    $metadata->$fieldname = $this->__get($name);
70
                }
71
            }
72
            $ret['metadata'] = $metadata;
73
        }
74
75
        return $ret;
76
    }
77
78 101
    public function __set($field, $value)
79
    {
80 101
        if ($field == 'guid')
81 101
        {
82 6
            return;
83
        }
84 101
        parent::__set($field, $value);
85 101
    }
86
87 109
    public function __get($field)
88 1
    {
89
        if (   $field === 'metadata'
90 109
            && property_exists($this, 'metadata')
91 109
            && $this->metadata === null)
0 ignored issues
show
Documentation introduced by
The property metadata does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
92 109
        {
93 96
            $this->metadata = new metadata($this);
0 ignored issues
show
Documentation introduced by
The property metadata does not exist on object<midgard\portable\api\object>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
94 96
        }
95
96 109
        return parent::__get($field);
97
    }
98
99 1
    public function __call($method, $args)
100
    {
101 1
        if ($method === 'list')
102 1
        {
103 1
            return $this->_list();
104
        }
105
        throw new \BadMethodCallException("Unknown method " . $method . " on " . get_class($this));
106
    }
107
108 27
    protected function load_parent(array $candidates)
109
    {
110 27
        foreach ($candidates as $candidate)
111
        {
112 4
            if ($this->$candidate !== null)
113 4
            {
114
                //Proxies become stale if the object itself is detached, so we have to re-fetch
115 4
                if (   $this->$candidate instanceof \Doctrine\ORM\Proxy\Proxy
116 4
                    && $this->$candidate->__isInitialized())
117 4
                {
118
                    try
119
                    {
120 1
                        $this->$candidate->get_by_id($this->$candidate->id);
121
                    }
122 26
                    catch (exception $e)
123
                    {
124
                        connection::log()->error('Failed to refresh parent from proxy: ' . $e->getMessage());
125
                        return null;
126
                    }
127 1
                }
128 4
                return $this->$candidate;
129
            }
130 2
        }
131 1
        return null;
132
    }
133
134 40
    public function get_by_id($id)
135
    {
136 40
        $entity = connection::get_em()->find(get_class($this), $id);
137
138 40
        if ($entity === null)
139 40
        {
140 3
            throw exception::not_exists();
141
        }
142
        // According to Doctrine documentation, proxies should be transparent, but in practice,
143
        // there will be problems if we don't force-load
144
        if (   $entity instanceof \Doctrine\ORM\Proxy\Proxy
145 39
            && !$entity->__isInitialized())
146 39
        {
147
            try
148
            {
149 7
                $entity->__load();
150
            }
151 7
            catch (\Doctrine\ORM\EntityNotFoundException $e)
152
            {
153 1
                throw exception::object_purged();
154
            }
155 6
        }
156 39
        if ($entity->metadata_deleted)
157 39
        {
158
            // This can happen when the "deleted" entity is still in EM's identity map
159
            throw exception::object_deleted();
160
        }
161 39
        if (empty($entity->guid))
162 39
        {
163
            // This can happen when a reference proxy to a purged entity is still in EM's identity map
164
            throw exception::object_purged();
165
        }
166
167 39
        $this->populate_from_entity($entity);
168
169 39
        connection::get_em()->detach($entity);
170 39
        midgard_connection::get_instance()->set_error(MGD_ERR_OK);
171 39
        return true;
172
    }
173
174 14
    public function get_by_guid($guid)
175
    {
176 14
        if (!mgd_is_guid($guid))
177 14
        {
178 1
            throw new \InvalidArgumentException("'$guid' is not a valid guid");
179
        }
180 14
        $entity = connection::get_em()->getRepository(get_class($this))->findOneBy(array('guid' => $guid));
181 14
        if ($entity === null)
182 14
        {
183 1
            throw exception::not_exists();
184
        }
185 13
        $this->populate_from_entity($entity);
186
187 13
        connection::get_em()->detach($entity);
188 13
        midgard_connection::get_instance()->set_error(MGD_ERR_OK);
189 13
        return true;
190
    }
191
192 94
    public function create()
193
    {
194 94
        if (!empty($this->id))
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
195 94
        {
196 3
            exception::duplicate();
197 3
            return false;
198
        }
199 94
        if (   !$this->is_unique()
200 94
            || !$this->check_parent())
201 94
        {
202 2
            return false;
203
        }
204 94
        if (!$this->check_fields())
205 94
        {
206 1
            return false;
207 1
        }
208 1
        try
209
        {
210 93
            $om = new objectmanager(connection::get_em());
0 ignored issues
show
Compatibility introduced by
\midgard\portable\storage\connection::get_em() of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
211 93
            $om->create($this);
212
        }
213 93
        catch (\Exception $e)
214
        {
215
            exception::internal($e);
216
            return false;
217
        }
218
219 93
        midgard_connection::get_instance()->set_error(MGD_ERR_OK);
220
221 93
        return ($this->id != 0);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
222
    }
223
224 14 View Code Duplication
    public function update()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
225
    {
226 14
        if (empty($this->id))
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
227 14
        {
228 1
            midgard_connection::get_instance()->set_error(MGD_ERR_INTERNAL);
229 1
            return false;
230
        }
231 13
        if (!$this->check_fields())
232 13
        {
233 1
            return false;
234
        }
235
        try
236
        {
237 12
            $om = new objectmanager(connection::get_em());
0 ignored issues
show
Compatibility introduced by
\midgard\portable\storage\connection::get_em() of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
238 12
            $om->update($this);
239
        }
240 12
        catch (\Exception $e)
241
        {
242
            exception::internal($e);
243
            return false;
244
        }
245 12
        midgard_connection::get_instance()->set_error(MGD_ERR_OK);
246
247 12
        return true;
248
    }
249
250
    /**
251
     * @todo: Tests indicate that $check_dependencies is ignored in the mgd2 extension,
252
     * so we might consider ignoring it, too
253
     */
254 27
    public function delete($check_dependencies = true)
255
    {
256 27
        if (empty($this->id))
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
257 27
        {
258 1
            midgard_connection::get_instance()->set_error(MGD_ERR_INVALID_PROPERTY_VALUE);
259 1
            return false;
260
        }
261
        if (   $check_dependencies
262 26
            && $this->has_dependents())
263 26
        {
264 4
            exception::has_dependants();
265 4
            return false;
266
        }
267 26
        if (!($this instanceof metadata_interface))
268 26
        {
269
            return $this->purge($check_dependencies);
270
        }
271 26
        if ($this->metadata_deleted)
0 ignored issues
show
Documentation introduced by
The property metadata_deleted does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
272 26
        {
273 1
            return true;
274
        }
275
276
        try
277
        {
278 26
            $om = new objectmanager(connection::get_em());
0 ignored issues
show
Compatibility introduced by
\midgard\portable\storage\connection::get_em() of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
279 26
            $om->delete($this);
280
        }
281 26
        catch (\Exception $e)
282
        {
283
            exception::internal($e);
284
            return false;
285
        }
286
287 26
        midgard_connection::get_instance()->set_error(MGD_ERR_OK);
288 26
        return true;
289
    }
290
291 94
    private function is_unique()
292
    {
293 94
        $this->initialize();
294
295 94
        if (empty($this->cm->midgard['unique_fields']))
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
296 94
        {
297 89
            return true;
298
        }
299
300 7
        $qb = connection::get_em()->createQueryBuilder();
301 7
        $qb->from(get_class($this), 'c');
302 7
        $conditions = $qb->expr()->andX();
303 7 View Code Duplication
        if ($this->id)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
304 7
        {
305
            $parameters = array
306
            (
307
                'id' => $this->id
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
308
            );
309
            $conditions->add($qb->expr()->neq('c.id', ':id'));
310
        }
311 7
        $found = false;
312 7
        foreach ($this->cm->midgard['unique_fields'] as $field)
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
313
        {
314 7
            if (empty($this->$field))
315 7
            {
316
                //empty names automatically pass according to Midgard logic
317 2
                continue;
318
            }
319 6
            $conditions->add($qb->expr()->eq('c.' . $field, ':' . $field));
320 6
            $parameters[$field] = $this->$field;
0 ignored issues
show
Bug introduced by
The variable $parameters 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...
321 6
            $found = true;
322 7
        }
323
324 7
        if (!$found)
325 7
        {
326 2
            return true;
327
        }
328
329 6
        if (!empty($this->cm->midgard['upfield']))
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
330 6
        {
331
            // TODO: This needs to be changed so that value is always numeric, since this is how midgard does it
332 5
            if ($this->{$this->cm->midgard['upfield']} === null)
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
333 5
            {
334 5
                $conditions->add($qb->expr()->isNull('c.' . $this->cm->midgard['upfield']));
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
335 5
            }
336
            else
337
            {
338 3
                $conditions->add($qb->expr()->eq('c.' . $this->cm->midgard['upfield'], ':' . $this->cm->midgard['upfield']));
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
339 3
                $parameters[$this->cm->midgard['upfield']] = $this->{$this->cm->midgard['upfield']};
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
340
            }
341 5
        }
342 6
        $qb->where($conditions)
343 6
            ->setParameters($parameters);
344
345 6
        $qb->select("count(c)");
346 6
        $count = intval($qb->getQuery()->getSingleScalarResult());
347
348 6
        if ($count !== 0)
349 6
        {
350 1
            exception::object_name_exists();
351 1
            return false;
352
        }
353 6
        return true;
354
    }
355
356 94
    private function check_parent()
357
    {
358 94
        $this->initialize();
359
360 94
        if (   empty($this->cm->midgard['parentfield'])
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
361 12
            || empty($this->cm->midgard['parent']))
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
362 94
        {
363 94
            return true;
364
        }
365
366 7
        if (empty($this->{$this->cm->midgard['parentfield']}))
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
367 7
        {
368 1
            exception::object_no_parent();
369 1
            return false;
370
        }
371 7
        return true;
372
    }
373
374 94
    private function check_fields()
375
    {
376 94
        $this->initialize();
377
378 94
        foreach ($this->cm->fieldMappings as $name => $field)
0 ignored issues
show
Bug introduced by
Accessing fieldMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
379
        {
380 94
            if (   $field['midgard:midgard_type'] == translator::TYPE_GUID
381 94
                && !empty($this->$name)
382 94
                && !mgd_is_guid($this->$name))
383 94
            {
384 2
                exception::invalid_property_value("'" . $name . "' property's value is not a guid.");
385 2
                return false;
386
            }
387 94
        }
388 93
        return true;
389
    }
390
391
    public function is_in_parent_tree($root_id, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $root_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
392
    {
393
        return false;
394
    }
395
396
    public function is_in_tree($root_id, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $root_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
397
    {
398
        return false;
399
    }
400
401 32
    public function has_dependents()
402
    {
403 32
        $this->initialize();
404
405 32
        $stat = false;
406
407 32
        if (!empty($this->cm->midgard['upfield']))
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
408 32
        {
409 27
            $qb = connection::get_em()->createQueryBuilder();
410 27
            $qb->from(get_class($this), 'c')
411 27
                ->where('c.' . $this->cm->midgard['upfield'] . ' = ?0')
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
412 27
                ->setParameter(0, $this->id)
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
413 27
                ->select("COUNT(c)");
414 27
            $results = intval($qb->getQuery()->getSingleScalarResult());
415 27
            $stat = ($results > 0);
416 27
        }
417
418
        if (   !$stat
419 32
            && !empty($this->cm->midgard['childtypes']))
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
420 32
        {
421 27
            foreach ($this->cm->midgard['childtypes'] as $typename => $parentfield)
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
422
            {
423 27
                $qb = connection::get_em()->createQueryBuilder();
424 27
                $qb->from('midgard:' . $typename, 'c')
425 27
                    ->where('c.' . $parentfield . ' = ?0')
426 27
                    ->setParameter(0, $this->id)
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
427 27
                    ->select("COUNT(c)");
428
429 27
                $results = intval($qb->getQuery()->getSingleScalarResult());
430 27
                $stat = ($results > 0);
431
                if ($stat)
432 27
                {
433 3
                    break;
434
                }
435 27
            }
436 27
        }
437
438 32
        return $stat;
439
    }
440
441
    public function get_parent()
442
    {
443
        return null;
444
    }
445
446
    /**
447
     * This function is called list() in Midgard, but that doesn't work in plain PHP
448
     *
449
     * @return array
450
     */
451 1
    private function _list()
452
    {
453 1
        $this->initialize();
454
455 1
        if (!empty($this->cm->midgard['upfield']))
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
456 1
        {
457 1
            $qb = connection::get_em()->createQueryBuilder();
458 1
            $qb->from(get_class($this), 'c')
459 1
                ->where('c.' . $this->cm->midgard['upfield'] . ' = ?0')
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
460 1
                ->setParameter(0, $this->id)
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
461 1
                ->select("c");
462 1
            return $qb->getQuery()->getResult();
463
        }
464
465
        return array();
466
    }
467
468
    /**
469
     * This should return child objects, but only if they are of a different type
470
     * For all other input, an empty array is returned
471
     * (not implemented yet)
472
     *
473
     * @param string $classname
474
     * @return array
475
     */
476
    public function list_children($classname)
0 ignored issues
show
Unused Code introduced by
The parameter $classname is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
477
    {
478
        return array();
479
    }
480
481 1
    public function get_by_path($path)
482
    {
483 1
        $parts = explode('/', trim($path, '/'));
484 1
        if (empty($parts))
485 1
        {
486
            return false;
487
        }
488 1
        $this->initialize();
489
490 1
        if (count($this->cm->midgard['unique_fields']) != 1)
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
491 1
        {
492
            return false;
493
        }
494
495 1
        $field = $this->cm->midgard['unique_fields'][0];
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
496
497 1
        if (!empty($this->cm->midgard['parent']))
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
498 1
        {
499 1
            $parent_cm = connection::get_em()->getClassMetadata('midgard:' . $this->cm->midgard['parent']);
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
500 1
            $parentclass = $this->cm->fullyQualifiedClassName($this->cm->midgard['parent']);
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
501 1
            $parentfield = $parent_cm->midgard['upfield'];
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
502 1
            $upfield = $this->cm->midgard['parentfield'];
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
503 1
        }
504 1
        else if (!empty($this->cm->midgard['upfield']))
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
505 1
        {
506 1
            $parentclass = get_class($this);
507 1
            $upfield = $this->cm->midgard['upfield'];
0 ignored issues
show
Bug introduced by
Accessing midgard on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
508 1
            $parentfield = $upfield;
509 1
        }
510
        else
511
        {
512
            return false;
513
        }
514
515 1
        $name = array_pop($parts);
516 1
        $up = 0;
517 1
        foreach ($parts as $part)
518
        {
519 1
            $qb = $this->get_uniquefield_query($parentclass, $field, $part, $parentfield, $up);
520 1
            $qb->select("c.id");
521 1
            $up = intval($qb->getQuery()->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR));
522 1 View Code Duplication
            if ($up === 0)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
523 1
            {
524
                exception::not_exists();
525
                $this->id = 0;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
526
                $this->set_guid('');
527
                return false;
528
            }
529 1
        }
530
531 1
        $qb = $this->get_uniquefield_query(get_class($this), $field, $name, $upfield, $up);
532 1
        $qb->select("c");
533
534 1
        $entity = $qb->getQuery()->getOneOrNullResult();
535
536 1 View Code Duplication
        if ($entity === null)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
537 1
        {
538 1
            exception::not_exists();
539 1
            $this->id = 0;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
540 1
            $this->set_guid('');
541 1
            return false;
542
        }
543 1
        $this->populate_from_entity($entity);
544
545 1
        return true;
546
    }
547
548
    /**
549
     * @return int
550
     */
551 1
    protected function get_uniquefield_query($classname, $field, $part, $upfield, $up)
552
    {
553 1
        $qb = connection::get_em()->createQueryBuilder();
554 1
        $qb->from($classname, 'c');
555 1
        $conditions = $qb->expr()->andX();
556 1
        $conditions->add($qb->expr()->eq('c.' . $field, ':' . $field));
557
        $parameters = array
558
        (
559
            $field => $part
560 1
        );
561
562 1
        if (empty($up))
563 1
        {
564
            // If the database was created by Midgard, it might contain 0 instead of NULL, so...
565 1
            $empty_conditions = $qb->expr()->orX()
566 1
                ->add($qb->expr()->isNull('c.' . $upfield))
567 1
                ->add($qb->expr()->eq('c.' . $upfield, '0'));
568 1
            $conditions->add($empty_conditions);
569 1
        }
570
        else
571
        {
572 1
            $conditions->add($qb->expr()->eq('c.' . $upfield, ':' . $upfield));
573 1
            $parameters[$upfield] = $up;
574
        }
575
576 1
        $qb->where($conditions)
577 1
            ->setParameters($parameters);
578
579 1
        return $qb;
580
    }
581
582
    public function parent()
583
    {
584
        return false;
585
    }
586
587 1
    public function has_parameters()
588
    {
589 1
        return $this->get_collection('midgard_parameter')->is_empty($this->guid);
590
    }
591
592 4
    public function list_parameters($domain = false)
593
    {
594 4
        $constraints = array();
595
        if ($domain)
596 4
        {
597 1
            $constraints[] = array("domain", "=", $domain);
598 1
        }
599
600 4
        return $this->get_collection('midgard_parameter')->find($this->guid, $constraints);
601
    }
602
603 3
    public function find_parameters(array $constraints = array())
604
    {
605 3
        return $this->get_collection('midgard_parameter')->find($this->guid, $constraints);
606
    }
607
608 1
    public function delete_parameters(array $constraints = array())
609
    {
610 1
        return $this->get_collection('midgard_parameter')->delete($this->guid, $constraints);
611
    }
612
613 1
    public function purge_parameters(array $constraints = array())
614
    {
615 1
        return $this->get_collection('midgard_parameter')->purge($this->guid, $constraints);
616
    }
617
618 2
    public function get_parameter($domain, $name)
619
    {
620 2
        if (!$this->guid)
621 2
        {
622 1
            return false;
623
        }
624 2
        $qb = connection::get_em()->createQueryBuilder();
625
        $qb
626 2
            ->select('c.value')
627 2
            ->from('midgard:midgard_parameter', 'c')
628 2
            ->where('c.domain = :domain AND c.name = :name AND c.parentguid = :parentguid')
629 2
            ->setParameters(array('domain' => $domain, 'name' => $name, 'parentguid' => $this->guid));
630
631
        // workaround for http://www.doctrine-project.org/jira/browse/DDC-2655
632
        try
633
        {
634 2
            return $qb->getQuery()->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR);
635
        }
636
        catch (\Doctrine\ORM\NoResultException $e)
637
        {
638
            return null;
639
        }
640
    }
641
642 11
    public function set_parameter($domain, $name, $value)
643
    {
644
        $constraints = array
645
        (
646 11
            array ('domain', '=', $domain),
647 11
            array ('name', '=', $name),
648 11
        );
649 11
        $params = $this->get_collection('midgard_parameter')->find($this->guid, $constraints);
650
651
        // check value
652 11
        if ($value === false || $value === null || $value === "")
653 11
        {
654 2
            if (count($params) == 0)
655 2
            {
656 1
                exception::not_exists();
657 1
                return false;
658
            }
659 2
            foreach ($params as $param)
660
            {
661 2
                $stat = $param->delete();
662 2
            }
663 2
            return $stat;
0 ignored issues
show
Bug introduced by
The variable $stat 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...
664
        }
665
666 11
        $om = new objectmanager(connection::get_em());
0 ignored issues
show
Compatibility introduced by
\midgard\portable\storage\connection::get_em() of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
667
        try
668
        {
669
            // create new
670 11
            if (count($params) == 0)
671 11
            {
672 11
                $parameter = $om->new_instance(connection::get_em()->getClassMetadata('midgard:midgard_parameter')->getName());
673 11
                $parameter->parentguid = $this->guid;
0 ignored issues
show
Documentation introduced by
The property parentguid does not exist on object<midgard\portable\api\dbobject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
674 11
                $parameter->domain = $domain;
0 ignored issues
show
Documentation introduced by
The property domain does not exist on object<midgard\portable\api\dbobject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
675 11
                $parameter->name = $name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<midgard\portable\api\dbobject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
676 11
                $parameter->value = $value;
0 ignored issues
show
Documentation introduced by
The property value does not exist on object<midgard\portable\api\dbobject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
677 11
                $om->create($parameter);
678 11
            }
679
            // use existing
680
            else
681
            {
682 1
                $parameter = array_shift($params);
683 1
                $parameter->value = $value;
684 1
                $om->update($parameter);
685
            }
686 11
            midgard_connection::get_instance()->set_error(MGD_ERR_OK);
687 11
            return true;
688
        }
689
        catch (\Exception $e)
690
        {
691
            exception::internal($e);
692
            return false;
693
        }
694
    }
695
696
    /**
697
     * The signature is a little different from original, because Doctrine doesn't support func_get_args() in proxies
698
     */
699 2
    public function parameter($domain, $name, $value = '__UNINITIALIZED__')
700
    {
701 2
        if ($value === '__UNINITIALIZED__')
702 2
        {
703 1
            return $this->get_parameter($domain, $name);
704
        }
705 2
        return $this->set_parameter($domain, $name, $value);
706
    }
707
708 1
    public function has_attachments()
709
    {
710 1
        return $this->get_collection('midgard_attachment')->is_empty($this->guid);
711
    }
712
713 2
    public function list_attachments()
714
    {
715 2
        return $this->get_collection('midgard_attachment')->find($this->guid, array());
716
    }
717
718
    public function find_attachments(array $constraints = array())
719
    {
720
        return $this->get_collection('midgard_attachment')->find($this->guid, $constraints);
721
    }
722
723
    public function delete_attachments(array $constraints = array())
724
    {
725
        return $this->get_collection('midgard_attachment')->delete($this->guid, $constraints);
726
    }
727
728
    /**
729
     *
730
     * @param array $constraints
731
     * @param boolean $delete_blob
732
     * @return boolean False if one or more attachments couldn't be deleted
733
     * @todo Implement delete_blob & return value
734
     */
735
    public function purge_attachments(array $constraints = array(), $delete_blob = true)
0 ignored issues
show
Unused Code introduced by
The parameter $delete_blob is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
736
    {
737
        return $this->get_collection('midgard_attachment')->purge($this->guid, $constraints);
738
    }
739
740 3
    public function create_attachment($name, $title = '', $mimetype = '')
741
    {
742 3
        $existing = $this->get_collection('midgard_attachment')->find($this->guid, array('name' => $name));
743 3
        if (count($existing) > 0)
744 3
        {
745 1
            exception::object_name_exists();
746 1
            return null;
747
        }
748 3
        $om = new objectmanager(connection::get_em());
0 ignored issues
show
Compatibility introduced by
\midgard\portable\storage\connection::get_em() of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
749 3
        $att = $om->new_instance(connection::get_em()->getClassMetadata('midgard:midgard_attachment')->getName());
750
751 3
        $att->parentguid = $this->guid;
0 ignored issues
show
Documentation introduced by
The property parentguid does not exist on object<midgard\portable\api\dbobject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
752 3
        $att->title = $title;
0 ignored issues
show
Documentation introduced by
The property title does not exist on object<midgard\portable\api\dbobject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
753 3
        $att->name = $name;
0 ignored issues
show
Documentation introduced by
The property name does not exist on object<midgard\portable\api\dbobject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
754 3
        $att->mimetype = $mimetype;
0 ignored issues
show
Documentation introduced by
The property mimetype does not exist on object<midgard\portable\api\dbobject>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
755
        try
756
        {
757 3
            $om->create($att);
758 3
            midgard_connection::get_instance()->set_error(MGD_ERR_OK);
759 3
            return $att;
760
        }
761
        catch (\Exception $e)
762
        {
763
            exception::internal($e);
764
            return null;
765
        }
766
    }
767
768
    public static function serve_attachment($guid)
0 ignored issues
show
Unused Code introduced by
The parameter $guid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
769
    {
770
        return false;
771
    }
772
773
    /**
774
     * @todo: Tests indicate that $check_dependencies is ignored in the mgd2 extension,
775
     * so we might consider ignoring it, too
776
     */
777 16
    public function purge($check_dependencies = true)
778
    {
779 16
        if (empty($this->id))
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
780 16
        {
781
            // This usually means that the object has been purged already
782
            exception::not_exists();
783
            return false;
784
        }
785
        if (   $check_dependencies
786 16
            && $this->has_dependents())
787 16
        {
788 2
            exception::has_dependants();
789 2
            return false;
790
        }
791
792
        try
793
        {
794 16
            $om = new objectmanager(connection::get_em());
0 ignored issues
show
Compatibility introduced by
\midgard\portable\storage\connection::get_em() of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
795 16
            $om->purge($this);
796
        }
797 16
        catch (\Doctrine\ORM\EntityNotFoundException $e)
798
        {
799 1
            exception::not_exists();
800 1
            return false;
801
        }
802
        catch (\Exception $e)
803
        {
804
            exception::internal($e);
805
            return false;
806
        }
807 16
        midgard_connection::get_instance()->set_error(MGD_ERR_OK);
808
809 16
        return true;
810
    }
811
812 1
    public static function undelete($guid)
813
    {
814 1
        return \midgard_object_class::undelete($guid);
815
    }
816
817
    public function connect($signal, $callback, $user_data)
0 ignored issues
show
Unused Code introduced by
The parameter $signal is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $callback is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $user_data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
818
    {
819
        return false;
820
    }
821
822 1
    public static function new_query_builder()
823
    {
824 1
        return new \midgard_query_builder(get_called_class());
825
    }
826
827 1
    public static function new_collector($field, $value)
828
    {
829 1
        return new \midgard_collector(get_called_class(), $field, $value);
830
    }
831
832
    public static function new_reflection_property()
833
    {
834
        return new \midgard_reflection_property(get_called_class());
835
    }
836
837 94
    public function set_guid($guid)
838
    {
839 94
        parent::__set('guid', $guid);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (__set() instead of set_guid()). Are you sure this is correct? If so, you might want to change this to $this->__set().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
840 94
    }
841
842
    public function emit($signal)
0 ignored issues
show
Unused Code introduced by
The parameter $signal is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
843
    {
844
        return false;
845
    }
846
847
    /**
848
     * Helper for managing the isapproved and islocked metadata properties
849
     *
850
     * @param string $action the property to manage (either approve or lock)
851
     * @param bool $value
852
     * @return boolean
853
     */
854 4
    private function manage_meta_property($action, $value)
855
    {
856 4
        $user = connection::get_user();
857 4
        if ($user === null)
858 4
        {
859 4
            exception::access_denied();
860 4
            return false;
861
        }
862 4
        if ($action == 'lock')
863 4
        {
864 2
            $flag = 'islocked';
865 2
        }
866 2
        else if ($action == 'approve')
867 2
        {
868 2
            $flag = 'isapproved';
869 2
        }
870
        else
871
        {
872
            throw new exception('Unsupported action ' . $action);
873
        }
874
        // same val
875 4
        if ($this->__get('metadata')->$flag === $value)
876 4
        {
877 3
            return false;
878
        }
879 4
        if ($value === false)
880 4
        {
881 2
            $action = 'un' . $action;
882 2
        }
883
884 4
        if ($this->id)
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
885 4
        {
886
            try
887
            {
888 4
                $om = new objectmanager(connection::get_em());
0 ignored issues
show
Compatibility introduced by
\midgard\portable\storage\connection::get_em() of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager>. It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
889 4
                $om->{$action}($this);
890
            }
891 4
            catch (\Exception $e)
892
            {
893
                exception::internal($e);
894
                return false;
895
            }
896 4
        }
897 4
        midgard_connection::get_instance()->set_error(MGD_ERR_OK);
898
899 4
        return true;
900
    }
901
902 2
    public function approve()
903
    {
904 2
       return $this->manage_meta_property("approve", true);
905
    }
906
907 2
    public function is_approved()
908
    {
909 2
        return $this->metadata_isapproved;
0 ignored issues
show
Documentation introduced by
The property metadata_isapproved does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
910
    }
911
912 1
    public function unapprove()
913
    {
914 1
        return $this->manage_meta_property("approve", false);
915
    }
916
917 2
    public function lock()
918
    {
919 2
        if ($this->is_locked())
920 2
        {
921 1
            exception::object_is_locked();
922 1
            return false;
923
        }
924 2
        return $this->manage_meta_property("lock", true);
925
    }
926
927 2
    public function is_locked()
928
    {
929 2
        return $this->metadata_islocked;
0 ignored issues
show
Documentation introduced by
The property metadata_islocked does not exist on object<midgard\portable\api\object>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
930
    }
931
932 1
    public function unlock()
933
    {
934 1
        return $this->manage_meta_property("lock", false);
935
    }
936
937
    public function get_workspace()
938
    {
939
        return false;
940
    }
941
}
942