Passed
Push — master ( b2b78e...4de28a )
by Sébastien
07:25
created

ArrayResultSet   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Test Coverage

Coverage 69.09%

Importance

Changes 0
Metric Value
wmc 25
eloc 48
dl 0
loc 183
ccs 38
cts 55
cp 0.6909
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A fetchMode() 0 6 1
A isWrite() 0 3 1
A isRead() 0 3 1
A asColumn() 0 3 1
A hasWrite() 0 3 1
A fetchValue() 0 20 6
A all() 0 5 2
A asList() 0 3 1
A asClass() 0 3 1
A asObject() 0 3 1
A fetchColum() 0 11 3
A asAssociative() 0 3 1
A current() 0 3 1
A fetchClass() 0 18 4
1
<?php
2
3
namespace Bdf\Prime\Connection\Result;
4
5
use Bdf\Prime\Exception\DBALException;
6
7
/**
8
 * Wrap simple associative array to ResultSet
9
 * This result is usefull for caches
10
 */
11
final class ArrayResultSet extends \ArrayIterator implements ResultSetInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    private $fetchMode = self::FETCH_ASSOC;
17
18
    /**
19
     * @var mixed
20
     */
21
    private $fetchOptions;
22
23
    /**
24
     * @var string[]
25
     */
26
    private $columns;
27
28
    /**
29
     * @var \ReflectionClass
30
     */
31
    private $reflectionClass;
32
33
    /**
34
     * @var \ReflectionProperty[]
35
     */
36
    private $reflectionProperties;
37
38
39
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $mode should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $options should have a doc-comment as per coding-style.
Loading history...
40
     * {@inheritdoc}
41
     */
42 5
    public function fetchMode($mode, $options = null)
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
43
    {
44 5
        $this->fetchMode = $mode;
45 5
        $this->fetchOptions = $options;
46
47 5
        return $this;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function asAssociative(): ResultSetInterface
54
    {
55
        return $this->fetchMode(self::FETCH_ASSOC);
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...tInterface::FETCH_ASSOC has been deprecated: Use asAssociative() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

55
        return $this->fetchMode(/** @scrutinizer ignore-deprecated */ self::FETCH_ASSOC);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function asList(): ResultSetInterface
62
    {
63
        return $this->fetchMode(self::FETCH_NUM);
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...SetInterface::FETCH_NUM has been deprecated: Use asList() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

63
        return $this->fetchMode(/** @scrutinizer ignore-deprecated */ self::FETCH_NUM);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function asObject(): ResultSetInterface
70
    {
71
        return $this->fetchMode(self::FETCH_OBJECT);
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...Interface::FETCH_OBJECT has been deprecated: Use asObject() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

71
        return $this->fetchMode(/** @scrutinizer ignore-deprecated */ self::FETCH_OBJECT);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
72
    }
73
74
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $className should have a doc-comment as per coding-style.
Loading history...
75
     * {@inheritdoc}
76
     */
77
    public function asClass(string $className): ResultSetInterface
78
    {
79
        return $this->fetchMode(self::FETCH_CLASS, $className);
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...tInterface::FETCH_CLASS has been deprecated: Use asClass() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

79
        return $this->fetchMode(/** @scrutinizer ignore-deprecated */ self::FETCH_CLASS, $className);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
80
    }
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
83
     * {@inheritdoc}
84
     */
85
    public function asColumn(int $column = 0): ResultSetInterface
86
    {
87
        return $this->fetchMode(self::FETCH_COLUMN, $column);
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...Interface::FETCH_COLUMN has been deprecated: Use asColumn() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

87
        return $this->fetchMode(/** @scrutinizer ignore-deprecated */ self::FETCH_COLUMN, $column);

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93 5
    public function all()
94
    {
95 5
        return $this->fetchMode === self::FETCH_ASSOC
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...tInterface::FETCH_ASSOC has been deprecated: Use asAssociative() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

95
        return $this->fetchMode === /** @scrutinizer ignore-deprecated */ self::FETCH_ASSOC

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
Bug Best Practice introduced by
The expression return $this->fetchMode ... $this->getArrayCopy()) returns the type array which is incompatible with the return type mandated by Bdf\Prime\Connection\Res...sultSetInterface::all() of Bdf\Prime\Connection\Result\list.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
96 1
            ? $this->getArrayCopy()
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement must be declared on a single line
Loading history...
97 5
            : array_map([$this, 'fetchValue'], $this->getArrayCopy())
0 ignored issues
show
Coding Style introduced by
Space after closing parenthesis of function call prohibited
Loading history...
98
        ;
0 ignored issues
show
Coding Style introduced by
Space found before semicolon; expected ");" but found ")
;"
Loading history...
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 3
    public function current()
105
    {
106 3
        return $this->fetchValue(parent::current());
107
    }
108
109
    /**
110
     * Transform the value according to the fetch mode
111
     *
112
     * @param array $current
113
     *
114
     * @return mixed
115
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
116 7
    private function fetchValue($current)
0 ignored issues
show
introduced by
Type hint "array" missing for $current
Loading history...
Coding Style introduced by
Private method name "ArrayResultSet::fetchValue" must be prefixed with an underscore
Loading history...
117
    {
118 7
        switch ($this->fetchMode) {
119 7
            case self::FETCH_ASSOC:
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...tInterface::FETCH_ASSOC has been deprecated: Use asAssociative() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

119
            case /** @scrutinizer ignore-deprecated */ self::FETCH_ASSOC:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
120 2
                return $current;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
121
122 5
            case self::FETCH_NUM:
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...SetInterface::FETCH_NUM has been deprecated: Use asList() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

122
            case /** @scrutinizer ignore-deprecated */ self::FETCH_NUM:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
123 1
                return array_values($current);
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
124
125 4
            case self::FETCH_COLUMN:
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...Interface::FETCH_COLUMN has been deprecated: Use asColumn() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

125
            case /** @scrutinizer ignore-deprecated */ self::FETCH_COLUMN:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
126 2
                return $this->fetchColum($current);
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
127
128 2
            case self::FETCH_OBJECT:
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...Interface::FETCH_OBJECT has been deprecated: Use asObject() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

128
            case /** @scrutinizer ignore-deprecated */ self::FETCH_OBJECT:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
129 1
                return (object) $current;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
Coding Style introduced by
A cast statement should not be followed as per the coding-style.
Loading history...
130
131 1
            case self::FETCH_CLASS:
0 ignored issues
show
Deprecated Code introduced by
The constant Bdf\Prime\Connection\Res...tInterface::FETCH_CLASS has been deprecated: Use asClass() instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

131
            case /** @scrutinizer ignore-deprecated */ self::FETCH_CLASS:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
132 1
                return $this->fetchClass($current);
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
133
134
            default:
135
                throw new DBALException('Unsupported fetch mode '.$this->fetchMode);
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
136
        }
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function isRead(): bool
143
    {
144
        return true;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function isWrite(): bool
151
    {
152
        return false;
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function hasWrite(): bool
159
    {
160
        return false;
161
    }
162
163 2
    private function fetchColum($current)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function fetchColum()
Loading history...
Coding Style introduced by
Private method name "ArrayResultSet::fetchColum" must be prefixed with an underscore
Loading history...
introduced by
Missing function doc comment
Loading history...
164
    {
165 2
        if (!$this->fetchOptions) {
166 2
            return reset($current);
167
        }
168
169 1
        if (!$this->columns) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->columns of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
170 1
            $this->columns = array_keys($current);
171
        }
172
173 1
        return $current[$this->columns[$this->fetchOptions]];
174
    }
175
176 1
    private function fetchClass($current)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function fetchClass()
Loading history...
introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "ArrayResultSet::fetchClass" must be prefixed with an underscore
Loading history...
177
    {
178 1
        if (!$this->reflectionClass) {
179 1
            $this->reflectionClass = new \ReflectionClass($this->fetchOptions);
180
        }
181
182 1
        $object = $this->reflectionClass->newInstance();
183
184 1
        foreach ($current as $property => $value) {
185 1
            if (!isset($this->reflectionProperties[$property])) {
186 1
                $this->reflectionProperties[$property] = $this->reflectionClass->getProperty($property);
187 1
                $this->reflectionProperties[$property]->setAccessible(true);
188
            }
189
190 1
            $this->reflectionProperties[$property]->setValue($object, $value);
191
        }
192
193 1
        return $object;
194
    }
195
}
196