enforcing parameter types, avoiding unnecessary metadata in version 2 pastes

This commit is contained in:
El RIDO 2019-05-05 18:22:57 +02:00
parent 6e15903f1e
commit b7a03cfdb9
No known key found for this signature in database
GPG key ID: 0F5C940A6BD81F92
2 changed files with 39 additions and 36 deletions

View file

@ -60,7 +60,7 @@ abstract class AbstractData
* @param array $options
* @return AbstractData
*/
public static function getInstance($options)
public static function getInstance(array $options)
{
}
@ -72,7 +72,7 @@ abstract class AbstractData
* @param array $paste
* @return bool
*/
abstract public function create($pasteid, $paste);
abstract public function create(string $pasteid, array $paste);
/**
* Read a paste.
@ -81,7 +81,7 @@ abstract class AbstractData
* @param string $pasteid
* @return stdClass|false
*/
abstract public function read($pasteid);
abstract public function read(string $pasteid);
/**
* Delete a paste and its discussion.
@ -89,7 +89,7 @@ abstract class AbstractData
* @access public
* @param string $pasteid
*/
abstract public function delete($pasteid);
abstract public function delete(string $pasteid);
/**
* Test if a paste exists.
@ -98,7 +98,7 @@ abstract class AbstractData
* @param string $pasteid
* @return bool
*/
abstract public function exists($pasteid);
abstract public function exists(string $pasteid);
/**
* Create a comment in a paste.
@ -110,7 +110,7 @@ abstract class AbstractData
* @param array $comment
* @return bool
*/
abstract public function createComment($pasteid, $parentid, $commentid, $comment);
abstract public function createComment(string $pasteid, string $parentid, string $commentid, array $comment);
/**
* Read all comments of paste.
@ -119,7 +119,7 @@ abstract class AbstractData
* @param string $pasteid
* @return array
*/
abstract public function readComments($pasteid);
abstract public function readComments(string $pasteid);
/**
* Test if a comment exists.
@ -130,7 +130,7 @@ abstract class AbstractData
* @param string $commentid
* @return bool
*/
abstract public function existsComment($pasteid, $parentid, $commentid);
abstract public function existsComment(string $pasteid, string $parentid, string $commentid);
/**
* Returns up to batch size number of paste ids that have expired
@ -139,7 +139,7 @@ abstract class AbstractData
* @param int $batchsize
* @return array
*/
abstract protected function _getExpiredPastes($batchsize);
abstract protected function _getExpiredPastes(int $batchsize);
/**
* Perform a purge of old pastes, at most the given batchsize is deleted.
@ -147,7 +147,7 @@ abstract class AbstractData
* @access public
* @param int $batchsize
*/
public function purge($batchsize)
public function purge(int $batchsize)
{
if ($batchsize < 1) {
return;
@ -168,7 +168,7 @@ abstract class AbstractData
* @param int|string $postdate
* @return int|string
*/
protected function getOpenSlot(&$comments, $postdate)
protected function getOpenSlot(array &$comments, $postdate)
{
if (array_key_exists($postdate, $comments)) {
$parts = explode('.', $postdate, 2);

View file

@ -155,7 +155,7 @@ class Database extends AbstractData
* @param array $paste
* @return bool
*/
public function create($pasteid, $paste)
public function create(string $pasteid, array $paste)
{
if (
array_key_exists($pasteid, self::$_cache)
@ -180,11 +180,11 @@ class Database extends AbstractData
unset($meta['expire_date']);
}
if (array_key_exists('opendiscussion', $meta)) {
$opendiscussion = (bool) $meta['opendiscussion'];
$opendiscussion = $meta['opendiscussion'];
unset($meta['opendiscussion']);
}
if (array_key_exists('burnafterreading', $meta)) {
$burnafterreading = (bool) $meta['burnafterreading'];
$burnafterreading = $meta['burnafterreading'];
unset($meta['burnafterreading']);
}
if ($isVersion1) {
@ -196,6 +196,9 @@ class Database extends AbstractData
$attachmentname = $meta['attachmentname'];
unset($meta['attachmentname']);
}
} else {
$opendiscussion = $paste['adata'][2];
$burnafterreading = $paste['adata'][3];
}
return self::_exec(
'INSERT INTO ' . self::_sanitizeIdentifier('paste') .
@ -221,7 +224,7 @@ class Database extends AbstractData
* @param string $pasteid
* @return stdClass|false
*/
public function read($pasteid)
public function read(string $pasteid)
{
if (
!array_key_exists($pasteid, self::$_cache)
@ -250,6 +253,13 @@ class Database extends AbstractData
$meta = new stdClass;
}
self::$_cache[$pasteid]->meta = $meta;
self::$_cache[$pasteid]->meta->$createdKey = (int) $paste['postdate'];
$expire_date = (int) $paste['expiredate'];
if ($expire_date > 0) {
self::$_cache[$pasteid]->meta->expire_date = $expire_date;
}
if (!$isVersion2) {
// support pre v1 attachments
if (property_exists($meta, 'attachment')) {
@ -267,19 +277,12 @@ class Database extends AbstractData
self::$_cache[$pasteid]->attachmentname = $paste['attachmentname'];
}
}
}
self::$_cache[$pasteid]->meta = $meta;
self::$_cache[$pasteid]->meta->$createdKey = (int) $paste['postdate'];
$expire_date = (int) $paste['expiredate'];
if ($expire_date > 0) {
self::$_cache[$pasteid]->meta->expire_date = $expire_date;
}
if ($paste['opendiscussion']) {
self::$_cache[$pasteid]->meta->opendiscussion = true;
}
if ($paste['burnafterreading']) {
self::$_cache[$pasteid]->meta->burnafterreading = true;
if ($paste['opendiscussion']) {
self::$_cache[$pasteid]->meta->opendiscussion = true;
}
if ($paste['burnafterreading']) {
self::$_cache[$pasteid]->meta->burnafterreading = true;
}
}
}
}
@ -293,7 +296,7 @@ class Database extends AbstractData
* @access public
* @param string $pasteid
*/
public function delete($pasteid)
public function delete(string $pasteid)
{
self::_exec(
'DELETE FROM ' . self::_sanitizeIdentifier('paste') .
@ -317,7 +320,7 @@ class Database extends AbstractData
* @param string $pasteid
* @return bool
*/
public function exists($pasteid)
public function exists(string $pasteid)
{
if (
!array_key_exists($pasteid, self::$_cache)
@ -337,7 +340,7 @@ class Database extends AbstractData
* @param array $comment
* @return bool
*/
public function createComment($pasteid, $parentid, $commentid, $comment)
public function createComment(string $pasteid, string $parentid, string $commentid, array $comment)
{
if (array_key_exists('data', $comment)) {
$version = 1;
@ -376,7 +379,7 @@ class Database extends AbstractData
* @param string $pasteid
* @return array
*/
public function readComments($pasteid)
public function readComments(string $pasteid)
{
$rows = self::_select(
'SELECT * FROM ' . self::_sanitizeIdentifier('comment') .
@ -422,7 +425,7 @@ class Database extends AbstractData
* @param string $commentid
* @return bool
*/
public function existsComment($pasteid, $parentid, $commentid)
public function existsComment(string $pasteid, string $parentid, string $commentid)
{
return (bool) self::_select(
'SELECT dataid FROM ' . self::_sanitizeIdentifier('comment') .
@ -438,7 +441,7 @@ class Database extends AbstractData
* @param int $batchsize
* @return array
*/
protected function _getExpiredPastes($batchsize)
protected function _getExpiredPastes(int $batchsize)
{
$pastes = array();
$rows = self::_select(
@ -463,7 +466,7 @@ class Database extends AbstractData
* @throws PDOException
* @return bool
*/
private static function _exec($sql, array $params)
private static function _exec(string $sql, array $params)
{
$statement = self::$_db->prepare($sql);
$result = $statement->execute($params);
@ -482,7 +485,7 @@ class Database extends AbstractData
* @throws PDOException
* @return array
*/
private static function _select($sql, array $params, $firstOnly = false)
private static function _select(string $sql, array $params, bool $firstOnly = false)
{
$statement = self::$_db->prepare($sql);
$statement->execute($params);