diff --git a/docs/schema-definition.md b/docs/schema-definition.md index e3c74b5ed..35b7c2b70 100644 --- a/docs/schema-definition.md +++ b/docs/schema-definition.md @@ -171,6 +171,8 @@ final class Types return self::$types[$typeName]; } + // For every type, this class must define a method with the same name + // but the first letter is in lower case. $methodName = match ($typeName) { 'ID' => 'id', default => lcfirst($typeName), @@ -190,8 +192,11 @@ final class Types /** @return Type&NamedType */ private static function byClassName(string $className): Type { - $parts = \explode('\\', $className); - $typeName = \preg_replace('~Type$~', '', $parts[\count($parts) - 1]); + $classNameParts = explode('\\', $className); + $baseClassName = end($classNameParts); + // All type classes must use the suffix Type. + // This prevents name collisions between types and PHP keywords. + $typeName = preg_replace('~Type$~', '', $baseClassName); // Type loading is very similar to PHP class loading, but keep in mind // that the **typeLoader** must always return the same instance of a type. diff --git a/examples/01-blog/Blog/Types.php b/examples/01-blog/Blog/Types.php index d9451a4f8..d1c108458 100644 --- a/examples/01-blog/Blog/Types.php +++ b/examples/01-blog/Blog/Types.php @@ -40,6 +40,8 @@ public static function load(string $typeName): Type return self::$types[$typeName]; } + // For every type, this class must define a method with the same name + // but the first letter is in lower case. switch ($typeName) { case 'ID': $methodName = 'id'; @@ -66,9 +68,11 @@ public static function load(string $typeName): Type */ private static function byClassName(string $className): Type { - $parts = \explode('\\', $className); - - $typeName = \preg_replace('~Type$~', '', $parts[\count($parts) - 1]); + $classNameParts = \explode('\\', $className); + $baseClassName = end($classNameParts); + // All type classes must use the suffix Type. + // This prevents name collisions between types and PHP keywords. + $typeName = \preg_replace('~Type$~', '', $baseClassName); assert(is_string($typeName), 'regex is statically known to be correct'); // Type loading is very similar to PHP class loading, but keep in mind