Code optimizations

This commit is contained in:
Dennis Eichhorn 2017-10-29 15:27:19 +01:00
parent 3442d1d817
commit e85164c986
8 changed files with 50 additions and 80 deletions

View File

@ -560,7 +560,7 @@ class Account implements ArrayableInterface, \JsonSerializable
*/ */
public function generatePassword(string $password) /* : void */ public function generatePassword(string $password) /* : void */
{ {
$this->password = password_hash($password, PASSWORD_DEFAULT); $this->password = \password_hash($password, \PASSWORD_DEFAULT);
} }
/** /**

View File

@ -60,32 +60,11 @@ class MysqlConnection extends ConnectionAbstract
{ {
$this->dbdata = isset($dbdata) ? $dbdata : $this->dbdata; $this->dbdata = isset($dbdata) ? $dbdata : $this->dbdata;
if (!isset($this->dbdata['db'])) { if (!isset($this->dbdata['db'], $this->dbdata['host'], $this->dbdata['port'], $this->dbdata['database'], $this->dbdata['login'], $this->dbdata['password'])) {
throw new InvalidConnectionConfigException('db'); throw new InvalidConnectionConfigException(json_encode($this->dbdata));
}
if (!isset($this->dbdata['host'])) {
throw new InvalidConnectionConfigException('host');
}
if (!isset($this->dbdata['port'])) {
throw new InvalidConnectionConfigException('port');
}
if (!isset($this->dbdata['database'])) {
throw new InvalidConnectionConfigException('database');
}
if (!isset($this->dbdata['login'])) {
throw new InvalidConnectionConfigException('login');
}
if (!isset($this->dbdata['password'])) {
throw new InvalidConnectionConfigException('password');
} }
$this->close(); $this->close();
$this->prefix = $dbdata['prefix'] ?? ''; $this->prefix = $dbdata['prefix'] ?? '';
try { try {

View File

@ -149,7 +149,7 @@ class DataMapperAbstract implements DataMapperInterface
/** /**
* Highest mapper to know when to clear initialized objects * Highest mapper to know when to clear initialized objects
* *
* @var DataMapperAbstract * @var string
* @since 1.0.0 * @since 1.0.0
*/ */
protected static $parentMapper = null; protected static $parentMapper = null;

View File

@ -133,7 +133,6 @@ class Grammar extends GrammarAbstract
break; break;
case QueryType::RAW: case QueryType::RAW:
return [$query->raw]; return [$query->raw];
break;
default: default:
throw new \InvalidArgumentException('Unknown query type.'); throw new \InvalidArgumentException('Unknown query type.');
} }

View File

@ -265,7 +265,7 @@ abstract class RequestAbstract implements MessageInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract function getOrigin() : string; abstract public function getOrigin() : string;
/** /**
* {@inheritdoc} * {@inheritdoc}

View File

@ -156,27 +156,23 @@ class Directory extends FileAbstract implements DirectoryInterface
*/ */
public static function size(string $dir, bool $recursive = true) : int public static function size(string $dir, bool $recursive = true) : int
{ {
if (!file_exists($dir)) { if (!file_exists($dir) || !is_readable($dir)) {
throw new PathException($dir); throw new PathException($dir);
} }
$countSize = 0; $countSize = 0;
$count = 0; $dir_array = scandir($dir);
if (is_readable($dir)) { foreach ($dir_array as $key => $filename) {
$dir_array = scandir($dir); if ($filename === ".." || $filename === ".") {
continue;
}
foreach ($dir_array as $key => $filename) { $path = $dir . "/" . $filename;
if ($filename != ".." && $filename != ".") { if (is_dir($path) && $recursive) {
if (is_dir($dir . "/" . $filename) && $recursive) { $countSize += self::size($path, $recursive);
$countSize += self::size($dir . "/" . $filename, $recursive); } elseif (is_file($path)) {
} else { $countSize += filesize($path);
if (is_file($dir . "/" . $filename)) {
$countSize += filesize($dir . "/" . $filename);
$count++;
}
}
}
} }
} }
@ -312,14 +308,12 @@ class Directory extends FileAbstract implements DirectoryInterface
throw new PathException($from); throw new PathException($from);
} }
if (!$overwrite && file_exists($to)) {
return false;
}
if (!file_exists($to)) { if (!file_exists($to)) {
self::create($to, 0644, true); self::create($to, 0644, true);
} elseif ($overwrite && file_exists($to)) { } elseif ($overwrite && file_exists($to)) {
self::delete($to); self::delete($to);
} else {
return false;
} }
foreach ($iterator = new \RecursiveIteratorIterator( foreach ($iterator = new \RecursiveIteratorIterator(

View File

@ -72,120 +72,120 @@ abstract class StorageAbstract
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function created(string $path) : \DateTime; abstract public static function created(string $path) : \DateTime;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function changed(string $path) : \DateTime; abstract public static function changed(string $path) : \DateTime;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function owner(string $path) : int; abstract public static function owner(string $path) : int;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function permission(string $path) : int; abstract public static function permission(string $path) : int;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function parent(string $path) : string; abstract public static function parent(string $path) : string;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function create(string $path) : bool; abstract public static function create(string $path) : bool;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function delete(string $path) : bool; abstract public static function delete(string $path) : bool;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function copy(string $from, string $to, bool $overwrite = false) : bool; abstract public static function copy(string $from, string $to, bool $overwrite = false) : bool;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function move(string $from, string $to, bool $overwrite = false) : bool; abstract public static function move(string $from, string $to, bool $overwrite = false) : bool;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function size(string $path, bool $recursive = true) : int; abstract public static function size(string $path, bool $recursive = true) : int;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function exists(string $path) : bool; abstract public static function exists(string $path) : bool;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function name(string $path) : string; abstract public static function name(string $path) : string;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function basename(string $path) : string; abstract public static function basename(string $path) : string;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function dirname(string $path) : string; abstract public static function dirname(string $path) : string;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function dirpath(string $path) : string; abstract public static function dirpath(string $path) : string;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function list(string $path, string $filter = '*') : array; abstract public static function list(string $path, string $filter = '*') : array;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function count(string $path, bool $recursive = true, array $ignore = []) : int; abstract public static function count(string $path, bool $recursive = true, array $ignore = []) : int;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function put(string $path, string $content, int $mode = 0) : bool; abstract public static function put(string $path, string $content, int $mode = 0) : bool;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function get(string $path) : string; abstract public static function get(string $path) : string;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function sanitize(string $path, string $replace = '') : string; abstract public static function sanitize(string $path, string $replace = '') : string;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function set(string $path, string $content) : bool; abstract public static function set(string $path, string $content) : bool;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function append(string $path, string $content) : bool; abstract public static function append(string $path, string $content) : bool;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function prepend(string $path, string $content) : bool; abstract public static function prepend(string $path, string $content) : bool;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public abstract static function extension(string $path) : string; abstract public static function extension(string $path) : string;
} }

View File

@ -97,16 +97,14 @@ class ArrayUtils
if ($overwrite) { if ($overwrite) {
$current = $value; $current = $value;
} elseif (is_array($current) && !is_array($value)) {
$current[] = $value;
} elseif (is_array($current) && is_array($value)) {
$current += $value;
} elseif (is_scalar($current) && $current !== null) {
$current = [$current, $value];
} else { } else {
if (is_array($current) && !is_array($value)) { $current = $value;
$current[] = $value;
} elseif (is_array($current) && is_array($value)) {
$current += $value;
} elseif (is_scalar($current) && $current !== null) {
$current = [$current, $value];
} else {
$current = $value;
}
} }
return $data; return $data;
@ -156,7 +154,7 @@ class ArrayUtils
$found = self::inArrayRecursive($needle, $item); $found = self::inArrayRecursive($needle, $item);
if ($found) { if ($found) {
break; return true;
} }
} }
} }