Skip to content

Commit

Permalink
Fix issues identified by Psalm
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Sep 9, 2019
1 parent 7f6a56b commit 3d432d2
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Util/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public static function parseRequirements(string $docComment, int $offset = 0, ar
/**
* Returns the missing requirements for a test.
*
* @throws Exception
* @throws Warning
*/
public static function getMissingRequirements(string $className, string $methodName): array
Expand All @@ -300,6 +301,8 @@ public static function getMissingRequirements(string $className, string $methodN
if (!empty($required['PHP'])) {
$operator = empty($required['PHP']['operator']) ? '>=' : $required['PHP']['operator'];

self::ensureOperatorIsValid($operator);

if (!\version_compare(\PHP_VERSION, $required['PHP']['version'], $operator)) {
$missing[] = \sprintf('PHP %s %s is required.', $operator, $required['PHP']['version']);
$hint = $hint ?? 'PHP';
Expand All @@ -321,6 +324,8 @@ public static function getMissingRequirements(string $className, string $methodN

$operator = empty($required['PHPUnit']['operator']) ? '>=' : $required['PHPUnit']['operator'];

self::ensureOperatorIsValid($operator);

if (!\version_compare($phpunitVersion, $required['PHPUnit']['version'], $operator)) {
$missing[] = \sprintf('PHPUnit %s %s is required.', $operator, $required['PHPUnit']['version']);
$hint = $hint ?? 'PHPUnit';
Expand Down Expand Up @@ -396,6 +401,8 @@ public static function getMissingRequirements(string $className, string $methodN

$operator = empty($req['operator']) ? '>=' : $req['operator'];

self::ensureOperatorIsValid($operator);

if ($actualVersion === false || !\version_compare($actualVersion, $req['version'], $operator)) {
$missing[] = \sprintf('Extension %s %s %s is required.', $extension, $operator, $req['version']);
$hint = $hint ?? 'extension_' . $extension;
Expand Down Expand Up @@ -1279,4 +1286,19 @@ private static function shouldCoversAnnotationBeUsed(array $annotations): bool

return true;
}

/**
* @throws Exception
*/
private static function ensureOperatorIsValid(string $operator): void
{
if (!\in_array($operator, ['<', 'lt', '<=', 'le', '>', 'gt', '>=', 'ge', '==', '=', 'eq', '!=', '<>', 'ne'])) {
throw new Exception(
\sprintf(
'"%s" is not a valid version_compare() operator',
$operator
)
);
}
}
}

0 comments on commit 3d432d2

Please sign in to comment.