Completed
Push — master ( 718d85...a50954 )
by Piotr
43:28
created

OC_DB_StatementWrapper::fetchRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Bart Visscher <[email protected]>
5
 * @author Jörn Friedrich Dreyer <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 * @author Robin McCorkell <[email protected]>
9
 * @author Thomas Müller <[email protected]>
10
 *
11
 * @copyright Copyright (c) 2017, ownCloud GmbH
12
 * @license AGPL-3.0
13
 *
14
 * This code is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License, version 3,
16
 * as published by the Free Software Foundation.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
 * GNU Affero General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU Affero General Public License, version 3,
24
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
25
 *
26
 */
27
28
/**
29
 * small wrapper around \Doctrine\DBAL\Driver\Statement to make it behave, more like an MDB2 Statement
30
 *
31
 * @method boolean bindValue(mixed $param, mixed $value, integer $type = null);
32
 * @method string errorCode();
33
 * @method array errorInfo();
34
 * @method integer rowCount();
35
 * @method array fetchAll(integer $fetchMode = null);
36
 */
37
class OC_DB_StatementWrapper {
38
	/**
39
	 * @var \Doctrine\DBAL\Driver\Statement
40
	 */
41
	private $statement = null;
42
	private $isManipulation = false;
43
	private $lastArguments = [];
44
45
	/**
46
	 * @param boolean $isManipulation
47
	 */
48
	public function __construct($statement, $isManipulation) {
49
		$this->statement = $statement;
50
		$this->isManipulation = $isManipulation;
51
	}
52
53
	/**
54
	 * pass all other function directly to the \Doctrine\DBAL\Driver\Statement
55
	 */
56
	public function __call($name,$arguments) {
57
		return call_user_func_array([$this->statement,$name], $arguments);
58
	}
59
60
	/**
61
	 * make execute return the result instead of a bool
62
	 *
63
	 * @param array $input
64
	 * @return \OC_DB_StatementWrapper|int
65
	 */
66
	public function execute($input= []) {
67
		$this->lastArguments = $input;
68
		if (count($input) > 0) {
69
			$result = $this->statement->execute($input);
70
		} else {
71
			$result = $this->statement->execute();
72
		}
73
74
		if ($result === false) {
75
			return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by OC_DB_StatementWrapper::execute of type integer|OC_DB_StatementWrapper.

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...
76
		}
77
		if ($this->isManipulation) {
78
			$count = $this->statement->rowCount();
79
			return $count;
80
		} else {
81
			return $this;
82
		}
83
	}
84
85
	/**
86
	 * provide an alias for fetch
87
	 *
88
	 * @return mixed
89
	 */
90
	public function fetchRow() {
91
		return $this->statement->fetch();
92
	}
93
94
	/**
95
	 * Provide a simple fetchOne.
96
	 *
97
	 * fetch single column from the next row
98
	 * @param int $column the column number to fetch
99
	 * @return string
100
	 */
101
	public function fetchOne($column = 0) {
102
		return $this->statement->fetchColumn($column);
103
	}
104
105
	/**
106
	 * Binds a PHP variable to a corresponding named or question mark placeholder in the
107
	 * SQL statement that was use to prepare the statement.
108
	 *
109
	 * @param mixed $column Either the placeholder name or the 1-indexed placeholder index
110
	 * @param mixed $variable The variable to bind
111
	 * @param integer|null $type one of the  PDO::PARAM_* constants
112
	 * @param integer|null $length max length when using an OUT bind
113
	 * @return boolean
114
	 */
115
	public function bindParam($column, &$variable, $type = null, $length = null){
116
		return $this->statement->bindParam($column, $variable, $type, $length);
117
	}
118
}
119