|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Morris Jobke <[email protected]> |
|
6
|
|
|
* @author Robin Appelman <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @license AGPL-3.0 |
|
9
|
|
|
* |
|
10
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
12
|
|
|
* as published by the Free Software Foundation. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OC\Diagnostics; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\Diagnostics\IQuery; |
|
27
|
|
|
|
|
28
|
|
|
class Query implements IQuery { |
|
29
|
|
|
private $sql; |
|
30
|
|
|
|
|
31
|
|
|
private $params; |
|
32
|
|
|
|
|
33
|
|
|
private $start; |
|
34
|
|
|
|
|
35
|
|
|
private $end; |
|
36
|
|
|
|
|
37
|
|
|
private $stack; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $sql |
|
41
|
|
|
* @param array $params |
|
42
|
|
|
* @param int $start |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct($sql, $params, $start, array $stack) { |
|
45
|
|
|
$this->sql = $sql; |
|
46
|
|
|
$this->params = $params; |
|
47
|
|
|
$this->start = $start; |
|
48
|
|
|
$this->stack = $stack; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function end($time) { |
|
52
|
|
|
$this->end = $time; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getParams() { |
|
59
|
|
|
return $this->params; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getSql() { |
|
66
|
|
|
return $this->sql; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return float |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getStart() { |
|
73
|
|
|
return $this->start; |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return float |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getDuration() { |
|
80
|
|
|
return $this->end - $this->start; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function getStartTime() { |
|
84
|
|
|
return $this->start; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function getStacktrace() { |
|
88
|
|
|
return $this->stack; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
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:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.