Completed
Push — master ( b05d3c...2a9f41 )
by Filipe
02:53
created

HasAndBelongsToMany   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 127
Duplicated Lines 16.54 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 96.08%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 8
dl 21
loc 127
ccs 49
cts 51
cp 0.9608
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getRelatedForeignKey() 0 8 2
A getRelationTable() 0 14 2
A load() 21 21 1
A add() 0 4 1
A getConditions() 0 10 1
A addJoinTable() 0 14 1

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 slick/orm package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Orm\Mapper\Relation;
11
use Slick\Common\Utils\Text;
12
use Slick\Database\Sql\Select;
13
use Slick\Orm\Entity\EntityCollectionInterface;
14
use Slick\Orm\EntityInterface;
15
use Slick\Orm\Event\EntityAdded;
16
17
/**
18
 * HasAndBelongsToMany relation
19
 * 
20
 * @package Slick\Orm\Mapper\Relation
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
class HasAndBelongsToMany extends HasMany
24
{
25
26
    /**
27
     * @readwrite
28
     * @var string
29
     */
30
    protected $relatedForeignKey;
31
32
    /**
33
     * @readwrite
34
     * @var string
35
     */
36
    protected $relationTable;
37
38
    /**
39
     * Gets the related foreign key
40
     * 
41
     * @return string
42
     */
43 4
    public function getRelatedForeignKey()
44
    {
45 4
        if (null == $this->relatedForeignKey) {
46 4
            $name = $this->getParentTableName();
47 4
            $this->relatedForeignKey = "{$this->normalizeFieldName($name)}_id";
48 4
        }
49 4
        return $this->relatedForeignKey;
50
    }
51
52
    /**
53
     * Gets the related table
54
     * 
55
     * @return string
56
     */
57 4
    public function getRelationTable()
58
    {
59 4
        if (is_null($this->relationTable)) {
60 4
            $parentTable = $this->getParentTableName();
61 4
            $table = $this->getEntityDescriptor()->getTableName();
62 4
            $names = [$parentTable, $table];
63 4
            asort($names);
64 4
            $first = array_shift($names);
65 4
            $tableName = $this->normalizeFieldName($first);
66 4
            array_unshift($names, $tableName);
67 4
            $this->relationTable = implode('_', $names);
68 4
        }
69 4
        return $this->relationTable;
70
    }
71
72
    /**
73
     * Loads the entity or entity collection for this relation
74
     *
75
     * @param EntityInterface $entity
76
     *
77
     * @return EntityCollectionInterface|EntityInterface[]
78
     */
79 2 View Code Duplication
    public function load(EntityInterface $entity)
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...
80
    {
81 2
        $repository = $this->getParentRepository();
82
83 2
        $query = $repository->find()
84 2
            ->where($this->getConditions($entity))
0 ignored issues
show
Documentation introduced by
$this->getConditions($entity) is of type array<string|integer,array<string|integer,*>>, but the function expects a string.

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...
85 2
            ->limit($this->limit);
86 2
        $this->addJoinTable($query);
87 2
        $this->checkConditions($query)
88 2
            ->checkOrder($query);
89
90
        /** @var EntityCollectionInterface $collection */
91 2
        $collection = $query->all();
92
93
        $collection
94 2
            ->setParentEntity($entity)
95 2
            ->getEmitter()
96 2
            ->addListener(EntityAdded::ACTION_ADD, [$this, 'add']);
97
98 2
        return $collection;
99
    }
100
101
    /**
102
     * Saves the relation row upon entity add
103
     *
104
     * @param EntityAdded $event
105
     */
106
    public function add(EntityAdded $event)
107
    {
108
        // TODO: Change the autogenerated stub
109
    }
110
111
    /**
112
     * Gets the relation conditions
113
     *
114
     * @param EntityInterface $entity
115
     * @return array
116
     */
117 2
    protected function getConditions(EntityInterface $entity)
118
    {
119 2
        $parts = explode('\\', $this->getEntityDescriptor()->className());
120 2
        $property = lcfirst(array_pop($parts));
121
        return [
122 2
            "rel.{$this->getForeignKey()} = :{$property}" => [
123 2
                ":{$property}" => $entity->getId()
124 2
            ]
125 2
        ];
126
    }
127
128
    /**
129
     * Adds the join table to the query
130
     *
131
     * @param Select $query
132
     * @return self
133
     */
134 2
    protected function addJoinTable(Select $query)
135
    {
136 2
        $relationTable = $this->getRelationTable();
137 2
        $table = $this->getParentTableName();
138 2
        $pmk = $this->getParentPrimaryKey();
139 2
        $rfk = $this->getRelatedForeignKey();
140 2
        $query->join(
141 2
            $relationTable,
142 2
            "rel.{$rfk} = {$table}.{$pmk}",
143 2
            null,
144
            'rel'
145 2
        );
146 2
        return $this;
147
    }
148
149
}