SQLServerQuery::seek()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
namespace SilverStripe\MSSQL;
4
5
use SilverStripe\ORM\Connect\DatabaseException;
6
use SilverStripe\ORM\Connect\Query;
7
use function sqlsrv_fetch_array;
8
use function sqlsrv_num_rows;
9
use function sqlsrv_free_stmt;
10
use const SQLSRV_SCROLL_ABSOLUTE;
11
12
/**
13
 * A result-set from a MSSQL database.
14
 */
15
class SQLServerQuery extends Query
16
{
17
18
    /**
19
     * The SQLServerConnector object that created this result set.
20
     *
21
     * @var SQLServerConnector
22
     */
23
    protected $connector;
24
25
    /**
26
     * The internal MSSQL handle that points to the result set.
27
     *
28
     * @var resource
29
     */
30
    protected $handle;
31
32
    /**
33
     * Hook the result-set given into a Query class, suitable for use by sapphire.
34
     * @param SQLServerConnector $connector The database object that created this query.
35
     * @param resource $handle the internal mssql handle that is points to the resultset.
36
     */
37
    public function __construct(SQLServerConnector $connector, $handle)
38
    {
39
        $this->connector = $connector;
40
        $this->handle = $handle;
41
    }
42
43
    public function __destruct()
44
    {
45
        if (is_resource($this->handle)) {
46
            sqlsrv_free_stmt($this->handle);
47
        }
48
    }
49
50
    public function numRecords()
51
    {
52
        if (!is_resource($this->handle)) {
53
            return null;
54
        }
55
56
        // WARNING: This will only work if the cursor type is scrollable!
57
        if (function_exists('sqlsrv_num_rows')) {
58
            return sqlsrv_num_rows($this->handle);
59
        } else {
60
            user_error('MSSQLQuery::numRecords() not supported in this version of sqlsrv', E_USER_WARNING);
61
        }
62
    }
63
64 View Code Duplication
    public function nextRecord()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
65
    {
66
        if (is_resource($this->handle)) {
67
            $result = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_ASSOC);
68
69
            if ($result && !empty($result)) {
70
                return $result;
71
            }
72
        }
73
74
        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::nextRecord of type array.

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...
75
    }
76
77 View Code Duplication
    public function seek($row)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
78
    {
79
        if (is_resource($this->handle)) {
80
            $result = sqlsrv_fetch_array($this->handle, SQLSRV_FETCH_ASSOC, SQLSRV_SCROLL_ABSOLUTE, $row);
81
82
            if ($result && !empty($result)) {
83
                return $result;
84
            }
85
        }
86
87
        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::seek of type array.

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...
88
    }
89
}
90