Static test fixes

This commit is contained in:
Dennis Eichhorn 2018-07-14 22:19:48 +02:00
parent d1fafd760b
commit 0069bdac9a
10 changed files with 157 additions and 89 deletions

View File

@ -28,8 +28,8 @@ interface LoggerInterface
/**
* System is unusable.
*
* @param string $message Logging message schema
* @param array<string, string> $context Context to log
* @param string $message Logging message schema
* @param array<string, mixed> $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<string, string> $context Context to log
* @param string $message Logging message schema
* @param array<string, mixed> $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<string, string> $context Context to log
* @param string $message Logging message schema
* @param array<string, mixed> $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<string, string> $context Context to log
* @param string $message Logging message schema
* @param array<string, mixed> $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<string, string> $context Context to log
* @param string $message Logging message schema
* @param array<string, mixed> $context Context to log
*
* @return void
*/
@ -87,8 +87,8 @@ interface LoggerInterface
/**
* Normal but significant events.
*
* @param string $message Logging message schema
* @param array<string, string> $context Context to log
* @param string $message Logging message schema
* @param array<string, mixed> $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<string, string> $context Context to log
* @param string $message Logging message schema
* @param array<string, mixed> $context Context to log
*
* @return void
*/
@ -109,8 +109,8 @@ interface LoggerInterface
/**
* Detailed debug information.
*
* @param string $message Logging message schema
* @param array<string, string> $context Context to log
* @param string $message Logging message schema
* @param array<string, mixed> $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<string, string> $context Context to log
* @param string $message Logging message schema
* @param array<string, mixed> $context Context to log
*
* @return void
*/

View File

@ -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);
}
/**

View File

@ -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);
}
}

View File

@ -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;
}

View File

@ -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;
}
/**

View File

@ -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;

View File

@ -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];

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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'));