Skip to content

Commit 90e1ea4

Browse files
committed
Added tools for warnings with ability to suppress them
1 parent 88b85c9 commit 90e1ea4

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

src/Error/Warning.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,39 @@
33

44
final class Warning
55
{
6-
static $supressWarnings = false;
6+
const NAME_WARNING = 1;
7+
const ASSIGN_WARNING = 2;
8+
const CONFIG_WARNING = 4;
9+
10+
const ALL = self::NAME_WARNING | self::ASSIGN_WARNING | self::CONFIG_WARNING;
11+
12+
static $enableWarnings = self::ALL;
713

814
static $warned = [];
915

10-
static function supress($set = true)
16+
static function suppress($suppress = true)
1117
{
12-
self::$supressWarnings = $set;
18+
if (true === $suppress) {
19+
self::$enableWarnings = 0;
20+
} else if (false === $suppress) {
21+
self::$enableWarnings = self::ALL;
22+
} else {
23+
$suppress = (int) $suppress;
24+
self::$enableWarnings &= ~$suppress;
25+
}
1326
}
1427

15-
static function warnOnce($errorMessage, $errorId = null)
28+
static function warnOnce($errorMessage, $warningId)
1629
{
17-
$errorId = $errorId ?: $errorMessage;
30+
if ((self::$enableWarnings & $warningId) > 0 && !isset(self::$warned[$warningId])) {
31+
self::$warned[$warningId] = true;
32+
trigger_error($errorMessage, E_USER_WARNING);
33+
}
34+
}
1835

19-
if (!self::$supressWarnings && !isset(self::$warned[$errorId])) {
20-
self::$warned[$errorId] = true;
36+
static function warn($errorMessage, $warningId)
37+
{
38+
if ((self::$enableWarnings & $warningId) > 0) {
2139
trigger_error($errorMessage, E_USER_WARNING);
2240
}
2341
}

src/Type/Definition/Config.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace GraphQL\Type\Definition;
33

44
use GraphQL\Error\InvariantViolation;
5+
use GraphQL\Error\Warning;
56
use GraphQL\Utils;
67

78
/**
@@ -127,7 +128,10 @@ private static function validateMap($typeName, array $map, array $definitions, $
127128

128129
if (!empty($unexpectedKeys)) {
129130
if (!self::$allowCustomOptions) {
130-
trigger_error(sprintf('Error in "%s" type definition: Non-standard keys "%s" ' . $suffix, $typeName, implode(', ', $unexpectedKeys)));
131+
Warning::warnOnce(
132+
sprintf('Error in "%s" type definition: Non-standard keys "%s" ' . $suffix, $typeName, implode(', ', $unexpectedKeys)),
133+
Warning::CONFIG_WARNING
134+
);
131135
}
132136
$map = array_intersect_key($map, $definitions);
133137
}

src/Utils.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public static function assign($obj, array $vars, array $requiredKeys = [])
3434
foreach ($vars as $key => $value) {
3535
if (!property_exists($obj, $key)) {
3636
$cls = get_class($obj);
37-
trigger_error("Trying to set non-existing property '$key' on class '$cls'");
37+
Warning::warn(
38+
"Trying to set non-existing property '$key' on class '$cls'",
39+
Warning::ASSIGN_WARNING
40+
);
3841
}
3942
$obj->{$key} = $value;
4043
}
@@ -356,7 +359,7 @@ public static function assertValidName($name, $isIntrospection = false)
356359
'Name "'.$name.'" must not begin with "__", which is reserved by ' .
357360
'GraphQL introspection. In a future release of graphql this will ' .
358361
'become an exception',
359-
'warnAboutDunder'
362+
Warning::NAME_WARNING
360363
);
361364
}
362365

tests/Type/ConfigTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ public function testAllowCustomOptions()
643643
['test' => Config::STRING]
644644
);
645645
$this->fail('Expected exception not thrown');
646-
} catch (\PHPUnit_Framework_Error_Notice $e) {
646+
} catch (\PHPUnit_Framework_Error_Warning $e) {
647647
$this->assertEquals(
648648
$this->typeError('Non-standard keys "test2" '),
649649
$e->getMessage()

0 commit comments

Comments
 (0)