From 7aa99fa98773b99cbd4f1322c81c271f2bc567b5 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 13 Feb 2020 18:54:50 +0100 Subject: [PATCH] fix typehints --- Localization/Money.php | 12 +++--------- Log/FileLogger.php | 2 +- Module/NullModule.php | 7 +------ Module/PackageManager.php | 4 +++- Socket/Server/ClientManager.php | 3 ++- Socket/SocketAbstract.php | 6 +++--- Stdlib/Base/Enum.php | 2 +- System/File/Ftp/Directory.php | 13 +++++++++++-- System/File/Ftp/FileAbstract.php | 8 ++++---- System/File/Local/FileAbstract.php | 8 ++++---- System/File/Storage.php | 4 +++- Utils/Converter/Numeric.php | 4 ---- Utils/IO/IODatabaseMapper.php | 2 +- Utils/Permutation.php | 4 ---- Utils/TaskSchedule/Interval.php | 4 +++- tests/Utils/PermutationTest.php | 12 ------------ 16 files changed, 40 insertions(+), 55 deletions(-) diff --git a/Localization/Money.php b/Localization/Money.php index 9e910b745..4d6999285 100644 --- a/Localization/Money.php +++ b/Localization/Money.php @@ -272,9 +272,7 @@ final class Money implements \Serializable */ public function mult($value) : self { - if (\is_float($value) || \is_int($value)) { - $this->value = (int) ($this->value * $value); - } + $this->value = (int) ($this->value * $value); return $this; } @@ -290,9 +288,7 @@ final class Money implements \Serializable */ public function div($value) : self { - if (\is_float($value) || \is_int($value)) { - $this->value = (int) ($this->value / $value); - } + $this->value = (int) ($this->value / $value); return $this; } @@ -322,9 +318,7 @@ final class Money implements \Serializable */ public function pow($value) : self { - if (\is_float($value) || \is_int($value)) { - $this->value = (int) ($this->value ** $value); - } + $this->value = (int) ($this->value ** $value); return $this; } diff --git a/Log/FileLogger.php b/Log/FileLogger.php index 8c30cf171..60d1d9b36 100644 --- a/Log/FileLogger.php +++ b/Log/FileLogger.php @@ -552,7 +552,7 @@ final class FileLogger implements LoggerInterface while (($line = \fgetcsv($this->fp, 0, ';')) !== false && $current <= $id) { ++$current; - if ($current < $id) { + if ($current < $id || $line === null) { continue; } diff --git a/Module/NullModule.php b/Module/NullModule.php index 2903582cc..c6fcb1e6e 100644 --- a/Module/NullModule.php +++ b/Module/NullModule.php @@ -24,10 +24,5 @@ namespace phpOMS\Module; */ final class NullModule extends ModuleAbstract { - /** - * Constructor. - * - * @since 1.0.0 - */ - public function __construct() {} + } diff --git a/Module/PackageManager.php b/Module/PackageManager.php index 34ff6ac9c..02884de5e 100644 --- a/Module/PackageManager.php +++ b/Module/PackageManager.php @@ -314,7 +314,9 @@ final class PackageManager \fclose($pipe); } - \proc_close($resource); + if ($resource !== false) { + \proc_close($resource); + } } } } diff --git a/Socket/Server/ClientManager.php b/Socket/Server/ClientManager.php index 44827aee9..d224a8030 100644 --- a/Socket/Server/ClientManager.php +++ b/Socket/Server/ClientManager.php @@ -16,6 +16,7 @@ namespace phpOMS\Socket\Server; use phpOMS\Socket\Client\ClientConnection; use phpOMS\Socket\Client\NullClientConnection; +use phpOMS\Account\NullAccount; /** * Client manager class. @@ -72,7 +73,7 @@ class ClientManager } } - return new NullClientConnection($id, null); + return new NullClientConnection(new NullAccount(), null); } /** diff --git a/Socket/SocketAbstract.php b/Socket/SocketAbstract.php index a0123b7c7..0639b9777 100644 --- a/Socket/SocketAbstract.php +++ b/Socket/SocketAbstract.php @@ -54,7 +54,7 @@ abstract class SocketAbstract implements SocketInterface * @var resource * @since 1.0.0 */ - protected $sock = null; + protected $sock; /** * {@inheritdoc} @@ -81,10 +81,10 @@ abstract class SocketAbstract implements SocketInterface */ public function close() : void { - if ($this->sock !== null) { + if (isset($this->sock)) { \socket_shutdown($this->sock, 2); \socket_close($this->sock); - $this->sock = null; + unset($this->sock); } } } diff --git a/Stdlib/Base/Enum.php b/Stdlib/Base/Enum.php index b7c2fd754..13db871be 100644 --- a/Stdlib/Base/Enum.php +++ b/Stdlib/Base/Enum.php @@ -98,7 +98,7 @@ abstract class Enum * * @param string $value Enum value * - * @return mixed + * @return false|int|string * * @since 1.0.0 */ diff --git a/System/File/Ftp/Directory.php b/System/File/Ftp/Directory.php index e68b60627..3b5609e3b 100644 --- a/System/File/Ftp/Directory.php +++ b/System/File/Ftp/Directory.php @@ -292,7 +292,7 @@ class Directory extends FileAbstract implements FtpContainerInterface, Directory * @param resource $con FTP connection * @param string $path Path of the resource * - * @return array + * @return array * * @since 1.0.0 */ @@ -302,8 +302,17 @@ class Directory extends FileAbstract implements FtpContainerInterface, Directory $names = \ftp_nlist($con, $path); $data = []; + if ($names === false || $listData === false) { + return []; + } + foreach ($listData as $key => $item) { $chunks = \preg_split("/\s+/", $item); + + if ($chunks === false) { + continue; + } + list( $e['permission'], $e['number'], @@ -313,7 +322,7 @@ class Directory extends FileAbstract implements FtpContainerInterface, Directory $e['month'], $e['day'], $e['time'] - ) = $chunks; + ) = $chunks; $e['permission'] = FileUtils::permissionToOctal(\substr($e['permission'], 1)); $e['type'] = $chunks[0][0] === 'd' ? 'dir' : 'file'; diff --git a/System/File/Ftp/FileAbstract.php b/System/File/Ftp/FileAbstract.php index 2e639a86a..1a2a9a651 100644 --- a/System/File/Ftp/FileAbstract.php +++ b/System/File/Ftp/FileAbstract.php @@ -63,18 +63,18 @@ abstract class FileAbstract implements ContainerInterface /** * Created at. * - * @var null|\DateTime + * @var \DateTime * @since 1.0.0 */ - protected ?\DateTime $createdAt = null; + protected \DateTime $createdAt; /** * Last changed at. * - * @var null|\DateTime + * @var \DateTime * @since 1.0.0 */ - protected ?\DateTime $changedAt = null; + protected \DateTime $changedAt; /** * Owner. diff --git a/System/File/Local/FileAbstract.php b/System/File/Local/FileAbstract.php index 5c7d02a6f..3b5119d95 100644 --- a/System/File/Local/FileAbstract.php +++ b/System/File/Local/FileAbstract.php @@ -63,18 +63,18 @@ abstract class FileAbstract implements ContainerInterface /** * Created at. * - * @var null|\DateTime + * @var \DateTime * @since 1.0.0 */ - protected ?\DateTime $createdAt = null; + protected \DateTime $createdAt; /** * Last changed at. * - * @var null|\DateTime + * @var \DateTime * @since 1.0.0 */ - protected ?\DateTime $changedAt = null; + protected \DateTime $changedAt; /** * Owner. diff --git a/System/File/Storage.php b/System/File/Storage.php index b252fb115..dc0a95f18 100644 --- a/System/File/Storage.php +++ b/System/File/Storage.php @@ -61,7 +61,9 @@ final class Storage if (\is_string(self::$registered[$env])) { $instance = new self::$registered[$env](); self::$registered[$env] = $instance; - } elseif (self::$registered[$env] instanceof StorageAbstract || self::$registered[$env] instanceof ContainerInterface) { + } elseif (self::$registered[$env] instanceof StorageAbstract + || self::$registered[$env] instanceof ContainerInterface + ) { $instance = self::$registered[$env]; } else { throw new \Exception('Invalid type'); diff --git a/Utils/Converter/Numeric.php b/Utils/Converter/Numeric.php index 2843435af..815ced723 100644 --- a/Utils/Converter/Numeric.php +++ b/Utils/Converter/Numeric.php @@ -73,10 +73,6 @@ final class Numeric $numberLen = \strlen($numberInput); $newOutput = ''; - if ($fromBase === false || $toBase === false || $number === false) { - throw new \Exception(); - } - if ($toBaseInput === '0123456789') { $newOutput = '0'; diff --git a/Utils/IO/IODatabaseMapper.php b/Utils/IO/IODatabaseMapper.php index db518686f..719247805 100644 --- a/Utils/IO/IODatabaseMapper.php +++ b/Utils/IO/IODatabaseMapper.php @@ -36,7 +36,7 @@ interface IODatabaseMapper /** * Select data from database and store in excel sheet * - * @param Builder[] $queries Queries to execute + * @param \phpOMS\DataStorage\Database\Query\Builder[] $queries Queries to execute * * @return void * diff --git a/Utils/Permutation.php b/Utils/Permutation.php index eb1e276e8..4fc3abbfe 100644 --- a/Utils/Permutation.php +++ b/Utils/Permutation.php @@ -112,10 +112,6 @@ final class Permutation */ public static function permutate($toPermute, array $key) { - if (!\is_array($toPermute) && !\is_string($toPermute)) { - throw new \InvalidArgumentException('Parameter has to be array or string'); - } - $length = \is_array($toPermute) ? \count($toPermute) : \strlen($toPermute); if (\count($key) > $length) { diff --git a/Utils/TaskSchedule/Interval.php b/Utils/TaskSchedule/Interval.php index a7c6c729a..888c142db 100644 --- a/Utils/TaskSchedule/Interval.php +++ b/Utils/TaskSchedule/Interval.php @@ -526,7 +526,7 @@ class Interval implements \Serializable */ public function serialize() : string { - return \json_encode([ + $serialized = \json_encode([ 'start' => $this->start->format('Y-m-d H:i:s'), 'end' => $this->end === null ? null : $this->end->format('Y-m-d H:i:s'), 'maxDuration' => $this->maxDuration, @@ -536,6 +536,8 @@ class Interval implements \Serializable 'dayOfWeek' => $this->dayOfWeek, 'year' => $this->year, ]); + + return $serialized === false ? '{}' : $serialized; } /** diff --git a/tests/Utils/PermutationTest.php b/tests/Utils/PermutationTest.php index d222797a3..175f00e0d 100644 --- a/tests/Utils/PermutationTest.php +++ b/tests/Utils/PermutationTest.php @@ -73,18 +73,6 @@ class PermutationTest extends \PHPUnit\Framework\TestCase self::assertEquals(['c', 'b', 'a'], Permutation::permutate(['a', 'b', 'c'], [2, 1, 1])); } - /** - * @testdox A invalid permutation type throws a InvalidArgumentException - * @covers phpOMS\Utils\Permutation - * @group framework - */ - public function testWrongPermuteParameterType() : void - { - self::expectException(\InvalidArgumentException::class); - - Permutation::permutate(4, [2, 1, 1]); - } - /** * @testdox A none-existing permutation key throws a OutOfBoundsException * @covers phpOMS\Utils\Permutation