Cleanup Warning

This commit is contained in:
Simon Podlipsky 2019-06-12 10:33:08 +02:00
parent e17f578842
commit a22a083220
3 changed files with 57 additions and 13 deletions

View File

@ -4,6 +4,8 @@ declare(strict_types=1);
namespace GraphQL\Error;
use GraphQL\Exception\InvalidArgument;
use function is_int;
use function trigger_error;
use const E_USER_WARNING;
@ -15,12 +17,12 @@ use const E_USER_WARNING;
*/
final class Warning
{
const WARNING_ASSIGN = 2;
const WARNING_CONFIG = 4;
const WARNING_FULL_SCHEMA_SCAN = 8;
const WARNING_CONFIG_DEPRECATION = 16;
const WARNING_NOT_A_TYPE = 32;
const ALL = 63;
public const WARNING_ASSIGN = 2;
public const WARNING_CONFIG = 4;
public const WARNING_FULL_SCHEMA_SCAN = 8;
public const WARNING_CONFIG_DEPRECATION = 16;
public const WARNING_NOT_A_TYPE = 32;
public const ALL = 63;
/** @var int */
private static $enableWarnings = self::ALL;
@ -37,7 +39,7 @@ final class Warning
*
* @api
*/
public static function setWarningHandler(?callable $warningHandler = null)
public static function setWarningHandler(?callable $warningHandler = null) : void
{
self::$warningHandler = $warningHandler;
}
@ -54,14 +56,16 @@ final class Warning
*
* @api
*/
public static function suppress($suppress = true)
public static function suppress($suppress = true) : void
{
if ($suppress === true) {
self::$enableWarnings = 0;
} elseif ($suppress === false) {
self::$enableWarnings = self::ALL;
} else {
} elseif (is_int($suppress)) {
self::$enableWarnings &= ~$suppress;
} else {
throw InvalidArgument::fromExpectedTypeAndArgument('bool|int', $suppress);
}
}
@ -77,18 +81,20 @@ final class Warning
*
* @api
*/
public static function enable($enable = true)
public static function enable($enable = true) : void
{
if ($enable === true) {
self::$enableWarnings = self::ALL;
} elseif ($enable === false) {
self::$enableWarnings = 0;
} else {
} elseif (is_int($enable)) {
self::$enableWarnings |= $enable;
} else {
throw InvalidArgument::fromExpectedTypeAndArgument('bool|int', $enable);
}
}
public static function warnOnce($errorMessage, $warningId, $messageLevel = null)
public static function warnOnce(string $errorMessage, int $warningId, ?int $messageLevel = null) : void
{
if (self::$warningHandler) {
$fn = self::$warningHandler;
@ -99,7 +105,7 @@ final class Warning
}
}
public static function warn($errorMessage, $warningId, $messageLevel = null)
public static function warn(string $errorMessage, int $warningId, ?int $messageLevel = null) : void
{
if (self::$warningHandler) {
$fn = self::$warningHandler;

View File

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace GraphQL\Exception;
use InvalidArgumentException;
use function gettype;
use function sprintf;
final class InvalidArgument extends InvalidArgumentException
{
/**
* @param mixed $argument
*/
public static function fromExpectedTypeAndArgument(string $expectedType, $argument) : self
{
return new self(sprintf('Expected type "%s", got "%s"', $expectedType, gettype($argument)));
}
}

View File

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace GraphQL\Tests\Exception;
use GraphQL\Exception\InvalidArgument;
use PHPUnit\Framework\TestCase;
final class InvalidArgumentTest extends TestCase
{
public function testFromExpectedTypeAndArgument() : void
{
$exception = InvalidArgument::fromExpectedTypeAndArgument('bool|int', 'stringValue');
self::assertSame('Expected type "bool|int", got "string"', $exception->getMessage());
}
}