OneRelation   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 95
Duplicated Lines 22.11 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 12
c 6
b 1
f 0
lcom 1
cbo 6
dl 21
loc 95
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B read() 12 30 6
B write() 9 24 4
A delete() 0 13 2

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 one relation handler
16
 *
17
 * @author  Michal Wachowski <[email protected]>
18
 * @package Moss\Storage
19
 */
20
class OneRelation extends AbstractRelation implements RelationInterface
21
{
22
    /**
23
     * Executes read for one-to-one 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
            foreach ($this->definition->keys() as $local => $refer) {
36
                $conditions[$refer][] = $this->accessor->getPropertyValue($entity, $local);
37
            }
38
39
            $relations[$this->buildLocalKey($entity, $this->definition->keys())][] = &$result[$i];
40
        }
41
42
        $collection = $this->fetch($this->definition->entity(), $conditions, true);
43
44 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...
45
            $key = $this->buildForeignKey($relEntity, $this->definition->keys());
46
47
            if (!isset($relations[$key])) {
48
                continue;
49
            }
50
51
            foreach ($relations[$key] as &$entity) {
52
                $this->accessor->setPropertyValue($entity, $this->definition->container(), $relEntity);
53
                unset($entity);
54
            }
55
        }
56
57
        return $result;
58
    }
59
60
    /**
61
     * Executes write fro one-to-one relation
62
     *
63
     * @param array|\ArrayAccess $result
64
     *
65
     * @return array|\ArrayAccess
66
     * @throws RelationException
67
     */
68
    public function write(&$result)
69
    {
70
        $entity = $this->accessor->getPropertyValue($result, $this->definition->container());
71 View Code Duplication
        if (empty($entity)) {
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...
72
            $conditions = [];
73
            foreach ($this->definition->keys() as $local => $foreign) {
74
                $conditions[$foreign][] = $this->accessor->getPropertyValue($result, $local);
75
            }
76
77
            $this->cleanup($this->definition->entity(), [], $conditions);
78
            return $result;
79
        }
80
81
        $this->assertInstance($entity);
82
83
        foreach ($this->definition->keys() as $local => $foreign) {
84
            $this->accessor->setPropertyValue($entity, $foreign, $this->accessor->getPropertyValue($result, $local));
85
        }
86
87
        $this->storage->write($entity, $this->definition->entity())->execute();
88
        $this->accessor->setPropertyValue($result, $this->definition->container(), $entity);
89
90
        return $result;
91
    }
92
93
    /**
94
     * Executes delete for one-to-one relation
95
     *
96
     * @param array|\ArrayAccess $result
97
     *
98
     * @return array|\ArrayAccess
99
     * @throws RelationException
100
     */
101
    public function delete(&$result)
102
    {
103
        $entity = $this->accessor->getPropertyValue($result, $this->definition->container());
104
        if (empty($entity)) {
105
            return $result;
106
        }
107
108
        $this->assertInstance($entity);
109
110
        $this->storage->delete($entity, $this->definition->entity())->execute();
111
112
        return $result;
113
    }
114
}
115