From a95b0ea026ca2b9763dfcb4a4fcdde8197c3c667 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 12 Sep 2018 22:15:44 +0200 Subject: [PATCH] PhpStan fixes --- .../Database/Query/Grammar/Grammar.php | 3 ++- Localization/Localization.php | 27 ++++++++++--------- Math/Geometry/Shape/D2/Polygon.php | 8 +++--- Math/Matrix/Matrix.php | 2 +- Math/Number/Integer.php | 2 +- Math/Statistic/MeasureOfDispersion.php | 1 + .../Distribution/ChiSquaredDistribution.php | 5 ++-- Message/Http/Request.php | 8 ++---- Module/ModuleManager.php | 8 +++--- Router/Router.php | 4 +-- Stdlib/Graph/Edge.php | 2 +- Stdlib/Queue/PriorityQueue.php | 2 +- Utils/Git/Repository.php | 7 +++-- Utils/Parser/Php/ArrayParser.php | 4 +-- Utils/StringUtils.php | 8 +++--- Utils/TestUtils.php | 2 +- tests/Account/AccountTest.php | 2 +- tests/Autoloader.php | 2 -- .../Database/Query/BuilderTest.php | 2 +- tests/Dispatcher/DispatcherTest.php | 15 ++++------- tests/Utils/Parser/Php/ArrayParserTest.php | 2 +- 21 files changed, 57 insertions(+), 59 deletions(-) diff --git a/DataStorage/Database/Query/Grammar/Grammar.php b/DataStorage/Database/Query/Grammar/Grammar.php index 67a10d547..0421d6605 100644 --- a/DataStorage/Database/Query/Grammar/Grammar.php +++ b/DataStorage/Database/Query/Grammar/Grammar.php @@ -420,7 +420,8 @@ class Grammar extends GrammarAbstract * Compile on. * * @param Builder $query Builder - * @param array $joins Joins + * @param array $ons On values + * @param bool $first Is first on element * * @return string * diff --git a/Localization/Localization.php b/Localization/Localization.php index c89b7a4e8..aa7887a7f 100644 --- a/Localization/Localization.php +++ b/Localization/Localization.php @@ -158,22 +158,25 @@ final class Localization foreach ($files as $file) { if (\stripos($file, $langCode) === 0) { - $this->importLocale( - \json_decode( - \file_get_contents(__DIR__ . '/../Localization/Defaults/Definitions/' . $file), - true - ) - ); + $fileContent = \file_get_contents(__DIR__ . '/../Localization/Defaults/Definitions/' . $file); + + if ($fileContent === false) { + return; + } + + $this->importLocale(\json_decode($fileContent, true)); + return; } } - $this->importLocale( - \json_decode( - \file_get_contents(__DIR__ . '/../Localization/Defaults/Definitions/en_US.json'), - true - ) - ); + $fileContent = \file_get_contents(__DIR__ . '/../Localization/Defaults/Definitions/en_US.json'); + + if ($fileContent === false) { + return; + } + + $this->importLocale(\json_decode($fileContent, true)); } /** diff --git a/Math/Geometry/Shape/D2/Polygon.php b/Math/Geometry/Shape/D2/Polygon.php index a97dfd66d..5e0848d2f 100644 --- a/Math/Geometry/Shape/D2/Polygon.php +++ b/Math/Geometry/Shape/D2/Polygon.php @@ -76,8 +76,8 @@ final class Polygon implements D2ShapeInterface /** * Point polygon relative position * - * @param array $point Point location - * @param array $polygon Polygon definition + * @param array $point Point location + * @param array> $polygon Polygon definition * * @return int -1 inside polygon 0 on vertice 1 outside * @@ -141,8 +141,8 @@ final class Polygon implements D2ShapeInterface /** * Is point on vertex? * - * @param array $point Point location - * @param array $polygon Polygon definition + * @param array $point Point location + * @param array> $polygon Polygon definition * * @return bool * diff --git a/Math/Matrix/Matrix.php b/Math/Matrix/Matrix.php index 54d0c8be6..302861609 100644 --- a/Math/Matrix/Matrix.php +++ b/Math/Matrix/Matrix.php @@ -381,7 +381,7 @@ class Matrix implements \ArrayAccess, \Iterator public function setMatrix(array $matrix) : Matrix { $this->m = \count($matrix); - $this->n = \count($matrix[0] ?? 1); + $this->n = \count($matrix[0] ?? [1]); $this->matrix = $matrix; return $this; diff --git a/Math/Number/Integer.php b/Math/Number/Integer.php index 973da99a3..3f251b5ba 100644 --- a/Math/Number/Integer.php +++ b/Math/Number/Integer.php @@ -54,7 +54,7 @@ final class Integer * * @param int $value Integer to factorize * - * @return array + * @return array * * @since 1.0.0 */ diff --git a/Math/Statistic/MeasureOfDispersion.php b/Math/Statistic/MeasureOfDispersion.php index d5151526c..f4b9337e5 100644 --- a/Math/Statistic/MeasureOfDispersion.php +++ b/Math/Statistic/MeasureOfDispersion.php @@ -235,6 +235,7 @@ final class MeasureOfDispersion --$count; } + /** @var int $count */ $count /= 2; \sort($x); diff --git a/Math/Stochastic/Distribution/ChiSquaredDistribution.php b/Math/Stochastic/Distribution/ChiSquaredDistribution.php index a3126a5a0..b1250b6b4 100644 --- a/Math/Stochastic/Distribution/ChiSquaredDistribution.php +++ b/Math/Stochastic/Distribution/ChiSquaredDistribution.php @@ -117,8 +117,9 @@ class ChiSquaredDistribution } } - $key = \key(\end(self::TABLE[$df])); - $p = 1 - ($p ?? ($key === false ? 1 : (float) $key)); + $tableCopy = self::TABLE[$df]; + $key = \key(\end($tableCopy)); + $p = 1 - ($p ?? ($key === false ? 1 : (float) $key)); return ['P' => $p, 'H0' => ($p > $significance), 'df' => $df]; } diff --git a/Message/Http/Request.php b/Message/Http/Request.php index 15a74066c..fec0daf95 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -168,12 +168,8 @@ final class Request extends RequestAbstract return 'EN'; } - $components = \explode(';', $_SERVER['HTTP_ACCEPT_LANGUAGE']); - - if (\stripos($components[0], ',') !== false) { - $locals = \explode(',', $components[0]); - } - + $components = \explode(';', $_SERVER['HTTP_ACCEPT_LANGUAGE']); + $locals = \stripos($components[0], ',') !== false ? $locals = \explode(',', $components[0]) : $components; $firstLocalComponents = \explode('-', $locals[0]); return $firstLocalComponents[0]; diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index 0f6dfb9d0..2d3f8c857 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -121,8 +121,8 @@ final class ModuleManager $files = $this->getUriLoad($request); $lang = []; - if (isset($files[5])) { - foreach ($files[5] as $module) { + if (isset($files['5'])) { + foreach ($files['5'] as $module) { $lang[] = '/Modules/' . $module['module_load_from'] . '/Theme/' . $this->app->appName . '/Lang/' . $module['module_load_file']; } } @@ -689,8 +689,8 @@ final class ModuleManager $files = $this->getUriLoad($request); $modules = []; - if (isset($files[4])) { - foreach ($files[4] as $module) { + if (isset($files['4'])) { + foreach ($files['4'] as $module) { $modules[] = $module['module_load_file']; } } diff --git a/Router/Router.php b/Router/Router.php index cac6691fd..ab42c56f4 100644 --- a/Router/Router.php +++ b/Router/Router.php @@ -82,8 +82,8 @@ final class Router /** * Route request. * - * @param string|RequestAbstract $request Request to route - * @param int $verb Route verb + * @param string $request Request to route + * @param int $verb Route verb * * @return array[] * diff --git a/Stdlib/Graph/Edge.php b/Stdlib/Graph/Edge.php index a2f3e2bf1..e7358fee4 100644 --- a/Stdlib/Graph/Edge.php +++ b/Stdlib/Graph/Edge.php @@ -47,7 +47,7 @@ class Edge /** * Is graph/edge directed * - * @var Node + * @var bool * @since 1.0.0 */ private $isDirected = false; diff --git a/Stdlib/Queue/PriorityQueue.php b/Stdlib/Queue/PriorityQueue.php index 1e7e4e2dc..80fd0cc8c 100644 --- a/Stdlib/Queue/PriorityQueue.php +++ b/Stdlib/Queue/PriorityQueue.php @@ -209,7 +209,7 @@ class PriorityQueue implements \Countable, \Serializable /** * Pop element. * - * @return mixed + * @return array * * @since 1.0.0 */ diff --git a/Utils/Git/Repository.php b/Utils/Git/Repository.php index b6dad6604..757ce3169 100644 --- a/Utils/Git/Repository.php +++ b/Utils/Git/Repository.php @@ -687,7 +687,7 @@ class Repository * @param \DateTime $start Start date * @param \DateTime $end End date * - * @return array + * @return array * * @since 1.0.0 */ @@ -747,7 +747,10 @@ class Repository foreach ($lines as $line) { \preg_match('/^[0-9]*/', $line, $matches); - $commits[\substr($line, \strlen($matches[0]) + 1)] = (int) $matches[0]; + $temp = \substr($line, \strlen($matches[0]) + 1); + if ($temp !== false) { + $commits[$temp] = (int) $matches[0]; + } } return $commits; diff --git a/Utils/Parser/Php/ArrayParser.php b/Utils/Parser/Php/ArrayParser.php index 2b311f7c3..9438a77d8 100644 --- a/Utils/Parser/Php/ArrayParser.php +++ b/Utils/Parser/Php/ArrayParser.php @@ -73,9 +73,9 @@ class ArrayParser return 'null'; } elseif (\is_scalar($value)) { return (string) $value; - } elseif ($value instanceOf \Serializable) { + } elseif ($value instanceof \Serializable) { return self::parseVariable($value->serialize()); - } elseif ($value instanceOf \jsonSerializable) { + } elseif ($value instanceof \JsonSerializable) { return self::parseVariable($value->jsonSerialize()); } else { throw new \UnexpectedValueException(); diff --git a/Utils/StringUtils.php b/Utils/StringUtils.php index da1abc6eb..1b28a95f3 100644 --- a/Utils/StringUtils.php +++ b/Utils/StringUtils.php @@ -46,8 +46,8 @@ final class StringUtils * * The validation is done case sensitive. * - * @param string $haystack Haystack - * @param array $needles Needles to check if any of them are part of the haystack + * @param string $haystack Haystack + * @param array $needles Needles to check if any of them are part of the haystack * * @example StringUtils::contains('This string', ['This', 'test']); // true * @@ -71,8 +71,8 @@ final class StringUtils * * The validation is done case sensitive. * - * @param string $haystack Haystack - * @param array $needles Needles to check if any of them are part of the haystack + * @param string $haystack Haystack + * @param array $needles Needles to check if any of them are part of the haystack * * @example StringUtils::mb_contains('This string', ['This', 'test']); // true * diff --git a/Utils/TestUtils.php b/Utils/TestUtils.php index 4f273790a..ef17ce2e3 100644 --- a/Utils/TestUtils.php +++ b/Utils/TestUtils.php @@ -87,7 +87,7 @@ final class TestUtils */ public static function getMember(object $obj, string $name) { - $reflectionClass = new \ReflectionClass(\is_string($obj) ? $obj : \get_class($obj)); + $reflectionClass = new \ReflectionClass($obj); if (!$reflectionClass->hasProperty($name)) { return null; diff --git a/tests/Account/AccountTest.php b/tests/Account/AccountTest.php index 2220ac948..379469489 100644 --- a/tests/Account/AccountTest.php +++ b/tests/Account/AccountTest.php @@ -151,7 +151,7 @@ class AccountTest extends \PHPUnit\Framework\TestCase ]); self::assertEquals(4, \count($account->getPermissions())); - self::assertFalse($account->hasPermission(PermissionType::READ, 1, 'a', 1, 1, 1, 1)); + self::assertFalse($account->hasPermission(PermissionType::READ, 1, 'a', 'a', 1, 1, 1)); self::assertTrue($account->hasPermission(PermissionType::NONE)); $account->setL11n(new Localization()); diff --git a/tests/Autoloader.php b/tests/Autoloader.php index a9246b4eb..4e94912be 100644 --- a/tests/Autoloader.php +++ b/tests/Autoloader.php @@ -36,8 +36,6 @@ class Autoloader * * @return void * - * @throws AutoloadException Throws this exception if the class to autoload doesn't exist. This could also be related to a wrong namespace/file path correlation. - * * @since 1.0.0 */ public static function defaultAutoloader(string $class) : void diff --git a/tests/DataStorage/Database/Query/BuilderTest.php b/tests/DataStorage/Database/Query/BuilderTest.php index c2053ed7c..0671bf327 100644 --- a/tests/DataStorage/Database/Query/BuilderTest.php +++ b/tests/DataStorage/Database/Query/BuilderTest.php @@ -40,7 +40,7 @@ class BuilderTest extends \PHPUnit\Framework\TestCase self::assertEquals($sql, $query->select('a.test', 'b.test')->from('a', 'b')->where('a.test', '=', 'abc')->toSql()); $query = new Builder($this->con); - $datetime = new \Datetime('now'); + $datetime = new \DateTime('now'); $sql = 'SELECT `a`.`test`, `b`.`test` FROM `a`, `b` WHERE `a`.`test` = \'' . $datetime->format('Y-m-d H:i:s') . '\';'; self::assertEquals($sql, $query->select('a.test', 'b.test')->from('a', 'b')->where('a.test', '=', $datetime)->toSql()); diff --git a/tests/Dispatcher/DispatcherTest.php b/tests/Dispatcher/DispatcherTest.php index fc1058150..90b8d0d16 100644 --- a/tests/Dispatcher/DispatcherTest.php +++ b/tests/Dispatcher/DispatcherTest.php @@ -44,8 +44,7 @@ class DispatcherTest extends \PHPUnit\Framework\TestCase public function testClosure() { - $l11nManager = new L11nManager(); - $localization = new Localization($l11nManager); + $localization = new Localization(); self::assertTrue( !empty( @@ -60,8 +59,7 @@ class DispatcherTest extends \PHPUnit\Framework\TestCase public function testPathMethod() { - $l11nManager = new L11nManager(); - $localization = new Localization($l11nManager); + $localization = new Localization(); self::assertTrue( !empty( @@ -76,8 +74,7 @@ class DispatcherTest extends \PHPUnit\Framework\TestCase public function testPathMethodInArray() { - $l11nManager = new L11nManager(); - $localization = new Localization($l11nManager); + $localization = new Localization(); self::assertTrue( !empty( @@ -92,8 +89,7 @@ class DispatcherTest extends \PHPUnit\Framework\TestCase public function testPathStatic() { - $l11nManager = new L11nManager(); - $localization = new Localization($l11nManager); + $localization = new Localization(); self::assertTrue( !empty( @@ -108,8 +104,7 @@ class DispatcherTest extends \PHPUnit\Framework\TestCase public function testArray() { - $l11nManager = new L11nManager(); - $localization = new Localization($l11nManager); + $localization = new Localization(); self::assertTrue( !empty( diff --git a/tests/Utils/Parser/Php/ArrayParserTest.php b/tests/Utils/Parser/Php/ArrayParserTest.php index d7145d283..001adfe90 100644 --- a/tests/Utils/Parser/Php/ArrayParserTest.php +++ b/tests/Utils/Parser/Php/ArrayParserTest.php @@ -24,7 +24,7 @@ class ArrayParserTest extends \PHPUnit\Framework\TestCase public function unserialize($raw) {} }; - $jsonSerialize = new class implements \jsonSerializable { + $jsonSerialize = new class implements \JsonSerializable { public function jsonSerialize() { return [6, 7]; } };