From 9f26894b2e7a6416b26f05e049f13a5b6cc51d2e Mon Sep 17 00:00:00 2001 From: El RIDO Date: Sun, 8 Oct 2017 11:31:41 +0200 Subject: [PATCH] PHP < 5.6 compatibility and StyleCI recommendations --- lib/Configuration.php | 6 +++--- lib/Data/Filesystem.php | 19 +++---------------- lib/Persistence/DataStore.php | 13 +++++++++++++ tst/Data/FilesystemTest.php | 8 ++++---- tst/PrivateBinTest.php | 2 +- tst/PrivateBinWithDbTest.php | 1 - 6 files changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/Configuration.php b/lib/Configuration.php index baee718..5b8813d 100644 --- a/lib/Configuration.php +++ b/lib/Configuration.php @@ -27,7 +27,7 @@ class Configuration * * @const string */ - const PROTECTION_LINE = ';exists($pasteid)) { return false; } - $paste = self::_decodeFile(self::_dataid2path($pasteid) . $pasteid . '.php'); + $paste = DataStore::get(self::_dataid2path($pasteid) . $pasteid . '.php'); if (property_exists($paste->meta, 'attachment')) { $paste->attachment = $paste->meta->attachment; unset($paste->meta->attachment); @@ -210,7 +210,7 @@ class Filesystem extends AbstractData // - commentid is the comment identifier itself. // - parentid is the comment this comment replies to (It can be pasteid) if (is_file($discdir . $filename)) { - $comment = self::_decodeFile($discdir . $filename); + $comment = DataStore::get($discdir . $filename); $items = explode('.', $filename); // Add some meta information not contained in file. $comment->id = $items[1]; @@ -285,7 +285,7 @@ class Filesystem extends AbstractData } $thirdLevel = array_filter( array_map( - function($filename) { + function ($filename) { return strlen($filename) >= 20 ? substr($filename, 0, -4) : $filename; @@ -385,17 +385,4 @@ class Filesystem extends AbstractData { return (bool) preg_match('/^[a-f0-9]{2}$/', $element); } - - /** - * Decodes a paste or comment file. - * - * @access private - * @static - * @param string $file - * @return array - */ - private static function _decodeFile($file) - { - return json_decode(substr(file_get_contents($file), strlen(DataStore::PROTECTION_LINE . PHP_EOL))); - } } diff --git a/lib/Persistence/DataStore.php b/lib/Persistence/DataStore.php index 0c03f27..c5f14ff 100644 --- a/lib/Persistence/DataStore.php +++ b/lib/Persistence/DataStore.php @@ -51,4 +51,17 @@ class DataStore extends AbstractPersistence return false; } } + + /** + * get the data + * + * @access public + * @static + * @param string $filename + * @return array $data + */ + public static function get($filename) + { + return json_decode(substr(file_get_contents($filename), strlen(self::PROTECTION_LINE . PHP_EOL))); + } } diff --git a/tst/Data/FilesystemTest.php b/tst/Data/FilesystemTest.php index 8b04928..0a122f0 100644 --- a/tst/Data/FilesystemTest.php +++ b/tst/Data/FilesystemTest.php @@ -134,15 +134,15 @@ class FilesystemTest extends PHPUnit_Framework_TestCase public function testOldFilesGetConverted() { // generate 10 (default purge batch size) pastes in the old format - $paste = Helper::getPaste(); - $comment = Helper::getComment(); + $paste = Helper::getPaste(); + $comment = Helper::getComment(); $commentid = Helper::getCommentId(); - $ids = array(); + $ids = array(); for ($i = 0, $max = 10; $i < $max; ++$i) { // PHPs mt_rand only supports 32 bit or up 0x7fffffff on 64 bit systems to be precise :-/ $dataid = str_pad(dechex(mt_rand(0, mt_getrandmax())), 8, '0', STR_PAD_LEFT) . str_pad(dechex(mt_rand(0, mt_getrandmax())), 8, '0', STR_PAD_LEFT); - $storagedir = $this->_path . DIRECTORY_SEPARATOR . substr($dataid, 0, 2) . + $storagedir = $this->_path . DIRECTORY_SEPARATOR . substr($dataid, 0, 2) . DIRECTORY_SEPARATOR . substr($dataid, 2, 2) . DIRECTORY_SEPARATOR; $ids[$dataid] = $storagedir; diff --git a/tst/PrivateBinTest.php b/tst/PrivateBinTest.php index e1f3113..5c5e145 100644 --- a/tst/PrivateBinTest.php +++ b/tst/PrivateBinTest.php @@ -111,7 +111,7 @@ class PrivateBinTest extends PHPUnit_Framework_TestCase */ public function testViewUrlShortener() { - $shortener = 'https://shortener.example.com/api?link='; + $shortener = 'https://shortener.example.com/api?link='; $options = parse_ini_file(CONF, true); $options['main']['urlshortener'] = $shortener; Helper::createIniFile(CONF, $options); diff --git a/tst/PrivateBinWithDbTest.php b/tst/PrivateBinWithDbTest.php index a6ec2e0..a438d4c 100644 --- a/tst/PrivateBinWithDbTest.php +++ b/tst/PrivateBinWithDbTest.php @@ -1,7 +1,6 @@