Passed
Push — fix-8832 ( 2eb5fa )
by Sam
07:48
created

PDOQuery::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Connect;
4
5
use ArrayIterator;
6
use PDO;
7
use PDOStatement;
8
9
/**
10
 * A result-set from a PDO database.
11
 */
12
class PDOQuery extends Query
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $results = null;
18
19
    /**
20
     * Hook the result-set given into a Query class, suitable for use by SilverStripe.
21
     * @param PDOStatement $statement The internal PDOStatement containing the results
22
     */
23
    public function __construct(PDOStatementHandle $statement)
24
    {
25
        // Since no more than one PDOStatement for any one connection can be safely
26
        // traversed, each statement simply requests all rows at once for safety.
27
        // This could be re-engineered to call fetchAll on an as-needed basis
28
29
        $this->results = $statement->typeCorrectedFetchAll();
30
        $statement->closeCursor();
31
    }
32
33
    public function getIterator()
34
    {
35
        return new ArrayIterator($this->results);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new ArrayIterator($this->results) returns the type ArrayIterator which is incompatible with the return type mandated by SilverStripe\ORM\Connect\Query::getIterator() of array.

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...
36
    }
37
38
    public function numRecords()
39
    {
40
        return count($this->results);
41
    }
42
}
43