From 0da1a2e17d921dc27ce9afa76b123cb4c8b73b17 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 28 May 2020 11:07:51 -0700 Subject: [PATCH] Allow PhutilArrayCheck to accept a list of objects as a context Summary: This simplifies merging a list-of-lists, which is a common pattern when asking a list of extensions to each provide some kind of list of items. Test Plan: Used elsewhere in Piledriver. Differential Revision: https://secure.phabricator.com/D21294 --- src/utils/PhutilArrayCheck.php | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/utils/PhutilArrayCheck.php b/src/utils/PhutilArrayCheck.php index 0cefc637..1b8ce131 100644 --- a/src/utils/PhutilArrayCheck.php +++ b/src/utils/PhutilArrayCheck.php @@ -29,10 +29,24 @@ final class PhutilArrayCheck } public function setContext($object, $method) { - if (!is_object($object) && !is_string($object)) { + if (is_array($object)) { + foreach ($object as $idx => $value) { + if (!is_object($value)) { + throw new Exception( + pht( + 'Expected an object, string, or list of objects for "object" '. + 'context. Got a list ("%s"), but the list item at index '. + '"%s" (with type "%s") is not an object.', + phutil_describe_type($object), + $idx, + phutil_describe_type($value))); + } + } + } else if (!is_object($object) && !is_string($object)) { throw new Exception( pht( - 'Expected an object or string for "object" context, got "%s".', + 'Expected an object, string, or list of objects for "object" '. + 'context, got "%s".', phutil_describe_type($object))); } @@ -219,7 +233,20 @@ final class PhutilArrayCheck $method = $context['method']; $argv = $context['argv']; - if (is_object($object)) { + if (is_array($object)) { + $classes = array(); + foreach ($object as $item) { + $classes[] = get_class($item); + } + $classes = array_fuse($classes); + $n = count($object); + + $object_display = sprintf( + '[%s]<%d>->%s', + implode(', ', $classes), + $n, + $method); + } else if (is_object($object)) { $object_display = sprintf( '%s->%s', get_class($object),