Completed
Push — master ( c428e5...52fc88 )
by Andreas
03:07
created

midgard_replicator   F

Complexity

Total Complexity 63

Size/Duplication

Total Lines 367
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Test Coverage

Coverage 96.49%

Importance

Changes 0
Metric Value
wmc 63
lcom 1
cbo 15
dl 0
loc 367
ccs 220
cts 228
cp 0.9649
rs 3.6585
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
B export_by_guid() 0 33 3
C serialize() 0 38 8
A serialize_blob() 0 9 1
A unserialize() 0 19 4
D import_object() 0 81 20
A import_from_xml() 0 11 3
A import_blob() 0 4 1
A resolve_link_id() 0 15 2
A resolve_link_guid() 0 15 2
C object_from_xml() 0 32 7
A blob_from_xml() 0 8 1
B get_object_action() 0 25 6
A convert_value() 0 7 2
A export_purged() 0 4 1
A export() 0 8 2

How to fix   Complexity   

Complex Class

Complex classes like midgard_replicator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use midgard_replicator, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
4
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
6
 */
7
8
use midgard\portable\api\dbobject;
9
use midgard\portable\api\attachment;
10
use midgard\portable\storage\connection;
11
use \midgard_datetime as midgard_datetime;
12
use \midgard_connection as midgard_connection;
13
use \SimpleXMLElement as SimpleXMLElement;
14
use midgard\portable\storage\subscriber;
15
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
16
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...
17
use midgard\portable\api\blob;
18
use Doctrine\ORM\NoResultException;
19
20
class midgard_replicator
21
{
22
    /**
23
     * @return boolean Indicating success
24
     */
25
    public static function export(dbobject $object)
26
    {
27
        if (!mgd_is_guid($object->guid)) {
0 ignored issues
show
Documentation introduced by
The property $guid is declared protected in midgard\portable\api\dbobject. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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...
28
            return false;
29
        }
30
        throw new Exception('not implemented');
31
        return true;
0 ignored issues
show
Unused Code introduced by
return true; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
32
    }
33
34
    /**
35
     * @return boolean Indicating success
36
     */
37 4
    public static function export_by_guid($guid)
38
    {
39 1
        if (!mgd_is_guid($guid)) {
40 1
            midgard_connection::get_instance()->set_error(exception::INVALID_PROPERTY_VALUE);
41 1
            return false;
42
        }
43 1
        $result = connection::get_em()
44 1
            ->createQueryBuilder()
45 1
            ->from('midgard:midgard_repligard', 'c')
46 1
            ->select('c.typename', 'c.object_action')
47 4
            ->where('c.guid = ?0')
48 1
            ->setParameter(0, $guid)
49 1
            ->getQuery()
50 1
            ->getSingleResult();
51
52 1
        if ($result['object_action'] === subscriber::ACTION_PURGE) {
53 4
            midgard_connection::get_instance()->set_error(exception::OBJECT_PURGED);
54 1
            return false;
55
        }
56
57 1
        $result = connection::get_em()
58 1
            ->createQueryBuilder()
59 1
            ->update('midgard:' . $result['typename'], 'c')
60 1
            ->set('c.metadata_exported', '?0')
61 1
            ->setParameter(0, new midgard_datetime)
62 1
            ->where('c.guid = ?1')
63 1
            ->setParameter(1, $guid)
64 1
            ->getQuery()
65 1
            ->execute();
66
67 1
        midgard_connection::get_instance()->set_error(exception::OK);
68 1
        return ($result > 0);
69
    }
70
71
    /**
72
     * @return string XML document containing purged object information
73
     */
74
    public static function export_purged($class, $startdate = null, $enddate = null)
0 ignored issues
show
Unused Code introduced by
The parameter $class 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 $startdate 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 $enddate 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...
75
    {
76
        throw new Exception('not implemented');
77
    }
78
79
    /**
80
     * @return string XML representation of the object
81
     */
82 5
    public static function serialize(dbobject $object)
83
    {
84 5
        $xml = new SimpleXMLElement('<midgard_object xmlns="http://www.midgard-project.org/midgard_object/1.8"/>');
85
86 5
        $cm = connection::get_em()->getClassMetadata(get_class($object));
87 5
        $node = $xml->addChild($cm->getReflectionClass()->getShortName());
88 5
        $node->addAttribute('guid', $object->guid);
0 ignored issues
show
Documentation introduced by
The property $guid is declared protected in midgard\portable\api\dbobject. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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...
89 5
        $node->addAttribute('purge', 'no');
90
91 5
        if (mgd_is_guid($object->guid)) {
0 ignored issues
show
Documentation introduced by
The property $guid is declared protected in midgard\portable\api\dbobject. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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...
92 4
            $node->addAttribute('action', self::get_object_action($object->guid));
0 ignored issues
show
Documentation introduced by
The property $guid is declared protected in midgard\portable\api\dbobject. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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...
93 4
        }
94
95 5
        $metadata = array();
96
97 5
        foreach ($cm->getAssociationNames() as $name) {
98 5
            $node->addChild($name, self::resolve_link_id($cm, $object, $name));
99 5
        }
100 5
        foreach ($cm->getFieldNames() as $name) {
101 5
            if ($name == 'guid') {
102 5
                continue;
103
            }
104 5
            if (strpos($name, 'metadata_') === 0) {
105 5
                $metadata[substr($name, 9)] = $object->$name;
106 5
            } else {
107 5
                $node->addChild($name, self::convert_value($object->$name));
108
            }
109 5
        }
110
111 5
        if (!empty($metadata)) {
112 5
            $mnode = $node->addChild('metadata');
113 5
            foreach ($metadata as $name => $value) {
114 5
                $mnode->addChild($name, self::convert_value($value));
115 5
            }
116 5
        }
117
118 5
        return $xml->asXML();
119
    }
120
121
    /**
122
     * @return string XML representation of the blob (content is base64 encoded)
123
     */
124 2
    public static function serialize_blob(attachment $object)
125
    {
126 2
        $blob = new blob($object);
127 2
        $xml = new SimpleXMLElement('<midgard_object xmlns="http://www.midgard-project.org/midgard_object/1.8"/>');
128 2
        $node = $xml->addChild('midgard_blob', base64_encode($blob->read_content()));
129 2
        $node->addAttribute('guid', $object->guid);
0 ignored issues
show
Documentation introduced by
The property $guid is declared protected in midgard\portable\api\attachment. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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...
130
131 2
        return $xml->asXML();
132
    }
133
134
    /**
135
     * @return dbobject[] Array of objects read from input XML
136
     */
137 7
    public static function unserialize($xml, $force = false)
138
    {
139 7
        $ret = array();
140
141 7
        $xml = new SimpleXMLElement($xml);
142 7
        foreach ($xml as $node) {
143
            try {
144 7
                if ($node->getName() == 'midgard_blob') {
145 2
                    $ret[] = self::blob_from_xml($node, $force);
146 2
                } else {
147 6
                    $ret[] = self::object_from_xml($node, $force);
148
                }
149 7
            } catch (\Exception $e) {
150 2
                connection::log()->warning($e->getMessage());
151
            }
152 7
        }
153
154 7
        return $ret;
155
    }
156
157
    /**
158
     * @return boolean Indicating success
159
     */
160 5
    public static function import_object(dbobject $object, $force = false)
161
    {
162 5
        if (!mgd_is_guid($object->guid)) {
0 ignored issues
show
Documentation introduced by
The property $guid is declared protected in midgard\portable\api\dbobject. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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...
163 1
            midgard_connection::get_instance()->set_error(exception::INVALID_PROPERTY_VALUE);
164 1
            return false;
165
        }
166
167 5
        $classname = get_class($object);
168
169 5
        switch (self::get_object_action($object->guid)) {
0 ignored issues
show
Documentation introduced by
The property $guid is declared protected in midgard\portable\api\dbobject. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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...
170 5
            case 'created':
171 5
            case 'updated':
172 1
                $dbobject = new $classname($object->guid);
0 ignored issues
show
Documentation introduced by
The property $guid is declared protected in midgard\portable\api\dbobject. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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...
173 1
                break;
174
175 5
            case 'deleted':
176 1
                connection::get_em()->getFilters()->disable('softdelete');
177 1
                $dbobject = new $classname($object->guid);
0 ignored issues
show
Documentation introduced by
The property $guid is declared protected in midgard\portable\api\dbobject. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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...
178 1
                connection::get_em()->getFilters()->enable('softdelete');
179 1
                break;
180
181 4
            case 'purged':
182 1
                if (!$force) {
183 1
                    return false;
184
                }
185 1
                $result = connection::get_em()
186 1
                    ->createQueryBuilder()
187 1
                    ->delete('midgard:midgard_repligard', 'c')
188 1
                    ->where('c.guid = ?0')
189 1
                    ->setParameter(0, $object->guid)
0 ignored issues
show
Documentation introduced by
The property $guid is declared protected in midgard\portable\api\dbobject. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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...
190 1
                    ->getQuery()
191 1
                    ->execute();
192
193 1
                if ($result == 0) {
194
                    return false;
195
                }
196
                //fall-through
197
198 4
            default:
199 4
                $dbobject = new $classname;
200 4
                $dbobject->set_guid($object->guid);
0 ignored issues
show
Documentation introduced by
The property $guid is declared protected in midgard\portable\api\dbobject. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

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...
201 4
                break;
202 5
        }
203
204 5
        if (   $dbobject->id > 0
205 5
            && $dbobject->metadata->revised >= $object->metadata->revised) {
0 ignored issues
show
Documentation introduced by
The property metadata does not exist on object<midgard\portable\api\dbobject>. 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...
206 1
            midgard_connection::get_instance()->set_error(exception::OBJECT_IMPORTED);
207 1
            return false;
208
        }
209
210 5
        if (   $dbobject->metadata->deleted
211 5
            && !$object->metadata->deleted) {
0 ignored issues
show
Documentation introduced by
The property metadata does not exist on object<midgard\portable\api\dbobject>. 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...
212 1
            if (!midgard_object_class::undelete($dbobject->guid)) {
213
                return false;
214
            }
215 1
            $dbobject->metadata_deleted = false;
216 5
        } elseif (   !$dbobject->metadata->deleted
217 5
                 && $object->metadata->deleted) {
0 ignored issues
show
Documentation introduced by
The property metadata does not exist on object<midgard\portable\api\dbobject>. 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...
218 1
            return $dbobject->delete();
219
        }
220
221 5
        $cm = connection::get_em()->getClassMetadata(get_class($object));
222
223 5
        foreach ($cm->getAssociationNames() as $name) {
224 5
            $dbobject->$name = self::resolve_link_guid($cm, $name, $object->$name);
225 5
        }
226 5
        foreach ($cm->getFieldNames() as $name) {
227 5
            if ($name == 'id') {
228 5
                continue;
229
            }
230 5
            if (strpos($name, 'metadata_') === false) {
231 5
                $dbobject->$name = $object->$name;
232 5
            }
233 5
        }
234 5
        $dbobject->metadata->imported = new \midgard_datetime();
235 5
        if ($dbobject->id > 0) {
236 1
            return $dbobject->update();
237
        }
238
239 4
        return $dbobject->create();
240
    }
241
242
    /**
243
     * @return boolean Indicating success
244
     */
245 3
    public static function import_from_xml($xml, $force = false)
246
    {
247 3
        $objects = self::unserialize($xml, $force);
248 3
        foreach ($objects as $object) {
249 3
            if ($object instanceof blob) {
250 1
                self::import_blob($object, $force);
251 1
            } else {
252 3
                self::import_object($object, $force);
253
            }
254 3
        }
255 3
    }
256
257
    /**
258
     *
259
     * @param blob $blob
260
     * @param boolean $force
261
     */
262 1
    private static function import_blob(blob $blob, $force)
0 ignored issues
show
Unused Code introduced by
The parameter $force 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...
263
    {
264 1
        $blob->write_content($blob->content);
265 1
    }
266
267 5
    private static function resolve_link_id(ClassMetadata $cm, dbobject $object, $name)
268
    {
269 5
        if ($object->$name == 0) {
270 5
            return '0';
271
        }
272 2
        $target_class = $cm->getAssociationTargetClass($name);
273 2
        return connection::get_em()
274 2
            ->createQueryBuilder()
275 2
            ->from($target_class, 'c')
276 2
            ->select('c.guid')
277 2
            ->where('c.id = ?0')
278 2
            ->setParameter(0, $object->$name)
279 2
            ->getQuery()
280 2
            ->getSingleScalarResult();
281
    }
282
283 8
    private static function resolve_link_guid(ClassMetadata $cm, $name, $value)
284
    {
285 8
        if (!mgd_is_guid($value)) {
286 8
            return 0;
287
        }
288 2
        $target_class = $cm->getAssociationTargetClass($name);
289 2
        return connection::get_em()
290 2
            ->createQueryBuilder()
291 2
            ->from($target_class, 'c')
292 2
            ->select('c.id')
293 2
            ->where('c.guid = ?0')
294 2
            ->setParameter(0, $value)
295 2
            ->getQuery()
296 2
            ->getSingleScalarResult();
297
    }
298
299
    /**
300
     *
301
     * @param SimpleXMLElement $node
302
     * @param boolean $force
303
     * @return dbobject
304
     */
305 6
    private static function object_from_xml(SimpleXMLElement $node, $force)
306
    {
307 6
        $cm = connection::get_em()->getClassMetadata('midgard:' . $node->getName());
308 6
        $classname = $cm->getName();
309 6
        $object = new $classname;
310 6
        $object->set_guid($node['guid']);
311 6
        $object->action = $node['action'];
312 6
        foreach ($node as $child) {
313 6
            $field = $child->getName();
314 6
            if ($field == 'metadata') {
315 6
                foreach ($child as $mchild) {
316 6
                    $field = 'metadata_' . $mchild->getName();
317 6
                    $object->$field = (string) $mchild;
318 6
                }
319 6
                continue;
320
            }
321 6
            $value = (string) $child;
322 6
            if ($cm->isSingleValuedAssociation($field)) {
323
                try {
324 6
                    $value = self::resolve_link_guid($cm, $field, $value);
325 6
                } catch (NoResultException $e) {
326 1
                    if (!$force) {
327 1
                        throw $e;
328
                    }
329 1
                    $value = 0;
330
                }
331 6
            }
332
333 6
            $object->$field = $value;
334 6
        }
335 6
        return $object;
336
    }
337
338
    /**
339
     *
340
     * @param SimpleXMLElement $node
341
     * @param boolean $force
342
     * @return dbobject
343
     */
344 2
    private static function blob_from_xml(SimpleXMLElement $node, $force)
0 ignored issues
show
Unused Code introduced by
The parameter $force 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...
345
    {
346 2
        $attachment = midgard_object_class::get_object_by_guid((string) $node['guid']);
347
348 2
        $blob = new blob($attachment);
0 ignored issues
show
Documentation introduced by
$attachment is of type null|object, but the function expects a object<midgard\portable\api\attachment>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
349 2
        $blob->content = base64_decode($node);
350 2
        return $blob;
351
    }
352
353 9
    private static function get_object_action($guid)
354
    {
355 9
        $result = connection::get_em()
356 9
            ->createQueryBuilder()
357 9
            ->from('midgard:midgard_repligard', 'c')
358 9
            ->select('c.object_action')
359 9
            ->where('c.guid = ?0')
360 9
            ->setParameter(0, $guid)
361 9
            ->getQuery()
362 9
            ->getScalarResult();
363 9
        $action = (empty($result)) ? 0 : (int) $result[0]['object_action'];
364
365
        switch ($action) {
366 9
            case subscriber::ACTION_CREATE:
367 5
                return 'created';
368 7
            case subscriber::ACTION_UPDATE:
369 3
                return 'updated';
370 7
            case subscriber::ACTION_DELETE:
371 3
                return 'deleted';
372 6
            case subscriber::ACTION_PURGE:
373 3
                return 'purged';
374 3
            default:
375 3
                return 'none';
376 3
        }
377
    }
378
379 5
    private static function convert_value($value)
380
    {
381 5
        if ($value instanceof midgard_datetime) {
382 5
            return $value->format('Y-m-d H:i:sO');
383
        }
384 5
        return $value;
385
    }
386
}
387