More type fixes

This commit is contained in:
Dennis Eichhorn 2018-07-14 23:41:51 +02:00
parent adf83f0867
commit 23eb9ab80a
11 changed files with 84 additions and 54 deletions

View File

@ -43,7 +43,7 @@ final class Iban
/** /**
* Iban chars. * Iban chars.
* *
* @var string * @var int
* @since 1.0.0 * @since 1.0.0
*/ */
private $chars = 2; private $chars = 2;

View File

@ -107,6 +107,11 @@ final class Money implements \Serializable
public static function toInt(string $value, string $thousands = ',', string $decimal = '.') : int public static function toInt(string $value, string $thousands = ',', string $decimal = '.') : int
{ {
$split = \explode($decimal, $value); $split = \explode($decimal, $value);
if ($split === false) {
throw new \Exception();
}
$left = $split[0]; $left = $split[0];
$left = \str_replace($thousands, '', $left); $left = \str_replace($thousands, '', $left);
$right = ''; $right = '';
@ -115,9 +120,9 @@ final class Money implements \Serializable
$right = $split[1]; $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); $value = (string) round($this->value, -self::MAX_DECIMALS + $decimals);
$left = substr($value, 0, -self::MAX_DECIMALS); $left = \substr($value, 0, -self::MAX_DECIMALS);
$right = substr($value, -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 public function pow($value) : Money
{ {
if (is_float($value) || is_int($value)) { if (is_float($value) || is_int($value)) {
$this->value = $this->value ** $value; $this->value = (int) ($this->value ** $value);
} }
return $this; return $this;

View File

@ -98,10 +98,10 @@ final class FileLogger implements LoggerInterface
*/ */
public function __construct(string $lpath, bool $verbose = false) public function __construct(string $lpath, bool $verbose = false)
{ {
$path = realpath($lpath); $path = \realpath($lpath);
$this->verbose = $verbose; $this->verbose = $verbose;
if (is_dir($lpath) || strpos($lpath, '.') === false) { if (\is_dir($lpath) || \strpos($lpath, '.') === false) {
$path = $lpath . '/' . date('Y-m-d') . '.log'; $path = $lpath . '/' . date('Y-m-d') . '.log';
} else { } else {
$path = $lpath; $path = $lpath;
@ -155,7 +155,7 @@ final class FileLogger implements LoggerInterface
public function __destruct() public function __destruct()
{ {
if (is_resource($this->fp)) { if (is_resource($this->fp)) {
fclose($this->fp); \fclose($this->fp);
} }
} }
@ -187,7 +187,7 @@ final class FileLogger implements LoggerInterface
} }
$mtime = \explode(' ', microtime()); $mtime = \explode(' ', microtime());
$mtime = $mtime[1] + $mtime[0]; $mtime = (int) $mtime[1] + (int) $mtime[0];
self::$timings[$id] = ['start' => $mtime]; self::$timings[$id] = ['start' => $mtime];
@ -206,7 +206,7 @@ final class FileLogger implements LoggerInterface
public static function endTimeLog($id = '') : float public static function endTimeLog($id = '') : float
{ {
$mtime = \explode(' ', microtime()); $mtime = \explode(' ', microtime());
$mtime = $mtime[1] + $mtime[0]; $mtime = (int) $mtime[1] + (int) $mtime[0];
self::$timings[$id]['end'] = $mtime; self::$timings[$id]['end'] = $mtime;
self::$timings[$id]['time'] = $mtime - self::$timings[$id]['start']; self::$timings[$id]['time'] = $mtime - self::$timings[$id]['start'];
@ -267,17 +267,17 @@ final class FileLogger implements LoggerInterface
private function write(string $message) : void private function write(string $message) : void
{ {
$this->createFile(); $this->createFile();
if (!is_writable($this->path)) { if (!\is_writable($this->path)) {
return; return;
} }
$this->fp = fopen($this->path, 'a'); $this->fp = \fopen($this->path, 'a');
if (flock($this->fp, LOCK_EX) && $this->fp !== false) { if ($this->fp !== false && \flock($this->fp, LOCK_EX)) {
fwrite($this->fp, $message . "\n"); \fwrite($this->fp, $message . "\n");
fflush($this->fp); \fflush($this->fp);
flock($this->fp, LOCK_UN); \flock($this->fp, LOCK_UN);
fclose($this->fp); \fclose($this->fp);
$this->fp = false; $this->fp = false;
} }
@ -386,10 +386,15 @@ final class FileLogger implements LoggerInterface
return $levels; return $levels;
} }
$this->fp = fopen($this->path, 'r'); $this->fp = \fopen($this->path, 'r');
fseek($this->fp, 0);
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]); $line[1] = trim($line[1]);
if (!isset($levels[$line[1]])) { if (!isset($levels[$line[1]])) {
@ -399,8 +404,8 @@ final class FileLogger implements LoggerInterface
$levels[$line[1]]++; $levels[$line[1]]++;
} }
fseek($this->fp, 0, SEEK_END); \fseek($this->fp, 0, SEEK_END);
fclose($this->fp); \fclose($this->fp);
return $levels; return $levels;
} }
@ -422,10 +427,15 @@ final class FileLogger implements LoggerInterface
return $connection; return $connection;
} }
$this->fp = fopen($this->path, 'r'); $this->fp = \fopen($this->path, 'r');
fseek($this->fp, 0);
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]); $line[2] = trim($line[2]);
if (!isset($connection[$line[2]])) { if (!isset($connection[$line[2]])) {
@ -435,8 +445,8 @@ final class FileLogger implements LoggerInterface
$connection[$line[2]]++; $connection[$line[2]]++;
} }
fseek($this->fp, 0, SEEK_END); \fseek($this->fp, 0, SEEK_END);
fclose($this->fp); \fclose($this->fp);
asort($connection); asort($connection);
return array_slice($connection, 0, $limit); return array_slice($connection, 0, $limit);
@ -461,10 +471,16 @@ final class FileLogger implements LoggerInterface
return $logs; return $logs;
} }
$this->fp = fopen($this->path, 'r'); $this->fp = \fopen($this->path, 'r');
fseek($this->fp, 0);
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++; $id++;
if ($offset > 0) { if ($offset > 0) {
@ -484,10 +500,11 @@ final class FileLogger implements LoggerInterface
$logs[$id] = $line; $logs[$id] = $line;
$limit--; $limit--;
ksort($logs); ksort($logs);
$line = \fgetcsv($this->fp, 0, ';');
} }
fseek($this->fp, 0, SEEK_END); \fseek($this->fp, 0, SEEK_END);
fclose($this->fp); \fclose($this->fp);
return $logs; return $logs;
} }
@ -510,10 +527,15 @@ final class FileLogger implements LoggerInterface
return $log; return $log;
} }
$this->fp = fopen($this->path, 'r'); $this->fp = \fopen($this->path, 'r');
fseek($this->fp, 0);
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++; $current++;
if ($current < $id) { if ($current < $id) {
@ -533,8 +555,8 @@ final class FileLogger implements LoggerInterface
break; break;
} }
fseek($this->fp, 0, SEEK_END); \fseek($this->fp, 0, SEEK_END);
fclose($this->fp); \fclose($this->fp);
return $log; return $log;
} }

View File

@ -27,13 +27,12 @@ final class ZeroDevisionException extends \UnexpectedValueException
/** /**
* Constructor. * Constructor.
* *
* @param string $message Exception message
* @param int $code Exception code * @param int $code Exception code
* @param \Exception $previous Previous exception * @param \Exception $previous Previous exception
* *
* @since 1.0.0 * @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); parent::__construct('Devision by zero is not defined.', $code, $previous);
} }

View File

@ -315,6 +315,6 @@ final class Functions
*/ */
public static function getRelativeDegree(int $value, int $length, int $start = 0) : int 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));
} }
} }

View File

@ -192,7 +192,7 @@ final class Polygon implements D2ShapeInterface
*/ */
public function getSurface() : float public function getSurface() : float
{ {
return (float) abs($this->getSignedSurface()); return abs($this->getSignedSurface());
} }
/** /**

View File

@ -116,7 +116,7 @@ final class Sphere implements D3ShapeInterface
*/ */
public static function getRadiusByVolume(float $v) : float public static function getRadiusByVolume(float $v) : float
{ {
return (float) pow($v * 3 / (4 * pi()), 1 / 3); return pow($v * 3 / (4 * pi()), 1 / 3);
} }
/** /**

View File

@ -402,7 +402,7 @@ class Matrix implements \ArrayAccess, \Iterator
{ {
if ($value instanceof Matrix) { if ($value instanceof Matrix) {
return $this->add($value->mult(-1)); return $this->add($value->mult(-1));
} elseif (is_numeric($value)) { } elseif (!is_string($value) && is_numeric($value)) {
return $this->add(-$value); return $this->add(-$value);
} }
@ -424,7 +424,7 @@ class Matrix implements \ArrayAccess, \Iterator
{ {
if ($value instanceof Matrix) { if ($value instanceof Matrix) {
return $this->addMatrix($value); return $this->addMatrix($value);
} elseif (is_numeric($value)) { } elseif (!is_string($value) && is_numeric($value)) {
return $this->addScalar($value); return $this->addScalar($value);
} }
@ -529,7 +529,7 @@ class Matrix implements \ArrayAccess, \Iterator
{ {
if ($value instanceof Matrix) { if ($value instanceof Matrix) {
return $this->multMatrix($value); return $this->multMatrix($value);
} elseif (is_numeric($value)) { } elseif (!is_string($value) && is_numeric($value)) {
return $this->multScalar($value); return $this->multScalar($value);
} }

View File

@ -138,7 +138,7 @@ final class Integer
} }
} }
return (int) $m; return $m;
} }
/** /**
@ -160,13 +160,13 @@ final class Integer
} }
$a = (int) ceil(sqrt($value)); $a = (int) ceil(sqrt($value));
$b2 = (int) ($a * $a - $value); $b2 = ($a * $a - $value);
$i = 1; $i = 1;
while (!Numbers::isSquare($b2) && $i < $limit) { while (!Numbers::isSquare($b2) && $i < $limit) {
$i++; $i++;
$a += 1; $a += 1;
$b2 = (int) ($a * $a - $value); $b2 = ($a * $a - $value);
} }
return [(int) round($a - sqrt($b2)), (int) round($a + sqrt($b2))]; return [(int) round($a - sqrt($b2)), (int) round($a + sqrt($b2))];

View File

@ -69,10 +69,14 @@ final class Numbers
public static function isSelfdescribing(int $n) : bool public static function isSelfdescribing(int $n) : bool
{ {
$n = (string) $n; $n = (string) $n;
$split = str_split($n); $split = \str_split($n);
if ($split === false) {
return false;
}
foreach ($split as $place => $value) { foreach ($split as $place => $value) {
if (substr_count($n, (string) $place) != $value) { if (\substr_count($n, (string) $place) != $value) {
return false; return false;
} }
} }

View File

@ -168,7 +168,7 @@ final class Average
* *
* @return float * @return float
* *
* @throws phpOMS\Math\Exception\ZeroDevisionException * @throws ZeroDevisionException
* *
* @since 1.0.0 * @since 1.0.0
*/ */
@ -252,7 +252,7 @@ final class Average
throw new ZeroDevisionException(); throw new ZeroDevisionException();
} }
return (float) pow(array_product($values), 1 / $count); return pow(\array_product($values), 1 / $count);
} }
/** /**