Add property type declarations

Property type declarations for class variables were added in PHP
7.4.
This commit is contained in:
Joshua Ramon Enslin 2020-09-03 19:56:55 +02:00 committed by Stefan Rohde-Enslin
parent f811bf5a16
commit f40613cb05
2 changed files with 28 additions and 33 deletions

View File

@ -42,8 +42,8 @@ final class MDConsole {
throw new MDConsoleInvalidHeadingNumber("Invalid heading number"); throw new MDConsoleInvalidHeadingNumber("Invalid heading number");
} }
$msgLen = strlen($msg); $msgLen = \strlen($msg);
echo sprintf(" \033[1;30;m\033[0m\033[1m%s \033[0m" . PHP_EOL, $msg); echo \sprintf(" \033[1;30;m\033[0m\033[1m%s \033[0m" . PHP_EOL, $msg);
if ($level >= 3) return; if ($level >= 3) return;
@ -69,7 +69,7 @@ final class MDConsole {
*/ */
final public static function note(string $msg):void { final public static function note(string $msg):void {
echo PHP_EOL . sprintf("\033[30m(%s)%s \033[0m" . PHP_EOL . PHP_EOL, date("H:i:s"), $msg); echo PHP_EOL . \sprintf("\033[30m(%s)%s \033[0m" . PHP_EOL . PHP_EOL, \date("H:i:s"), $msg);
} }
@ -87,8 +87,8 @@ final class MDConsole {
final public static function write(string $context, int $level = self::STATUS_NOTICE, int $editedNo = 0, string $additionalNote = "", string $link = ""):void { final public static function write(string $context, int $level = self::STATUS_NOTICE, int $editedNo = 0, string $additionalNote = "", string $link = ""):void {
// Get parts for generating the message to log. // Get parts for generating the message to log.
$additionalNote = strtr($additionalNote, ["ä" => "ae", "ö" => "oe", "ü" => "ue"]); $additionalNote = \strtr($additionalNote, ["ä" => "ae", "ö" => "oe", "ü" => "ue"]);
$date = date("H:i:s"); // Get current date & time. $date = \date("H:i:s"); // Get current date & time.
// Generate the message to log. // Generate the message to log.
# $message = sprintf("%1$07s", memory_get_usage()) . " :: {$date} :: {$msg}" . PHP_EOL; # $message = sprintf("%1$07s", memory_get_usage()) . " :: {$date} :: {$msg}" . PHP_EOL;
@ -96,9 +96,9 @@ final class MDConsole {
$levelColor = self::CONSOLE_LEVEL_COLORS[$level]; $levelColor = self::CONSOLE_LEVEL_COLORS[$level];
if ($link === "") if ($link === "")
echo sprintf("\033[1;30;" . $levelColor . "m_____\033[0m\033[0m %-20s \033[40m::\033[0m # \033[32m%+9s\033[0m \033[40m::\033[0m 📝 %-50s \033[40m::\033[0m 📅 %+20s\n", $context, $editedNo, $additionalNote, $date); echo \sprintf("\033[1;30;" . $levelColor . "m_____\033[0m\033[0m %-20s \033[40m::\033[0m # \033[32m%+9s\033[0m \033[40m::\033[0m 📝 %-50s \033[40m::\033[0m 📅 %+20s\n", $context, $editedNo, $additionalNote, $date);
else else
echo sprintf("\033[1;30;" . $levelColor . "m_____\033[0m\033[0m %-20s \033[40m::\033[0m # \033[32m%+9s\033[0m \033[40m::\033[0m 📝 %-50s \033[40m::\033[0m 📅 %+20s \033[40m::\033[0m $link\n", $context, $editedNo, $additionalNote, $date);; echo \sprintf("\033[1;30;" . $levelColor . "m_____\033[0m\033[0m %-20s \033[40m::\033[0m # \033[32m%+9s\033[0m \033[40m::\033[0m 📝 %-50s \033[40m::\033[0m 📅 %+20s \033[40m::\033[0m $link\n", $context, $editedNo, $additionalNote, $date);;
} }
@ -109,14 +109,14 @@ final class MDConsole {
*/ */
final public static function setupCLI():void { final public static function setupCLI():void {
$startTime = microtime(true); $startTime = \microtime(true);
register_shutdown_function(function($startTime) :void { \register_shutdown_function(function($startTime) :void {
$duration = microtime(true) - $startTime; $duration = \microtime(true) - $startTime;
if ($duration < 30) return; if ($duration < 30) return;
self::note("Long script run" . PHP_EOL . "Script run time, that's: " . round($duration, 2) . PHP_EOL . "Peak memory usage: " . memory_get_peak_usage() . PHP_EOL . "Memory usage at end of script: " . memory_get_usage()); self::note("Long script run" . PHP_EOL . "Script run time, that's: " . \round($duration, 2) . PHP_EOL . "Peak memory usage: " . memory_get_peak_usage() . PHP_EOL . "Memory usage at end of script: " . \memory_get_usage());
}, $startTime); }, $startTime);
} }

View File

@ -11,22 +11,18 @@ declare(strict_types = 1);
*/ */
final class MDOutputHandler { final class MDOutputHandler {
/*
* Variables
*/
/** @var integer */ /** @var integer */
private $_verbosity; private int $_verbosity = 0;
/** @var string */ /** @var string */
private $_context; private string $_context = "";
/** @var string */ /** @var string */
private $_logfile; private string $_logfile;
/** @var integer */ /** @var integer */
private $_counter; private int $_counter = 0;
/** @var boolean */ /** @var boolean */
private $_file_logging; private bool $_file_logging = false;
/** @var float */ /** @var float */
private $_startTime; private float $_startTime;
/** /**
* Function human_filesize translates byte-level filesizes to human readable ones. * Function human_filesize translates byte-level filesizes to human readable ones.
@ -83,8 +79,8 @@ final class MDOutputHandler {
$this->_file_logging = $input; $this->_file_logging = $input;
if ($this->_file_logging === true) { if ($this->_file_logging === true) {
if (!is_dir(__DIR__ . "/../logs")) mkdir(__DIR__ . "/../logs", 0755); if (!\is_dir(__DIR__ . "/../logs")) mkdir(__DIR__ . "/../logs", 0755);
touch($this->_logfile); \touch($this->_logfile);
} }
} }
@ -106,7 +102,7 @@ final class MDOutputHandler {
// Get parts for generating the message to log. // Get parts for generating the message to log.
$this->_counter++; // Increment counter $this->_counter++; // Increment counter
$date = date("Y-m-d H:i:s"); // Get current date & time. $date = \date("Y-m-d H:i:s"); // Get current date & time.
switch ($statusCode) { switch ($statusCode) {
case MDConsole::STATUS_SKIPPED: case MDConsole::STATUS_SKIPPED:
@ -131,16 +127,16 @@ final class MDOutputHandler {
else $contextStr = ""; else $contextStr = "";
// Generate the message to log. // Generate the message to log.
$message = "$statusSymbol{$contextStr}#" . sprintf("%1$07s", $this->_counter) . "" . sprintf("%1$7s", $this->_human_filesize(memory_get_usage())) . "{$date}{$msg}" . PHP_EOL; $message = "$statusSymbol{$contextStr}#" . sprintf("%1$07s", $this->_counter) . "" . \sprintf("%1$7s", $this->_human_filesize(\memory_get_usage())) . "{$date}{$msg}" . PHP_EOL;
if ($this->_file_logging === true) { if ($this->_file_logging === true) {
file_put_contents($this->_logfile, $message, FILE_APPEND); \file_put_contents($this->_logfile, $message, FILE_APPEND);
} }
if (($this->_verbosity > 0 && in_array($statusCode, [MDConsole::STATUS_UPDATE, MDConsole::STATUS_DELETION, MDConsole::STATUS_ERROR], true)) || $this->_verbosity === 2) echo $message; if (($this->_verbosity > 0 && \in_array($statusCode, [MDConsole::STATUS_UPDATE, MDConsole::STATUS_DELETION, MDConsole::STATUS_ERROR], true)) || $this->_verbosity === 2) echo $message;
if ($this->_counter % 250 === 0) { if ($this->_counter % 250 === 0) {
echo '―――> ' . sprintf("%1$07d", $this->_counter) . " operations processed" . PHP_EOL; echo '―――> ' . \sprintf("%1$07d", $this->_counter) . " operations processed" . PHP_EOL;
echo str_replace("_", "", sprintf("%'_80s", "_")) . PHP_EOL; echo str_replace("_", "", sprintf("%'_80s", "_")) . PHP_EOL;
} }
@ -151,12 +147,11 @@ final class MDOutputHandler {
*/ */
public function __construct() { public function __construct() {
$this->_startTime = microtime(true); $this->_startTime = \microtime(true);
$this->_counter = 0;
$this->setVerbosity(); $this->setVerbosity();
$this->setContext(); $this->setContext();
$this->setFileLogging(); $this->setFileLogging();
$this->_logfile = __DIR__ . "/../logs/" . date("Ymd_His") . ".txt"; $this->_logfile = __DIR__ . "/../logs/" . \date("Ymd_His") . ".txt";
// Ensure the log directory exists // Ensure the log directory exists
@ -170,8 +165,8 @@ final class MDOutputHandler {
* @return void * @return void
*/ */
function __destruct() { function __destruct() {
$endtime = microtime(true); $endtime = \microtime(true);
$this->toLog("Finished operation. Processing duration: " . strval($endtime - $this->_startTime)); $this->toLog("Finished operation. Processing duration: " . \strval($endtime - $this->_startTime));
} }