From f40613cb052f6c21b772531b4d017fc419a9b3fc Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Thu, 3 Sep 2020 19:56:55 +0200 Subject: [PATCH] Add property type declarations Property type declarations for class variables were added in PHP 7.4. --- src/MDConsole.php | 22 +++++++++++----------- src/MDOutputHandler.php | 39 +++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/MDConsole.php b/src/MDConsole.php index f6ff8fa..bb935d2 100644 --- a/src/MDConsole.php +++ b/src/MDConsole.php @@ -42,8 +42,8 @@ final class MDConsole { throw new MDConsoleInvalidHeadingNumber("Invalid heading number"); } - $msgLen = strlen($msg); - echo sprintf(" \033[1;30;m\033[0m\033[1m%s \033[0m" . PHP_EOL, $msg); + $msgLen = \strlen($msg); + echo \sprintf(" \033[1;30;m\033[0m\033[1m%s \033[0m" . PHP_EOL, $msg); if ($level >= 3) return; @@ -69,7 +69,7 @@ final class MDConsole { */ 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 { // Get parts for generating the message to log. - $additionalNote = strtr($additionalNote, ["ä" => "ae", "ö" => "oe", "ü" => "ue"]); - $date = date("H:i:s"); // Get current date & time. + $additionalNote = \strtr($additionalNote, ["ä" => "ae", "ö" => "oe", "ü" => "ue"]); + $date = \date("H:i:s"); // Get current date & time. // Generate the message to log. # $message = sprintf("%1$07s", memory_get_usage()) . " :: {$date} :: {$msg}" . PHP_EOL; @@ -96,9 +96,9 @@ final class MDConsole { $levelColor = self::CONSOLE_LEVEL_COLORS[$level]; 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 - 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 { - $startTime = microtime(true); - register_shutdown_function(function($startTime) :void { + $startTime = \microtime(true); + \register_shutdown_function(function($startTime) :void { - $duration = microtime(true) - $startTime; + $duration = \microtime(true) - $startTime; 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); } diff --git a/src/MDOutputHandler.php b/src/MDOutputHandler.php index 899489a..264ab5a 100644 --- a/src/MDOutputHandler.php +++ b/src/MDOutputHandler.php @@ -11,22 +11,18 @@ declare(strict_types = 1); */ final class MDOutputHandler { - /* - * Variables - */ - /** @var integer */ - private $_verbosity; + private int $_verbosity = 0; /** @var string */ - private $_context; + private string $_context = ""; /** @var string */ - private $_logfile; + private string $_logfile; /** @var integer */ - private $_counter; + private int $_counter = 0; /** @var boolean */ - private $_file_logging; + private bool $_file_logging = false; /** @var float */ - private $_startTime; + private float $_startTime; /** * Function human_filesize translates byte-level filesizes to human readable ones. @@ -83,8 +79,8 @@ final class MDOutputHandler { $this->_file_logging = $input; if ($this->_file_logging === true) { - if (!is_dir(__DIR__ . "/../logs")) mkdir(__DIR__ . "/../logs", 0755); - touch($this->_logfile); + if (!\is_dir(__DIR__ . "/../logs")) mkdir(__DIR__ . "/../logs", 0755); + \touch($this->_logfile); } } @@ -106,7 +102,7 @@ final class MDOutputHandler { // Get parts for generating the message to log. $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) { case MDConsole::STATUS_SKIPPED: @@ -131,16 +127,16 @@ final class MDOutputHandler { else $contextStr = ""; // 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) { - 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) { - 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; } @@ -151,12 +147,11 @@ final class MDOutputHandler { */ public function __construct() { - $this->_startTime = microtime(true); - $this->_counter = 0; + $this->_startTime = \microtime(true); $this->setVerbosity(); $this->setContext(); $this->setFileLogging(); - $this->_logfile = __DIR__ . "/../logs/" . date("Ymd_His") . ".txt"; + $this->_logfile = __DIR__ . "/../logs/" . \date("Ymd_His") . ".txt"; // Ensure the log directory exists @@ -170,8 +165,8 @@ final class MDOutputHandler { * @return void */ function __destruct() { - $endtime = microtime(true); - $this->toLog("Finished operation. Processing duration: " . strval($endtime - $this->_startTime)); + $endtime = \microtime(true); + $this->toLog("Finished operation. Processing duration: " . \strval($endtime - $this->_startTime)); }