diff --git a/src/Error/Warning.php b/src/Error/Warning.php index 0d2e299..1ebf329 100644 --- a/src/Error/Warning.php +++ b/src/Error/Warning.php @@ -3,21 +3,39 @@ namespace GraphQL\Error; 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 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])) { - self::$warned[$errorId] = true; + static function warn($errorMessage, $warningId) + { + if ((self::$enableWarnings & $warningId) > 0) { trigger_error($errorMessage, E_USER_WARNING); } } diff --git a/src/Type/Definition/Config.php b/src/Type/Definition/Config.php index 9cc8fa5..1ddcda1 100644 --- a/src/Type/Definition/Config.php +++ b/src/Type/Definition/Config.php @@ -2,6 +2,7 @@ namespace GraphQL\Type\Definition; use GraphQL\Error\InvariantViolation; +use GraphQL\Error\Warning; use GraphQL\Utils; /** @@ -127,7 +128,10 @@ class Config if (!empty($unexpectedKeys)) { 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); } diff --git a/src/Utils.php b/src/Utils.php index ce3ec61..252c9c0 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -34,7 +34,10 @@ class Utils foreach ($vars as $key => $value) { if (!property_exists($obj, $key)) { $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; } @@ -356,7 +359,7 @@ class Utils 'Name "'.$name.'" must not begin with "__", which is reserved by ' . 'GraphQL introspection. In a future release of graphql this will ' . 'become an exception', - 'warnAboutDunder' + Warning::NAME_WARNING ); } diff --git a/tests/Type/ConfigTest.php b/tests/Type/ConfigTest.php index 103c5fb..8ab3622 100644 --- a/tests/Type/ConfigTest.php +++ b/tests/Type/ConfigTest.php @@ -643,7 +643,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase ['test' => Config::STRING] ); $this->fail('Expected exception not thrown'); - } catch (\PHPUnit_Framework_Error_Notice $e) { + } catch (\PHPUnit_Framework_Error_Warning $e) { $this->assertEquals( $this->typeError('Non-standard keys "test2" '), $e->getMessage()