Completed
Push — master ( 01705b...10ec3f )
by Morris
15:02 queued 02:44
created

OC_DB_StatementWrapper::execute()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 6
nop 1
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Bart Visscher <[email protected]>
7
 * @author Jörn Friedrich Dreyer <[email protected]>
8
 * @author Morris Jobke <[email protected]>
9
 * @author Robin Appelman <[email protected]>
10
 * @author Robin McCorkell <[email protected]>
11
 * @author Thomas Müller <[email protected]>
12
 *
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
/**
30
 * small wrapper around \Doctrine\DBAL\Driver\Statement to make it behave, more like an MDB2 Statement
31
 *
32
 * @method boolean bindValue(mixed $param, mixed $value, integer $type = null);
33
 * @method string errorCode();
34
 * @method array errorInfo();
35
 * @method integer rowCount();
36
 * @method array fetchAll(integer $fetchMode = null);
37
 */
38
class OC_DB_StatementWrapper {
39
	/**
40
	 * @var \Doctrine\DBAL\Driver\Statement
41
	 */
42
	private $statement = null;
43
	private $isManipulation = false;
44
	private $lastArguments = array();
45
46
	/**
47
	 * @param boolean $isManipulation
48
	 */
49
	public function __construct($statement, $isManipulation) {
50
		$this->statement = $statement;
51
		$this->isManipulation = $isManipulation;
52
	}
53
54
	/**
55
	 * pass all other function directly to the \Doctrine\DBAL\Driver\Statement
56
	 */
57
	public function __call($name,$arguments) {
58
		return call_user_func_array(array($this->statement,$name), $arguments);
59
	}
60
61
	/**
62
	 * make execute return the result instead of a bool
63
	 *
64
	 * @param array $input
65
	 * @return \OC_DB_StatementWrapper|int
66
	 */
67
	public function execute($input= []) {
68
		$this->lastArguments = $input;
69
		if (count($input) > 0) {
70
			$result = $this->statement->execute($input);
71
		} else {
72
			$result = $this->statement->execute();
73
		}
74
75
		if ($result === false) {
76
			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 OC_DB_StatementWrapper|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...
77
		}
78
		if ($this->isManipulation) {
79
			$count = $this->statement->rowCount();
80
			return $count;
81
		} else {
82
			return $this;
83
		}
84
	}
85
86
	/**
87
	 * provide an alias for fetch
88
	 *
89
	 * @return mixed
90
	 */
91
	public function fetchRow() {
92
		return $this->statement->fetch();
93
	}
94
95
	/**
96
	 * Provide a simple fetchOne.
97
	 *
98
	 * fetch single column from the next row
99
	 * @param int $column the column number to fetch
100
	 * @return string
101
	 */
102
	public function fetchOne($column = 0) {
103
		return $this->statement->fetchColumn($column);
104
	}
105
106
	/**
107
	 * Binds a PHP variable to a corresponding named or question mark placeholder in the
108
	 * SQL statement that was use to prepare the statement.
109
	 *
110
	 * @param mixed $column Either the placeholder name or the 1-indexed placeholder index
111
	 * @param mixed $variable The variable to bind
112
	 * @param integer|null $type one of the  PDO::PARAM_* constants
113
	 * @param integer|null $length max length when using an OUT bind
114
	 * @return boolean
115
	 */
116
	public function bindParam($column, &$variable, $type = null, $length = null){
117
		return $this->statement->bindParam($column, $variable, $type, $length);
118
	}
119
}
120