Skip to content

Commit

Permalink
Leverage PHP8's get_debug_type()
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Mar 16, 2020
1 parent 6e9eff3 commit fef50c9
Show file tree
Hide file tree
Showing 26 changed files with 49 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Argument/ReferenceSetArgumentTrait.php
Expand Up @@ -45,7 +45,7 @@ public function setValues(array $values)
{
foreach ($values as $k => $v) {
if (null !== $v && !$v instanceof Reference) {
throw new InvalidArgumentException(sprintf('A "%s" must hold only Reference instances, "%s" given.', __CLASS__, \is_object($v) ? \get_class($v) : \gettype($v)));
throw new InvalidArgumentException(sprintf('A "%s" must hold only Reference instances, "%s" given.', __CLASS__, get_debug_type($v)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Compiler/AbstractRecursivePass.php
Expand Up @@ -209,7 +209,7 @@ private function getExpressionLanguage(): ExpressionLanguage
$arg = $this->processValue(new Reference($id));
$this->inExpression = false;
if (!$arg instanceof Reference) {
throw new RuntimeException(sprintf('"%s::processValue()" must return a Reference when processing an expression, "%s" returned for service("%s").', static::class, \is_object($arg) ? \get_class($arg) : \gettype($arg), $id));
throw new RuntimeException(sprintf('"%s::processValue()" must return a Reference when processing an expression, "%s" returned for service("%s").', static::class, get_debug_type($arg), $id));
}
$arg = sprintf('"%s"', $arg);
}
Expand Down
2 changes: 1 addition & 1 deletion Compiler/CheckTypeDeclarationsPass.php
Expand Up @@ -265,7 +265,7 @@ private function checkType(Definition $checkedDefinition, $value, \ReflectionPar
$checkFunction = sprintf('is_%s', $parameter->getType()->getName());

if (!$parameter->getType()->isBuiltin() || !$checkFunction($value)) {
throw new InvalidParameterTypeException($this->currentId, \is_object($value) ? $class : \gettype($value), $parameter);
throw new InvalidParameterTypeException($this->currentId, \is_object($value) ? $class : get_debug_type($value), $parameter);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Compiler/MergeExtensionConfigurationPass.php
Expand Up @@ -169,15 +169,15 @@ public function __construct(ExtensionInterface $extension, ParameterBagInterface
*/
public function addCompilerPass(CompilerPassInterface $pass, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, int $priority = 0): self
{
throw new LogicException(sprintf('You cannot add compiler pass "%s" from extension "%s". Compiler passes must be registered before the container is compiled.', \get_class($pass), $this->extensionClass));
throw new LogicException(sprintf('You cannot add compiler pass "%s" from extension "%s". Compiler passes must be registered before the container is compiled.', get_debug_type($pass), $this->extensionClass));
}

/**
* {@inheritdoc}
*/
public function registerExtension(ExtensionInterface $extension)
{
throw new LogicException(sprintf('You cannot register extension "%s" from "%s". Extensions must be registered before the container is compiled.', \get_class($extension), $this->extensionClass));
throw new LogicException(sprintf('You cannot register extension "%s" from "%s". Extensions must be registered before the container is compiled.', get_debug_type($extension), $this->extensionClass));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Compiler/PriorityTaggedServiceTrait.php
Expand Up @@ -125,7 +125,7 @@ public static function getDefaultIndex(ContainerBuilder $container, string $serv
$defaultIndex = $rm->invoke(null);

if (!\is_string($defaultIndex)) {
throw new InvalidArgumentException(sprintf('Either method "%s::%s()" should return a string (got "%s") or tag "%s" on service "%s" is missing attribute "%s".', $class, $defaultIndexMethod, \gettype($defaultIndex), $tagName, $serviceId, $indexAttribute));
throw new InvalidArgumentException(sprintf('Either method "%s::%s()" should return a string (got "%s") or tag "%s" on service "%s" is missing attribute "%s".', $class, $defaultIndexMethod, get_debug_type($defaultIndex), $tagName, $serviceId, $indexAttribute));
}

return $defaultIndex;
Expand Down Expand Up @@ -154,7 +154,7 @@ public static function getDefaultPriority(ContainerBuilder $container, string $s
$defaultPriority = $rm->invoke(null);

if (!\is_int($defaultPriority)) {
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should return an integer (got "%s") or tag "%s" on service "%s" is missing attribute "priority".', $class, $defaultPriorityMethod, \gettype($defaultPriority), $tagName, $serviceId));
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should return an integer (got "%s") or tag "%s" on service "%s" is missing attribute "priority".', $class, $defaultPriorityMethod, get_debug_type($defaultPriority), $tagName, $serviceId));
}

return $defaultPriority;
Expand Down
2 changes: 1 addition & 1 deletion Compiler/RegisterServiceSubscribersPass.php
Expand Up @@ -71,7 +71,7 @@ protected function processValue($value, bool $isRoot = false)

foreach ($class::getSubscribedServices() as $key => $type) {
if (!\is_string($type) || !preg_match('/^\??[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $type)) {
throw new InvalidArgumentException(sprintf('"%s::getSubscribedServices()" must return valid PHP types for service "%s" key "%s", "%s" returned.', $class, $this->currentId, $key, \is_string($type) ? $type : \gettype($type)));
throw new InvalidArgumentException(sprintf('"%s::getSubscribedServices()" must return valid PHP types for service "%s" key "%s", "%s" returned.', $class, $this->currentId, $key, \is_string($type) ? $type : get_debug_type($type)));
}
if ($optionalBehavior = '?' === $type[0]) {
$type = substr($type, 1);
Expand Down
2 changes: 1 addition & 1 deletion Compiler/ResolveBindingsPass.php
Expand Up @@ -134,7 +134,7 @@ protected function processValue($value, bool $isRoot = false)
}

if (null !== $bindingValue && !$bindingValue instanceof Reference && !$bindingValue instanceof Definition && !$bindingValue instanceof TaggedIteratorArgument && !$bindingValue instanceof ServiceLocatorArgument) {
throw new InvalidArgumentException(sprintf('Invalid value for binding key "%s" for service "%s": expected null, "%s", "%s", "%s" or ServiceLocatorArgument, "%s" given.', $key, $this->currentId, Reference::class, Definition::class, TaggedIteratorArgument::class, \gettype($bindingValue)));
throw new InvalidArgumentException(sprintf('Invalid value for binding key "%s" for service "%s": expected null, "%s", "%s", "%s" or ServiceLocatorArgument, "%s" given.', $key, $this->currentId, Reference::class, Definition::class, TaggedIteratorArgument::class, get_debug_type($bindingValue)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Compiler/ResolveNamedArgumentsPass.php
Expand Up @@ -76,7 +76,7 @@ protected function processValue($value, bool $isRoot = false)
}

if (null !== $argument && !$argument instanceof Reference && !$argument instanceof Definition) {
throw new InvalidArgumentException(sprintf('Invalid service "%s": the value of argument "%s" of method "%s()" must be null, an instance of "%s" or an instance of "%s", "%s" given.', $this->currentId, $key, $class !== $this->currentId ? $class.'::'.$method : $method, Reference::class, Definition::class, \gettype($argument)));
throw new InvalidArgumentException(sprintf('Invalid service "%s": the value of argument "%s" of method "%s()" must be null, an instance of "%s" or an instance of "%s", "%s" given.', $this->currentId, $key, $class !== $this->currentId ? $class.'::'.$method : $method, Reference::class, Definition::class, get_debug_type($argument)));
}

$typeFound = false;
Expand Down
4 changes: 2 additions & 2 deletions Compiler/ServiceLocatorTagPass.php
Expand Up @@ -59,7 +59,7 @@ protected function processValue($value, bool $isRoot = false)
continue;
}
if (!$v instanceof Reference) {
throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set, "%s" found for key "%s".', $this->currentId, \is_object($v) ? \get_class($v) : \gettype($v), $k));
throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": an array of references is expected as first argument when the "container.service_locator" tag is set, "%s" found for key "%s".', $this->currentId, get_debug_type($v), $k));
}

if ($i === $k) {
Expand Down Expand Up @@ -98,7 +98,7 @@ public static function register(ContainerBuilder $container, array $refMap, stri
{
foreach ($refMap as $id => $ref) {
if (!$ref instanceof Reference) {
throw new InvalidArgumentException(sprintf('Invalid service locator definition: only services can be referenced, "%s" found for key "%s". Inject parameter values using constructors instead.', \is_object($ref) ? \get_class($ref) : \gettype($ref), $id));
throw new InvalidArgumentException(sprintf('Invalid service locator definition: only services can be referenced, "%s" found for key "%s". Inject parameter values using constructors instead.', get_debug_type($ref), $id));
}
$refMap[$id] = new ServiceClosureArgument($ref);
}
Expand Down
16 changes: 1 addition & 15 deletions Compiler/ValidateEnvPlaceholdersPass.php
Expand Up @@ -52,7 +52,7 @@ public function process(ContainerBuilder $container)
$values = [];
if (false === $i = strpos($env, ':')) {
$default = $defaultBag->has("env($env)") ? $defaultBag->get("env($env)") : self::$typeFixtures['string'];
$defaultType = null !== $default ? self::getType($default) : 'string';
$defaultType = null !== $default ? get_debug_type($default) : 'string';
$values[$defaultType] = $default;
} else {
$prefix = substr($env, 0, $i);
Expand Down Expand Up @@ -99,18 +99,4 @@ public function getExtensionConfig(): array
$this->extensionConfig = [];
}
}

private static function getType($value): string
{
switch ($type = \gettype($value)) {
case 'boolean':
return 'bool';
case 'double':
return 'float';
case 'integer':
return 'int';
}

return $type;
}
}
4 changes: 2 additions & 2 deletions ContainerBuilder.php
Expand Up @@ -1131,7 +1131,7 @@ private function createService(Definition $definition, array &$inlineServices, b
}

if (!\is_callable($callable)) {
throw new InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', \get_class($service)));
throw new InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', get_debug_type($service)));
}

$callable($service);
Expand Down Expand Up @@ -1390,7 +1390,7 @@ public function resolveEnvPlaceholders($value, $format = null, array &$usedEnvs
$completed = true;
} else {
if (!\is_string($resolved) && !is_numeric($resolved)) {
throw new RuntimeException(sprintf('A string value must be composed of strings and/or numbers, but found parameter "env(%s)" of type "%s" inside string value "%s".', $env, \gettype($resolved), $this->resolveEnvPlaceholders($value)));
throw new RuntimeException(sprintf('A string value must be composed of strings and/or numbers, but found parameter "env(%s)" of type "%s" inside string value "%s".', $env, get_debug_type($resolved), $this->resolveEnvPlaceholders($value)));
}
$value = str_ireplace($placeholder, $resolved, $value);
}
Expand Down
2 changes: 1 addition & 1 deletion Dumper/PhpDumper.php
Expand Up @@ -1490,7 +1490,7 @@ private function exportParameters(array $parameters, string $path = '', int $ind
if (\is_array($value)) {
$value = $this->exportParameters($value, $path.'/'.$key, $indent + 4);
} elseif ($value instanceof ArgumentInterface) {
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain special arguments. "%s" found in "%s".', \get_class($value), $path.'/'.$key));
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain special arguments. "%s" found in "%s".', get_debug_type($value), $path.'/'.$key));
} elseif ($value instanceof Variable) {
throw new InvalidArgumentException(sprintf('You cannot dump a container with parameters that contain variable references. Variable "%s" found in "%s".', $value, $path.'/'.$key));
} elseif ($value instanceof Definition) {
Expand Down
2 changes: 1 addition & 1 deletion Dumper/YamlDumper.php
Expand Up @@ -264,7 +264,7 @@ private function dumpValue($value)
} elseif ($value instanceof ServiceLocatorArgument) {
$tag = 'service_locator';
} else {
throw new RuntimeException(sprintf('Unspecified Yaml tag for type "%s".', \get_class($value)));
throw new RuntimeException(sprintf('Unspecified Yaml tag for type "%s".', get_debug_type($value)));
}

return new TaggedValue($tag, $this->dumpValue($value->getValues()));
Expand Down
6 changes: 3 additions & 3 deletions EnvVarProcessor.php
Expand Up @@ -79,7 +79,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
}

if (!isset($array[$key]) && !\array_key_exists($key, $array)) {
throw new EnvNotFoundException(sprintf('Key "%s" not found in "%s" (resolved from "%s").', $key, json_encode($array), $next));
throw new EnvNotFoundException(sprintf('Key "%s" not found in %s (resolved from "%s").', $key, json_encode($array), $next));
}

return $array[$key];
Expand Down Expand Up @@ -231,7 +231,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
}

if (null !== $env && !\is_array($env)) {
throw new RuntimeException(sprintf('Invalid JSON env var "%s": array or null expected, "%s" given.', $name, \gettype($env)));
throw new RuntimeException(sprintf('Invalid JSON env var "%s": array or null expected, "%s" given.', $name, get_debug_type($env)));
}

return $env;
Expand Down Expand Up @@ -275,7 +275,7 @@ public function getEnv(string $prefix, string $name, \Closure $getEnv)
}
$value = $this->container->getParameter($match[1]);
if (!is_scalar($value)) {
throw new RuntimeException(sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, \gettype($value)));
throw new RuntimeException(sprintf('Parameter "%s" found when resolving env var "%s" must be scalar, "%s" given.', $match[1], $name, get_debug_type($value)));
}

return $value;
Expand Down
2 changes: 1 addition & 1 deletion Loader/Configurator/AbstractConfigurator.php
Expand Up @@ -91,6 +91,6 @@ public static function processValue($value, $allowServices = false)
}
}

throw new InvalidArgumentException(sprintf('Cannot use values of type "%s" in service configuration files.', \is_object($value) ? \get_class($value) : \gettype($value)));
throw new InvalidArgumentException(sprintf('Cannot use values of type "%s" in service configuration files.', get_debug_type($value)));
}
}
4 changes: 2 additions & 2 deletions Loader/FileLoader.php
Expand Up @@ -57,7 +57,7 @@ public function import($resource, $type = null, $ignoreErrors = false, $sourceRe
if ($ignoreNotFound = 'not_found' === $ignoreErrors) {
$args[2] = false;
} elseif (!\is_bool($ignoreErrors)) {
throw new \TypeError(sprintf('Invalid argument $ignoreErrors provided to "%s::import()": boolean or "not_found" expected, "%s" given.', static::class, \gettype($ignoreErrors)));
throw new \TypeError(sprintf('Invalid argument $ignoreErrors provided to "%s::import()": boolean or "not_found" expected, "%s" given.', static::class, get_debug_type($ignoreErrors)));
}

try {
Expand Down Expand Up @@ -143,7 +143,7 @@ protected function setDefinition($id, Definition $definition)

if ($this->isLoadingInstanceof) {
if (!$definition instanceof ChildDefinition) {
throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, \get_class($definition)));
throw new InvalidArgumentException(sprintf('Invalid type definition "%s": ChildDefinition expected, "%s" given.', $id, get_debug_type($definition)));
}
$this->instanceof[$id] = $definition;
} else {
Expand Down
2 changes: 1 addition & 1 deletion Loader/XmlFileLoader.php
Expand Up @@ -596,7 +596,7 @@ public function validateSchema(\DOMDocument $dom)
$path = str_replace([$ns, str_replace('http://', 'https://', $ns)], str_replace('\\', '/', $extension->getXsdValidationBasePath()).'/', $items[$i + 1]);

if (!is_file($path)) {
throw new RuntimeException(sprintf('Extension "%s" references a non-existent XSD file "%s".', \get_class($extension), $path));
throw new RuntimeException(sprintf('Extension "%s" references a non-existent XSD file "%s".', get_debug_type($extension), $path));
}

$schemaLocations[$items[$i]] = $path;
Expand Down
8 changes: 4 additions & 4 deletions Loader/YamlFileLoader.php
Expand Up @@ -213,7 +213,7 @@ private function parseDefinitions(array $content, string $file)
unset($content['services']['_instanceof']);

if (!\is_array($instanceof)) {
throw new InvalidArgumentException(sprintf('Service "_instanceof" key must be an array, "%s" given in "%s".', \gettype($instanceof), $file));
throw new InvalidArgumentException(sprintf('Service "_instanceof" key must be an array, "%s" given in "%s".', get_debug_type($instanceof), $file));
}
$this->instanceof = [];
$this->isLoadingInstanceof = true;
Expand Down Expand Up @@ -247,7 +247,7 @@ private function parseDefaults(array &$content, string $file): array
unset($content['services']['_defaults']);

if (!\is_array($defaults)) {
throw new InvalidArgumentException(sprintf('Service "_defaults" key must be an array, "%s" given in "%s".', \gettype($defaults), $file));
throw new InvalidArgumentException(sprintf('Service "_defaults" key must be an array, "%s" given in "%s".', get_debug_type($defaults), $file));
}

foreach ($defaults as $key => $default) {
Expand Down Expand Up @@ -339,7 +339,7 @@ private function parseDefinition(string $id, $service, string $file, array $defa
}

if (!\is_array($service)) {
throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but "%s" found for service "%s" in "%s". Check your YAML syntax.', \gettype($service), $id, $file));
throw new InvalidArgumentException(sprintf('A service definition must be an array or a string starting with "@" but "%s" found for service "%s" in "%s". Check your YAML syntax.', get_debug_type($service), $id, $file));
}

$this->checkDefinition($id, $service, $file);
Expand Down Expand Up @@ -465,7 +465,7 @@ private function parseDefinition(string $id, $service, string $file, array $defa

foreach ($service['calls'] as $k => $call) {
if (!\is_array($call) && (!\is_string($k) || !$call instanceof TaggedValue)) {
throw new InvalidArgumentException(sprintf('Invalid method call for service "%s": expected map or array, "%s" given in "%s".', $id, $call instanceof TaggedValue ? '!'.$call->getTag() : \gettype($call), $file));
throw new InvalidArgumentException(sprintf('Invalid method call for service "%s": expected map or array, "%s" given in "%s".', $id, $call instanceof TaggedValue ? '!'.$call->getTag() : get_debug_type($call), $file));
}

if (\is_string($k)) {
Expand Down

0 comments on commit fef50c9

Please sign in to comment.