From 0069bdac9a20ec43d218e204901b88d21f527b00 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 14 Jul 2018 22:19:48 +0200 Subject: [PATCH] Static test fixes --- Log/LoggerInterface.php | 36 ++++++++++----------- Stdlib/Base/SmartDateTime.php | 12 +++++-- System/File/FileUtils.php | 8 ++--- System/SystemUtils.php | 43 ++++++++++++++----------- Utils/ArrayUtils.php | 40 +++++++++++++++++------- Utils/Barcode/C128Abstract.php | 45 +++++++++++++++------------ Utils/Compression/LZW.php | 18 ++++++----- Utils/Converter/Ip.php | 5 ++- Utils/Encoding/Huffman/Huffman.php | 28 +++++++++++++---- tests/Utils/Converter/NumericTest.php | 11 +++++++ 10 files changed, 157 insertions(+), 89 deletions(-) diff --git a/Log/LoggerInterface.php b/Log/LoggerInterface.php index dde03237b..1cb97d030 100644 --- a/Log/LoggerInterface.php +++ b/Log/LoggerInterface.php @@ -28,8 +28,8 @@ interface LoggerInterface /** * System is unusable. * - * @param string $message Logging message schema - * @param array $context Context to log + * @param string $message Logging message schema + * @param array $context Context to log * * @return void */ @@ -41,8 +41,8 @@ interface LoggerInterface * Example: Entire website down, database unavailable, etc. This should * trigger the SMS alerts and wake you up. * - * @param string $message Logging message schema - * @param array $context Context to log + * @param string $message Logging message schema + * @param array $context Context to log * * @return void */ @@ -53,8 +53,8 @@ interface LoggerInterface * * Example: Application component unavailable, unexpected exception. * - * @param string $message Logging message schema - * @param array $context Context to log + * @param string $message Logging message schema + * @param array $context Context to log * * @return void */ @@ -64,8 +64,8 @@ interface LoggerInterface * Runtime errors that do not require immediate action but should typically * be logged and monitored. * - * @param string $message Logging message schema - * @param array $context Context to log + * @param string $message Logging message schema + * @param array $context Context to log * * @return void */ @@ -77,8 +77,8 @@ interface LoggerInterface * Example: Use of deprecated APIs, poor use of an API, undesirable things * that are not necessarily wrong. * - * @param string $message Logging message schema - * @param array $context Context to log + * @param string $message Logging message schema + * @param array $context Context to log * * @return void */ @@ -87,8 +87,8 @@ interface LoggerInterface /** * Normal but significant events. * - * @param string $message Logging message schema - * @param array $context Context to log + * @param string $message Logging message schema + * @param array $context Context to log * * @return void */ @@ -99,8 +99,8 @@ interface LoggerInterface * * Example: User logs in, SQL logs. * - * @param string $message Logging message schema - * @param array $context Context to log + * @param string $message Logging message schema + * @param array $context Context to log * * @return void */ @@ -109,8 +109,8 @@ interface LoggerInterface /** * Detailed debug information. * - * @param string $message Logging message schema - * @param array $context Context to log + * @param string $message Logging message schema + * @param array $context Context to log * * @return void */ @@ -120,8 +120,8 @@ interface LoggerInterface * Logs with an arbitrary level. * * @param string $level Log level/severeness - * @param string $message Logging message schema - * @param array $context Context to log + * @param string $message Logging message schema + * @param array $context Context to log * * @return void */ diff --git a/Stdlib/Base/SmartDateTime.php b/Stdlib/Base/SmartDateTime.php index cf3e617b0..d0a95f317 100644 --- a/Stdlib/Base/SmartDateTime.php +++ b/Stdlib/Base/SmartDateTime.php @@ -97,8 +97,8 @@ class SmartDateTime extends \DateTime $yearNew = (int) $this->format('Y') + $y + $yearChange; $monthNew = ((int) $this->format('m') + $m) % 12; $monthNew = $monthNew === 0 ? 12 : $monthNew < 0 ? 12 + $monthNew : $monthNew; - $dayMonthOld = cal_days_in_month($calendar, (int) $this->format('m'), (int) $this->format('Y')); - $dayMonthNew = cal_days_in_month($calendar, $monthNew, $yearNew); + $dayMonthOld = \cal_days_in_month($calendar, (int) $this->format('m'), (int) $this->format('Y')); + $dayMonthNew = \cal_days_in_month($calendar, $monthNew, $yearNew); $dayOld = (int) $this->format('d'); if ($dayOld > $dayMonthNew) { @@ -219,7 +219,13 @@ class SmartDateTime extends \DateTime */ public static function getDayOfWeek(int $y, int $m, int $d) : int { - return (int) date('w', strtotime($d . '-' . $m . '-' . $y)); + $time = \strtotime($d . '-' . $m . '-' . $y); + + if ($time === false) { + return -1; + } + + return (int) date('w', $time); } /** diff --git a/System/File/FileUtils.php b/System/File/FileUtils.php index c61690791..9b044fe8b 100644 --- a/System/File/FileUtils.php +++ b/System/File/FileUtils.php @@ -92,7 +92,7 @@ final class FileUtils */ public static function absolute(string $origPath) : string { - if (!\file_exists($origPath)) { + if (!\file_exists($origPath) || \realpath($origPath) === false) { $startsWithSlash = strpos($origPath, '/') === 0 ? '/' : ''; $path = []; @@ -106,15 +106,15 @@ final class FileUtils if ($part !== '..') { $path[] = $part; } elseif (!empty($path)) { - array_pop($path); + \array_pop($path); } else { throw new PathException($origPath); } } - return $startsWithSlash . implode('/', $path); + return $startsWithSlash . \implode('/', $path); } - return realpath($origPath); + return \realpath($origPath); } } diff --git a/System/SystemUtils.php b/System/SystemUtils.php index ce40b5a34..729e9a1d3 100644 --- a/System/SystemUtils.php +++ b/System/SystemUtils.php @@ -46,25 +46,27 @@ final class SystemUtils { $mem = 0; - if (stristr(PHP_OS, 'WIN')) { - $mem = null; - exec('wmic memorychip get capacity', $mem); + if (\stristr(PHP_OS, 'WIN')) { + $memArr = []; + exec('wmic memorychip get capacity', $memArr); - /** @var array $mem */ - $mem = array_sum($mem) / 1024; - } elseif (stristr(PHP_OS, 'LINUX')) { - $fh = fopen('/proc/meminfo', 'r'); - $mem = 0; + $mem = \array_sum($memArr) / 1024; + } elseif (\stristr(PHP_OS, 'LINUX')) { + $fh = \fopen('/proc/meminfo', 'r'); - while ($line = fgets($fh)) { + if ($fh === false) { + return $mem; + } + + while ($line = \fgets($fh)) { $pieces = []; if (\preg_match('/^MemTotal:\s+(\d+)\skB$/', $line, $pieces)) { - $mem = $pieces[1] * 1024; + $mem = (int) ($pieces[1] ?? 0) * 1024; break; } } - fclose($fh); + \fclose($fh); } return (int) $mem; @@ -81,12 +83,17 @@ final class SystemUtils { $memUsage = 0; - if (stristr(PHP_OS, 'LINUX')) { - $free = shell_exec('free'); - $free = (string) trim($free); + if (\stristr(PHP_OS, 'LINUX')) { + $free = \shell_exec('free'); + + if ($free === null) { + return $memUsage; + } + + $free = trim($free); $freeArr = \explode("\n", $free); - $mem = \explode(" ", $freeArr[1]); - $mem = array_values(array_filter($mem)); + $mem = \explode(' ', $freeArr[1]); + $mem = \array_values(\array_filter($mem)); $memUsage = $mem[2] / $mem[1] * 100; } @@ -104,11 +111,11 @@ final class SystemUtils { $cpuUsage = 0; - if (stristr(PHP_OS, 'WIN') !== false) { + if (\stristr(PHP_OS, 'WIN') !== false) { $cpuUsage = null; exec('wmic cpu get LoadPercentage', $cpuUsage); $cpuUsage = $cpuUsage[1]; - } elseif (stristr(PHP_OS, 'LINUX') !== false) { + } elseif (\stristr(PHP_OS, 'LINUX') !== false) { $cpuUsage = \sys_getloadavg()[0] * 100; } diff --git a/Utils/ArrayUtils.php b/Utils/ArrayUtils.php index 02733d7e6..839bb9eae 100644 --- a/Utils/ArrayUtils.php +++ b/Utils/ArrayUtils.php @@ -51,8 +51,11 @@ final class ArrayUtils $nodes = \explode($delim, trim($path, $delim)); $prevEl = null; $el = &$data; + $node = null; - $node = null; + if ($nodes === false) { + throw new \Exception(); + } foreach ($nodes as &$node) { $prevEl = &$el; @@ -89,6 +92,10 @@ final class ArrayUtils $pathParts = \explode($delim, trim($path, $delim)); $current = &$data; + if ($pathParts === false) { + throw new \Exception(); + } + foreach ($pathParts as $key) { $current = &$current[$key]; } @@ -124,6 +131,10 @@ final class ArrayUtils $pathParts = \explode($delim, trim($path, $delim)); $current = $data; + if ($pathParts === false) { + throw new \Exception(); + } + foreach ($pathParts as $key) { if (!isset($current[$key])) { return null; @@ -266,14 +277,19 @@ final class ArrayUtils */ public static function arrayToCsv(array $data, string $delimiter = ';', string $enclosure = '"', string $escape = '\\') : string { - $outstream = fopen('php://memory', 'r+'); - /** @noinspection PhpMethodParametersCountMismatchInspection */ - fputcsv($outstream, $data, $delimiter, $enclosure, $escape); - rewind($outstream); - $csv = fgets($outstream); - fclose($outstream); + $outstream = \fopen('php://memory', 'r+'); - return $csv; + if ($outstream === false) { + throw new \Exception(); + } + + /** @noinspection PhpMethodParametersCountMismatchInspection */ + \fputcsv($outstream, $data, $delimiter, $enclosure, $escape); + rewind($outstream); + $csv = \fgets($outstream); + \fclose($outstream); + + return $csv === false ? '' : $csv; } /** @@ -290,11 +306,11 @@ final class ArrayUtils */ public static function getArg(string $id, array $args) : ?string { - if (($key = array_search($id, $args)) === false || $key === count($args) - 1) { + if (($key = \array_search($id, $args)) === false || $key === count($args) - 1) { return null; } - return trim($args[$key + 1], '" '); + return trim($args[(int) $key + 1], '" '); } /** @@ -309,11 +325,11 @@ final class ArrayUtils */ public static function hasArg(string $id, array $args) : ?int { - if (($key = array_search($id, $args)) === false) { + if (($key = \array_search($id, $args)) === false) { return null; } - return $key; + return (int) $key; } /** diff --git a/Utils/Barcode/C128Abstract.php b/Utils/Barcode/C128Abstract.php index 9c730fd8d..e66572cea 100644 --- a/Utils/Barcode/C128Abstract.php +++ b/Utils/Barcode/C128Abstract.php @@ -147,11 +147,11 @@ abstract class C128Abstract public function setDimension(int $width, int $height) : void { if ($width < 0) { - throw new \OutOfBoundsException($width); + throw new \OutOfBoundsException((string) $width); } if ($height < 0) { - throw new \OutOfBoundsException($height); + throw new \OutOfBoundsException((string) $height); } $this->dimension['width'] = $width; @@ -243,8 +243,8 @@ abstract class C128Abstract { $res = $this->get(); - imagepng($res, $file); - imagedestroy($res); + \imagepng($res, $file); + \imagedestroy($res); } /** @@ -260,8 +260,8 @@ abstract class C128Abstract { $res = $this->get(); - imagejpeg($res, $file); - imagedestroy($res); + \imagejpeg($res, $file); + \imagedestroy($res); } /** @@ -273,14 +273,14 @@ abstract class C128Abstract */ protected function generateCodeString() : string { - $keys = array_keys(static::$CODEARRAY); - $values = array_flip($keys); + $keys = \array_keys(static::$CODEARRAY); + $values = \array_flip($keys); $codeString = ''; - $length = strlen($this->content); + $length = \strlen($this->content); $checksum = static::$CHECKSUM; for ($pos = 1; $pos <= $length; $pos++) { - $activeKey = substr($this->content, ($pos - 1), 1); + $activeKey = \substr($this->content, ($pos - 1), 1); $codeString .= static::$CODEARRAY[$activeKey]; $checksum += $values[$activeKey] * $pos; } @@ -302,18 +302,23 @@ abstract class C128Abstract protected function createImage(string $codeString) { $dimensions = $this->calculateDimensions($codeString); - $image = imagecreate($dimensions['width'], $dimensions['height']); - $black = imagecolorallocate($image, 0, 0, 0); - $white = imagecolorallocate($image, 255, 255, 255); + $image = \imagecreate($dimensions['width'], $dimensions['height']); + + if ($image === false) { + throw new \Exception(); + } + + $black = \imagecolorallocate($image, 0, 0, 0); + $white = \imagecolorallocate($image, 255, 255, 255); $location = 0; - $length = strlen($codeString); - imagefill($image, 0, 0, $white); + $length = \strlen($codeString); + \imagefill($image, 0, 0, $white); for ($position = 1; $position <= $length; $position++) { - $cur_size = $location + (int) (substr($codeString, ($position - 1), 1)); + $cur_size = $location + (int) (\substr($codeString, ($position - 1), 1)); if ($this->orientation === OrientationType::HORIZONTAL) { - imagefilledrectangle( + \imagefilledrectangle( $image, $location + $this->margin, 0 + $this->margin, @@ -322,7 +327,7 @@ abstract class C128Abstract ($position % 2 == 0 ? $white : $black) ); } else { - imagefilledrectangle( + \imagefilledrectangle( $image, 0 + $this->margin, $location + $this->margin, @@ -350,10 +355,10 @@ abstract class C128Abstract private function calculateCodeLength(string $codeString) : int { $codeLength = 0; - $length = strlen($codeString); + $length = \strlen($codeString); for ($i = 1; $i <= $length; ++$i) { - $codeLength = $codeLength + (int) (substr($codeString, ($i - 1), 1)); + $codeLength = $codeLength + (int) (\substr($codeString, ($i - 1), 1)); } return $codeLength; diff --git a/Utils/Compression/LZW.php b/Utils/Compression/LZW.php index 3e8cee371..5757f9fe8 100644 --- a/Utils/Compression/LZW.php +++ b/Utils/Compression/LZW.php @@ -39,17 +39,17 @@ class LZW implements CompressionInterface $dictionary[chr($i)] = $i; } - $length = strlen($source); + $length = \strlen($source); for ($i = 0; $i < $length; ++$i) { $c = $source[$i]; $wc = $w . $c; - if (array_key_exists($w . $c, $dictionary)) { + if (\array_key_exists($w . $c, $dictionary)) { $w = $w . $c; } else { $result[] = $dictionary[$w]; $dictionary[$wc] = $dictSize++; - $w = (string) $c; + $w = $c; } } @@ -57,7 +57,7 @@ class LZW implements CompressionInterface $result[] = $dictionary[$w]; } - return implode(',', $result); + return \implode(',', $result); } /** @@ -70,16 +70,20 @@ class LZW implements CompressionInterface $entry = ''; $dictSize = 256; + if (empty($compressed)) { + return ''; + } + for ($i = 0; $i < 256; ++$i) { $dictionary[$i] = chr($i); } - $w = chr($compressed[0]); - $result = $dictionary[$compressed[0]]; + $w = chr((int) $compressed[0]); + $result = $dictionary[(int) ($compressed[0])] ?? 0; $count = count($compressed); for ($i = 1; $i < $count; ++$i) { - $k = $compressed[$i]; + $k = (int) $compressed[$i]; if ($dictionary[$k]) { $entry = $dictionary[$k]; diff --git a/Utils/Converter/Ip.php b/Utils/Converter/Ip.php index a2e686b8b..21a86dad0 100644 --- a/Utils/Converter/Ip.php +++ b/Utils/Converter/Ip.php @@ -49,6 +49,9 @@ class Ip { $split = \explode('.', $ip); - return $split[0] * (256 ** 3) + $split[1] * (256 ** 2) + $split[2] * (256 ** 1) + $split[3]; + return ((int) $split[0] ?? 0) * (256 ** 3) + + ((int) $split[1] ?? 0) * (256 ** 2) + + ((int) $split[2] ?? 0) * (256 ** 1) + + ((int) $split[3] ?? 0); } } diff --git a/Utils/Encoding/Huffman/Huffman.php b/Utils/Encoding/Huffman/Huffman.php index b79f5d7d3..25542ecd9 100644 --- a/Utils/Encoding/Huffman/Huffman.php +++ b/Utils/Encoding/Huffman/Huffman.php @@ -82,11 +82,15 @@ final class Huffman $binary .= $this->dictionary->get($source[$i]); } - $splittedBinaryString = str_split('1' . $binary . '1', 8); + $splittedBinaryString = \str_split('1' . $binary . '1', 8); $binary = ''; + if ($splittedBinaryString === false) { + return $binary; + } + foreach ($splittedBinaryString as $i => $c) { - while (strlen($c) < 8) { + while (\strlen($c) < 8) { $c .= '0'; } @@ -112,22 +116,34 @@ final class Huffman } $binary = ''; - $rawLenght = strlen($raw); + $rawLenght = \strlen($raw); $source = ''; for ($i = 0; $i < $rawLenght; ++$i) { $decbin = decbin(ord($raw[$i])); - while (strlen($decbin) < 8) { + while (\strlen($decbin) < 8) { $decbin = '0' . $decbin; } if ($i === 0) { - $decbin = substr($decbin, strpos($decbin, '1') + 1); + $pos = \strpos($decbin, '1'); + + if ($pos === false) { + throw new \Exception(); + } + + $decbin = \substr($decbin, $pos + 1); } if ($i + 1 === $rawLenght) { - $decbin = substr($decbin, 0, strrpos($decbin, '1')); + $pos = \strrpos($decbin, '1'); + + if ($pos === false) { + throw new \Exception(); + } + + $decbin = \substr($decbin, 0, $pos); } $binary .= $decbin; diff --git a/tests/Utils/Converter/NumericTest.php b/tests/Utils/Converter/NumericTest.php index ffee92f23..ef30364e2 100644 --- a/tests/Utils/Converter/NumericTest.php +++ b/tests/Utils/Converter/NumericTest.php @@ -28,6 +28,17 @@ class NumericTest extends \PHPUnit\Framework\TestCase self::assertEquals('XI', Numeric::arabicToRoman(11)); } + public function testAlphaNumeric() + { + self::assertEquals(0, Numeric::alphaToNumeric('A')); + self::assertEquals(1, Numeric::alphaToNumeric('B')); + self::assertEquals(53, Numeric::alphaToNumeric('BB')); + + self::assertEquals('A', Numeric::numericToAlpha(0)); + self::assertEquals('B', Numeric::numericToAlpha(1)); + self::assertEquals('BB', Numeric::numericToAlpha(53)); + } + public function testBase() { self::assertEquals('443', Numeric::convertBase('123', '0123456789', '01234'));