Reduce potential errors

This commit is contained in:
Dennis Eichhorn 2018-07-14 13:40:49 +02:00
parent 94afffec8e
commit d9ecb8400f
17 changed files with 165 additions and 121 deletions

View File

@ -189,7 +189,7 @@ class Group implements ArrayableInterface, \JsonSerializable
*/ */
public function __toString() : string public function __toString() : string
{ {
return \json_encode($this->toArray()); return (string) \json_encode($this->toArray());
} }
/** /**

View File

@ -48,7 +48,7 @@ final class FinanceFormulas
*/ */
public static function getAnnualPercentageYield(float $r, int $n) : float public static function getAnnualPercentageYield(float $r, int $n) : float
{ {
return (float) pow(1 + $r / $n, $n) - 1; return pow(1 + $r / $n, $n) - 1;
} }
/** /**
@ -65,7 +65,7 @@ final class FinanceFormulas
*/ */
public static function getStateAnnualInterestRateOfAPY(float $apy, int $n) : float public static function getStateAnnualInterestRateOfAPY(float $apy, int $n) : float
{ {
return (float) (pow($apy + 1, 1 / $n) - 1) * $n; return (pow($apy + 1, 1 / $n) - 1) * $n;
} }
/** /**
@ -922,7 +922,7 @@ final class FinanceFormulas
*/ */
public static function getFutureValueFactor(float $r, int $n) : float public static function getFutureValueFactor(float $r, int $n) : float
{ {
return (float) pow(1 + $r, $n); return pow(1 + $r, $n);
} }
/** /**

View File

@ -369,6 +369,6 @@ final class StockBonds
*/ */
public static function getZeroCouponBondEffectiveYield(float $F, float $PV, int $n) : float public static function getZeroCouponBondEffectiveYield(float $F, float $PV, int $n) : float
{ {
return (float) pow($F / $PV, 1 / $n) - 1; return pow($F / $PV, 1 / $n) - 1;
} }
} }

View File

@ -64,7 +64,7 @@ abstract class ConnectionAbstract implements ConnectionInterface
* @var string * @var string
* @since 1.0.0 * @since 1.0.0
*/ */
protected $type = CacheStatus::UNDEFINED; protected $type = CacheType::UNDEFINED;
/** /**
* Database status. * Database status.

View File

@ -218,7 +218,7 @@ class FileCache extends ConnectionAbstract
if ($type === CacheValueType::_INT || $type === CacheValueType::_FLOAT || $type === CacheValueType::_STRING || $type === CacheValueType::_BOOL) { if ($type === CacheValueType::_INT || $type === CacheValueType::_FLOAT || $type === CacheValueType::_STRING || $type === CacheValueType::_BOOL) {
return (string) $value; return (string) $value;
} elseif ($type === CacheValueType::_ARRAY) { } elseif ($type === CacheValueType::_ARRAY) {
return \json_encode($value); return (string) \json_encode($value);
} elseif ($type === CacheValueType::_SERIALIZABLE) { } elseif ($type === CacheValueType::_SERIALIZABLE) {
return get_class($value) . self::DELIM . $value->serialize(); return get_class($value) . self::DELIM . $value->serialize();
} elseif ($type === CacheValueType::_JSONSERIALIZABLE) { } elseif ($type === CacheValueType::_JSONSERIALIZABLE) {
@ -241,8 +241,8 @@ class FileCache extends ConnectionAbstract
*/ */
private function getExpire(string $raw) : int private function getExpire(string $raw) : int
{ {
$expireStart = \strpos($raw, self::DELIM); $expireStart = (int) \strpos($raw, self::DELIM);
$expireEnd = \strpos($raw, self::DELIM, $expireStart + 1); $expireEnd = (int) \strpos($raw, self::DELIM, $expireStart + 1);
return (int) \substr($raw, $expireStart + 1, $expireEnd - ($expireStart + 1)); return (int) \substr($raw, $expireStart + 1, $expireEnd - ($expireStart + 1));
} }

View File

@ -178,6 +178,22 @@ class Directory extends FileAbstract implements DirectoryInterface
} }
/**
* {@inheritdoc}
*/
public static function dirname(string $path) : string
{
}
/**
* {@inheritdoc}
*/
public static function dirpath(string $path) : string
{
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -138,6 +138,14 @@ class File extends FileAbstract implements FileInterface
return $content; return $content;
} }
/**
* {@inheritdoc}
*/
public static function count(string $path, bool $recursive = true, array $ignore = []) : int
{
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -101,7 +101,7 @@ class Commit
$this->author = new Author(); $this->author = new Author();
$this->branch = new Branch(); $this->branch = new Branch();
$this->tag = new Tag(); $this->tag = new Tag();
$this->repository = new Repository(realpath(__DIR__ . '/../../../../../')); $this->repository = new Repository();
} }
/** /**

View File

@ -45,16 +45,16 @@ class Git
public static function test() : bool public static function test() : bool
{ {
$pipes = []; $pipes = [];
$resource = proc_open(escapeshellarg(Git::getBin()), [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); $resource = \proc_open(\escapeshellarg(Git::getBin()), [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes);
$stdout = stream_get_contents($pipes[1]); $stdout = \stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]); $stderr = \stream_get_contents($pipes[2]);
foreach ($pipes as $pipe) { foreach ($pipes as $pipe) {
fclose($pipe); \fclose($pipe);
} }
return trim(proc_close($resource)) !== 127; return $resource !== false && \proc_close($resource) !== 127;
} }
/** /**
@ -82,10 +82,10 @@ class Git
*/ */
public static function setBin(string $path) : void public static function setBin(string $path) : void
{ {
if (realpath($path) === false) { if (\realpath($path) === false) {
throw new PathException($path); throw new PathException($path);
} }
self::$bin = realpath($path); self::$bin = \realpath($path);
} }
} }

View File

@ -67,9 +67,11 @@ class Repository
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public function __construct(string $path) public function __construct(string $path = '')
{ {
$this->setPath($path); if (\is_dir($path)) {
$this->setPath($path);
}
} }
/** /**
@ -85,20 +87,16 @@ class Repository
*/ */
private function setPath(string $path) : void private function setPath(string $path) : void
{ {
if (!is_dir($path)) { if (!\is_dir($path) || \realpath($path) === false) {
throw new PathException($path); throw new PathException($path);
} }
$this->path = realpath($path); $this->path = \realpath($path);
if ($this->path === false) { if (\file_exists($this->path . '/.git') && \is_dir($this->path . '/.git')) {
throw new PathException($path);
}
if (file_exists($this->path . '/.git') && \is_dir($this->path . '/.git')) {
$this->bare = false; $this->bare = false;
} elseif (is_file($this->path . '/config')) { // Is this a bare repo? } elseif (\is_file($this->path . '/config')) { // Is this a bare repo?
$parseIni = parse_ini_file($this->path . '/config'); $parseIni = \parse_ini_file($this->path . '/config');
if ($parseIni['bare']) { if ($parseIni['bare']) {
$this->bare = true; $this->bare = true;
@ -128,7 +126,7 @@ class Repository
public function getActiveBranch() : Branch public function getActiveBranch() : Branch
{ {
$branches = $this->getBranches(); $branches = $this->getBranches();
$active = preg_grep('/^\*/', $branches); $active = \preg_grep('/^\*/', $branches);
reset($active); reset($active);
return new Branch(current($active)); return new Branch(current($active));
@ -170,14 +168,14 @@ class Repository
*/ */
private function run(string $cmd) : array private function run(string $cmd) : array
{ {
if (strtolower(substr(PHP_OS, 0, 3)) == 'win') { if (\strtolower(\substr(PHP_OS, 0, 3)) == 'win') {
$cmd = 'cd ' . escapeshellarg(\dirname(Git::getBin())) $cmd = 'cd ' . \escapeshellarg(\dirname(Git::getBin()))
. ' && ' . basename(Git::getBin()) . ' && ' . \basename(Git::getBin())
. ' -C ' . escapeshellarg($this->path) . ' ' . ' -C ' . \escapeshellarg($this->path) . ' '
. $cmd; . $cmd;
} else { } else {
$cmd = escapeshellarg(Git::getBin()) $cmd = \escapeshellarg(Git::getBin())
. ' -C ' . escapeshellarg($this->path) . ' ' . ' -C ' . \escapeshellarg($this->path) . ' '
. $cmd; . $cmd;
} }
@ -187,15 +185,20 @@ class Repository
2 => ['pipe', 'w'], 2 => ['pipe', 'w'],
]; ];
$resource = proc_open($cmd, $desc, $pipes, $this->path, null); $resource = \proc_open($cmd, $desc, $pipes, $this->path, null);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
foreach ($pipes as $pipe) { if ($resource === false) {
fclose($pipe); throw new \Exception();
} }
$status = trim(proc_close($resource)); $stdout = \stream_get_contents($pipes[1]);
$stderr = \stream_get_contents($pipes[2]);
foreach ($pipes as $pipe) {
\fclose($pipe);
}
$status = \proc_close($resource);
if ($status == -1) { if ($status == -1) {
throw new \Exception($stderr); throw new \Exception($stderr);
@ -215,11 +218,15 @@ class Repository
*/ */
private function parseLines(string $lines) : array private function parseLines(string $lines) : array
{ {
$lineArray = preg_split('/\r\n|\n|\r/', $lines); $lineArray = \preg_split('/\r\n|\n|\r/', $lines);
$lines = []; $lines = [];
if ($lineArray === false) {
return $lines;
}
foreach ($lineArray as $key => $line) { foreach ($lineArray as $key => $line) {
$temp = preg_replace('/\s+/', ' ', trim($line, ' ')); $temp = \preg_replace('/\s+/', ' ', trim($line, ' '));
if (!empty($temp)) { if (!empty($temp)) {
$lines[] = $temp; $lines[] = $temp;
@ -242,7 +249,7 @@ class Repository
*/ */
public function create(string $source = null) : void public function create(string $source = null) : void
{ {
if (!is_dir($this->path) || \file_exists($this->path . '/.git')) { if (!\is_dir($this->path) || \file_exists($this->path . '/.git')) {
throw new \Exception('Already repository'); throw new \Exception('Already repository');
} }
@ -264,7 +271,7 @@ class Repository
*/ */
public function status() : string public function status() : string
{ {
return implode("\n", $this->run('status')); return \implode("\n", $this->run('status'));
} }
/** /**
@ -282,7 +289,7 @@ class Repository
{ {
$files = $this->parseFileList($files); $files = $this->parseFileList($files);
return implode("\n", $this->run('add ' . $files . ' -v')); return \implode("\n", $this->run('add ' . $files . ' -v'));
} }
/** /**
@ -299,7 +306,7 @@ class Repository
{ {
$files = $this->parseFileList($files); $files = $this->parseFileList($files);
return implode("\n", $this->run('rm ' . ($cached ? '--cached ' : '') . $files)); return \implode("\n", $this->run('rm ' . ($cached ? '--cached ' : '') . $files));
} }
/** /**
@ -315,9 +322,9 @@ class Repository
*/ */
private function parseFileList($files) : string private function parseFileList($files) : string
{ {
if (is_array($files)) { if (\is_array($files)) {
return '"' . implode('" "', $files) . '"'; return '"' . \implode('" "', $files) . '"';
} elseif (!is_string($files)) { } elseif (!\is_string($files)) {
throw new \InvalidArgumentException('Wrong type for $files.'); throw new \InvalidArgumentException('Wrong type for $files.');
} }
@ -336,7 +343,7 @@ class Repository
*/ */
public function commit(Commit $commit, $all = true) : string public function commit(Commit $commit, $all = true) : string
{ {
return implode("\n", $this->run('commit ' . ($all ? '-av' : '-v') . ' -m ' . escapeshellarg($commit->getMessage()))); return \implode("\n", $this->run('commit ' . ($all ? '-av' : '-v') . ' -m ' . \escapeshellarg($commit->getMessage())));
} }
/** /**
@ -352,11 +359,11 @@ class Repository
*/ */
public function cloneTo(string $target) : string public function cloneTo(string $target) : string
{ {
if (!is_dir($target)) { if (!\is_dir($target)) {
throw new PathException($target); throw new PathException($target);
} }
return implode("\n", $this->run('clone --local ' . $this->path . ' ' . $target)); return \implode("\n", $this->run('clone --local ' . $this->path . ' ' . $target));
} }
/** /**
@ -372,11 +379,11 @@ class Repository
*/ */
public function cloneFrom(string $source) : string public function cloneFrom(string $source) : string
{ {
if (!is_dir($source)) { if (!\is_dir($source)) {
throw new PathException($source); throw new PathException($source);
} }
return implode("\n", $this->run('clone --local ' . $source . ' ' . $this->path)); return \implode("\n", $this->run('clone --local ' . $source . ' ' . $this->path));
} }
/** /**
@ -390,7 +397,7 @@ class Repository
*/ */
public function cloneRemote(string $source) : string public function cloneRemote(string $source) : string
{ {
return implode("\n", $this->run('clone ' . $source . ' ' . $this->path)); return \implode("\n", $this->run('clone ' . $source . ' ' . $this->path));
} }
/** /**
@ -405,7 +412,7 @@ class Repository
*/ */
public function clean(bool $dirs = false, bool $force = false) : string public function clean(bool $dirs = false, bool $force = false) : string
{ {
return implode("\n", $this->run('clean' . ($force ? ' -f' : '') . ($dirs ? ' -d' : ''))); return \implode("\n", $this->run('clean' . ($force ? ' -f' : '') . ($dirs ? ' -d' : '')));
} }
/** /**
@ -420,7 +427,7 @@ class Repository
*/ */
public function createBranch(Branch $branch, bool $force = false) : string public function createBranch(Branch $branch, bool $force = false) : string
{ {
return implode("\n", $this->run('branch ' . ($force ? '-D' : '-d') . ' ' . $branch->getName())); return \implode("\n", $this->run('branch ' . ($force ? '-D' : '-d') . ' ' . $branch->getName()));
} }
/** /**
@ -488,7 +495,7 @@ class Repository
*/ */
public function checkout(Branch $branch) : string public function checkout(Branch $branch) : string
{ {
$result = implode("\n", $this->run('checkout ' . $branch->getName())); $result = \implode("\n", $this->run('checkout ' . $branch->getName()));
$this->branch = $branch; $this->branch = $branch;
return $result; return $result;
@ -505,7 +512,7 @@ class Repository
*/ */
public function merge(Branch $branch) : string public function merge(Branch $branch) : string
{ {
return implode("\n", $this->run('merge ' . $branch->getName() . ' --no-ff')); return \implode("\n", $this->run('merge ' . $branch->getName() . ' --no-ff'));
} }
/** /**
@ -517,7 +524,7 @@ class Repository
*/ */
public function fetch() : string public function fetch() : string
{ {
return implode("\n", $this->run('fetch')); return \implode("\n", $this->run('fetch'));
} }
/** /**
@ -531,7 +538,7 @@ class Repository
*/ */
public function createTag(Tag $tag) : string public function createTag(Tag $tag) : string
{ {
return implode("\n", $this->run('tag -a ' . $tag->getName() . ' -m ' . escapeshellarg($tag->getMessage()))); return \implode("\n", $this->run('tag -a ' . $tag->getName() . ' -m ' . \escapeshellarg($tag->getMessage())));
} }
/** /**
@ -568,9 +575,9 @@ class Repository
*/ */
public function push(string $remote, Branch $branch) : string public function push(string $remote, Branch $branch) : string
{ {
$remote = escapeshellarg($remote); $remote = \escapeshellarg($remote);
return implode("\n", $this->run('push --tags ' . $remote . ' ' . $branch->getName())); return \implode("\n", $this->run('push --tags ' . $remote . ' ' . $branch->getName()));
} }
/** /**
@ -587,7 +594,7 @@ class Repository
{ {
$remote = escapeshellarg($remote); $remote = escapeshellarg($remote);
return implode("\n", $this->run('pull ' . $remote . ' ' . $branch->getName())); return \implode("\n", $this->run('pull ' . $remote . ' ' . $branch->getName()));
} }
/** /**
@ -613,7 +620,7 @@ class Repository
*/ */
public function getDescription() : string public function getDescription() : string
{ {
return \file_get_contents($this->getDirectoryPath() . '/description'); return (string) \file_get_contents($this->getDirectoryPath() . '/description');
} }
/** /**
@ -657,18 +664,18 @@ class Repository
return 0; return 0;
} }
$fh = fopen($path, 'r'); $fh = \fopen($path, 'r');
if (!$fh) { if (!$fh) {
return 0; return 0;
} }
while (!feof($fh)) { while (!feof($fh)) {
fgets($fh); \fgets($fh);
$loc++; $loc++;
} }
fclose($fh); \fclose($fh);
} }
return $loc; return $loc;
@ -700,7 +707,7 @@ class Repository
foreach ($lines as $line) { foreach ($lines as $line) {
\preg_match('/^[0-9]*/', $line, $matches); \preg_match('/^[0-9]*/', $line, $matches);
$contributor = new Author(substr($line, strlen($matches[0]) + 1)); $contributor = new Author(\substr($line, \strlen($matches[0]) + 1));
$contributor->setCommitCount($this->getCommitsCount($start, $end)[$contributor->getName()]); $contributor->setCommitCount($this->getCommitsCount($start, $end)[$contributor->getName()]);
$addremove = $this->getAdditionsRemovalsByContributor($contributor, $start, $end); $addremove = $this->getAdditionsRemovalsByContributor($contributor, $start, $end);
@ -739,7 +746,7 @@ class Repository
foreach ($lines as $line) { foreach ($lines as $line) {
\preg_match('/^[0-9]*/', $line, $matches); \preg_match('/^[0-9]*/', $line, $matches);
$commits[substr($line, strlen($matches[0]) + 1)] = (int) $matches[0]; $commits[\substr($line, \strlen($matches[0]) + 1)] = (int) $matches[0];
} }
return $commits; return $commits;
@ -768,7 +775,7 @@ class Repository
$addremove = ['added' => 0, 'removed' => 0]; $addremove = ['added' => 0, 'removed' => 0];
$lines = $this->run( $lines = $this->run(
'log --author=' . escapeshellarg($author->getName()) 'log --author=' . \escapeshellarg($author->getName())
. ' --since="' . $start->format('Y-m-d') . ' --since="' . $start->format('Y-m-d')
. '" --before="' . $end->format('Y-m-d') . '" --before="' . $end->format('Y-m-d')
. '" --pretty=tformat: --numstat' . '" --pretty=tformat: --numstat'
@ -793,7 +800,7 @@ class Repository
*/ */
public function getRemote() : string public function getRemote() : string
{ {
return implode("\n", $this->run('config --get remote.origin.url')); return \implode("\n", $this->run('config --get remote.origin.url'));
} }
/** /**
@ -820,7 +827,7 @@ class Repository
if ($author === null) { if ($author === null) {
$author = ''; $author = '';
} else { } else {
$author = ' --author=' . escapeshellarg($author->getName()) . ''; $author = ' --author=' . \escapeshellarg($author->getName()) . '';
} }
$lines = $this->run( $lines = $this->run(
@ -865,7 +872,7 @@ class Repository
\preg_match('/[0-9ABCDEFabcdef]{40}/', $lines[0], $matches); \preg_match('/[0-9ABCDEFabcdef]{40}/', $lines[0], $matches);
if (!isset($matches[0]) || strlen($matches[0]) !== 40) { if (!isset($matches[0]) || \strlen($matches[0]) !== 40) {
throw new \Exception('Invalid commit id'); throw new \Exception('Invalid commit id');
} }
@ -875,7 +882,7 @@ class Repository
$author = \explode(':', $lines[1] ?? ''); $author = \explode(':', $lines[1] ?? '');
$author = \explode('<', trim($author[1] ?? '')); $author = \explode('<', trim($author[1] ?? ''));
$date = substr($lines[2] ?? '', 6); $date = \substr($lines[2] ?? '', 6);
$commit = new Commit($matches[0]); $commit = new Commit($matches[0]);
$commit->setAuthor(new Author(trim($author[0] ?? ''), rtrim($author[1] ?? '', '>'))); $commit->setAuthor(new Author(trim($author[0] ?? ''), rtrim($author[1] ?? '', '>')));
@ -913,7 +920,7 @@ class Repository
\preg_match('/[0-9ABCDEFabcdef]{40}/', $lines[0], $matches); \preg_match('/[0-9ABCDEFabcdef]{40}/', $lines[0], $matches);
if (!isset($matches[0]) || strlen($matches[0]) !== 40) { if (!isset($matches[0]) || \strlen($matches[0]) !== 40) {
throw new \Exception('Invalid commit id'); throw new \Exception('Invalid commit id');
} }

View File

@ -47,34 +47,42 @@ class Zip implements ArchiveInterface
/** @var array $sources */ /** @var array $sources */
foreach ($sources as $source => $relative) { foreach ($sources as $source => $relative) {
$source = \str_replace('\\', '/', realpath($source)); $source = \realpath($source);
if ($source === false) {
continue;
}
$source = \str_replace('\\', '/', $source);
if (!\file_exists($source)) { if (!\file_exists($source)) {
continue; continue;
} }
if (is_dir($source)) { if (\is_dir($source)) {
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST); $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source), \RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file) { foreach ($files as $file) {
$file = \str_replace('\\', '/', $file); $file = \str_replace('\\', '/', $file);
/* Ignore . and .. */ /* Ignore . and .. */
if (\in_array(mb_substr($file, mb_strrpos($file, '/') + 1), ['.', '..'])) { if (($pos = \mb_strrpos($file, '/')) === false
|| \in_array(\mb_substr($file, $pos + 1), ['.', '..'])
) {
continue; continue;
} }
$absolute = realpath($file); $absolute = \realpath($file);
$absolute = \str_replace('\\', '/', $absolute); $absolute = \str_replace('\\', '/', (string) $absolute);
$dir = \str_replace($source . '/', '', $relative . '/' . $absolute); $dir = \str_replace($source . '/', '', $relative . '/' . $absolute);
if (is_dir($absolute)) { if (\is_dir($absolute)) {
$zip->addEmptyDir($dir . '/'); $zip->addEmptyDir($dir . '/');
} elseif (is_file($absolute)) { } elseif (\is_file($absolute)) {
$zip->addFile($absolute, $dir); $zip->addFile($absolute, $dir);
} }
} }
} elseif (is_file($source)) { } elseif (\is_file($source)) {
$zip->addFile($source, $relative); $zip->addFile($source, $relative);
} }
} }

View File

@ -51,6 +51,6 @@ final class ImageUtils
$img = \str_replace('data:image/png;base64,', '', $img); $img = \str_replace('data:image/png;base64,', '', $img);
$img = \str_replace(' ', '+', $img); $img = \str_replace(' ', '+', $img);
return base64_decode($img); return (string) base64_decode($img);
} }
} }

View File

@ -98,15 +98,19 @@ final class StringCompare
*/ */
public static function valueWords(string $s1, string $s2) : int public static function valueWords(string $s1, string $s2) : int
{ {
$words1 = preg_split('/[ _-]/', $s1); $words1 = \preg_split('/[ _-]/', $s1);
$words2 = preg_split('/[ _-]/', $s2); $words2 = \preg_split('/[ _-]/', $s2);
$total = 0; $total = 0;
if ($words1 === false || $words2 === false) {
return PHP_INT_MAX;
}
foreach ($words1 as $word1) { foreach ($words1 as $word1) {
$best = strlen($s2); $best = \strlen($s2);
foreach ($words2 as $word2) { foreach ($words2 as $word2) {
$wordDist = levenshtein($word1, $word2); $wordDist = \levenshtein($word1, $word2);
if ($wordDist < $best) { if ($wordDist < $best) {
$best = $wordDist; $best = $wordDist;
@ -136,7 +140,7 @@ final class StringCompare
*/ */
public static function valuePhrase(string $s1, string $s2) : int public static function valuePhrase(string $s1, string $s2) : int
{ {
return levenshtein($s1, $s2); return \levenshtein($s1, $s2);
} }
/** /**
@ -151,7 +155,7 @@ final class StringCompare
*/ */
public static function valueLength(string $s1, string $s2) : int public static function valueLength(string $s1, string $s2) : int
{ {
return (int) abs(strlen($s1) - strlen($s2)); return abs(\strlen($s1) - \strlen($s2));
} }
/** /**

View File

@ -40,17 +40,17 @@ final class TestUtils
/** /**
* Set private object member * Set private object member
* *
* @param object|string $obj Object to modify * @param object $obj Object to modify
* @param string $name Member name to modify * @param string $name Member name to modify
* @param mixed $value Value to set * @param mixed $value Value to set
* *
* @return bool The function returns true after setting the member * @return bool The function returns true after setting the member
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function setMember(/* object */ $obj, string $name, $value) : bool public static function setMember(object $obj, string $name, $value) : bool
{ {
$reflectionClass = new \ReflectionClass(is_string($obj) ? $obj : get_class($obj)); $reflectionClass = new \ReflectionClass(\get_class($obj));
if (!$reflectionClass->hasProperty($name)) { if (!$reflectionClass->hasProperty($name)) {
return false; return false;
@ -74,16 +74,16 @@ final class TestUtils
/** /**
* Get private object member * Get private object member
* *
* @param object|string $obj Object to read * @param object $obj Object to read
* @param string $name Member name to read * @param string $name Member name to read
* *
* @return mixed Returns the member variable value * @return mixed Returns the member variable value
* *
* @since 1.0.0 * @since 1.0.0
*/ */
public static function getMember($obj, string $name) public static function getMember(object $obj, string $name)
{ {
$reflectionClass = new \ReflectionClass(is_string($obj) ? $obj : get_class($obj)); $reflectionClass = new \ReflectionClass(\get_class($obj));
if (!$reflectionClass->hasProperty($name)) { if (!$reflectionClass->hasProperty($name)) {
return null; return null;

View File

@ -31,8 +31,8 @@ final class Iban extends ValidatorAbstract
*/ */
public static function isValid($value, array $constraints = null) : bool public static function isValid($value, array $constraints = null) : bool
{ {
$value = \str_replace(' ', '', strtolower($value)); $value = \str_replace(' ', '', \strtolower($value));
$enumName = 'C_' . strtoupper(substr($value, 0, 2)); $enumName = 'C_' . \strtoupper(\substr($value, 0, 2));
if (!IbanEnum::isValidName($enumName)) { if (!IbanEnum::isValidName($enumName)) {
self::$error = IbanErrorType::INVALID_COUNTRY; self::$error = IbanErrorType::INVALID_COUNTRY;
@ -42,7 +42,7 @@ final class Iban extends ValidatorAbstract
$layout = \str_replace(' ', '', IbanEnum::getByName($enumName)); $layout = \str_replace(' ', '', IbanEnum::getByName($enumName));
if (strlen($value) !== strlen($layout)) { if (\strlen($value) !== \strlen($layout)) {
self::$error = IbanErrorType::INVALID_LENGTH; self::$error = IbanErrorType::INVALID_LENGTH;
return false; return false;
@ -81,12 +81,12 @@ final class Iban extends ValidatorAbstract
*/ */
private static function validateZeros(string $iban, string $layout) : bool private static function validateZeros(string $iban, string $layout) : bool
{ {
if (strpos($layout, '0') === false) { if (\strpos($layout, '0') === false) {
return true; return true;
} }
$lastPos = 0; $lastPos = 0;
while (($lastPos = strpos($layout, '0', $lastPos)) !== false) { while (($lastPos = \strpos($layout, '0', $lastPos)) !== false) {
if ($iban[$lastPos] !== '0') { if ($iban[$lastPos] !== '0') {
return false; return false;
} }
@ -109,13 +109,13 @@ final class Iban extends ValidatorAbstract
*/ */
private static function validateNumeric(string $iban, string $layout) : bool private static function validateNumeric(string $iban, string $layout) : bool
{ {
if (strpos($layout, 'n') === false) { if (\strpos($layout, 'n') === false) {
return true; return true;
} }
$lastPos = 0; $lastPos = 0;
while (($lastPos = strpos($layout, 'n', $lastPos)) !== false) { while (($lastPos = \strpos($layout, 'n', $lastPos)) !== false) {
if (!is_numeric($iban[$lastPos])) { if (!\is_numeric($iban[$lastPos])) {
return false; return false;
} }
@ -139,18 +139,18 @@ final class Iban extends ValidatorAbstract
$chars = ['a' => 10, 'b' => 11, 'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17, 'i' => 18, $chars = ['a' => 10, 'b' => 11, 'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17, 'i' => 18,
'j' => 19, 'k' => 20, 'l' => 21, 'm' => 22, 'n' => 23, 'o' => 24, 'p' => 25, 'q' => 26, 'r' => 27, 'j' => 19, 'k' => 20, 'l' => 21, 'm' => 22, 'n' => 23, 'o' => 24, 'p' => 25, 'q' => 26, 'r' => 27,
's' => 28, 't' => 29, 'u' => 30, 'v' => 31, 'w' => 32, 'x' => 33, 'y' => 34, 'z' => 35,]; 's' => 28, 't' => 29, 'u' => 30, 'v' => 31, 'w' => 32, 'x' => 33, 'y' => 34, 'z' => 35,];
$moved = substr($iban, 4) . substr($iban, 0, 4); $moved = \substr($iban, 4) . \substr($iban, 0, 4);
$movedArray = str_split($moved); $movedArray = (array) \str_split($moved);
$new = ''; $new = '';
foreach ($movedArray as $key => $value) { foreach ($movedArray as $key => $value) {
if (!is_numeric($movedArray[$key])) { if (!\is_numeric($movedArray[$key])) {
$movedArray[$key] = $chars[$movedArray[$key]]; $movedArray[$key] = $chars[$movedArray[$key]];
} }
$new .= $movedArray[$key]; $new .= $movedArray[$key];
} }
return bcmod($new, '97') == 1; return \bcmod($new, '97') == 1;
} }
} }

View File

@ -35,6 +35,8 @@ final class Validator extends ValidatorAbstract
* *
* @return bool * @return bool
* *
* @throws \Exception
*
* @since 1.0.0 * @since 1.0.0
*/ */
public static function isValid($var, array $constraints = null) : bool public static function isValid($var, array $constraints = null) : bool
@ -44,14 +46,13 @@ final class Validator extends ValidatorAbstract
} }
foreach ($constraints as $test => $settings) { foreach ($constraints as $test => $settings) {
$callback = StringUtils::endsWith($test, 'Not') ? substr($test, 0, -3) : $test; $callback = StringUtils::endsWith($test, 'Not') ? \substr($test, 0, -3) : (string) $test;
if (!empty($settings)) { if (!\is_callable($callback)) {
$valid = $callback($var, ...$settings); throw new \Exception();
} else {
$valid = $callback($var);
} }
$valid = !empty($settings) ? $callback($var, ...$settings) : $callback($var);
$valid = (StringUtils::endsWith($test, 'Not') ? !$valid : $valid); $valid = (StringUtils::endsWith($test, 'Not') ? !$valid : $valid);
if (!$valid) { if (!$valid) {

View File

@ -203,7 +203,7 @@ abstract class ViewAbstract implements \Serializable
public function serialize() : string public function serialize() : string
{ {
if (empty($this->template)) { if (empty($this->template)) {
return \json_encode($this->toArray()); return (string) \json_encode($this->toArray());
} }
return $this->render(); return $this->render();
@ -253,10 +253,10 @@ abstract class ViewAbstract implements \Serializable
ob_start(); ob_start();
/** @noinspection PhpIncludeInspection */ /** @noinspection PhpIncludeInspection */
$includeData = include $path; $includeData = include $path;
$ob = ob_get_clean(); $ob = (string) ob_get_clean();
if (is_array($includeData)) { if (is_array($includeData)) {
return \json_encode($includeData); return (string) \json_encode($includeData);
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
$ob = ''; $ob = '';