Skip to content

Commit

Permalink
Merge pull request #68 from samsonasik/apply-php80
Browse files Browse the repository at this point in the history
Apply PHP 8.0 Syntax and constructor promotion
  • Loading branch information
Ocramius authored Feb 1, 2023
2 parents d797ab6 + 08e2127 commit bd234ea
Show file tree
Hide file tree
Showing 32 changed files with 143 additions and 280 deletions.
3 changes: 1 addition & 2 deletions src/Check/AbstractFileCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Laminas\Diagnostics\Result\Success;
use Traversable;

use function get_class;
use function is_array;
use function is_file;
use function is_object;
Expand All @@ -33,7 +32,7 @@ public function __construct($files)
{
if (is_object($files) && ! $files instanceof Traversable) {
throw new InvalidArgumentException(
'Expected a file name (string) , an array or Traversable of strings, got ' . get_class($files)
'Expected a file name (string) , an array or Traversable of strings, got ' . $files::class
);
}

Expand Down
10 changes: 7 additions & 3 deletions src/Check/ApcMemory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Laminas\Diagnostics\Result\Warning;

use function apcu_sma_info;
use function assert;
use function function_exists;
use function ini_get;
use function is_array;
use function sprintf;

use const PHP_SAPI;
Expand All @@ -28,10 +30,8 @@ class ApcMemory extends AbstractMemoryCheck
{
/**
* APC information
*
* @var array
*/
private $apcInfo;
private array|bool $apcInfo = false;

/**
* Perform the check
Expand Down Expand Up @@ -71,6 +71,8 @@ public function check()
*/
protected function getTotalMemory()
{
assert(is_array($this->apcInfo));

return $this->apcInfo['num_seg'] * $this->apcInfo['seg_size'];
}

Expand All @@ -81,6 +83,8 @@ protected function getTotalMemory()
*/
protected function getUsedMemory()
{
assert(is_array($this->apcInfo));

return $this->getTotalMemory() - $this->apcInfo['avail_mem'];
}
}
6 changes: 1 addition & 5 deletions src/Check/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,18 @@ class Callback extends AbstractCheck implements CheckInterface
/** @var callable */
protected $callback;

/** @var array */
protected $params = [];

/**
* @param callable $callback
* @param array $params
* @throws InvalidArgumentException
*/
public function __construct($callback, $params = [])
public function __construct($callback, protected $params = [])
{
if (! is_callable($callback)) {
throw new InvalidArgumentException('Invalid callback provided; not callable');
}

$this->callback = $callback;
$this->params = $params;
}

/**
Expand Down
14 changes: 2 additions & 12 deletions src/Check/ClassExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use function class_exists;
use function count;
use function current;
use function get_class;
use function implode;
use function is_array;
use function is_object;
Expand All @@ -28,23 +27,16 @@ class ClassExists extends AbstractCheck implements CheckInterface
*/
protected $classes;

/**
* Use autoloader when looking for classes? (defaults to true)
*
* @var bool
*/
protected $autoload = true;

/**
* @param string|array|Traversable $classNames Class name or an array of classes
* @param bool $autoload Use autoloader when looking for classes? (defaults to true)
* @throws InvalidArgumentException
*/
public function __construct($classNames, $autoload = true)
public function __construct($classNames, protected $autoload = true)
{
if (is_object($classNames) && ! $classNames instanceof Traversable) {
throw new InvalidArgumentException(
'Expected a class name (string) , an array or Traversable of strings, got ' . get_class($classNames)
'Expected a class name (string) , an array or Traversable of strings, got ' . $classNames::class
);
}

Expand All @@ -57,8 +49,6 @@ public function __construct($classNames, $autoload = true)
} else {
$this->classes = $classNames;
}

$this->autoload = $autoload;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Check/DirReadable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use function count;
use function current;
use function get_class;
use function implode;
use function is_array;
use function is_dir;
Expand All @@ -34,7 +33,7 @@ public function __construct($path)
{
if (is_object($path) && ! $path instanceof Traversable) {
throw new InvalidArgumentException(
'Expected a dir name (string) , an array or Traversable of strings, got ' . get_class($path)
'Expected a dir name (string) , an array or Traversable of strings, got ' . $path::class
);
}

Expand Down
3 changes: 1 addition & 2 deletions src/Check/DirWritable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use function count;
use function current;
use function get_class;
use function implode;
use function is_array;
use function is_dir;
Expand All @@ -35,7 +34,7 @@ public function __construct($path)
{
if (is_object($path) && ! $path instanceof Traversable) {
throw new InvalidArgumentException(
'Expected a dir name (string), an array or Traversable of strings, got ' . get_class($path)
'Expected a dir name (string), an array or Traversable of strings, got ' . $path::class
);
}

Expand Down
34 changes: 17 additions & 17 deletions src/Check/DiskFree.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use function preg_match;
use function round;
use function sprintf;
use function str_starts_with;
use function strtolower;
use function substr;

/**
* Check if there is enough remaining free disk space.
Expand Down Expand Up @@ -93,11 +93,11 @@ class DiskFree extends AbstractCheck implements CheckInterface
public static $iecMultiplier = [
0 => 1, // 2^00 == 1024^0
1 => 1024, // 2^10 == 1024^1
2 => 1048576, // 2^20 == 1024^2
3 => 1073741824, // 2^30 == 1024^3
4 => 1099511627776, // 2^40 == 1024^4
5 => 1125899906842624, // 2^50 == 1024^5
6 => 1152921504606846976, // 2^60 == 1024^6
2 => 1_048_576, // 2^20 == 1024^2
3 => 1_073_741_824, // 2^30 == 1024^3
4 => 1_099_511_627_776, // 2^40 == 1024^4
5 => 1_125_899_906_842_624, // 2^50 == 1024^5
6 => 1_152_921_504_606_846_976, // 2^60 == 1024^6
];

/**
Expand All @@ -122,8 +122,8 @@ class DiskFree extends AbstractCheck implements CheckInterface
public static $jedecMultiplier = [
0 => 1, // 2^00 == 1024^0
1 => 1024, // 2^10 == 1024^1
2 => 1048576, // 2^20 == 1024^2
3 => 1073741824, // 2^30 == 1024^3
2 => 1_048_576, // 2^20 == 1024^2
3 => 1_073_741_824, // 2^30 == 1024^3
];

/**
Expand Down Expand Up @@ -203,17 +203,17 @@ public function check()
*/
public static function bytesToString($size, $precision = 0)
{
if ($size >= 1125899906842624) {
$size /= 1125899906842624;
if ($size >= 1_125_899_906_842_624) {
$size /= 1_125_899_906_842_624;
$suffix = 'PiB';
} elseif ($size >= 1099511627776) {
$size /= 1099511627776;
} elseif ($size >= 1_099_511_627_776) {
$size /= 1_099_511_627_776;
$suffix = 'TiB';
} elseif ($size >= 1073741824) {
$size /= 1073741824;
} elseif ($size >= 1_073_741_824) {
$size /= 1_073_741_824;
$suffix = 'GiB';
} elseif ($size >= 1048576) {
$size /= 1048576;
} elseif ($size >= 1_048_576) {
$size /= 1_048_576;
$suffix = 'MiB';
} elseif ($size >= 1024) {
$size /= 1024;
Expand Down Expand Up @@ -297,7 +297,7 @@ public static function stringToBytes($string, $jedec = false)
// Check whether we were provided with bits or bytes - divide if needed.
if (
isset($match[4]) && $match[4] === 'b' ||
isset($match[6]) && substr(strtolower($match[6]), 0, 3) === 'bit'
isset($match[6]) && str_starts_with(strtolower($match[6]), 'bit')
) {
$bytes /= 8;
}
Expand Down
16 changes: 10 additions & 6 deletions src/Check/DoctrineMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ private function getAvailableVersionsFromDependencyFactory(DependencyFactory $de
{
$allMigrations = $dependencyFactory->getMigrationRepository()->getMigrations();

return array_map(static function (AvailableMigration $availableMigration) {
return $availableMigration->getVersion();
}, $allMigrations->getItems());
return array_map(
static fn(AvailableMigration $availableMigration): Version =>
$availableMigration->getVersion(),
$allMigrations->getItems()
);
}

/**
Expand All @@ -112,8 +114,10 @@ private function getMigratedVersionsFromDependencyFactory(DependencyFactory $dep
{
$executedMigrations = $dependencyFactory->getMetadataStorage()->getExecutedMigrations();

return array_map(static function (ExecutedMigration $executedMigration) {
return $executedMigration->getVersion();
}, $executedMigrations->getItems());
return array_map(
static fn(ExecutedMigration $executedMigration): Version =>
$executedMigration->getVersion(),
$executedMigrations->getItems()
);
}
}
5 changes: 2 additions & 3 deletions src/Check/ExtensionLoaded.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use function count;
use function extension_loaded;
use function get_class;
use function implode;
use function is_array;
use function is_object;
Expand All @@ -35,7 +34,7 @@ public function __construct($extensionName)
{
if (is_object($extensionName) && ! $extensionName instanceof Traversable) {
throw new InvalidArgumentException(
'Expected a module name (string) , an array or Traversable of strings, got ' . get_class($extensionName)
'Expected a module name (string) , an array or Traversable of strings, got ' . $extensionName::class
);
}

Expand Down Expand Up @@ -87,7 +86,7 @@ public function check()

return new Success(
$ext . ' extension is loaded.',
$ext . ' ' . (phpversion($ext) ? phpversion($ext) : 'loaded')
$ext . ' ' . (phpversion($ext) ?: 'loaded')
);
}
}
Expand Down
19 changes: 5 additions & 14 deletions src/Check/GuzzleHttpService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@
use function json_encode;
use function method_exists;
use function sprintf;
use function strpos;
use function str_contains;
use function strstr;

class GuzzleHttpService extends AbstractCheck
{
/** @var null|string */
protected $content;

/** @var array */
protected $options;

Expand Down Expand Up @@ -62,7 +59,7 @@ public function __construct(
array $headers = [],
array $options = [],
$statusCode = 200,
$content = null,
protected $content = null,
$guzzle = null,
$method = 'GET',
$body = null
Expand All @@ -85,7 +82,6 @@ public function __construct(

$this->options = $options;
$this->statusCode = (int) $statusCode;
$this->content = $content;
}

/**
Expand All @@ -101,25 +97,20 @@ public function check()
/**
* @param string $url
* @param string $method
* @param array $headers
* @param mixed $body
* @param array $options
* @return PsrRequestInterface
*/
private function createRequestFromConstructorArguments($url, $method, array $headers, $body, array $options)
private function createRequestFromConstructorArguments($url, $method, array $headers, mixed $body, array $options)
{
return $this->createPsr7Request($url, $method, $headers, $body);
}

/**
* @param string $url
* @param string $method
* @param array $headers
* @param mixed $body
* @return PsrRequestInterface
* @throws InvalidArgumentException If unable to determine how to serialize the body content.
*/
private function createPsr7Request($url, $method, array $headers, $body)
private function createPsr7Request($url, $method, array $headers, mixed $body)
{
$request = new PsrRequest($method, $url, $headers);
if (empty($body)) {
Expand Down Expand Up @@ -229,7 +220,7 @@ private function analyzeStatusCode($statusCode)
*/
private function analyzeResponseContent($content)
{
return ! $this->content || false !== strpos($content, $this->content)
return ! $this->content || str_contains($content, $this->content)
? true
: new Failure(sprintf(
'Content %s not found in response from %s',
Expand Down
Loading

0 comments on commit bd234ea

Please sign in to comment.