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

View file

@ -389,6 +389,8 @@ function _sanitise_environment() {
date_default_timezone_set(TIMEZONE); date_default_timezone_set(TIMEZONE);
} }
ini_set('zend.assertions', 1); // generate assertions
ini_set('assert.exception', 1); // throw exceptions when failed
if(DEBUG) { if(DEBUG) {
error_reporting(E_ALL); error_reporting(E_ALL);
assert_options(ASSERT_ACTIVE, 1); 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. * 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 ( file_exists ( $tmpname ) );
assert('is_string($tags)');
$pathinfo = pathinfo ( $filename ); $pathinfo = pathinfo ( $filename );
if (! array_key_exists ( 'extension', $pathinfo )) { if (! array_key_exists ( 'extension', $pathinfo )) {

View file

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

View file

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

View file

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

View file

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

View file

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