Test Failed
Branch v5 (12d602)
by Alexey
04:51
created

Result   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 31.67 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 19
loc 60
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fetch() 10 19 3
B fetchAll() 9 24 6

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
 * Created by IntelliJ IDEA.
4
 * User: inji
5
 * Date: 07.01.2018
6
 * Time: 16:39
7
 */
8
9
namespace Inji\Db\InjiStorage;
10
11
12
use Inji\Db\DriverResult;
13
use Inji\Model;
14
15
class Result implements DriverResult {
16
    public $result = [];
17
    public $curKey = 0;
18
    /**
19
     * @var Query
20
     */
21
    public $query;
22
23
    public function __construct($dbResult, $query) {
24
        $this->result = $dbResult;
25
        $this->query = $query;
26
        reset($dbResult);
27
        $this->curKey = key($dbResult);
28
    }
29
30
    public function fetch(string $className = '') {
31
        if (!$this->result) {
32
            return false;
33
        }
34
        $item = $this->result[$this->curKey];
35
        next($this->result);
36
        $this->curKey = key($this->result);
37 View Code Duplication
        if ($className) {
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...
38
            /**
39
             * @var Model
40
             */
41
            $item = new $className($item);
42
            $item->connectionName = $this->query->connection->dbInstance;
43
            $item->dbOptions = $this->query->dbOptions;
44
            $item->app = $this->query->connection->dbInstance->app;
45
            return $item;
46
        }
47
        return $item;
48
    }
49
50
    public function fetchAll(string $className = '', $keyCol = '') {
51
        if (!$this->result) {
52
            return [];
53
        }
54
        $items = [];
55
        foreach ($this->result as $item) {
56 View Code Duplication
            if ($className) {
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...
57
                /**
58
                 * @var Model
59
                 */
60
                $item = new $className($item);
61
                $item->connectionName = $this->query->connection->dbInstance;
62
                $item->dbOptions = $this->query->dbOptions;
63
                $item->app = $this->query->connection->dbInstance->app;
64
            }
65
            if ($keyCol) {
66
                $key = $className ? $item->$keyCol : $item[$keyCol];
67
                $items[$key] = $item;
68
            } else {
69
                $items[] = $item;
70
            }
71
        }
72
        return $items;
73
    }
74
}