SQLServerQuery   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __destruct() 0 6 2
B getIterator() 0 16 5
A numRecords() 0 13 3
1
<?php
2
3
namespace SilverStripe\MSSQL;
4
5
use DateTime;
6
use SilverStripe\ORM\Connect\Query;
7
8
/**
9
 * A result-set from a MSSQL database.
10
 */
11
class SQLServerQuery extends Query
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: nextRecord, seek
Loading history...
12
{
13
14
    /**
15
     * The SQLServerConnector object that created this result set.
16
     *
17
     * @var SQLServerConnector
18
     */
19
    private $connector;
20
21
    /**
22
     * The internal MSSQL handle that points to the result set.
23
     *
24
     * @var resource
25
     */
26
    private $handle;
27
28
    /**
29
     * Hook the result-set given into a Query class, suitable for use by sapphire.
30
     * @param SQLServerConnector $connector The database object that created this query.
31
     * @param resource $handle the internal mssql handle that is points to the resultset.
32
     */
33
    public function __construct(SQLServerConnector $connector, $handle)
34
    {
35
        $this->connector = $connector;
36
        $this->handle = $handle;
37
    }
38
39
    public function __destruct()
40
    {
41
        if (is_resource($this->handle)) {
42
            sqlsrv_free_stmt($this->handle);
43
        }
44
    }
45
46
    public function getIterator()
47
    {
48
        if (is_resource($this->handle)) {
49
            while ($data = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_ASSOC)) {
50
                // special case for sqlsrv - date values are DateTime coming out of the sqlsrv drivers,
51
                // so we convert to the usual Y-m-d H:i:s value!
52
                foreach ($data as $name => $value) {
53
                    if ($value instanceof DateTime) {
54
                        $data[$name] = $value->format('Y-m-d H:i:s');
55
                    }
56
                }
57
58
                yield $data;
59
            }
60
        }
61
    }
62
63
    public function numRecords()
64
    {
65
        if (!is_resource($this->handle)) {
66
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the abstract method SilverStripe\ORM\Connect\Query::numRecords of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
67
        }
68
69
        // WARNING: This will only work if the cursor type is scrollable!
70
        if (function_exists('sqlsrv_num_rows')) {
71
            return sqlsrv_num_rows($this->handle);
72
        } else {
73
            user_error('MSSQLQuery::numRecords() not supported in this version of sqlsrv', E_USER_WARNING);
74
        }
75
    }
76
}
77