Skip to content

Commit

Permalink
Merge pull request #155 from elecena/cs-fixer/new-rules
Browse files Browse the repository at this point in the history
cs-fixer: use new rules
  • Loading branch information
macbre committed Sep 22, 2022
2 parents e8d5649 + 86a51d9 commit f595b34
Show file tree
Hide file tree
Showing 25 changed files with 80 additions and 76 deletions.
6 changes: 5 additions & 1 deletion .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@

$config = new PhpCsFixer\Config();

// https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/rules/index.rst
// https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/ruleSets/index.rst
return $config
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'list_syntax' => true,
'ternary_to_null_coalescing' => true,
'trailing_comma_in_multiline' => true,
])
->setFinder($finder)
;

2 changes: 1 addition & 1 deletion app/config/default.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// debug
$config['debug'] = [
'enabled' => true
'enabled' => true,
];

$config['assets.packages'] = [
Expand Down
8 changes: 4 additions & 4 deletions classes/Cache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ abstract class Cache
*/
public static function factory(array $settings)
{
$driver = isset($settings['driver']) ? $settings['driver'] : null;
$driver = $settings['driver'] ?? null;
$className = sprintf('Nano\\Cache\\Cache%s', ucfirst($driver));

try {
return new $className($settings);
} catch (\Exception $e) {
NanoLogger::getLogger('nano.cache')->error($e->getMessage(), [
'exception' => $e
'exception' => $e,
]);
throw $e;
}
Expand All @@ -67,7 +67,7 @@ protected function __construct(array $settings)
$events->bind('NanoAppTearDown', [$this, 'onNanoAppTearDown']);

// set prefix
$this->prefix = isset($settings['prefix']) ? $settings['prefix'] : false;
$this->prefix = $settings['prefix'] ?? false;
}

/**
Expand Down Expand Up @@ -191,7 +191,7 @@ public function onNanoAppTearDown(\NanoApp $app)
$debug->log("Cache: {$this->hits} hits and {$this->misses} misses");
$this->logger->info('Performance data', [
'hits' => $this->getHits(),
'misses' => $this->getMisses()
'misses' => $this->getMisses(),
]);
}
}
6 changes: 3 additions & 3 deletions classes/Database.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static function connect(NanoApp $app, $config = 'default')
} catch (DatabaseException $e) {
$logger->error($e->getMessage(), [
'exception' => $e,
'driver' => $className
'driver' => $className,
]);
throw $e;
}
Expand Down Expand Up @@ -527,7 +527,7 @@ public function iterateTable($table, $column, array $where = [], $fname = __METH

while (true) {
$column_where = [
sprintf('%s > "%s"', $column, $this->escape($start))
sprintf('%s > "%s"', $column, $this->escape($start)),
];

$ids = $this->selectFields(
Expand All @@ -536,7 +536,7 @@ public function iterateTable($table, $column, array $where = [], $fname = __METH
array_merge($column_where, $where),
[
'order' => $column,
'limit' => intval($batch)
'limit' => intval($batch),
],
$fname
);
Expand Down
14 changes: 7 additions & 7 deletions classes/Events.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function bind($eventName, $callback)
{
$this->events[$eventName][] = [
self::CALLBACK_SIMPLE,
$callback
$callback,
];
}

Expand All @@ -46,8 +46,8 @@ public function bindController($eventName, $controllerName, $controllerMethod)
self::CALLBACK_CONTROLLER,
[
$controllerName,
$controllerMethod
]
$controllerMethod,
],
];

$this->events[$eventName][] = $callback;
Expand All @@ -58,21 +58,21 @@ public function bindController($eventName, $controllerName, $controllerMethod)
*/
public function fire($eventName, $params = [])
{
$callbacks = isset($this->events[$eventName]) ? $this->events[$eventName] : null;
$callbacks = $this->events[$eventName] ?? null;

if ($callbacks) {
foreach ($callbacks as $entry) {
list($type, $callback) = $entry;
[$type, $callback] = $entry;

switch ($type) {
// lazy load of controllers
case self::CALLBACK_CONTROLLER:
list($name, $method) = $callback;
[$name, $method] = $callback;

$instance = $this->app->getController($name);
$callback = [
$instance,
$method
$method,
];
break;
}
Expand Down
2 changes: 1 addition & 1 deletion classes/MessageQueue.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected function __construct(NanoApp $app, array $settings)
$this->logger = NanoLogger::getLogger(__CLASS__);

// set prefix
$this->prefix = isset($settings['prefix']) ? $settings['prefix'] : false;
$this->prefix = $settings['prefix'] ?? false;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions classes/NanoApp.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ public function __construct($dir, $configSet = 'default', $logFile = 'debug')
}

// set request
$params = isset($_REQUEST) ? $_REQUEST : [];
$env = isset($_SERVER) ? $_SERVER : [];
$params = $_REQUEST ?? [];
$env = $_SERVER ?? [];

$this->request = new Request($params, $env);
if (!$this->request->isCLI()) {
Expand All @@ -143,7 +143,7 @@ public function tearDown()
// TODO: move to a Monolog processor
$responseDetails = [
'time' => $this->getResponse()->getResponseTime() * 1000, // [ms]
'response_code' => $this->getResponse()->getResponseCode()
'response_code' => $this->getResponse()->getResponseCode(),
];

// request type
Expand Down Expand Up @@ -346,7 +346,7 @@ public function handleException(callable $fn, $handler = false)
// log the exception
$logger = NanoLogger::getLogger('nano.app.exception');
$logger->error($e->getMessage(), [
'exception' => $e
'exception' => $e,
]);

if (is_callable($handler)) {
Expand Down
2 changes: 1 addition & 1 deletion classes/NanoScript.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(NanoApp $app)
$this->init();
} catch (Exception $e) {
$this->logger->error(__METHOD__ . ' failed', [
'exception' => $e
'exception' => $e,
]);
}
}
Expand Down
8 changes: 4 additions & 4 deletions classes/Request.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function __construct(array $params = [], array $env = [])
$this->env = $env;

// detect request type
$method = isset($env['REQUEST_METHOD']) ? $env['REQUEST_METHOD'] : false;
$method = $env['REQUEST_METHOD'] ?? false;

// CLI mode detection
if ($method == false && php_sapi_name() == 'cli') {
Expand Down Expand Up @@ -193,7 +193,7 @@ public function setType($type)
*/
public function get($param, $default = null)
{
return isset($this->params[$param]) ? $this->params[$param] : $default;
return $this->params[$param] ?? $default;
}

/**
Expand Down Expand Up @@ -308,7 +308,7 @@ public function getHeader($name, $default = null)
{
$key = 'HTTP_' . strtoupper(str_replace('-', '_', $name));

return isset($this->env[$key]) ? $this->env[$key] : $default;
return $this->env[$key] ?? $default;
}

/**
Expand All @@ -328,7 +328,7 @@ public function getIP()
$fields = [
'HTTP_CLIENT_IP',
'HTTP_X_FORWARDED_FOR',
'REMOTE_ADDR'
'REMOTE_ADDR',
];

// scan HTTP headers to find IP
Expand Down
6 changes: 3 additions & 3 deletions classes/Response.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function setHeader($name, $value)
*/
public function getHeader($name)
{
return isset($this->headers[$name]) ? $this->headers[$name] : null;
return $this->headers[$name] ?? null;
}

/**
Expand Down Expand Up @@ -184,7 +184,7 @@ private function sendHeaders()
header_remove('X-Powered-By');

// emit HTTP protocol and response code
$protocol = isset($this->env['SERVER_PROTOCOL']) ? $this->env['SERVER_PROTOCOL'] : 'HTTP/1.1';
$protocol = $this->env['SERVER_PROTOCOL'] ?? 'HTTP/1.1';

header("{$protocol} {$this->responseCode}", true /* $replace */, $this->responseCode);
$this->debug->log(__METHOD__ . " - HTTP {$this->responseCode}");
Expand Down Expand Up @@ -282,7 +282,7 @@ public function setETag($eTag)
*/
public function getAcceptedEncoding()
{
$acceptedEncoding = isset($this->env['HTTP_ACCEPT_ENCODING']) ? $this->env['HTTP_ACCEPT_ENCODING'] : '';
$acceptedEncoding = $this->env['HTTP_ACCEPT_ENCODING'] ?? '';

if ($acceptedEncoding === '') {
return false;
Expand Down
2 changes: 1 addition & 1 deletion classes/Router.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function sanitize(?string $string): string
'ó' => 'o',
'ś' => 's',
'ż' => 'z',
'ź' => 'z'
'ź' => 'z',
]);

$string = preg_replace('#[^a-z0-9]+#', '-', $string);
Expand Down
8 changes: 4 additions & 4 deletions classes/Skin.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function addMeta($name, $value)
{
$this->meta[] = [
'name' => $name,
'value' => $value
'value' => $value,
];
}

Expand All @@ -152,7 +152,7 @@ public function setRobotsPolicy($policy)
{
$this->meta[] = [
'name' => 'robots',
'content' => $policy
'content' => $policy,
];
}

Expand All @@ -165,7 +165,7 @@ public function addOpenGraph($name, $value)
{
$this->meta[] = [
'property' => "og:{$name}",
'content' => $value
'content' => $value,
];
}

Expand All @@ -180,7 +180,7 @@ public function addLink($rel, $value, array $attrs = [])
{
$this->link[] = array_merge([
'rel' => $rel,
'value' => $value
'value' => $value,
], $attrs);
}

Expand Down
2 changes: 1 addition & 1 deletion classes/cache/CacheFile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(array $settings)
parent::__construct($settings);

$app = \NanoApp::app();
$this->dir = isset($settings['directory']) ? $settings['directory'] : ($app->getDirectory() . '/cache');
$this->dir = $settings['directory'] ?? ($app->getDirectory() . '/cache');
}

/**
Expand Down
8 changes: 4 additions & 4 deletions classes/cache/CacheRedis.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public function __construct(array $settings)
parent::__construct($settings);

// read settings
$host = isset($settings['host']) ? $settings['host'] : 'localhost';
$port = isset($settings['port']) ? $settings['port'] : 6379;
$password = isset($settings['password']) ? $settings['password'] : false;
$timeout = isset($settings['timeout']) ? $settings['timeout'] : 5; // Predis default is 5 sec
$host = $settings['host'] ?? 'localhost';
$port = $settings['port'] ?? 6379;
$password = $settings['password'] ?? false;
$timeout = $settings['timeout'] ?? 5; // Predis default is 5 sec

// lazy connect
$this->redis = new Client([
Expand Down
8 changes: 4 additions & 4 deletions classes/database/DatabaseMysql.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function doConnect()
});

$this->logger->info('Connected', [
'time' => $time * 1000 // [ms]
'time' => $time * 1000, // [ms]
]);

$this->log(__METHOD__, 'connected with ' . $hostInfo, $time);
Expand Down Expand Up @@ -184,7 +184,7 @@ public function query(string $sql, ?string $fname = null): DatabaseResult
$this->logger->error($shortSql, [
'exception' => $e,
'method' => $method,
'time' => $time * 1000 // [ms]
'time' => $time * 1000, // [ms]
]);

$this->log(__METHOD__, "error #{$this->link->errno} - {$this->link->error}");
Expand All @@ -194,7 +194,7 @@ public function query(string $sql, ?string $fname = null): DatabaseResult
$this->logger->info("SQL {$shortSql}", [
'method' => $method,
'rows' => $res instanceof mysqli_result ? $res->num_rows : ($this->link->affected_rows ?: 0),
'time' => $time * 1000 // [ms]
'time' => $time * 1000, // [ms]
]);
}

Expand Down Expand Up @@ -327,7 +327,7 @@ public function insertRows($table, array $rows, array $options = [], $fname = 'D
$suffix = '';

if (!empty($options['ON DUPLICATE KEY UPDATE'])) {
list($column, $value) = $options['ON DUPLICATE KEY UPDATE'];
[$column, $value] = $options['ON DUPLICATE KEY UPDATE'];
$suffix .= sprintf(' ON DUPLICATE KEY UPDATE %s = "%s"', $column, $this->escape($value));
}

Expand Down
14 changes: 7 additions & 7 deletions classes/mq/MessageQueueRedis.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ protected function __construct(NanoApp $app, array $settings)
parent::__construct($app, $settings);

// read settings
$host = isset($settings['host']) ? $settings['host'] : 'localhost';
$port = isset($settings['port']) ? $settings['port'] : 6379;
$password = isset($settings['password']) ? $settings['password'] : null;
$timeout = isset($settings['timeout']) ? $settings['timeout'] : 5; // Predis default is 5 sec
$host = $settings['host'] ?? 'localhost';
$port = $settings['port'] ?? 6379;
$password = $settings['password'] ?? null;
$timeout = $settings['timeout'] ?? 5; // Predis default is 5 sec

// lazy connect
$this->redis = new Client([
Expand Down Expand Up @@ -77,7 +77,7 @@ public function push($message, $fname = __METHOD__)

// log the push()
$this->logger->info($fname, [
'queue' => $this->queueName
'queue' => $this->queueName,
]);

// return wrapped message
Expand All @@ -100,7 +100,7 @@ public function pop($fname = __METHOD__)

// log the pop()
$this->logger->info($fname, [
'queue' => $this->queueName
'queue' => $this->queueName,
]);

// return wrapped message
Expand All @@ -127,7 +127,7 @@ public function clean()
{
$this->redis->del([
$this->getQueueKey(),
$this->getLastIdKey()
$this->getLastIdKey(),
]);
}

Expand Down
Loading

0 comments on commit f595b34

Please sign in to comment.