Added tools for warnings with ability to suppress them

This commit is contained in:
Vladimir Razuvaev 2017-07-04 17:10:55 +07:00
parent 88b85c9761
commit 90e1ea4d22
4 changed files with 36 additions and 11 deletions

View File

@ -3,21 +3,39 @@ namespace GraphQL\Error;
final class Warning final class Warning
{ {
static $supressWarnings = false; const NAME_WARNING = 1;
const ASSIGN_WARNING = 2;
const CONFIG_WARNING = 4;
const ALL = self::NAME_WARNING | self::ASSIGN_WARNING | self::CONFIG_WARNING;
static $enableWarnings = self::ALL;
static $warned = []; static $warned = [];
static function supress($set = true) static function suppress($suppress = true)
{ {
self::$supressWarnings = $set; if (true === $suppress) {
self::$enableWarnings = 0;
} else if (false === $suppress) {
self::$enableWarnings = self::ALL;
} else {
$suppress = (int) $suppress;
self::$enableWarnings &= ~$suppress;
}
} }
static function warnOnce($errorMessage, $errorId = null) static function warnOnce($errorMessage, $warningId)
{ {
$errorId = $errorId ?: $errorMessage; if ((self::$enableWarnings & $warningId) > 0 && !isset(self::$warned[$warningId])) {
self::$warned[$warningId] = true;
trigger_error($errorMessage, E_USER_WARNING);
}
}
if (!self::$supressWarnings && !isset(self::$warned[$errorId])) { static function warn($errorMessage, $warningId)
self::$warned[$errorId] = true; {
if ((self::$enableWarnings & $warningId) > 0) {
trigger_error($errorMessage, E_USER_WARNING); trigger_error($errorMessage, E_USER_WARNING);
} }
} }

View File

@ -2,6 +2,7 @@
namespace GraphQL\Type\Definition; namespace GraphQL\Type\Definition;
use GraphQL\Error\InvariantViolation; use GraphQL\Error\InvariantViolation;
use GraphQL\Error\Warning;
use GraphQL\Utils; use GraphQL\Utils;
/** /**
@ -127,7 +128,10 @@ class Config
if (!empty($unexpectedKeys)) { if (!empty($unexpectedKeys)) {
if (!self::$allowCustomOptions) { if (!self::$allowCustomOptions) {
trigger_error(sprintf('Error in "%s" type definition: Non-standard keys "%s" ' . $suffix, $typeName, implode(', ', $unexpectedKeys))); Warning::warnOnce(
sprintf('Error in "%s" type definition: Non-standard keys "%s" ' . $suffix, $typeName, implode(', ', $unexpectedKeys)),
Warning::CONFIG_WARNING
);
} }
$map = array_intersect_key($map, $definitions); $map = array_intersect_key($map, $definitions);
} }

View File

@ -34,7 +34,10 @@ class Utils
foreach ($vars as $key => $value) { foreach ($vars as $key => $value) {
if (!property_exists($obj, $key)) { if (!property_exists($obj, $key)) {
$cls = get_class($obj); $cls = get_class($obj);
trigger_error("Trying to set non-existing property '$key' on class '$cls'"); Warning::warn(
"Trying to set non-existing property '$key' on class '$cls'",
Warning::ASSIGN_WARNING
);
} }
$obj->{$key} = $value; $obj->{$key} = $value;
} }
@ -356,7 +359,7 @@ class Utils
'Name "'.$name.'" must not begin with "__", which is reserved by ' . 'Name "'.$name.'" must not begin with "__", which is reserved by ' .
'GraphQL introspection. In a future release of graphql this will ' . 'GraphQL introspection. In a future release of graphql this will ' .
'become an exception', 'become an exception',
'warnAboutDunder' Warning::NAME_WARNING
); );
} }

View File

@ -643,7 +643,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
['test' => Config::STRING] ['test' => Config::STRING]
); );
$this->fail('Expected exception not thrown'); $this->fail('Expected exception not thrown');
} catch (\PHPUnit_Framework_Error_Notice $e) { } catch (\PHPUnit_Framework_Error_Warning $e) {
$this->assertEquals( $this->assertEquals(
$this->typeError('Non-standard keys "test2" '), $this->typeError('Non-standard keys "test2" '),
$e->getMessage() $e->getMessage()