static code analysis fixes

This commit is contained in:
Dennis Eichhorn 2022-04-05 20:01:46 +02:00
parent 8dcc7feaa1
commit b2efd91008
27 changed files with 88 additions and 94 deletions

View File

@ -107,12 +107,12 @@ final class BasicOcr
if (($read = \fread($fp, 4)) === false || ($unpack = \unpack('N', $read)) === false) {
return []; // @codeCoverageIgnore
}
$numberOfRows = $unpack[1];
$numberOfRows = (int) $unpack[1];
if (($read = \fread($fp, 4)) === false || ($unpack = \unpack('N', $read)) === false) {
return []; // @codeCoverageIgnore
}
$numberOfColumns = $unpack[1];
$numberOfColumns = (int) $unpack[1];
$images = [];
for ($i = 0; $i < $numberOfImages; ++$i) {

View File

@ -83,7 +83,9 @@ final class AStar implements PathFinderInterface
if (!$neighbor->isOpened() || $ng < $neighbor->getG()) {
$neighbor->setG($ng);
$neighbor->setH($neighbor->getG() ?? $neighbor->getWeight() * Heuristic::metric($neighbor->getCoordinates(), $endNode->getCoordinates(), $heuristic));
$neighbor->setH($neighbor->getH() ?? (
$neighbor->getWeight() * Heuristic::metric($neighbor->getCoordinates(), $endNode->getCoordinates(), $heuristic)
));
$neighbor->setF($neighbor->getG() + $neighbor->getH());
$neighbor->parent = $node;

View File

@ -71,9 +71,11 @@ class Grid
foreach ($gridArray as $y => $yRow) {
foreach ($yRow as $x => $xElement) {
if ($xElement === 0 || $xElement === 1 || $xElement === 2) {
/** @var \phpOMS\Algorithm\PathFinding\Node $empty */
$empty = new $node($x, $y, 1.0, true);
$grid->setNode($x, $y, $empty);
} elseif ($xElement === 9) {
/** @var \phpOMS\Algorithm\PathFinding\Node $wall */
$wall = new $node($x, $y, 1.0, false);
$grid->setNode($x, $y, $wall);
}

View File

@ -53,14 +53,6 @@ class Path
*/
private array $expandedNodes = [];
/**
* Path length
*
* @var float
* @since 1.0.0
*/
private float $length = 0.0;
/**
* Cosntructor.
*

View File

@ -156,7 +156,7 @@ abstract class DataMapperAbstract
$split = \explode('/', $member);
$memberSplit = \array_shift($split);
$this->with[$memberSplit ?? ''][] = [
$this->with[$memberSplit][] = [
'child' => \implode('/', $split),
];
@ -178,7 +178,7 @@ abstract class DataMapperAbstract
$split = \explode('/', $member);
$memberSplit = \array_shift($split);
$this->sort[$memberSplit ?? ''][] = [
$this->sort[$memberSplit][] = [
'child' => \implode('/', $split),
'order' => $order,
];
@ -201,7 +201,7 @@ abstract class DataMapperAbstract
$split = \explode('/', $member);
$memberSplit = \array_shift($split);
$this->offset[$memberSplit ?? ''][] = [
$this->offset[$memberSplit][] = [
'child' => \implode('/', $split),
'offset' => $offset,
];
@ -224,7 +224,7 @@ abstract class DataMapperAbstract
$split = \explode('/', $member);
$memberSplit = \array_shift($split);
$this->limit[$memberSplit ?? ''][] = [
$this->limit[$memberSplit][] = [
'child' => \implode('/', $split),
'limit' => $limit,
];
@ -249,7 +249,7 @@ abstract class DataMapperAbstract
$split = \explode('/', $member);
$memberSplit = \array_shift($split);
$this->where[$memberSplit ?? ''][] = [
$this->where[$memberSplit][] = [
'child' => \implode('/', $split),
'value' => $value,
'logic' => $logic,

View File

@ -155,19 +155,13 @@ final class ReadMapper extends DataMapperAbstract
switch($this->type) {
case MapperType::GET:
/** @var null|Builder ...$options */
return $options !== null
? $this->executeGet(...$options)
: $this->executeGet();
return $this->executeGet(...$options);
case MapperType::GET_RAW:
/** @var null|Builder ...$options */
return $options !== null
? $this->executeGetRaw(...$options)
: $this->executeGetRaw();
return $this->executeGetRaw(...$options);
case MapperType::GET_ALL:
/** @var null|Builder ...$options */
return $options !== null
? $this->executeGetAll(...$options)
: $this->executeGetAll();
return $this->executeGetAll(...$options);
case MapperType::GET_RANDOM:
return $this->executeGetRaw();
case MapperType::COUNT_MODELS:
@ -361,9 +355,9 @@ final class ReadMapper extends DataMapperAbstract
$comparison = \is_array($where['value']) && \count($where['value']) > 1 ? 'in' : $where['logic'];
$query->where($this->mapper::TABLE . '_d' . $this->depth . '.' . $col, $comparison, $where['value'], $where['comparison']);
}
} elseif (isset($this->mapper::HAS_MANY[$member])) {
/* variable in has many */
/* @todo: maybe needed in the future, but needs adjustment, doesn't make sense at the moment
} /* elseif (isset($this->mapper::HAS_MANY[$member])) {
// variable in has many
// @todo: maybe needed in the future, but needs adjustment, doesn't make sense at the moment
foreach ($values as $where) {
// @todo: the has many, etc. if checks only work if it is a relation on the first level, if we have a deeper where condition nesting this fails
if ($where['child'] !== '') {
@ -388,10 +382,10 @@ final class ReadMapper extends DataMapperAbstract
);
}
}
*/
} elseif (isset($this->mapper::BELONGS_TO[$member])) {
/* variable in belogns to */
/* @todo: maybe needed in the future, but needs adjustment, doesn't make sense at the moment
} */ /* elseif (isset($this->mapper::BELONGS_TO[$member])) {
// variable in belogns to
// @todo: maybe needed in the future, but needs adjustment, doesn't make sense at the moment
foreach ($values as $index => $where) {
// @todo: the has many, etc. if checks only work if it is a relation on the first level, if we have a deeper where condition nesting this fails
if ($where['child'] !== '') {
@ -408,10 +402,9 @@ final class ReadMapper extends DataMapperAbstract
$this->mapper::BELONGS_TO[$member]['mapper']::TABLE . '_d' . $this->depth
);
}
*/
} elseif (isset($this->mapper::OWNS_ONE[$member])) {
/* variable in owns one */
/* @todo: maybe needed in the future, but needs adjustment, doesn't make sense at the moment
} */ /* elseif (isset($this->mapper::OWNS_ONE[$member])) {
// variable in owns one
// @todo: maybe needed in the future, but needs adjustment, doesn't make sense at the moment
foreach ($values as $index => $where) {
// @todo: the has many, etc. if checks only work if it is a relation on the first level, if we have a deeper where condition nesting this fails
if ($where['child'] !== '') {
@ -427,8 +420,7 @@ final class ReadMapper extends DataMapperAbstract
$this->mapper::OWNS_ONE[$member]['mapper']::TABLE . '_d' . $this->depth
);
}
*/
}
} */
}
// load relations
@ -806,7 +798,11 @@ final class ReadMapper extends DataMapperAbstract
/** @var self $belongsToMapper */
$belongsToMapper = $this->createRelationMapper($mapper::get($this->db), $member);
$belongsToMapper->depth = $this->depth + 1;
$belongsToMapper->where($this->mapper::BELONGS_TO[$member]['by'], $result[$mapper::getColumnByMember($this->mapper::BELONGS_TO[$member]['by']) . '_d' . $this->depth + 1], '=');
$belongsToMapper->where(
$this->mapper::BELONGS_TO[$member]['by'],
$result[$mapper::getColumnByMember($this->mapper::BELONGS_TO[$member]['by']) . '_d' . ($this->depth + 1)],
'='
);
return $belongsToMapper->execute();
}

View File

@ -65,7 +65,7 @@ final class Dispatcher implements DispatcherInterface
public function dispatch(array | string | \Closure $controller, ...$data) : array
{
$views = [];
$data = $data !== null ? \array_values($data) : null;
$data = \array_values($data);
if (\is_array($controller) && isset($controller['dest'])) {
if (!empty($controller['data'])) {

View File

@ -257,8 +257,8 @@ final class EventManager implements \Countable
$data,
];
$data[':triggerGroup'] ??= $group;
$data[':triggerId'] = $id;
$data[':triggerGroup'] = $group;
$data[':triggerId'] = $id;
}
$this->dispatcher->dispatch($func, ...$data);

View File

@ -278,7 +278,7 @@ final class L11nManager
*/
public function getCurrency(Localization $l11n, int | float $currency, string $format = null, string $symbol = null, int $divide = 1) : string
{
$language = $l11n->getLanguage() ?? 'en';
$language = $l11n->getLanguage();
$symbol ??= $l11n->getCurrency();
if (\is_float($currency)) {
@ -299,7 +299,7 @@ final class L11nManager
(int) ($currency / $divide),
$l11n->getThousands(),
$l11n->getDecimal(),
$symbol ?? $l11n->getCurrency(),
$symbol,
(int) $l11n->getCurrencyFormat()
);

View File

@ -704,7 +704,8 @@ class Matrix implements \ArrayAccess, \Iterator
*/
public function offsetGet($offset)
{
$row = (int) ($offset / $this->m);
$offset = (int) $offset;
$row = (int) ($offset / $this->m);
return $this->matrix[$row][$offset - $row * $this->n];
}
@ -738,7 +739,8 @@ class Matrix implements \ArrayAccess, \Iterator
*/
public function offsetExists($offset) : bool
{
$row = (int) ($offset / $this->m);
$offset = (int) $offset;
$row = (int) ($offset / $this->m);
return isset($this->matrix[$row][$offset - $row * $this->n]);
}
@ -756,7 +758,8 @@ class Matrix implements \ArrayAccess, \Iterator
*/
public function offsetSet($offset, $value) : void
{
$row = (int) ((int) $offset / $this->m);
$offset = (int) $offset;
$row = (int) ($offset / $this->m);
$this->matrix[$row][$offset - $row * $this->n] = $value;
}
@ -765,7 +768,8 @@ class Matrix implements \ArrayAccess, \Iterator
*/
public function offsetUnset($offset) : void
{
$row = (int) ($offset / $this->m);
$offset = (int) $offset;
$row = (int) ($offset / $this->m);
unset($this->matrix[$row][$offset - $row * $this->n]);
}
}

View File

@ -34,14 +34,6 @@ final class NaiveBayesClassifier
*/
private array $dict = [];
/**
* Dictionary changed.
*
* @var bool
* @since 1.0.0
*/
private bool $changed = true;
/**
* Cached probabilities.
*

View File

@ -107,8 +107,8 @@ final class HttpRequest extends RequestAbstract
private function initCurrentRequest() : void
{
$this->uri = HttpUri::fromCurrent();
$this->data = ($_GET ?? []) + ($_POST ?? []);
$this->files = $_FILES ?? [];
$this->data = $_GET + $_POST;
$this->files = $_FILES;
$this->header->l11n->setLanguage($this->getRequestLanguage());
$this->initNonGetData();

View File

@ -59,7 +59,7 @@ abstract class ModuleAbstract
/**
* Module id.
*
* @var string
* @var int
* @since 1.0.0
*/
public const ID = 0;

View File

@ -202,7 +202,11 @@ final class ModuleManager
$content = \file_get_contents($path);
$json = \json_decode($content === false ? '[]' : $content, true);
$this->active[$json['name']['internal']] = $json === false ? [] : $json;
if ($json === false) {
return [];
}
$this->active[$json['name']['internal']] = $json;
}
}
@ -646,7 +650,8 @@ final class ModuleManager
* @todo Remove docblock type hint hack "object".
* The return type object is only used to stop the annoying warning that a method doesn't exist
* if you chain call the methods part of the returned ModuleAbstract implementation.
* Remove it once alternative inline type hinting is possible for the specific returned implementation
* Remove it once alternative inline type hinting is possible for the specific returned implementation.
* This also causes phpstan type inspection errors, which we have to live with or ignore in the settings
*
* @since 1.0.0
*/

View File

@ -262,10 +262,6 @@ abstract class StatusAbstract
return;
}
if (!\is_file($destRoutePath)) {
throw new PathException($destRoutePath); // @codeCoverageIgnore
}
if (!\is_writable($destRoutePath)) {
throw new PermissionException($destRoutePath); // @codeCoverageIgnore
}

View File

@ -55,7 +55,7 @@ abstract class Enum
{
$reflect = new \ReflectionClass(static::class);
return $reflect->getConstants() ?? [];
return $reflect->getConstants();
}
/**

View File

@ -131,10 +131,8 @@ final class FileUtils
if ($part !== '..' || empty($path)) {
$path[] = $part;
} elseif (!empty($path)) {
\array_pop($path);
} else {
throw new PathException($origPath); // @codeCoverageIgnore
\array_pop($path);
}
}

View File

@ -359,7 +359,7 @@ final class Directory extends FileAbstract implements DirectoryInterface
if (!\is_dir($to)) {
self::create($to, 0755, true);
} elseif ($overwrite && \is_dir($to)) {
} elseif ($overwrite) {
self::delete($to);
self::create($to, 0755, true);
}

View File

@ -59,11 +59,13 @@ final class Storage
{
if (isset(self::$registered[$env])) {
if (\is_string(self::$registered[$env])) {
/** @var StorageAbstract $instance */
$instance = new self::$registered[$env]();
self::$registered[$env] = $instance;
} elseif (self::$registered[$env] instanceof StorageAbstract
|| self::$registered[$env] instanceof ContainerInterface
) {
/** @var StorageAbstract $instance */
$instance = self::$registered[$env];
} else {
throw new \Exception('Invalid type');
@ -71,10 +73,11 @@ final class Storage
} else {
$stg = $env;
$env = \ucfirst(\strtolower($env));
/** @var StorageAbstract $env */
$env = __NAMESPACE__ . '\\' . $env . '\\' . $env . 'Storage';
try {
/** @var StorageAbstract $env */
/** @var StorageAbstract $instance */
$instance = new $env();
self::$registered[$stg] = $instance;

View File

@ -93,7 +93,7 @@ final class SystemUtils
$freeArr = \explode("\n", $free);
$mem = \explode(' ', $freeArr[1]);
$mem = \array_values(\array_filter($mem));
$memUsage = $mem[2] / $mem[1] * 100;
$memUsage = ((float) ($mem[2] ?? 0.0)) / ((float) ($mem[1] ?? 1.0)) * 100;
}
return (int) $memUsage;

View File

@ -297,7 +297,7 @@ final class Argument implements UriInterface
public function getQuery(string $key = null) : string
{
if ($key !== null) {
$key = \strtolower($key);
$key = (int) \strtolower($key);
return $this->query[$key] ?? '';
}

View File

@ -270,8 +270,8 @@ final class UriFactory
* @ =
* $ = Other data
*
* @param string $uri Path data
* @param array<string, bool|int|float|string> $toMatch Optional special replacements
* @param string $uri Path data
* @param array<string, string> $toMatch Optional special replacements
*
* @return string
*
@ -285,17 +285,21 @@ final class UriFactory
return $uri;
}
$parsed = \preg_replace_callback('(\{[\/#\?%@\.\$][a-zA-Z0-9\-]*\})', function ($match) use ($toMatch) {
$match = \substr($match[0], 1, \strlen($match[0]) - 2);
$parsed = \preg_replace_callback(
'(\{[\/#\?%@\.\$][a-zA-Z0-9\-]*\})',
function ($match) use ($toMatch) : string {
$match = \substr($match[0], 1, \strlen($match[0]) - 2);
return $toMatch[$match]
?? (self::$uri[$match] ?? (
($match[0] ?? '') === '?'
? '---' // only do this for query parameters
: ''
)
);
}, $uri);
return $toMatch[$match]
?? (self::$uri[$match] ?? (
($match[0] ?? '') === '?'
? '---' // only do this for query parameters
: ''
)
);
},
$uri
);
return self::unique($parsed ?? '');
}

View File

@ -69,7 +69,7 @@ class LZW implements CompressionInterface
$entry = '';
$dictSize = 256;
if (empty($compressed) || $compressed === [''] || $compressed === false) {
if ($compressed === [] || $compressed === [''] || $compressed === false) {
return '';
}

View File

@ -49,9 +49,9 @@ final class Ip
{
$split = \explode('.', $ip);
return ((int) $split[0] ?? 0) * (256 ** 3)
+ ((int) $split[1] ?? 0) * (256 ** 2)
+ ((int) $split[2] ?? 0) * (256 ** 1)
+ ((int) $split[3] ?? 0);
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

@ -95,7 +95,7 @@ class Repository
$this->path = \realpath($path);
if (\is_dir($this->path . '/.git') && \is_dir($this->path . '/.git')) {
if (\is_dir($this->path . '/.git')) {
$this->bare = false;
} elseif (\is_file($this->path . '/config')) { // Is this a bare repo?
$parseIni = \parse_ini_file($this->path . '/config');
@ -903,7 +903,7 @@ class Repository
$commit = new Commit($matches[0]);
$commit->setAuthor(new Author(\trim($author[0] ?? ''), \rtrim($author[1] ?? '', '>')));
$commit->setDate(new \DateTime(\trim($date ?? 'now')));
$commit->setDate(new \DateTime(\trim($date)));
$commit->setMessage($lines[3]);
$commit->setTag(new Tag());
$commit->setRepository($this);

View File

@ -214,7 +214,7 @@ class View extends ViewAbstract
/** @var string $theme */
$theme = $theme ?? $this->theme;
return $this->l11nManager->getText($this->l11n->getLanguage() ?? 'en', $module, $theme, $translation);
return $this->l11nManager->getText($this->l11n->getLanguage(), $module, $theme, $translation);
}
/**

View File

@ -12,7 +12,7 @@
"friendsofphp/php-cs-fixer": ">=3.2",
"squizlabs/php_codesniffer": ">=3.6",
"phpmd/phpmd": ">=2.9",
"phpstan/phpstan": ">=0.12.58",
"phpstan/phpstan": ">=1.5.4",
"phan/phan": ">=3.2.6"
},
"minimum-stability": "dev",