Skip to content

Commit

Permalink
added configuration for PHP Coding Standards Fixer, including its fix…
Browse files Browse the repository at this point in the history
…es, resolving #47
  • Loading branch information
elrido committed Aug 15, 2016
1 parent 87926ce commit 72aac25
Show file tree
Hide file tree
Showing 17 changed files with 114 additions and 90 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -8,4 +8,5 @@ tst/ export-ignore
.gitattributes export-ignore
.github export-ignore
.gitignore export-ignore
.php_cs export-ignore
.travis.yml export-ignore
23 changes: 23 additions & 0 deletions .php_cs
@@ -0,0 +1,23 @@
<?php
/**
* Configuration file for PHP Coding Standards Fixer (php-cs-fixer).
*
* On GitHub: https://github.com/FriendsOfPhp/php-cs-fixer
* More information: http://cs.sensiolabs.org/
*/

$finder = Symfony\CS\Finder\DefaultFinder::create()
->in('lib')
;

return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
->fixers(['concat_with_spaces', 'long_array_syntax', 'standardize_not_equal',
'operators_spaces', 'duplicate_semicolon',
'remove_leading_slash_use', 'align_equals',
'single_array_no_trailing_comma', 'phpdoc_indent', 'phpdoc_scalar',
'phpdoc_to_comment', 'phpdoc_trim',
'phpdoc_types', 'print_to_echo', 'self_accessor', 'single_quote',
'spaces_cast', 'ternary_spaces', 'phpdoc_order'])
->finder($finder)
;
2 changes: 1 addition & 1 deletion lib/Configuration.php
Expand Up @@ -98,7 +98,7 @@ class Configuration
*/
public function __construct()
{
$config = array();
$config = array();
$configFile = PATH . 'cfg' . DIRECTORY_SEPARATOR . 'conf.ini';
if (is_readable($configFile)) {
$config = parse_ini_file($configFile, true);
Expand Down
62 changes: 31 additions & 31 deletions lib/Data/Database.php
Expand Up @@ -71,7 +71,7 @@ class Database extends AbstractData
public static function getInstance($options = null)
{
// if needed initialize the singleton
if (!(self::$_instance instanceof Database)) {
if (!(self::$_instance instanceof self)) {
self::$_instance = new self;
}

Expand All @@ -89,17 +89,17 @@ public static function getInstance($options = null)
array_key_exists('opt', $options)
) {
// set default options
$options['opt'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$options['opt'][PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
$options['opt'][PDO::ATTR_EMULATE_PREPARES] = false;
$options['opt'][PDO::ATTR_PERSISTENT] = true;
$db_tables_exist = true;
$options['opt'][PDO::ATTR_PERSISTENT] = true;
$db_tables_exist = true;

// setup type and dabase connection
self::$_type = strtolower(
substr($options['dsn'], 0, strpos($options['dsn'], ':'))
);
$tableQuery = self::_getTableQuery(self::$_type);
self::$_db = new PDO(
self::$_db = new PDO(
$options['dsn'],
$options['usr'],
$options['pwd'],
Expand Down Expand Up @@ -168,8 +168,8 @@ public function create($pasteid, $paste)
}

$opendiscussion = $burnafterreading = false;
$attachment = $attachmentname = '';
$meta = $paste['meta'];
$attachment = $attachmentname = '';
$meta = $paste['meta'];
unset($meta['postdate']);
$expire_date = 0;
if (array_key_exists('expire_date', $paste['meta'])) {
Expand Down Expand Up @@ -222,14 +222,14 @@ public function read($pasteid)
!array_key_exists($pasteid, self::$_cache)
) {
self::$_cache[$pasteid] = false;
$paste = self::_select(
$paste = self::_select(
'SELECT * FROM ' . self::_sanitizeIdentifier('paste') .
' WHERE dataid = ?', array($pasteid), true
);

if (false !== $paste) {
// create object
self::$_cache[$pasteid] = new stdClass;
self::$_cache[$pasteid] = new stdClass;
self::$_cache[$pasteid]->data = $paste['data'];

$meta = json_decode($paste['meta']);
Expand All @@ -253,9 +253,9 @@ public function read($pasteid)
self::$_cache[$pasteid]->attachmentname = $paste['attachmentname'];
}
}
self::$_cache[$pasteid]->meta = $meta;
self::$_cache[$pasteid]->meta = $meta;
self::$_cache[$pasteid]->meta->postdate = (int) $paste['postdate'];
$expire_date = (int) $paste['expiredate'];
$expire_date = (int) $paste['expiredate'];
if (
$expire_date > 0
) {
Expand Down Expand Up @@ -368,12 +368,12 @@ public function readComments($pasteid)
$comments = array();
if (count($rows)) {
foreach ($rows as $row) {
$i = $this->getOpenSlot($comments, (int) $row['postdate']);
$comments[$i] = new stdClass;
$comments[$i]->id = $row['dataid'];
$comments[$i]->parentid = $row['parentid'];
$comments[$i]->data = $row['data'];
$comments[$i]->meta = new stdClass;
$i = $this->getOpenSlot($comments, (int) $row['postdate']);
$comments[$i] = new stdClass;
$comments[$i]->id = $row['dataid'];
$comments[$i]->parentid = $row['parentid'];
$comments[$i]->data = $row['data'];
$comments[$i]->meta = new stdClass;
$comments[$i]->meta->postdate = (int) $row['postdate'];
if (array_key_exists('nickname', $row) && !empty($row['nickname'])) {
$comments[$i]->meta->nickname = $row['nickname'];
Expand Down Expand Up @@ -415,7 +415,7 @@ public function existsComment($pasteid, $parentid, $commentid)
protected function _getExpiredPastes($batchsize)
{
$pastes = array();
$rows = self::_select(
$rows = self::_select(
'SELECT dataid FROM ' . self::_sanitizeIdentifier('paste') .
' WHERE expiredate < ? LIMIT ?', array(time(), $batchsize)
);
Expand All @@ -440,7 +440,7 @@ protected function _getExpiredPastes($batchsize)
private static function _exec($sql, array $params)
{
$statement = self::$_db->prepare($sql);
$result = $statement->execute($params);
$result = $statement->execute($params);
$statement->closeCursor();
return $result;
}
Expand Down Expand Up @@ -486,7 +486,7 @@ private static function _getTableQuery($type)
$sql = 'SELECT tabname FROM systables ';
break;
case 'mssql':
$sql = "SELECT name FROM sysobjects "
$sql = 'SELECT name FROM sysobjects '
. "WHERE type = 'U' ORDER BY name";
break;
case 'mysql':
Expand All @@ -496,22 +496,22 @@ private static function _getTableQuery($type)
$sql = 'SELECT table_name FROM all_tables';
break;
case 'pgsql':
$sql = "SELECT c.relname AS table_name "
. "FROM pg_class c, pg_user u "
$sql = 'SELECT c.relname AS table_name '
. 'FROM pg_class c, pg_user u '
. "WHERE c.relowner = u.usesysid AND c.relkind = 'r' "
. "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
. 'AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) '
. "AND c.relname !~ '^(pg_|sql_)' "
. "UNION "
. "SELECT c.relname AS table_name "
. "FROM pg_class c "
. 'UNION '
. 'SELECT c.relname AS table_name '
. 'FROM pg_class c '
. "WHERE c.relkind = 'r' "
. "AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) "
. "AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) "
. 'AND NOT EXISTS (SELECT 1 FROM pg_views WHERE viewname = c.relname) '
. 'AND NOT EXISTS (SELECT 1 FROM pg_user WHERE usesysid = c.relowner) '
. "AND c.relname !~ '^pg_'";
break;
case 'sqlite':
$sql = "SELECT name FROM sqlite_master WHERE type='table' "
. "UNION ALL SELECT name FROM sqlite_temp_master "
. 'UNION ALL SELECT name FROM sqlite_temp_master '
. "WHERE type='table' ORDER BY name";
break;
default:
Expand Down Expand Up @@ -569,7 +569,7 @@ private static function _getPrimaryKeyClauses($key = 'dataid')
private static function _createPasteTable()
{
list($main_key, $after_key) = self::_getPrimaryKeyClauses();
$dataType = self::$_type === 'pgsql' ? 'TEXT' : 'BLOB';
$dataType = self::$_type === 'pgsql' ? 'TEXT' : 'BLOB';
self::$_db->exec(
'CREATE TABLE ' . self::_sanitizeIdentifier('paste') . ' ( ' .
"dataid CHAR(16) NOT NULL$main_key, " .
Expand All @@ -594,7 +594,7 @@ private static function _createPasteTable()
private static function _createCommentTable()
{
list($main_key, $after_key) = self::_getPrimaryKeyClauses();
$dataType = self::$_type === 'pgsql' ? 'text' : 'BLOB';
$dataType = self::$_type === 'pgsql' ? 'text' : 'BLOB';
self::$_db->exec(
'CREATE TABLE ' . self::_sanitizeIdentifier('comment') . ' ( ' .
"dataid CHAR(16) NOT NULL$main_key, " .
Expand Down
20 changes: 10 additions & 10 deletions lib/Data/Filesystem.php
Expand Up @@ -49,7 +49,7 @@ public static function getInstance($options = null)
self::$_dir = $options['dir'] . DIRECTORY_SEPARATOR;
}
// if needed initialize the singleton
if (!(self::$_instance instanceof Filesystem)) {
if (!(self::$_instance instanceof self)) {
self::$_instance = new self;
self::_init();
}
Expand Down Expand Up @@ -161,7 +161,7 @@ public function exists($pasteid)
public function createComment($pasteid, $parentid, $commentid, $comment)
{
$storagedir = self::_dataid2discussionpath($pasteid);
$filename = $pasteid . '.' . $commentid . '.' . $parentid;
$filename = $pasteid . '.' . $commentid . '.' . $parentid;
if (is_file($storagedir . $filename)) {
return false;
}
Expand All @@ -181,7 +181,7 @@ public function createComment($pasteid, $parentid, $commentid, $comment)
public function readComments($pasteid)
{
$comments = array();
$discdir = self::_dataid2discussionpath($pasteid);
$discdir = self::_dataid2discussionpath($pasteid);
if (is_dir($discdir)) {
// Delete all files in discussion directory
$dir = dir($discdir);
Expand All @@ -192,13 +192,13 @@ public function readComments($pasteid)
// - parentid is the comment this comment replies to (It can be pasteid)
if (is_file($discdir . $filename)) {
$comment = json_decode(file_get_contents($discdir . $filename));
$items = explode('.', $filename);
$items = explode('.', $filename);
// Add some meta information not contained in file.
$comment->id = $items[1];
$comment->id = $items[1];
$comment->parentid = $items[2];

// Store in array
$key = $this->getOpenSlot($comments, (int) $comment->meta->postdate);
$key = $this->getOpenSlot($comments, (int) $comment->meta->postdate);
$comments[$key] = $comment;
}
}
Expand Down Expand Up @@ -236,15 +236,15 @@ public function existsComment($pasteid, $parentid, $commentid)
*/
protected function _getExpiredPastes($batchsize)
{
$pastes = array();
$pastes = array();
$firstLevel = array_filter(
scandir(self::$_dir),
'self::_isFirstLevelDir'
);
if (count($firstLevel) > 0) {
// try at most 10 times the $batchsize pastes before giving up
for ($i = 0, $max = $batchsize * 10; $i < $max; ++$i) {
$firstKey = array_rand($firstLevel);
$firstKey = array_rand($firstLevel);
$secondLevel = array_filter(
scandir(self::$_dir . $firstLevel[$firstKey]),
'self::_isSecondLevelDir'
Expand All @@ -257,7 +257,7 @@ protected function _getExpiredPastes($batchsize)
}

$secondKey = array_rand($secondLevel);
$path = self::$_dir . $firstLevel[$firstKey] .
$path = self::$_dir . $firstLevel[$firstKey] .
DIRECTORY_SEPARATOR . $secondLevel[$secondKey];
if (!is_dir($path)) {
continue;
Expand All @@ -270,7 +270,7 @@ protected function _getExpiredPastes($batchsize)
continue;
}
$thirdKey = array_rand($thirdLevel);
$pasteid = $thirdLevel[$thirdKey];
$pasteid = $thirdLevel[$thirdKey];
if (in_array($pasteid, $pastes)) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Filter.php
Expand Up @@ -77,7 +77,7 @@ public static function formatHumanReadableTime($time)
public static function formatHumanReadableSize($size)
{
$iec = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
$i = 0;
$i = 0;
while (($size / 1024) >= 1) {
$size = $size / 1024;
$i++;
Expand Down
12 changes: 6 additions & 6 deletions lib/I18n.php
Expand Up @@ -114,8 +114,8 @@ public static function translate($messageId)
$args = func_get_args();
if (is_array(self::$_translations[$messageId])) {
$number = (int) $args[1];
$key = self::_getPluralForm($number);
$max = count(self::$_translations[$messageId]) - 1;
$key = self::_getPluralForm($number);
$max = count(self::$_translations[$messageId]) - 1;
if ($key > $max) {
$key = $max;
}
Expand Down Expand Up @@ -143,7 +143,7 @@ public static function loadTranslations()

// check if the lang cookie was set and that language exists
if (array_key_exists('lang', $_COOKIE) && in_array($_COOKIE['lang'], $availableLanguages)) {
$match = $_COOKIE['lang'];
$match = $_COOKIE['lang'];
}
// find a translation file matching the browsers language preferences
else {
Expand All @@ -153,7 +153,7 @@ public static function loadTranslations()
}

// load translations
self::$_language = $match;
self::$_language = $match;
self::$_translations = ($match == 'en') ? array() : json_decode(
file_get_contents(self::_getPath($match . '.json')),
true
Expand Down Expand Up @@ -319,7 +319,7 @@ protected static function _getPluralForm($n)
protected static function _getMatchingLanguage($acceptedLanguages, $availableLanguages)
{
$matches = array();
$any = false;
$any = false;
foreach ($acceptedLanguages as $acceptedQuality => $acceptedValues) {
$acceptedQuality = floatval($acceptedQuality);
if ($acceptedQuality === 0.0) {
Expand Down Expand Up @@ -372,7 +372,7 @@ protected static function _matchLanguage($a, $b)
{
$a = explode('-', $a);
$b = explode('-', $b);
for ($i=0, $n = min(count($a), count($b)); $i < $n; ++$i) {
for ($i = 0, $n = min(count($a), count($b)); $i < $n; ++$i) {
if ($a[$i] !== $b[$i]) {
break;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Json.php
Expand Up @@ -33,7 +33,7 @@ class Json
public static function encode($input)
{
$jsonString = json_encode($input);
$errorCode = json_last_error();
$errorCode = json_last_error();
if ($errorCode === JSON_ERROR_NONE) {
return $jsonString;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/Model/AbstractModel.php
Expand Up @@ -67,9 +67,9 @@ abstract class AbstractModel
*/
public function __construct(Configuration $configuration, AbstractData $storage)
{
$this->_conf = $configuration;
$this->_store = $storage;
$this->_data = new stdClass;
$this->_conf = $configuration;
$this->_store = $storage;
$this->_data = new stdClass;
$this->_data->meta = new stdClass;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Model/Comment.php
Expand Up @@ -133,7 +133,7 @@ public function exists()
*/
public function setPaste(Paste $paste)
{
$this->_paste = $paste;
$this->_paste = $paste;
$this->_data->meta->pasteid = $paste->getId();
}

Expand Down Expand Up @@ -199,12 +199,12 @@ public function setNickname($nickname)
$icon = $this->_conf->getKey('icon');
if ($icon != 'none') {
$pngdata = '';
$hmac = TrafficLimiter::getHash();
$hmac = TrafficLimiter::getHash();
if ($icon == 'identicon') {
$identicon = new Identicon();
$pngdata = $identicon->getImageDataUri($hmac, 16);
$pngdata = $identicon->getImageDataUri($hmac, 16);
} elseif ($icon == 'vizhash') {
$vh = new Vizhash16x16();
$vh = new Vizhash16x16();
$pngdata = 'data:image/png;base64,' . base64_encode(
$vh->generate($hmac)
);
Expand Down

0 comments on commit 72aac25

Please sign in to comment.