Skip to content

Commit 9aea8c6

Browse files
committed
Refactor general method properties access and add phpunit tests
in reference to #150
1 parent f9a9058 commit 9aea8c6

File tree

2 files changed

+77
-49
lines changed

2 files changed

+77
-49
lines changed

shared/ez_sql_core.php

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ public function __construct()
9999
*/
100100
public function __call($function, $args)
101101
{
102-
$prefix = substr($function, 0, 3);
103-
$property = strtolower(substr($function, 3, strlen($function)));
104-
if ($prefix == 'set') {
102+
$prefix = \substr($function, 0, 3);
103+
$property = \strtolower(substr($function, 3, \strlen($function)));
104+
if (($prefix == 'set') && isset($this->$property)) {
105105
$this->$property = $args[0];
106-
} elseif ($prefix == 'get') {
106+
} elseif (($prefix == 'get') && isset($this->$property)){
107107
return $this->$property;
108108
} else {
109-
throw new Exception("$function does not exist");
109+
throw new \Exception("$function does not exist");
110110
}
111111
}
112112

@@ -117,8 +117,8 @@ public function __call($function, $args)
117117
public function get_host_port($host, $default = false)
118118
{
119119
$port = $default;
120-
if ( false !== strpos( $host, ':' ) ) {
121-
list( $host, $port ) = explode( ':', $host );
120+
if ( false !== \strpos( $host, ':' ) ) {
121+
list( $host, $port ) = \explode( ':', $host );
122122
$port = (int) $port;
123123
}
124124
return array( $host, $port );
@@ -175,7 +175,7 @@ public function log_query($query)
175175
$this->func_call = $query;
176176

177177
// Keep an running Log of all functions called
178-
array_push($this->all_func_calls, $this->func_call);
178+
\array_push($this->all_func_calls, $this->func_call);
179179
}
180180

181181
/**
@@ -193,7 +193,7 @@ public function get_var($query = null, $x = 0, $y = 0, $use_prepare = false)
193193

194194
// Extract public out of cached results based x,y vals
195195
if ( $this->last_result[$y] ) {
196-
$values = array_values(get_object_vars($this->last_result[$y]));
196+
$values = \array_values(\get_object_vars($this->last_result[$y]));
197197
}
198198

199199
// If there is a value return it else return null
@@ -218,13 +218,13 @@ public function get_row($query = null, $output = OBJECT, $y = 0, $use_prepare =
218218
return $this->last_result[$y]?$this->last_result[$y]:null;
219219
} elseif ( $output == ARRAY_A ) {
220220
// If the output is an associative array then return row as such..
221-
return $this->last_result[$y]?get_object_vars($this->last_result[$y]):null;
221+
return $this->last_result[$y]? \get_object_vars($this->last_result[$y]):null;
222222
} elseif ( $output == ARRAY_N ) {
223223
// If the output is an numerical array then return row as such..
224-
return $this->last_result[$y]?array_values(get_object_vars($this->last_result[$y])):null;
224+
return $this->last_result[$y]? \array_values(\get_object_vars($this->last_result[$y])):null;
225225
} else {
226226
// If invalid output type was specified..
227-
$this->show_errors ? trigger_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N",E_USER_WARNING) : null;
227+
$this->show_errors ? \trigger_error(" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N", \E_USER_WARNING) : null;
228228
}
229229
}
230230

@@ -241,7 +241,7 @@ public function get_col($query = null, $x = 0, $use_prepare = false)
241241
}
242242

243243
// Extract the column values
244-
$j = count($this->last_result);
244+
$j = \count($this->last_result);
245245
for ( $i=0; $i < $j; $i++ ) {
246246
$new_array[$i] = $this->get_var(null,$x,$i);
247247
}
@@ -265,15 +265,15 @@ public function get_results($query = null, $output = OBJECT, $use_prepare = fals
265265

266266
if ( $output == OBJECT ) {
267267
return $this->last_result;
268-
} elseif ( $output == _JSON ) {
269-
return json_encode($this->last_result); // return as json output
268+
} elseif ( $output == \_JSON ) {
269+
return \json_encode($this->last_result); // return as json output
270270
} elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
271271
if ( $this->last_result ) {
272272
$i=0;
273273
foreach( $this->last_result as $row ) {
274-
$new_array[$i] = get_object_vars($row);
274+
$new_array[$i] = \get_object_vars($row);
275275
if ( $output == ARRAY_N ) {
276-
$new_array[$i] = array_values($new_array[$i]);
276+
$new_array[$i] = \array_values($new_array[$i]);
277277
}
278278
$i++;
279279
}
@@ -288,11 +288,11 @@ public function get_results($query = null, $output = OBJECT, $use_prepare = fals
288288
* Function to get column meta data info pertaining to the last query
289289
* see docs for more info and usage
290290
*/
291-
public function get_col_info($info_type="name",$col_offset=-1)
291+
public function get_col_info($info_type = "name", $col_offset = -1)
292292
{
293293
if ( $this->col_info ) {
294294
if ( $col_offset == -1 ) {
295-
$i=0;
295+
$i = 0;
296296
foreach($this->col_info as $col ) {
297297
$new_array[$i] = $col->{$info_type};
298298
$i++;
@@ -311,13 +311,13 @@ public function get_col_info($info_type="name",$col_offset=-1)
311311
public function store_cache($query,$is_insert)
312312
{
313313
// The would be cache file for this query
314-
$cache_file = $this->cache_dir.'/'.md5($query);
314+
$cache_file = $this->cache_dir.'/'.\md5($query);
315315

316316
// disk caching of queries
317317
if ( $this->use_disk_cache && ( $this->cache_queries && ! $is_insert ) || ( $this->cache_inserts && $is_insert )) {
318-
if ( ! is_dir($this->cache_dir) ) {
318+
if ( ! \is_dir($this->cache_dir) ) {
319319
$this->register_error("Could not open cache dir: $this->cache_dir");
320-
$this->show_errors ? trigger_error("Could not open cache dir: $this->cache_dir",E_USER_WARNING) : null;
320+
$this->show_errors ? \trigger_error("Could not open cache dir: $this->cache_dir", \E_USER_WARNING) : null;
321321
} else {
322322
// Cache all result values
323323
$result_cache = array(
@@ -327,8 +327,8 @@ public function store_cache($query,$is_insert)
327327
'return_value' => $this->num_rows,
328328
);
329329

330-
file_put_contents($cache_file, serialize($result_cache));
331-
if( file_exists($cache_file . ".updating") )
330+
\file_put_contents($cache_file, \serialize($result_cache));
331+
if( \file_exists($cache_file . ".updating") )
332332
unlink($cache_file . ".updating");
333333
}
334334
}
@@ -340,16 +340,16 @@ public function store_cache($query,$is_insert)
340340
public function get_cache($query)
341341
{
342342
// The would be cache file for this query
343-
$cache_file = $this->cache_dir.'/'.md5($query);
343+
$cache_file = $this->cache_dir.'/'.\md5($query);
344344

345345
// Try to get previously cached version
346-
if ( $this->use_disk_cache && file_exists($cache_file) ) {
346+
if ( $this->use_disk_cache && \file_exists($cache_file) ) {
347347
// Only use this cache file if less than 'cache_timeout' (hours)
348-
if ( (time() - filemtime($cache_file)) > ($this->cache_timeout*3600) &&
349-
!(file_exists($cache_file . ".updating") && (time() - filemtime($cache_file . ".updating") < 60)) ) {
350-
touch($cache_file . ".updating"); // Show that we in the process of updating the cache
348+
if ( (\time() - \filemtime($cache_file)) > ($this->cache_timeout*3600) &&
349+
!(\file_exists($cache_file . ".updating") && (\time() - \filemtime($cache_file . ".updating") < 60)) ) {
350+
\touch($cache_file . ".updating"); // Show that we in the process of updating the cache
351351
} else {
352-
$result_cache = unserialize(file_get_contents($cache_file));
352+
$result_cache = \unserialize(\file_get_contents($cache_file));
353353

354354
$this->col_info = $result_cache['col_info'];
355355
$this->last_result = $result_cache['last_result'];
@@ -372,7 +372,7 @@ public function get_cache($query)
372372
public function vardump($mixed = '')
373373
{
374374
// Start output buffering
375-
ob_start();
375+
\ob_start();
376376

377377
echo "<p><table><tr><td bgcolor=ffffff><blockquote><font color=000090>";
378378
echo "<pre><font face=arial>";
@@ -381,9 +381,9 @@ public function vardump($mixed = '')
381381
echo "<font color=800080><b>ezSQL</b> (v".EZSQL_VERSION.") <b>Variable Dump..</b></font>\n\n";
382382
}
383383

384-
$var_type = gettype ($mixed);
385-
print_r(($mixed?$mixed:"<font color=red>No Value / False</font>"));
386-
echo "\n\n<b>Type:</b> " . ucfirst($var_type) . "\n";
384+
$var_type = \gettype ($mixed);
385+
\print_r(($mixed?$mixed:"<font color=red>No Value / False</font>"));
386+
echo "\n\n<b>Type:</b> " . \ucfirst($var_type) . "\n";
387387
echo "<b>Last Query</b> [$this->num_queries]<b>:</b> ".($this->last_query?$this->last_query:"NULL")."\n";
388388
echo "<b>Last Function Call:</b> " . ($this->func_call?$this->func_call:"None")."\n";
389389

@@ -393,13 +393,13 @@ public function vardump($mixed = '')
393393
echo " " . $func_string ."<br>\n";
394394
}
395395

396-
echo "<b>Last Rows Returned:</b> ".(count($this->last_result)>0 ? $this->last_result : '')."\n";
396+
echo "<b>Last Rows Returned:</b> ".(\count($this->last_result)>0 ? $this->last_result : '')."\n";
397397
echo "</font></pre></font></blockquote></td></tr></table>";//.$this->donation()
398398
echo "\n<hr size=1 noshade color=dddddd>";
399399

400400
// Stop output buffering and capture debug HTML
401-
$html = ob_get_contents();
402-
ob_end_clean();
401+
$html = \ob_get_contents();
402+
\ob_end_clean();
403403

404404
// Only echo output if it is turned on
405405
if ( $this->debug_echo_is_on ) {
@@ -426,7 +426,7 @@ public function dumpvar($mixed)
426426
public function debug($print_to_screen = true)
427427
{
428428
// Start outup buffering
429-
ob_start();
429+
\ob_start();
430430

431431
echo "<blockquote>";
432432

@@ -454,7 +454,7 @@ public function debug($print_to_screen = true)
454454
echo "<table cellpadding=5 cellspacing=1 bgcolor=555555>";
455455
echo "<tr bgcolor=eeeeee><td nowrap valign=bottom><font color=555599 face=arial size=2><b>(row)</b></font></td>";
456456

457-
for ( $i=0, $j=count($this->col_info); $i < $j; $i++ )
457+
for ( $i = 0, $j = \count($this->col_info); $i < $j; $i++ )
458458
{
459459
/* when selecting count(*) the maxlengh is not set, size is set instead. */
460460
echo "<td nowrap align=left valign=top><font size=1 color=555599 face=arial>{$this->col_info[$i]->type}";
@@ -486,7 +486,7 @@ public function debug($print_to_screen = true)
486486
}
487487
// if last result
488488
} else {
489-
echo "<tr bgcolor=ffffff><td colspan=".(count($this->col_info)+1)."><font face=arial size=2>No Results</font></td></tr>";
489+
echo "<tr bgcolor=ffffff><td colspan=".(\count($this->col_info)+1)."><font face=arial size=2>No Results</font></td></tr>";
490490
}
491491
echo "</table>";
492492
// if col_info
@@ -497,8 +497,8 @@ public function debug($print_to_screen = true)
497497
//echo "</blockquote></blockquote>".$this->donation()."<hr noshade color=dddddd size=1>";
498498

499499
// Stop output buffering and capture debug HTML
500-
$html = ob_get_contents();
501-
ob_end_clean();
500+
$html = \ob_get_contents();
501+
\ob_end_clean();
502502

503503
// Only echo output if it is turned on
504504
if ( $this->debug_echo_is_on && $print_to_screen)
@@ -523,7 +523,7 @@ public function donation()
523523
*/
524524
public function timer_get_cur()
525525
{
526-
list($usec, $sec) = explode(" ",microtime());
526+
list($usec, $sec) = \explode(" ",\microtime());
527527
return ((float)$usec + (float)$sec);
528528
}
529529

@@ -534,7 +534,7 @@ public function timer_start($timer_name)
534534

535535
public function timer_elapsed($timer_name)
536536
{
537-
return round($this->timer_get_cur() - $this->timers[$timer_name],2);
537+
return \round($this->timer_get_cur() - $this->timers[$timer_name],2);
538538
}
539539

540540
public function timer_update_global($timer_name)
@@ -569,7 +569,7 @@ public function timer_update_global($timer_name)
569569
*/
570570
public function get_set($params)
571571
{
572-
if( !is_array( $params ) )
572+
if( !\is_array( $params ) )
573573
{
574574
$this->register_error( 'get_set() parameter invalid. Expected array in '.__FILE__.' on line '.__LINE__);
575575
return;
@@ -591,7 +591,7 @@ public function get_set($params)
591591
$sql[] = "$field = '".$this->escape( $val )."'";
592592
}
593593
}
594-
return implode( ', ' , $sql );
594+
return \implode( ', ' , $sql );
595595
}
596596

597597
/**
@@ -651,7 +651,7 @@ public function query($query, $use_prepare=false)
651651
public function escape($data)
652652
{
653653
if ( !isset($data) ) return '';
654-
if ( is_numeric($data) ) return $data;
654+
if ( \is_numeric($data) ) return $data;
655655

656656
$non_displayables = array(
657657
'/%0[0-8bcef]/', // url encoded 00-08, 11, 12, 14, 15
@@ -663,10 +663,10 @@ public function escape($data)
663663
);
664664

665665
foreach ( $non_displayables as $regex )
666-
$data = preg_replace( $regex, '', $data );
666+
$data = \preg_replace( $regex, '', $data );
667667
$search = array("\\", "\x00", "\n", "\r", "'", '"', "\x1a");
668668
$replace = array("\\\\","\\0","\\n", "\\r", "\'", '\"', "\\Z");
669669

670-
return str_replace($search, $replace, $data);
670+
return \str_replace($search, $replace, $data);
671671
}
672672
} // ezSQLcore

tests/shared/ezSQLcoreTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,34 @@ public function testGet_host_port()
6464
$this->assertEquals($hostport[1],"8181");
6565
}
6666

67+
/**
68+
* @covers ezSQLcore::__call
69+
*/
70+
public function testGetCache_Timeout()
71+
{
72+
$res = $this->object->getCache_Timeout();
73+
$this->assertEquals(24, $res);
74+
}
75+
76+
/**
77+
* @covers ezSQLcore::__call
78+
*/
79+
public function testSetCache_Timeout()
80+
{
81+
$this->object->setCache_Timeout(44);
82+
$this->assertEquals(44, $this->object->getCache_Timeout());
83+
}
84+
85+
/**
86+
* @covers ezSQLcore::__call
87+
*/
88+
public function testgetNotProperty()
89+
{
90+
$this->expectException(\Exception::class);
91+
$this->expectExceptionMessageRegExp('/does not exist/');
92+
$res = $this->object->getNotProperty();
93+
}
94+
6795
/**
6896
* @covers ezSQLcore::register_error
6997
*/

0 commit comments

Comments
 (0)