Skip to content

Commit

Permalink
Optimize Code.
Browse files Browse the repository at this point in the history
  • Loading branch information
irazasyed committed Dec 1, 2018
1 parent c323c60 commit 8623fbd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

All notable changes will be documented in this file.

## 1.4 - 2018-12-02

### Changed

- Upgrade Dependency Packages.
- Typehint and cast.
- Optimize Code.

## 1.3.1 - 2018-08-16

### Changed
Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
}
],
"require": {
"php" : "^7.1.3",
"nesbot/carbon": "^1.29",
"php" : "^7.2",
"nesbot/carbon": "^2.5",
"illuminate/support": "~5.5",
"php-http/httplug": "^1.1",
"php-http/discovery": "^1.2",
"php-http/client-common": "^1.6",
"php-http/httplug": "^2.0",
"php-http/discovery": "^1.4",
"php-http/client-common": "^1.8",
"php-http/client-implementation": "^1.0"
},
"require-dev": {
Expand Down
4 changes: 1 addition & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,7 @@ public function get($ids = null, array $fields = null): Collection
return collect();
}

$torrents = collect($torrentsInfo)->mapInto(Torrent::class);

return $torrents;
return collect($torrentsInfo)->mapInto(Torrent::class);
}

/**
Expand Down
41 changes: 19 additions & 22 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
class Formatter
{
/** Used by formatBytes() modes */
const UNITS_MODE_DECIMAL = 0;
const UNITS_MODE_BINARY = 1;
public const UNITS_MODE_DECIMAL = 0;
public const UNITS_MODE_BINARY = 1;
/** Speed */
const SPEED_KBPS = 1000;
public const SPEED_KBPS = 1000;

/**
* Formats bytes into a human readable string if $format is true, otherwise return $bytes as is.
Expand All @@ -31,15 +31,12 @@ public static function formatBytes(int $bytes, bool $format = true, int $mode =
return $bytes;
}

switch ($mode) {
case static::UNITS_MODE_BINARY: // Binary (Used with memory size formating)
$base = 1024;
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
break;
default: // Decimal (Disk space)
$base = 1000;
$units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
break;
if ($mode === static::UNITS_MODE_BINARY) { // Binary (Used with memory size formating)
$base = 1024;
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
} else { // Decimal (Disk space)
$base = 1000;
$units = ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
}

$i = 0;
Expand All @@ -59,7 +56,7 @@ public static function formatBytes(int $bytes, bool $format = true, int $mode =
*
* @return string
*/
public static function speedBps($Bps)
public static function speedBps($Bps): string
{
return static::speed(static::bpsToKBps($Bps));
}
Expand All @@ -71,7 +68,7 @@ public static function speedBps($Bps)
*
* @return float
*/
public static function bpsToKBps($Bps)
public static function bpsToKBps($Bps): float
{
return floor($Bps / static::SPEED_KBPS);
}
Expand All @@ -83,7 +80,7 @@ public static function bpsToKBps($Bps)
*
* @return string
*/
public static function speed($KBps)
public static function speed($KBps): string
{
$speed = $KBps;

Expand Down Expand Up @@ -114,7 +111,7 @@ public static function speed($KBps)
*
* @return string
*/
public static function trunicateNumber($number, int $decimals = 2)
public static function trunicateNumber($number, int $decimals = 2): string
{
return bcdiv($number, 1, $decimals);
}
Expand All @@ -129,13 +126,13 @@ public static function trunicateNumber($number, int $decimals = 2)
*/
public static function castAttribute($type, $value)
{
if (is_null($value)) {
if ($value === null) {
return $value;
}

switch ($type) {
case 'collection':
return collect(is_array($value) ? $value : (new static)->fromJson($value));
return collect(\is_array($value) ? $value : (new static)->fromJson($value));
case 'interval':
return $value < 1 ? -1 : CarbonInterval::seconds($value)->cascade();
case 'date':
Expand All @@ -162,7 +159,7 @@ public static function castAttribute($type, $value)
*
* @return string
*/
protected function asJson($value)
protected function asJson($value): string
{
return json_encode($value);
}
Expand All @@ -187,7 +184,7 @@ protected function fromJson($value, $asObject = false)
*
* @return \Illuminate\Support\Carbon
*/
protected function asDate($value)
protected function asDate($value): Carbon
{
return $this->asDateTime($value)->startOfDay();
}
Expand All @@ -199,7 +196,7 @@ protected function asDate($value)
*
* @return \Illuminate\Support\Carbon
*/
protected function asDateTime($value)
protected function asDateTime($value): Carbon
{
// If this value is already a Carbon instance, we shall just return it as is.
// This prevents us having to re-instantiate a Carbon instance when we know
Expand All @@ -225,7 +222,7 @@ protected function asDateTime($value)
*
* @return int
*/
protected function asTimestamp($value)
protected function asTimestamp($value): int
{
return $this->asDateTime($value)->getTimestamp();
}
Expand Down

0 comments on commit 8623fbd

Please sign in to comment.