Completed
Push — master ( 2a53f3...5ddc8c )
by Andreas
03:19
created

midgard_object_class::get_property_parent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 8
loc 8
ccs 6
cts 6
cp 1
crap 2
rs 9.4285
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
8
use midgard\portable\storage\connection;
9
use midgard\portable\storage\subscriber;
10
use midgard\portable\api\error\exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, exception.

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

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

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

// Bar.php
namespace OtherDir;

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

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

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

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

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
use midgard\portable\storage\objectmanager;
12
use midgard\portable\storage\interfaces\metadata;
13
14
class midgard_object_class
15
{
16 9
    private static function resolve_classname($guid, $include_deleted = false)
17
    {
18 9
        $qb = connection::get_em()->createQueryBuilder();
19 9
        $qb->from('midgard:midgard_repligard', 'r')
20 9
            ->addSelect('r.typename')
21 9
            ->addSelect('r.object_action')
22 9
            ->where('r.guid = ?1')
23 9
            ->setParameter(1, $guid);
24
25
        try {
26 9
            $result = $qb->getQuery()->getSingleResult();
27 9
        } catch (\Doctrine\ORM\NoResultException $e) {
28 3
            $result = null;
29
        }
30 9
        if ($result === null) {
31 3
            throw exception::not_exists();
32
        }
33 9
        if ($result["object_action"] == subscriber::ACTION_PURGE) {
34 2
            throw exception::object_purged();
35
        }
36
        if (    !$include_deleted
37 8
             && $result["object_action"] == subscriber::ACTION_DELETE) {
38 1
            throw exception::object_deleted();
39
        }
40 7
        if (!self::has_metadata($result["typename"])) {
41 1
            throw exception::invalid_property_value();
42
        }
43
44 6
        return $result["typename"];
45
    }
46
47 7
    public static function resolve_fqn($classname)
48
    {
49 7
        $cm = connection::get_em()->getClassMetadata('midgard:' . $classname);
50 7
        return $cm->name;
0 ignored issues
show
Bug introduced by
Accessing name 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...
51
    }
52
53 4
    public static function factory($classname, $id = null)
54
    {
55 4
        if ($classname === null) {
56
            return null;
57
        }
58 4
        $classname = self::resolve_fqn($classname);
59 4
        return new $classname($id);
60
    }
61
62 4
    public static function undelete($guid)
63
    {
64
        try {
65 4
            $classname = self::resolve_classname($guid, true);
66 4
        } catch (exception $e) {
67 2
            return false;
68
        }
69 3
        $classname = self::resolve_fqn($classname);
70
71 3
        $qb = new \midgard_query_builder($classname);
72 3
        $qb->include_deleted();
73 3
        $qb->add_constraint('guid', '=', $guid);
74 3
        $results = $qb->execute();
75 3
        if (count($results) === 0) {
76
            exception::not_exists();
77
            return false;
78
        }
79 3
        $entity = array_shift($results);
80
81 3
        if (!$entity->metadata_deleted) {
82 1
            exception::internal(new \Exception("Object is not deleted."));
83 1
            return false;
84
        }
85
86
        try {
87 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...
88 3
            $om->undelete($entity);
89 3
        } catch (\Exception $e) {
90
            exception::internal($e);
91
            return false;
92
        }
93
94 3
        midgard_connection::get_instance()->set_error(MGD_ERR_OK);
95 3
        return true;
96
    }
97
98 6
    public static function get_object_by_guid($guid)
99
    {
100 6
        if (!mgd_is_guid($guid)) {
101 1
            throw exception::not_exists();
102
        }
103
104 5
        $type = self::resolve_classname($guid);
105 3
        return self::factory($type, $guid);
106
    }
107
108 1 View Code Duplication
    public static function get_property_up($classname)
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...
109
    {
110 1
        if (is_object($classname)) {
111 1
            $classname = get_class($classname);
112 1
        }
113 1
        $cm = connection::get_em()->getClassMetadata($classname);
114 1
        return $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...
115
    }
116
117 4 View Code Duplication
    public static function get_property_parent($classname)
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...
118
    {
119 1
        if (is_object($classname)) {
120 1
            $classname = get_class($classname);
121 1
        }
122 1
        $cm = connection::get_em()->getClassMetadata($classname);
123 4
        return $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...
124
    }
125
126
    public static function connect_default($classname, $signal, $callback, $userdata = null) // <== check!
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...
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 $userdata 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...
127
    {
128
    }
129
130 8
    public static function has_metadata($classname)
131
    {
132 8
        if (is_string($classname)) {
133 8
            return in_array('midgard\\portable\\storage\\interfaces\\metadata', class_implements($classname));
134
        }
135 1
        if (is_object($classname)) {
136 1
            return ($classname instanceof metadata);
137
        }
138
        return false;
139
    }
140
141
    public static function get_schema_value($classname, $node_name)
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...
Unused Code introduced by
The parameter $node_name 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...
142
    {
143
    }
144
}
145