From 23eb9ab80a99e8fcd5a92c4fd247ba2d145e766d Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 14 Jul 2018 23:41:51 +0200 Subject: [PATCH] More type fixes --- Localization/Defaults/Iban.php | 2 +- Localization/Money.php | 17 +++-- Log/FileLogger.php | 86 +++++++++++++++--------- Math/Exception/ZeroDevisionException.php | 3 +- Math/Functions/Functions.php | 2 +- Math/Geometry/Shape/D2/Polygon.php | 2 +- Math/Geometry/Shape/D3/Sphere.php | 2 +- Math/Matrix/Matrix.php | 6 +- Math/Number/Integer.php | 6 +- Math/Number/Numbers.php | 8 ++- Math/Statistic/Average.php | 4 +- 11 files changed, 84 insertions(+), 54 deletions(-) diff --git a/Localization/Defaults/Iban.php b/Localization/Defaults/Iban.php index b84c55c07..c008a6535 100644 --- a/Localization/Defaults/Iban.php +++ b/Localization/Defaults/Iban.php @@ -43,7 +43,7 @@ final class Iban /** * Iban chars. * - * @var string + * @var int * @since 1.0.0 */ private $chars = 2; diff --git a/Localization/Money.php b/Localization/Money.php index d3534fc8e..c5085db47 100644 --- a/Localization/Money.php +++ b/Localization/Money.php @@ -107,6 +107,11 @@ final class Money implements \Serializable public static function toInt(string $value, string $thousands = ',', string $decimal = '.') : int { $split = \explode($decimal, $value); + + if ($split === false) { + throw new \Exception(); + } + $left = $split[0]; $left = \str_replace($thousands, '', $left); $right = ''; @@ -115,9 +120,9 @@ final class Money implements \Serializable $right = $split[1]; } - $right = substr($right, 0, self::MAX_DECIMALS); + $right = \substr($right, 0, self::MAX_DECIMALS); - return ((int) $left) * 10 ** self::MAX_DECIMALS + (int) str_pad($right, self::MAX_DECIMALS, '0'); + return ((int) $left) * 10 ** self::MAX_DECIMALS + (int) \str_pad($right, self::MAX_DECIMALS, '0'); } /** @@ -185,10 +190,10 @@ final class Money implements \Serializable { $value = (string) round($this->value, -self::MAX_DECIMALS + $decimals); - $left = substr($value, 0, -self::MAX_DECIMALS); - $right = substr($value, -self::MAX_DECIMALS); + $left = \substr($value, 0, -self::MAX_DECIMALS); + $right = \substr($value, -self::MAX_DECIMALS); - return ($decimals > 0) ? number_format((float) $left, 0, $this->decimal, $this->thousands) . $this->decimal . substr($right, 0, $decimals) : (string) $left; + return ($decimals > 0) ? number_format((float) $left, 0, $this->decimal, $this->thousands) . $this->decimal . \substr($right, 0, $decimals) : $left; } /** @@ -309,7 +314,7 @@ final class Money implements \Serializable public function pow($value) : Money { if (is_float($value) || is_int($value)) { - $this->value = $this->value ** $value; + $this->value = (int) ($this->value ** $value); } return $this; diff --git a/Log/FileLogger.php b/Log/FileLogger.php index c7b37c44d..b8f0ec565 100644 --- a/Log/FileLogger.php +++ b/Log/FileLogger.php @@ -98,10 +98,10 @@ final class FileLogger implements LoggerInterface */ public function __construct(string $lpath, bool $verbose = false) { - $path = realpath($lpath); + $path = \realpath($lpath); $this->verbose = $verbose; - if (is_dir($lpath) || strpos($lpath, '.') === false) { + if (\is_dir($lpath) || \strpos($lpath, '.') === false) { $path = $lpath . '/' . date('Y-m-d') . '.log'; } else { $path = $lpath; @@ -155,7 +155,7 @@ final class FileLogger implements LoggerInterface public function __destruct() { if (is_resource($this->fp)) { - fclose($this->fp); + \fclose($this->fp); } } @@ -187,7 +187,7 @@ final class FileLogger implements LoggerInterface } $mtime = \explode(' ', microtime()); - $mtime = $mtime[1] + $mtime[0]; + $mtime = (int) $mtime[1] + (int) $mtime[0]; self::$timings[$id] = ['start' => $mtime]; @@ -206,7 +206,7 @@ final class FileLogger implements LoggerInterface public static function endTimeLog($id = '') : float { $mtime = \explode(' ', microtime()); - $mtime = $mtime[1] + $mtime[0]; + $mtime = (int) $mtime[1] + (int) $mtime[0]; self::$timings[$id]['end'] = $mtime; self::$timings[$id]['time'] = $mtime - self::$timings[$id]['start']; @@ -267,17 +267,17 @@ final class FileLogger implements LoggerInterface private function write(string $message) : void { $this->createFile(); - if (!is_writable($this->path)) { + if (!\is_writable($this->path)) { return; } - $this->fp = fopen($this->path, 'a'); + $this->fp = \fopen($this->path, 'a'); - if (flock($this->fp, LOCK_EX) && $this->fp !== false) { - fwrite($this->fp, $message . "\n"); - fflush($this->fp); - flock($this->fp, LOCK_UN); - fclose($this->fp); + if ($this->fp !== false && \flock($this->fp, LOCK_EX)) { + \fwrite($this->fp, $message . "\n"); + \fflush($this->fp); + \flock($this->fp, LOCK_UN); + \fclose($this->fp); $this->fp = false; } @@ -386,10 +386,15 @@ final class FileLogger implements LoggerInterface return $levels; } - $this->fp = fopen($this->path, 'r'); - fseek($this->fp, 0); + $this->fp = \fopen($this->path, 'r'); - while (($line = fgetcsv($this->fp, 0, ';')) !== false) { + if ($this->fp === false) { + return $levels; + } + + \fseek($this->fp, 0); + + while (($line = \fgetcsv($this->fp, 0, ';')) !== false) { $line[1] = trim($line[1]); if (!isset($levels[$line[1]])) { @@ -399,8 +404,8 @@ final class FileLogger implements LoggerInterface $levels[$line[1]]++; } - fseek($this->fp, 0, SEEK_END); - fclose($this->fp); + \fseek($this->fp, 0, SEEK_END); + \fclose($this->fp); return $levels; } @@ -422,10 +427,15 @@ final class FileLogger implements LoggerInterface return $connection; } - $this->fp = fopen($this->path, 'r'); - fseek($this->fp, 0); + $this->fp = \fopen($this->path, 'r'); - while (($line = fgetcsv($this->fp, 0, ';')) !== false) { + if ($this->fp === false) { + return $connection; + } + + \fseek($this->fp, 0); + + while (($line = \fgetcsv($this->fp, 0, ';')) !== false) { $line[2] = trim($line[2]); if (!isset($connection[$line[2]])) { @@ -435,8 +445,8 @@ final class FileLogger implements LoggerInterface $connection[$line[2]]++; } - fseek($this->fp, 0, SEEK_END); - fclose($this->fp); + \fseek($this->fp, 0, SEEK_END); + \fclose($this->fp); asort($connection); return array_slice($connection, 0, $limit); @@ -461,10 +471,16 @@ final class FileLogger implements LoggerInterface return $logs; } - $this->fp = fopen($this->path, 'r'); - fseek($this->fp, 0); + $this->fp = \fopen($this->path, 'r'); - while (($line = fgetcsv($this->fp, 0, ';')) !== false) { + if ($this->fp === false) { + return $logs; + } + + \fseek($this->fp, 0); + + $line = \fgetcsv($this->fp, 0, ';'); + while ($line !== false && $line !== null) { $id++; if ($offset > 0) { @@ -484,10 +500,11 @@ final class FileLogger implements LoggerInterface $logs[$id] = $line; $limit--; ksort($logs); + $line = \fgetcsv($this->fp, 0, ';'); } - fseek($this->fp, 0, SEEK_END); - fclose($this->fp); + \fseek($this->fp, 0, SEEK_END); + \fclose($this->fp); return $logs; } @@ -510,10 +527,15 @@ final class FileLogger implements LoggerInterface return $log; } - $this->fp = fopen($this->path, 'r'); - fseek($this->fp, 0); + $this->fp = \fopen($this->path, 'r'); - while (($line = fgetcsv($this->fp, 0, ';')) !== false && $current <= $id) { + if ($this->fp === false) { + return $log; + } + + \fseek($this->fp, 0); + + while (($line = \fgetcsv($this->fp, 0, ';')) !== false && $current <= $id) { $current++; if ($current < $id) { @@ -533,8 +555,8 @@ final class FileLogger implements LoggerInterface break; } - fseek($this->fp, 0, SEEK_END); - fclose($this->fp); + \fseek($this->fp, 0, SEEK_END); + \fclose($this->fp); return $log; } diff --git a/Math/Exception/ZeroDevisionException.php b/Math/Exception/ZeroDevisionException.php index ed3ba5417..538d463ea 100644 --- a/Math/Exception/ZeroDevisionException.php +++ b/Math/Exception/ZeroDevisionException.php @@ -27,13 +27,12 @@ final class ZeroDevisionException extends \UnexpectedValueException /** * Constructor. * - * @param string $message Exception message * @param int $code Exception code * @param \Exception $previous Previous exception * * @since 1.0.0 */ - public function __construct(string $message = '', int $code = 0, \Exception $previous = null) + public function __construct(int $code = 0, \Exception $previous = null) { parent::__construct('Devision by zero is not defined.', $code, $previous); } diff --git a/Math/Functions/Functions.php b/Math/Functions/Functions.php index b82794bdc..36d1702b1 100644 --- a/Math/Functions/Functions.php +++ b/Math/Functions/Functions.php @@ -315,6 +315,6 @@ final class Functions */ public static function getRelativeDegree(int $value, int $length, int $start = 0) : int { - return (int) abs(self::mod($value - $start, $length)); + return abs(self::mod($value - $start, $length)); } } diff --git a/Math/Geometry/Shape/D2/Polygon.php b/Math/Geometry/Shape/D2/Polygon.php index ca67290c9..f718507c3 100644 --- a/Math/Geometry/Shape/D2/Polygon.php +++ b/Math/Geometry/Shape/D2/Polygon.php @@ -192,7 +192,7 @@ final class Polygon implements D2ShapeInterface */ public function getSurface() : float { - return (float) abs($this->getSignedSurface()); + return abs($this->getSignedSurface()); } /** diff --git a/Math/Geometry/Shape/D3/Sphere.php b/Math/Geometry/Shape/D3/Sphere.php index e440f3db6..64debdbb1 100644 --- a/Math/Geometry/Shape/D3/Sphere.php +++ b/Math/Geometry/Shape/D3/Sphere.php @@ -116,7 +116,7 @@ final class Sphere implements D3ShapeInterface */ public static function getRadiusByVolume(float $v) : float { - return (float) pow($v * 3 / (4 * pi()), 1 / 3); + return pow($v * 3 / (4 * pi()), 1 / 3); } /** diff --git a/Math/Matrix/Matrix.php b/Math/Matrix/Matrix.php index 2f41933ad..135de8fba 100644 --- a/Math/Matrix/Matrix.php +++ b/Math/Matrix/Matrix.php @@ -402,7 +402,7 @@ class Matrix implements \ArrayAccess, \Iterator { if ($value instanceof Matrix) { return $this->add($value->mult(-1)); - } elseif (is_numeric($value)) { + } elseif (!is_string($value) && is_numeric($value)) { return $this->add(-$value); } @@ -424,7 +424,7 @@ class Matrix implements \ArrayAccess, \Iterator { if ($value instanceof Matrix) { return $this->addMatrix($value); - } elseif (is_numeric($value)) { + } elseif (!is_string($value) && is_numeric($value)) { return $this->addScalar($value); } @@ -529,7 +529,7 @@ class Matrix implements \ArrayAccess, \Iterator { if ($value instanceof Matrix) { return $this->multMatrix($value); - } elseif (is_numeric($value)) { + } elseif (!is_string($value) && is_numeric($value)) { return $this->multScalar($value); } diff --git a/Math/Number/Integer.php b/Math/Number/Integer.php index 0f503e187..91a7f6586 100644 --- a/Math/Number/Integer.php +++ b/Math/Number/Integer.php @@ -138,7 +138,7 @@ final class Integer } } - return (int) $m; + return $m; } /** @@ -160,13 +160,13 @@ final class Integer } $a = (int) ceil(sqrt($value)); - $b2 = (int) ($a * $a - $value); + $b2 = ($a * $a - $value); $i = 1; while (!Numbers::isSquare($b2) && $i < $limit) { $i++; $a += 1; - $b2 = (int) ($a * $a - $value); + $b2 = ($a * $a - $value); } return [(int) round($a - sqrt($b2)), (int) round($a + sqrt($b2))]; diff --git a/Math/Number/Numbers.php b/Math/Number/Numbers.php index 454634d1b..0eb76b044 100644 --- a/Math/Number/Numbers.php +++ b/Math/Number/Numbers.php @@ -69,10 +69,14 @@ final class Numbers public static function isSelfdescribing(int $n) : bool { $n = (string) $n; - $split = str_split($n); + $split = \str_split($n); + + if ($split === false) { + return false; + } foreach ($split as $place => $value) { - if (substr_count($n, (string) $place) != $value) { + if (\substr_count($n, (string) $place) != $value) { return false; } } diff --git a/Math/Statistic/Average.php b/Math/Statistic/Average.php index 48d7ed4fc..8f52a7233 100644 --- a/Math/Statistic/Average.php +++ b/Math/Statistic/Average.php @@ -168,7 +168,7 @@ final class Average * * @return float * - * @throws phpOMS\Math\Exception\ZeroDevisionException + * @throws ZeroDevisionException * * @since 1.0.0 */ @@ -252,7 +252,7 @@ final class Average throw new ZeroDevisionException(); } - return (float) pow(array_product($values), 1 / $count); + return pow(\array_product($values), 1 / $count); } /**