GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

BelongsToRelationship   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 18.18 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 73.08%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 8
loc 44
ccs 19
cts 26
cp 0.7308
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareQuery() 8 20 4
A runSetup() 0 11 3
A preSave() 0 6 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
 * The MIT License
5
 *
6
 * Copyright 2014-2018 James Ekow Abaka Ainooson
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace ntentan\nibii\relationships;
28
29
use ntentan\nibii\exceptions\FieldNotFoundException;
30
use ntentan\nibii\ORMContext;
31
use ntentan\nibii\Relationship;
32
use ntentan\utils\Text;
33
34
/**
35
 * Represents a one to many belongs to relationship.
36
 */
37
class BelongsToRelationship extends Relationship
38
{
39
    protected $type = self::BELONGS_TO;
40
41 4
    public function prepareQuery($data)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
42
    {
43 4
        if (!array_key_exists($this->options['local_key'], $data)) {
44
            throw new FieldNotFoundException("Field {$this->options['local_key']} not found for belongs to relationship query.");
45
        }
46 4
        if (!isset($data[$this->options['local_key']])) {
47
            return;
48
        }
49 4
        $query = $this->getQuery();
50 4 View Code Duplication
        if ($this->queryPrepared) {
51 4
            $query->setBoundData($this->options['foreign_key'], $data[$this->options['local_key']]);
52
        } else {
53 4
            $query->setTable($this->getModelInstance()->getDBStoreInformation()['quoted_table'])
54 4
                ->addFilter($this->options['foreign_key'], $data[$this->options['local_key']])
55 4
                ->setFirstOnly(true);
56 4
            $this->queryPrepared = true;
57
        }
58
59 4
        return $query;
60
    }
61
62 4
    public function runSetup()
63
    {
64 4
        $model = ORMContext::getInstance()->getModelFactory()->createModel($this->options['model'], self::BELONGS_TO);
65 4
        $table = $model->getDBStoreInformation()['table'];
66 4
        if ($this->options['foreign_key'] == null) {
67 4
            $this->options['foreign_key'] = $model->getDescription()->getPrimaryKey()[0];
68
        }
69 4
        if ($this->options['local_key'] == null) {
70 4
            $this->options['local_key'] = Text::singularize($table).'_id';
71
        }
72 4
    }
73
74
    public function preSave(&$wrapper, $value)
75
    {
76
        $value->save();
77
        $wrapper[$this->options['local_key']] = $value[$this->options['foreign_key']];
78
        unset($wrapper[$this->options['model']]);
79
    }
80
}
81