ManyRelation   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 115
Duplicated Lines 18.26 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 7
Bugs 1 Features 0
Metric Value
wmc 16
c 7
b 1
f 0
lcom 1
cbo 6
dl 21
loc 115
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C read() 12 35 7
B write() 9 34 6
A delete() 0 18 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of the Storage package
5
 *
6
 * (c) Michal Wachowski <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Moss\Storage\Query\Relation;
13
14
/**
15
 * One to many relation handler
16
 *
17
 * @author  Michal Wachowski <[email protected]>
18
 * @package Moss\Storage
19
 */
20
class ManyRelation extends AbstractRelation implements RelationInterface
21
{
22
    /**
23
     * Executes read for one-to-many relation
24
     *
25
     * @param array $result
26
     *
27
     * @return array
28
     */
29
    public function read(&$result)
30
    {
31
        $relations = [];
32
        $conditions = [];
33
34
        foreach ($result as $i => $entity) {
35
            $container = $this->accessor->getPropertyValue($entity, $this->definition->container());
36
            if (empty($container)) {
37
                $this->accessor->setPropertyValue($entity, $this->definition->container(), []);
38
            }
39
40
            foreach ($this->definition->keys() as $local => $refer) {
41
                $conditions[$refer][] = $this->accessor->getPropertyValue($entity, $local);
42
            }
43
44
            $relations[$this->buildLocalKey($entity, $this->definition->keys())][] = &$result[$i];
45
        }
46
47
        $collection = $this->fetch($this->definition->entity(), $conditions, true);
48
49 View Code Duplication
        foreach ($collection as $relEntity) {
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...
50
            $key = $this->buildForeignKey($relEntity, $this->definition->keys());
51
52
            if (!isset($relations[$key])) {
53
                continue;
54
            }
55
56
            foreach ($relations[$key] as &$entity) {
57
                $this->accessor->addPropertyValue($entity, $this->definition->container(), $relEntity);
58
                unset($entity);
59
            }
60
        }
61
62
        return $result;
63
    }
64
65
    /**
66
     * Executes write for one-to-many relation
67
     *
68
     * @param array|\ArrayAccess $result
69
     *
70
     * @return array|\ArrayAccess
71
     * @throws RelationException
72
     */
73
    public function write(&$result)
74
    {
75
        $container = $this->accessor->getPropertyValue($result, $this->definition->container());
76 View Code Duplication
        if (empty($container)) {
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...
77
            $conditions = [];
78
            foreach ($this->definition->keys() as $local => $foreign) {
79
                $conditions[$foreign][] = $this->accessor->getPropertyValue($result, $local);
80
            }
81
82
            $this->cleanup($this->definition->entity(), [], $conditions);
83
            return $result;
84
        }
85
86
        $this->assertArrayAccess($container);
87
88
        foreach ($container as $relEntity) {
89
            foreach ($this->definition->keys() as $local => $foreign) {
90
                $this->accessor->setPropertyValue($relEntity, $foreign, $this->accessor->getPropertyValue($result, $local));
91
            }
92
93
            $this->storage->write($relEntity, $this->definition->entity())->execute();
94
        }
95
96
        $this->accessor->setPropertyValue($result, $this->definition->container(), $container);
97
98
        $conditions = [];
99
        foreach ($this->definition->keys() as $local => $foreign) {
100
            $conditions[$foreign] = $this->accessor->getPropertyValue($result, $local);
101
        }
102
103
        $this->cleanup($this->definition->entity(), $container, $conditions);
104
105
        return $result;
106
    }
107
108
    /**
109
     * Executes delete for one-to-many relation
110
     *
111
     * @param array|\ArrayAccess $result
112
     *
113
     * @return array|\ArrayAccess
114
     * @throws RelationException
115
     */
116
    public function delete(&$result)
117
    {
118
        $container = $this->accessor->getPropertyValue($result, $this->definition->container());
119
        if (empty($container)) {
120
            return $result;
121
        }
122
123
        $this->assertArrayAccess($container);
124
125
        foreach ($container as $relEntity) {
126
            $this->storage->delete($this->definition->entity(), $relEntity)
0 ignored issues
show
Documentation introduced by
$this->definition->entity() is of type string, but the function expects a array|object.

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...
127
                ->execute();
128
        }
129
130
        $this->accessor->setPropertyValue($result, $this->definition->container(), $container);
131
132
        return $result;
133
    }
134
}
135