Skip to content

Commit

Permalink
Simplified and documented data structures of all checks and their con…
Browse files Browse the repository at this point in the history
…structors

This mostly replaces many docblock-only union types with PHP 8 union types (now usable),
and removes many `Traversable` inputs where unusable.

Specifically, in order to operate properly, many of these checks do things like
`$this->settings[0]` or `count($this->settings)`, even in happy path scenarios,
leading to invalid execution.

This means that the accepted input type is not `array|Traversable` for many of our checks,
but rather `non-empty-list` or stricter.

While this could potentially mean that this is a BC break, these checks did **NOT**
work, when a `Traversable` was given as input, and the errors change from being an
`Exception` to being a more specific `TypeError`.

Tests verifying impossible types/scenarios have been removed.
  • Loading branch information
Ocramius committed Dec 13, 2022
1 parent c25e721 commit 38a225c
Show file tree
Hide file tree
Showing 27 changed files with 173 additions and 539 deletions.
20 changes: 3 additions & 17 deletions src/Check/AbstractFileCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
use Laminas\Diagnostics\Result\FailureInterface;
use Laminas\Diagnostics\Result\ResultInterface;
use Laminas\Diagnostics\Result\Success;
use Traversable;

use function get_class;
use function is_array;
use function is_file;
use function is_object;
use function is_readable;
use function is_string;
use function sprintf;
Expand All @@ -22,25 +18,15 @@
*/
abstract class AbstractFileCheck extends AbstractCheck
{
/** @var array|Traversable */
/** @var iterable<string> */
protected $files;

/**
* @param string|array|Traversable $files Path name or an array / Traversable of paths
* @param iterable<string>|string $files Path name or an array / Traversable of paths
* @throws InvalidArgumentException
*/
public function __construct($files)
public function __construct(iterable|string $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)
);
}

if (! is_object($files) && ! is_array($files) && ! is_string($files)) {
throw new InvalidArgumentException('Expected a file name (string) or an array of strings');
}

if (is_string($files)) {
$this->files = [$files];
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/Check/AbstractMemoryCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ abstract class AbstractMemoryCheck extends AbstractCheck implements CheckInterfa
protected $criticalThreshold;

/**
* @param int $warningThreshold A number between 0 and 100
* @param int $criticalThreshold A number between 0 and 100
* @param numeric $warningThreshold A number between 0 and 100
* @param numeric $criticalThreshold A number between 0 and 100
* @throws InvalidArgumentException
*/
public function __construct($warningThreshold, $criticalThreshold)
public function __construct(string|int|float $warningThreshold, string|int|float $criticalThreshold)
{
if (! is_numeric($warningThreshold)) {
throw new InvalidArgumentException(
Expand Down
6 changes: 3 additions & 3 deletions src/Check/ApcFragmentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ class ApcFragmentation extends AbstractCheck implements CheckInterface
protected $criticalThreshold;

/**
* @param int $warningThreshold A number between 0 and 100
* @param int $criticalThreshold A number between 0 and 100
* @param numeric $warningThreshold A number between 0 and 100
* @param numeric $criticalThreshold A number between 0 and 100
* @throws InvalidArgumentException
*/
public function __construct($warningThreshold, $criticalThreshold)
public function __construct(int|float|string $warningThreshold, int|float|string $criticalThreshold)
{
if (! is_numeric($warningThreshold)) {
throw new InvalidArgumentException(
Expand Down
13 changes: 2 additions & 11 deletions src/Check/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use InvalidArgumentException;

use function call_user_func_array;
use function is_callable;

/**
* Run a callback function and return result.
Expand All @@ -18,17 +17,9 @@ class Callback extends AbstractCheck implements CheckInterface
/** @var array */
protected $params = [];

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

$this->callback = $callback;
$this->params = $params;
}
Expand Down
22 changes: 4 additions & 18 deletions src/Check/ClassExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
use InvalidArgumentException;
use Laminas\Diagnostics\Result\Failure;
use Laminas\Diagnostics\Result\Success;
use Traversable;

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;
use function is_string;

/**
Expand All @@ -24,7 +20,7 @@ class ClassExists extends AbstractCheck implements CheckInterface
/**
* An array of classes to check
*
* @var array|Traversable
* @var iterable<string>
*/
protected $classes;

Expand All @@ -36,22 +32,12 @@ class ClassExists extends AbstractCheck implements CheckInterface
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)
* @param string|iterable<string> $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(string|iterable $classNames, $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)
);
}

if (! is_object($classNames) && ! is_array($classNames) && ! is_string($classNames)) {
throw new InvalidArgumentException('Expected a class name (string) or an array of strings');
}

if (is_string($classNames)) {
$this->classes = [$classNames];
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/Check/CpuPerformance.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,11 @@ public static function calcPi($precision)
$limit = ceil(log($precision) / log(2)) - 1;
bcscale($precision + 6);
$a = 1;
$b = bcdiv(1, bcsqrt(2));
$b = bcdiv('1', bcsqrt(2));
$t = 1 / 4;
$p = 1;
for ($n = 0; $n < $limit; $n++) {
$x = bcdiv(bcadd($a, $b), 2);
$x = bcdiv(bcadd($a, $b), '2');
$y = bcsqrt(bcmul($a, $b));
$t = bcsub($t, bcmul($p, bcpow(bcsub($a, $x), 2)));
$a = $x;
Expand Down
20 changes: 3 additions & 17 deletions src/Check/DirReadable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
use InvalidArgumentException;
use Laminas\Diagnostics\Result\Failure;
use Laminas\Diagnostics\Result\Success;
use Traversable;

use function count;
use function current;
use function get_class;
use function implode;
use function is_array;
use function is_dir;
use function is_object;
use function is_readable;
use function is_string;
use function trim;
Expand All @@ -23,25 +19,15 @@
*/
class DirReadable extends AbstractCheck implements CheckInterface
{
/** @var array|Traversable */
/** @var array<string> */
protected $dir;

/**
* @param string|array|Traversable $path Path name or an array of paths
* @param string|array<string> $path Path name or an array of paths
* @throws InvalidArgumentException
*/
public function __construct($path)
public function __construct(string|array $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)
);
}

if (! is_object($path) && ! is_array($path) && ! is_string($path)) {
throw new InvalidArgumentException('Expected a dir name (string) or an array of strings');
}

if (is_string($path)) {
$this->dir = [$path];
} else {
Expand Down
26 changes: 7 additions & 19 deletions src/Check/DirWritable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
use InvalidArgumentException;
use Laminas\Diagnostics\Result\Failure;
use Laminas\Diagnostics\Result\Success;
use Traversable;

use function count;
use function current;
use function get_class;
use function implode;
use function is_array;
use function is_dir;
use function is_object;
use function is_string;
use function is_writable;
use function sprintf;
Expand All @@ -24,30 +20,22 @@
*/
class DirWritable extends AbstractCheck implements CheckInterface
{
/** @var array|Traversable */
/** @var non-empty-list<string> */
protected $dir;

/**
* @param string|array|Traversable $path Path name or an array of paths
* @param string|non-empty-list<string> $path Path name or an array of paths
* @throws InvalidArgumentException
*/
public function __construct($path)
public function __construct(string|array $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)
);
}

if (! is_object($path) && ! is_array($path) && ! is_string($path)) {
throw new InvalidArgumentException('Expected a dir name (string) or an array of strings');
}

if (is_string($path)) {
$this->dir = [$path];
} else {
$this->dir = $path;

return;
}

$this->dir = $path;
}

/**
Expand Down
21 changes: 9 additions & 12 deletions src/Check/DiskFree.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use function is_float;
use function is_numeric;
use function is_scalar;
use function is_string;
use function preg_match;
use function round;
use function sprintf;
Expand Down Expand Up @@ -127,20 +126,16 @@ class DiskFree extends AbstractCheck implements CheckInterface
];

/**
* @param int|string $size Minimum disk size in bytes or a valid byte string (IEC, SI or Jedec).
* @param string $path The disk path to check, i.e. '/tmp' or 'C:' (defaults to /)
* @param numeric|string $size Minimum disk size in bytes or a valid byte string (IEC, SI or Jedec).
* @param string $path The disk path to check, i.e. '/tmp' or 'C:' (defaults to /)
* @throws InvalidArgumentException
*/
public function __construct($size, $path = '/')
public function __construct(int|float|string $size, string $path = '/')
{
if (! is_scalar($size)) {
throw new InvalidArgumentException('Invalid free disk space argument - expecting a positive number');
}

if (! is_string($path)) {
throw new InvalidArgumentException('Invalid disk path argument - expecting a string');
}

if (is_numeric($size)) {
$this->minDiskBytes = (int) $size;
} else {
Expand Down Expand Up @@ -188,11 +183,13 @@ public function check()
*
* @link https://en.wikipedia.org/wiki/Binary_prefix
*
* @param int $size Number of bytes to convert
* @param int $precision Rounding precision (defaults to 0)
* @return string Highest rounded multiplication with IEC suffix
* @param numeric $size Number of bytes to convert
* @param int $precision Rounding precision (defaults to 0)
*
* @return string Highest rounded multiplicatio
* n with IEC suffix
*/
public static function bytesToString($size, $precision = 0)
public static function bytesToString(int|float|string $size, int $precision = 0)
{
if ($size >= 1125899906842624) {
$size /= 1125899906842624;
Expand Down
20 changes: 8 additions & 12 deletions src/Check/DiskUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use function disk_free_space;
use function disk_total_space;
use function is_numeric;
use function is_string;
use function sprintf;

/**
Expand Down Expand Up @@ -40,19 +39,16 @@ class DiskUsage extends AbstractCheck implements CheckInterface
protected $path;

/**
* @param int $warningThreshold A number between 0 and 100
* @param int $criticalThreshold A number between 0 and 100
* @param string $path The disk path to check, i.e. '/tmp' or 'C:' (defaults to /)
* @param numeric $warningThreshold A number between 0 and 100
* @param numeric $criticalThreshold A number between 0 and 100
* @param string $path The disk path to check, i.e. '/tmp' or 'C:' (defaults to /)
* @throws InvalidArgumentException
*/
public function __construct($warningThreshold, $criticalThreshold, $path = '/')
{
if (! is_string($path)) {
throw new InvalidArgumentException(
'Invalid disk path argument - expecting a string'
);
}

public function __construct(
int|float|string $warningThreshold,
int|float|string $criticalThreshold,
string $path = '/'
) {
if (! is_numeric($warningThreshold)) {
throw new InvalidArgumentException(
'Invalid warningThreshold argument - expecting an integer'
Expand Down
Loading

0 comments on commit 38a225c

Please sign in to comment.