These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace ByJG\AnyDataset\Repository; |
||
| 4 | |||
| 5 | use ByJG\AnyDataset\ConnectionManagement; |
||
| 6 | use ByJG\AnyDataset\Database\DBDriverInterface; |
||
| 7 | use ByJG\AnyDataset\Database\DBFunctionsInterface; |
||
| 8 | use ByJG\AnyDataset\Database\DBOci8Driver; |
||
| 9 | use ByJG\AnyDataset\Database\DBPDODriver; |
||
| 10 | use ByJG\AnyDataset\Database\DBSQLRelayDriver; |
||
| 11 | use ByJG\AnyDataset\Exception\NotAvailableException; |
||
| 12 | use ByJG\Cache\CacheEngineInterface; |
||
| 13 | use PDO; |
||
| 14 | |||
| 15 | class DBDataset |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Enter description here... |
||
| 20 | * |
||
| 21 | * @var ConnectionManagement |
||
| 22 | */ |
||
| 23 | protected $_connectionManagement; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * |
||
| 27 | * @var DBDriverInterface |
||
| 28 | */ |
||
| 29 | private $_dbDriver = null; |
||
| 30 | |||
| 31 | |||
| 32 | /** |
||
| 33 | * @var CacheEngineInterface |
||
| 34 | */ |
||
| 35 | protected $_cacheEngine; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param string $dbname Name of file without '_db' and extention '.xml'. |
||
| 39 | */ |
||
| 40 | public function __construct($dbname) |
||
| 41 | { |
||
| 42 | $this->_connectionManagement = new ConnectionManagement($dbname); |
||
| 43 | |||
| 44 | if ($this->_connectionManagement->getDriver() == "sqlrelay") { |
||
| 45 | $this->_dbDriver = new DBSQLRelayDriver($this->_connectionManagement); |
||
| 46 | } elseif ($this->_connectionManagement->getDriver() == "oci8") { |
||
| 47 | $this->_dbDriver = new DBOci8Driver($this->_connectionManagement); |
||
| 48 | } else { |
||
| 49 | $this->_dbDriver = DBPDODriver::factory($this->_connectionManagement); |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return ConnectionManagement |
||
| 55 | */ |
||
| 56 | public function getConnectionManagement() |
||
| 57 | { |
||
| 58 | return $this->_connectionManagement; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function testConnection() |
||
| 62 | { |
||
| 63 | return true; |
||
| 64 | } |
||
| 65 | |||
| 66 | |||
| 67 | public function setCacheEngine(CacheEngineInterface $cache) |
||
| 68 | { |
||
| 69 | $this->_cacheEngine = $cache; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @return CacheEngineInterface |
||
| 74 | * @throws NotAvailableException |
||
| 75 | */ |
||
| 76 | public function getCacheEngine() |
||
| 77 | { |
||
| 78 | if (is_null($this->_cacheEngine)) { |
||
| 79 | throw new NotAvailableException('Cache Engine not available'); |
||
| 80 | } |
||
| 81 | return $this->_cacheEngine; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get the DBDriver |
||
| 86 | * @return DBDriverInterface |
||
| 87 | */ |
||
| 88 | public function getDbDriver() |
||
| 89 | { |
||
| 90 | return $this->_dbDriver; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @access public |
||
| 95 | * @param string $sql |
||
| 96 | * @param array $params |
||
| 97 | * @param int $ttl |
||
| 98 | * @return IteratorInterface |
||
| 99 | * @throws NotAvailableException |
||
| 100 | */ |
||
| 101 | public function getIterator($sql, $params = null, $ttl = null) |
||
| 102 | { |
||
| 103 | // If there is no TTL query, return the LIVE iterator |
||
| 104 | if (empty($ttl)) { |
||
| 105 | return $this->getDbDriver()->getIterator($sql, $params); |
||
| 106 | } |
||
| 107 | |||
| 108 | // Otherwise try to get from cache |
||
| 109 | $key = $this->getQueryKey($sql, $params); |
||
| 110 | |||
| 111 | // Get the CACHE |
||
| 112 | $cache = $this->getCacheEngine()->get($key, $ttl); |
||
| 113 | if ($cache === false) { |
||
| 114 | $cache = array(); |
||
| 115 | $it = $this->getDbDriver()->getIterator($sql, $params); |
||
| 116 | foreach ($it as $value) { |
||
| 117 | $cache[] = $value->toArray(); |
||
| 118 | } |
||
| 119 | |||
| 120 | $this->getCacheEngine()->set($key, $cache, $ttl); |
||
|
0 ignored issues
–
show
|
|||
| 121 | } |
||
| 122 | |||
| 123 | $arrayDS = new ArrayDataset($cache); |
||
|
0 ignored issues
–
show
It seems like
$cache defined by $this->getCacheEngine()->get($key, $ttl) on line 112 can also be of type object; however, ByJG\AnyDataset\Reposito...yDataset::__construct() does only seem to accept array, maybe add an additional type check?
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check: /**
* @return array|string
*/
function returnsDifferentValues($x) {
if ($x) {
return 'foo';
}
return array();
}
$x = returnsDifferentValues($y);
if (is_array($x)) {
// $x is an array.
}
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue. Loading history...
|
|||
| 124 | return $arrayDS->getIterator(); |
||
| 125 | } |
||
| 126 | |||
| 127 | protected function getQueryKey($sql, $array) |
||
| 128 | { |
||
| 129 | $key1 = md5($sql); |
||
| 130 | |||
| 131 | // Check which parameter exists in the SQL |
||
| 132 | $arKey2 = array(); |
||
| 133 | if (is_array($array)) { |
||
| 134 | foreach ($array as $key => $value) { |
||
| 135 | if (preg_match("/\[\[$key\]\]/", $sql)) { |
||
| 136 | $arKey2[$key] = $value; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | // Define the query key |
||
| 142 | if (is_array($arKey2) && count($arKey2) > 0) { |
||
| 143 | $key2 = ":" . md5(json_encode($arKey2)); |
||
| 144 | } else { |
||
| 145 | $key2 = ""; |
||
| 146 | } |
||
| 147 | |||
| 148 | return "qry:" . $key1 . $key2; |
||
| 149 | } |
||
| 150 | |||
| 151 | public function getScalar($sql, $array = null) |
||
| 152 | { |
||
| 153 | return $this->getDbDriver()->getScalar($sql, $array); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @access public |
||
| 158 | * @param string $tablename |
||
| 159 | * @return array |
||
| 160 | */ |
||
| 161 | public function getAllFields($tablename) |
||
| 162 | { |
||
| 163 | return $this->getDbDriver()->getAllFields($tablename); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @access public |
||
| 168 | * @param string $sql |
||
| 169 | * @param array $array |
||
| 170 | * @return Resource |
||
| 171 | */ |
||
| 172 | public function execSQL($sql, $array = null) |
||
| 173 | { |
||
| 174 | $this->getDbDriver()->executeSql($sql, $array); |
||
| 175 | } |
||
| 176 | |||
| 177 | public function beginTransaction() |
||
| 178 | { |
||
| 179 | $this->getDbDriver()->beginTransaction(); |
||
| 180 | } |
||
| 181 | |||
| 182 | public function commitTransaction() |
||
| 183 | { |
||
| 184 | $this->getDbDriver()->commitTransaction(); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function rollbackTransaction() |
||
| 188 | { |
||
| 189 | $this->getDbDriver()->rollbackTransaction(); |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @access public |
||
| 194 | * @param IteratorInterface $it |
||
| 195 | * @param string $fieldPK |
||
| 196 | * @param string $fieldName |
||
| 197 | * @return Resource |
||
| 198 | */ |
||
| 199 | View Code Duplication | public function getArrayField(IteratorInterface $it, $fieldPK, $fieldName) |
|
| 200 | { |
||
| 201 | $result = array(); |
||
| 202 | while ($it->hasNext()) { |
||
| 203 | $registro = $it->moveNext(); |
||
| 204 | $result [$registro->getField($fieldPK)] = $registro->getField($fieldName); |
||
| 205 | } |
||
| 206 | return $result; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @access public |
||
| 211 | * @return PDO |
||
| 212 | */ |
||
| 213 | public function getDBConnection() |
||
| 214 | { |
||
| 215 | return $this->getDbDriver()->getDbConnection(); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * |
||
| 220 | * @var DBFunctionsInterface |
||
| 221 | */ |
||
| 222 | protected $_dbFunction = null; |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get a IDbFunctions class to execute Database specific operations. |
||
| 226 | * @return DBFunctionsInterface |
||
| 227 | */ |
||
| 228 | public function getDbFunctions() |
||
| 229 | { |
||
| 230 | if (is_null($this->_dbFunction)) { |
||
| 231 | $dbFunc = "\\ByJG\\AnyDataset\\Database\\DB" . ucfirst($this->_connectionManagement->getDriver()) . "Functions"; |
||
| 232 | $this->_dbFunction = new $dbFunc(); |
||
| 233 | } |
||
| 234 | |||
| 235 | return $this->_dbFunction; |
||
| 236 | } |
||
| 237 | |||
| 238 | public function setDriverAttribute($name, $value) |
||
| 239 | { |
||
| 240 | return $this->getDbDriver()->setAttribute($name, $value); |
||
| 241 | } |
||
| 242 | |||
| 243 | public function getDriverAttribute($name) |
||
| 244 | { |
||
| 245 | return $this->getDbDriver()->getAttribute($name); |
||
| 246 | } |
||
| 247 | } |
||
| 248 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: