Completed
Push — master ( 37f54e...a8ce98 )
by Hong
02:32
created

Result   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 146
Duplicated Lines 10.96 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 19
c 1
b 0
f 0
lcom 1
cbo 1
dl 16
loc 146
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 5 1
A fieldCount() 0 4 1
A rowCount() 0 4 1
A affectedRows() 0 4 1
A close() 0 6 2
A realFetchAll() 8 13 3
A realFetchRow() 8 14 3
C getOneRow() 0 33 7

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
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Db
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Db\Driver\Mysqli;
16
17
use Phossa2\Db\Driver\ResultAbstract;
18
19
/**
20
 * Result
21
 *
22
 * @package Phossa2\Db
23
 * @author  Hong Zhang <[email protected]>
24
 * @see     ResultAbstract
25
 * @version 2.0.0
26
 * @since   2.0.0 added
27
 */
28
class Result extends ResultAbstract
29
{
30
    /**
31
     * the column names
32
     *
33
     * @var    array
34
     * @access protected
35
     */
36
    protected $cols;
37
38
    /**
39
     * the column values
40
     *
41
     * @var    array
42
     * @access protected
43
     */
44
    protected $vals;
45
46
    /**
47
     * @var    \mysqli_stmt
48
     * @access protected
49
     */
50
    protected $statement;
51
52
    /**
53
     * Invoke to set statement
54
     *
55
     * @param  \mysqli_stmt $statement
56
     * @return $this
57
     * @access public
58
     */
59
    public function __invoke(\mysqli_stmt $statement)
60
    {
61
        $this->statement = $statement;
62
        return $this;
63
    }
64
65
    /**
66
     * {@inheritDoc}
67
     */
68
    public function fieldCount()/*# : int */
69
    {
70
        return $this->statement->field_count;
71
    }
72
73
    /**
74
     * {@inheritDoc}
75
     */
76
    public function rowCount()/*# : int */
77
    {
78
        return $this->statement->num_rows();
79
    }
80
81
    /**
82
     * {@inheritDoc}
83
     */
84
    public function affectedRows()/*# : int */
85
    {
86
        return $this->statement->affected_rows;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function close()
93
    {
94
        if ($this->statement) {
95
            $this->statement->free_result();
96
        }
97
    }
98
99
    /**
100
     * {@inheritDoc}
101
     */
102
    protected function realFetchAll()/*# : array */
103
    {
104
        $result = [];
105 View Code Duplication
        while(true) {
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...
106
            $row = $this->getOneRow();
107
            if (is_array($row)) {
108
                $result[] = $row;
109
            } else {
110
                break;
111
            }
112
        }
113
        return $result;
114
    }
115
116
    /**
117
     * {@inheritDoc}
118
     */
119
    protected function realFetchRow($rowCount)/*# : array */
120
    {
121
        $count  = 0;
122
        $result = [];
123 View Code Duplication
        while ($count++ < $rowCount) {
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...
124
            $row = $this->getOneRow();
125
            if (is_array($row)) {
126
                $result[] = $row;
127
            } else {
128
                break;
129
            }
130
        }
131
        return $result;
132
    }
133
134
    /**
135
     * Get one row of data
136
     *
137
     * @return array|null
138
     * @access protected
139
     */
140
    protected function getOneRow()
141
    {
142
        if (null === $this->cols) {
143
            $result = $this->statement->result_metadata();
144
            if (false === $result) return false;
145
146
            $this->cols = [];
147
148
            // set column name
149
            foreach ($result->fetch_fields() as $col) {
150
                $this->cols[] = $col->name;
151
            }
152
153
            // bind values
154
            $this->vals = array_fill(0, count($this->cols), null);
155
156
            $refs = [];
157
            foreach ($this->vals as $i => &$f) {
158
                $refs[$i] = &$f;
159
            }
160
            call_user_func_array([$this->statement, 'bind_result'], $refs);
161
        }
162
163
        if ($this->statement->fetch()) {
164
            $row = [];
165
            foreach($this->cols as $i => $col) {
166
                $row[$col] = $this->vals[$i];
167
            }
168
            return $row;
169
        }
170
171
        return false;
172
    }
173
}
174