php7 assertions, no strings

This commit is contained in:
Shish 2019-01-06 10:40:39 +00:00
parent 6f5cf4d865
commit ead3a5a588
9 changed files with 18 additions and 52 deletions

View file

@ -559,6 +559,8 @@ class Image {
* Set the tags for this image.
*/
public function set_tags(array $unfiltered_tags) {
global $database;
$tags = [];
foreach ($unfiltered_tags as $tag) {
if(mb_strlen($tag, 'UTF-8') > 255){
@ -572,8 +574,6 @@ class Image {
$tags[] = $tag;
}
assert('count($tags) > 0', var_export($tags, true));
global $database;
if(count($tags) <= 0) {
throw new SCoreException('Tried to set zero tags');
@ -916,7 +916,7 @@ class Image {
}
}
assert('$positive_tag_id_array || $negative_tag_id_array', @$_GET['q']);
assert($positive_tag_id_array || $negative_tag_id_array, @$_GET['q']);
$wheres = array();
if (!empty($positive_tag_id_array)) {
$positive_tag_id_list = join(', ', $positive_tag_id_array);

View file

@ -546,7 +546,7 @@ function clamp(int $val, int $min=null, int $max=null): int {
$val = $max;
}
if(!is_null($min) && !is_null($max)) {
assert('$val >= $min && $val <= $max', "$min <= $val <= $max");
assert($val >= $min && $val <= $max, "$min <= $val <= $max");
}
return $val;
}

View file

@ -389,6 +389,8 @@ function _sanitise_environment() {
date_default_timezone_set(TIMEZONE);
}
ini_set('zend.assertions', 1); // generate assertions
ini_set('assert.exception', 1); // throw exceptions when failed
if(DEBUG) {
error_reporting(E_ALL);
assert_options(ASSERT_ACTIVE, 1);

View file

@ -304,14 +304,9 @@ class CronUploader extends Extension {
/**
* Generate the necessary DataUploadEvent for a given image and tags.
*
* @param string $tmpname
* @param string $filename
* @param string $tags
*/
private function add_image($tmpname, $filename, $tags) {
private function add_image(string $tmpname, string $filename, string $tags) {
assert ( file_exists ( $tmpname ) );
assert('is_string($tags)');
$pathinfo = pathinfo ( $filename );
if (! array_key_exists ( 'extension', $pathinfo )) {

View file

@ -48,12 +48,8 @@ class LiveFeed extends Extension {
public function get_priority(): int {return 99;}
/**
* @param string $data
*/
private function msg($data) {
private function msg(string $data) {
global $config;
assert('is_string($data)');
$host = $config->get_string("livefeed_host", "127.0.0.1:25252");

View file

@ -342,14 +342,7 @@ class TagEdit extends Extension {
}
}
/**
* @param string $tags
* @param string $source
*/
private function mass_source_edit($tags, $source) {
assert('is_string($tags)');
assert('is_string($source)');
private function mass_source_edit(string $tags, string $source) {
$tags = Tag::explode($tags);
$last_id = -1;

View file

@ -353,9 +353,8 @@ class Tag_History extends Extension {
* @param Image $image
* @param string[] $tags
*/
private function add_tag_history(Image $image, $tags) {
private function add_tag_history(Image $image, array $tags) {
global $database, $config, $user;
assert('is_array($tags)');
$new_tags = Tag::implode($tags);
$old_tags = $image->get_tag_list();

View file

@ -494,9 +494,8 @@ class TagList extends Extension {
* @param Page $page
* @param string[] $search
*/
private function add_refine_block(Page $page, $search) {
private function add_refine_block(Page $page, array $search) {
global $database, $config;
assert('is_array($search)');
$wild_tags = $search;
$str_search = Tag::implode($search);

View file

@ -24,16 +24,15 @@ class DataUploadEvent extends Event {
/**
* Some data is being uploaded.
* This should be caught by a file handler.
* -- Removed: param $user The user uploading the data.
* @param string $tmpname The temporary file used for upload.
* @param array $metadata Info about the file, should contain at least "filename", "extension", "tags" and "source".
*/
public function __construct(string $tmpname, array $metadata) {
assert('file_exists($tmpname)');
assert('is_string($metadata["filename"])');
assert('is_string($metadata["extension"])');
assert('is_array($metadata["tags"])');
assert('is_string($metadata["source"]) || is_null($metadata["source"])');
assert(file_exists($tmpname));
assert(is_string($metadata["filename"]));
assert(is_string($metadata["extension"]));
assert(is_array($metadata["tags"]));
assert(is_string($metadata["source"]) || is_null($metadata["source"]));
$this->tmpname = $tmpname;
@ -298,12 +297,8 @@ class Upload extends Extension {
* @param int $replace
* @return bool TRUE on upload successful.
*/
private function try_upload($file, $tags, $source, $replace=-1) {
private function try_upload(array $file, array $tags, string $source=null, int $replace=-1): bool {
global $page;
assert('is_array($file)');
assert('is_array($tags)');
assert('is_string($source) || is_null($source)');
assert('is_int($replace)');
if(empty($source)) $source = null;
@ -346,21 +341,8 @@ class Upload extends Extension {
return $ok;
}
/**
* Handle an transload.
*
* @param string $url
* @param string[] $tags
* @param string|null $source
* @param int $replace
* @return bool Returns TRUE on transload successful.
*/
private function try_transload($url, $tags, $source, $replace=-1) {
private function try_transload(string $url, array $tags, string $source=null, int $replace=-1): bool {
global $page, $config, $user;
assert('is_string($url)');
assert('is_array($tags)');
assert('is_string($source) || is_null($source)');
assert('is_int($replace)');
$ok = true;