better asserting

This commit is contained in:
Shish 2015-07-19 19:04:35 +01:00
parent 90539a32bc
commit 2b6f3b7266
4 changed files with 48 additions and 36 deletions

View File

@ -20,7 +20,7 @@ class Querylet {
* @param \Querylet $querylet * @param \Querylet $querylet
*/ */
public function append($querylet) { public function append($querylet) {
assert(!is_null($querylet)); assert('!is_null($querylet)');
$this->sql .= $querylet->sql; $this->sql .= $querylet->sql;
$this->variables = array_merge($this->variables, $querylet->variables); $this->variables = array_merge($this->variables, $querylet->variables);
} }
@ -295,7 +295,7 @@ class MemcacheCache implements CacheEngine {
* @return array|bool|string * @return array|bool|string
*/ */
public function get($key) { public function get($key) {
assert(!is_null($key)); assert('!is_null($key)');
$val = $this->memcache->get($key); $val = $this->memcache->get($key);
if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) { if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
$hit = $val === false ? "miss" : "hit"; $hit = $val === false ? "miss" : "hit";
@ -317,7 +317,7 @@ class MemcacheCache implements CacheEngine {
* @param int $time * @param int $time
*/ */
public function set($key, $val, $time=0) { public function set($key, $val, $time=0) {
assert(!is_null($key)); assert('!is_null($key)');
$this->memcache->set($key, $val, false, $time); $this->memcache->set($key, $val, false, $time);
if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) { if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
file_put_contents("data/cache.log", "Cache set: $key ($time)\n", FILE_APPEND); file_put_contents("data/cache.log", "Cache set: $key ($time)\n", FILE_APPEND);
@ -328,7 +328,7 @@ class MemcacheCache implements CacheEngine {
* @param string $key * @param string $key
*/ */
public function delete($key) { public function delete($key) {
assert(!is_null($key)); assert('!is_null($key)');
$this->memcache->delete($key); $this->memcache->delete($key);
if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) { if((DEBUG_CACHE === true) || (is_null(DEBUG_CACHE) && @$_GET['DEBUG_CACHE'])) {
file_put_contents("data/cache.log", "Cache delete: $key\n", FILE_APPEND); file_put_contents("data/cache.log", "Cache delete: $key\n", FILE_APPEND);
@ -354,7 +354,7 @@ class APCCache implements CacheEngine {
} }
public function get($key) { public function get($key) {
assert(!is_null($key)); assert('!is_null($key)');
$val = apc_fetch($key); $val = apc_fetch($key);
if($val) { if($val) {
$this->hits++; $this->hits++;
@ -367,12 +367,12 @@ class APCCache implements CacheEngine {
} }
public function set($key, $val, $time=0) { public function set($key, $val, $time=0) {
assert(!is_null($key)); assert('!is_null($key)');
apc_store($key, $val, $time); apc_store($key, $val, $time);
} }
public function delete($key) { public function delete($key) {
assert(!is_null($key)); assert('!is_null($key)');
apc_delete($key); apc_delete($key);
} }

View File

@ -97,7 +97,7 @@ class Image {
* @return Image * @return Image
*/ */
public static function by_id(/*int*/ $id) { public static function by_id(/*int*/ $id) {
assert(is_numeric($id)); assert('is_numeric($id)');
global $database; global $database;
$row = $database->get_row("SELECT * FROM images WHERE images.id=:id", array("id"=>$id)); $row = $database->get_row("SELECT * FROM images WHERE images.id=:id", array("id"=>$id));
return ($row ? new Image($row) : null); return ($row ? new Image($row) : null);
@ -110,7 +110,7 @@ class Image {
* @return Image * @return Image
*/ */
public static function by_hash(/*string*/ $hash) { public static function by_hash(/*string*/ $hash) {
assert(is_string($hash)); assert('is_string($hash)');
global $database; global $database;
$row = $database->get_row("SELECT images.* FROM images WHERE hash=:hash", array("hash"=>$hash)); $row = $database->get_row("SELECT images.* FROM images WHERE hash=:hash", array("hash"=>$hash));
return ($row ? new Image($row) : null); return ($row ? new Image($row) : null);
@ -123,7 +123,7 @@ class Image {
* @return Image * @return Image
*/ */
public static function by_random($tags=array()) { public static function by_random($tags=array()) {
assert(is_array($tags)); assert('is_array($tags)');
$max = Image::count_images($tags); $max = Image::count_images($tags);
if ($max < 1) return null; // From Issue #22 - opened by HungryFeline on May 30, 2011. if ($max < 1) return null; // From Issue #22 - opened by HungryFeline on May 30, 2011.
$rand = mt_rand(0, $max-1); $rand = mt_rand(0, $max-1);
@ -142,9 +142,9 @@ class Image {
* @return Image[] * @return Image[]
*/ */
public static function find_images(/*int*/ $start, /*int*/ $limit, $tags=array()) { public static function find_images(/*int*/ $start, /*int*/ $limit, $tags=array()) {
assert(is_numeric($start)); assert('is_numeric($start)');
assert(is_numeric($limit)); assert('is_numeric($limit)');
assert(is_array($tags)); assert('is_array($tags)');
global $database, $user, $config, $order_sql; global $database, $user, $config, $order_sql;
$images = array(); $images = array();
@ -246,7 +246,7 @@ class Image {
* @return mixed * @return mixed
*/ */
public static function count_images($tags=array()) { public static function count_images($tags=array()) {
assert(is_array($tags)); assert('is_array($tags)');
global $database; global $database;
$tag_count = count($tags); $tag_count = count($tags);
@ -278,7 +278,7 @@ class Image {
* @return float * @return float
*/ */
public static function count_pages($tags=array()) { public static function count_pages($tags=array()) {
assert(is_array($tags)); assert('is_array($tags)');
global $config, $database; global $config, $database;
return ceil(Image::count_images($tags) / $config->get_int('index_images')); return ceil(Image::count_images($tags) / $config->get_int('index_images'));
} }
@ -299,8 +299,8 @@ class Image {
* @return Image * @return Image
*/ */
public function get_next($tags=array(), $next=true) { public function get_next($tags=array(), $next=true) {
assert(is_array($tags)); assert('is_array($tags)');
assert(is_bool($next)); assert('is_bool($next)');
global $database; global $database;
if($next) { if($next) {
@ -576,18 +576,17 @@ class Image {
* @param string[] $tags * @param string[] $tags
*/ */
public function set_tags($tags) { public function set_tags($tags) {
assert('is_array($tags) && count($tags) > 0', var_export($tags, true));
global $database; global $database;
assert(is_array($tags));
$tags = array_map(array('Tag', 'sanitise'), $tags); $tags = array_map(array('Tag', 'sanitise'), $tags);
$tags = Tag::resolve_aliases($tags); $tags = Tag::resolve_aliases($tags);
assert(is_array($tags)); if(count($tags) <= 0) {
assert(count($tags) > 0); throw new SCoreException('Tried to set zero tags');
$new_tags = implode(" ", $tags); }
if($new_tags != $this->get_tag_list()) { if(implode(" ", $tags) != $this->get_tag_list()) {
// delete old // delete old
$this->delete_tags_from_image(); $this->delete_tags_from_image();
// insert each new tags // insert each new tags
@ -740,7 +739,7 @@ class Image {
* @return \Querylet * @return \Querylet
*/ */
private static function build_search_querylet($terms) { private static function build_search_querylet($terms) {
assert(is_array($terms)); assert('is_array($terms)');
global $database; global $database;
if($database->get_driver_name() === "mysql") if($database->get_driver_name() === "mysql")
return Image::build_ugly_search_querylet($terms); return Image::build_ugly_search_querylet($terms);
@ -1118,7 +1117,7 @@ class Tag {
* @return mixed * @return mixed
*/ */
public static function sanitise($tag) { public static function sanitise($tag) {
assert(is_string($tag)); assert('is_string($tag)');
$tag = preg_replace("/[\s?*]/", "", $tag); # whitespace $tag = preg_replace("/[\s?*]/", "", $tag); # whitespace
$tag = preg_replace('/\x20(\x0e|\x0f)/', '', $tag); # unicode RTL $tag = preg_replace('/\x20(\x0e|\x0f)/', '', $tag); # unicode RTL
$tag = preg_replace("/\.+/", ".", $tag); # strings of dots? $tag = preg_replace("/\.+/", ".", $tag); # strings of dots?
@ -1134,7 +1133,7 @@ class Tag {
* @return array * @return array
*/ */
public static function explode($tags, $tagme=true) { public static function explode($tags, $tagme=true) {
assert(is_string($tags) || is_array($tags)); assert('is_string($tags) || is_array($tags)');
if(is_string($tags)) { if(is_string($tags)) {
$tags = explode(' ', trim($tags)); $tags = explode(' ', trim($tags));
@ -1165,7 +1164,7 @@ class Tag {
* @return string * @return string
*/ */
public static function implode($tags) { public static function implode($tags) {
assert(is_string($tags) || is_array($tags)); assert('is_string($tags) || is_array($tags)');
if(is_array($tags)) { if(is_array($tags)) {
sort($tags); sort($tags);
@ -1183,7 +1182,7 @@ class Tag {
* @return string * @return string
*/ */
public static function resolve_alias($tag) { public static function resolve_alias($tag) {
assert(is_string($tag)); assert('is_string($tag)');
$negative = false; $negative = false;
if(!empty($tag) && ($tag[0] == '-')) { if(!empty($tag) && ($tag[0] == '-')) {
@ -1238,7 +1237,7 @@ class Tag {
* @return array * @return array
*/ */
public static function resolve_aliases($tags) { public static function resolve_aliases($tags) {
assert(is_array($tags)); assert('is_array($tags)');
$new = array(); $new = array();

View File

@ -94,7 +94,7 @@ class User {
* @return null|User * @return null|User
*/ */
public static function by_id(/*int*/ $id) { public static function by_id(/*int*/ $id) {
assert(is_numeric($id)); assert('is_numeric($id)', var_export($id, true));
global $database; global $database;
if($id === 1) { if($id === 1) {
$cached = $database->cache->get('user-id:'.$id); $cached = $database->cache->get('user-id:'.$id);
@ -111,7 +111,7 @@ class User {
* @return null|User * @return null|User
*/ */
public static function by_name(/*string*/ $name) { public static function by_name(/*string*/ $name) {
assert(is_string($name)); assert('is_string($name)', var_export($name, true));
global $database; global $database;
$row = $database->get_row($database->scoreql_to_sql("SELECT * FROM users WHERE SCORE_STRNORM(name) = SCORE_STRNORM(:name)"), array("name"=>$name)); $row = $database->get_row($database->scoreql_to_sql("SELECT * FROM users WHERE SCORE_STRNORM(name) = SCORE_STRNORM(:name)"), array("name"=>$name));
return is_null($row) ? null : new User($row); return is_null($row) ? null : new User($row);
@ -124,8 +124,8 @@ class User {
* @return null|User * @return null|User
*/ */
public static function by_name_and_pass(/*string*/ $name, /*string*/ $pass) { public static function by_name_and_pass(/*string*/ $name, /*string*/ $pass) {
assert(is_string($name)); assert('is_string($name)', var_export($name, true));
assert(is_string($pass)); assert('is_string($pass)', var_export($pass, true));
$user = User::by_name($name); $user = User::by_name($name);
if($user) { if($user) {
if($user->passhash == md5(strtolower($name) . $pass)) { if($user->passhash == md5(strtolower($name) . $pass)) {
@ -143,8 +143,8 @@ class User {
* @return array * @return array
*/ */
public static function by_list(/*int*/ $offset, /*int*/ $limit=50) { public static function by_list(/*int*/ $offset, /*int*/ $limit=50) {
assert(is_numeric($offset)); assert('is_numeric($offset)', var_export($offset, true));
assert(is_numeric($limit)); assert('is_numeric($limit)', var_export($limit, true));
global $database; global $database;
$rows = $database->get_all("SELECT * FROM users WHERE id >= :start AND id < :end", array("start"=>$offset, "end"=>$offset+$limit)); $rows = $database->get_all("SELECT * FROM users WHERE id >= :start AND id < :end", array("start"=>$offset, "end"=>$offset+$limit));
return array_map("_new_user", $rows); return array_map("_new_user", $rows);
@ -196,7 +196,7 @@ class User {
* @param string $class * @param string $class
*/ */
public function set_class(/*string*/ $class) { public function set_class(/*string*/ $class) {
assert(is_string($class)); assert('is_string($class)', var_export($class, true));
global $database; global $database;
$database->Execute("UPDATE users SET class=:class WHERE id=:id", array("class"=>$class, "id"=>$this->id)); $database->Execute("UPDATE users SET class=:class WHERE id=:id", array("class"=>$class, "id"=>$this->id));
log_info("core-user", 'Set class for '.$this->name.' to '.$class); log_info("core-user", 'Set class for '.$this->name.' to '.$class);

View File

@ -1441,6 +1441,19 @@ function get_debug_info() {
return $debug; return $debug;
} }
function score_assert_handler($file, $line, $code, $desc = null) {
$file = basename($file);
print("Assertion failed at $file:$line: $code ($desc)");
/*
print("<pre>");
debug_print_backtrace();
print("</pre>");
*/
}
//assert_options(ASSERT_ACTIVE, 1);
assert_options(ASSERT_WARNING, 0);
assert_options(ASSERT_QUIET_EVAL, 1);
assert_options(ASSERT_CALLBACK, 'score_assert_handler');
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* Request initialisation stuff * * Request initialisation stuff *