OneToMany   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 18.75 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 9
loc 48
rs 10
c 0
b 0
f 0
ccs 20
cts 20
cp 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fetch() 0 16 3
A addJoin() 9 9 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
namespace ORM\Relation;
4
5
use ORM\Entity;
6
use ORM\EntityFetcher;
7
use ORM\EntityManager;
8
use ORM\Exception\InvalidConfiguration;
9
use ORM\Relation;
10
11
/**
12
 * OneToMany Relation
13
 *
14
 * @package ORM\Relation
15
 * @author  Thomas Flori <[email protected]>
16
 */
17
class OneToMany extends Relation
18
{
19
    /**
20
     * Owner constructor.
21
     *
22
     * @param string $name
23
     * @param string $class
24
     * @param string $opponent
25
     */
26 7
    public function __construct($name, $class, $opponent)
27
    {
28 7
        $this->name     = $name;
29 7
        $this->class    = $class;
30 7
        $this->opponent = $opponent;
31 7
    }
32
33
    /**
34 8
     * {@inheritdoc}
35
     * @throws InvalidConfiguration
36 8
     */
37 6
    public function fetch(Entity $self, EntityManager $entityManager)
38 2
    {
39
        $reference = $this->getOpponent()->getReference();
40 4
        if (empty($reference)) {
41
            throw new InvalidConfiguration('Reference is not defined in opponent');
42
        }
43 3
        $foreignKey = $this->getForeignKey($self, array_flip($reference));
44 3
45 3
        /** @var EntityFetcher $fetcher */
46
        $fetcher = $entityManager->fetch($this->class);
47
        foreach ($foreignKey as $col => $value) {
48 3
            $fetcher->where($col, $value);
49
        }
50
51
        return $fetcher;
52 6
    }
53
54 6
    /** {@inheritdoc} */
55 6 View Code Duplication
    public function addJoin(EntityFetcher $fetcher, $join, $alias)
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...
56 6
    {
57
        $expression = [];
58
        foreach ($this->getOpponent()->getReference() as $hisVar => $myVar) {
59 6
            $expression[] = $alias . '.' . $myVar . ' = ' . $this->name . '.' . $hisVar;
60 6
        }
61
62
        call_user_func([ $fetcher, $join ], $this->class, implode(' AND ', $expression), $this->name, [], true);
63
    }
64
}
65