0

Is there a way to convert any value especially boolean and arrays into strings? I want to do this for a cleaner error message in a function. Right now all I can get in this example below it this for an error message

Fatal error: Uncaught InvalidArgumentException: $argument cannot be in /file.php:## Stack trace: #0 /file.php:(##): myFunction(false) #1 {main} thrown in /file.php on line 47

I want it to say "$argument cannot be false" not "$argument cannot be "

<?php

myFunction(false);
myFunction(array('a', 'b'));

//Version 0.5
function myFunction($argument) {
    if (!is_string($argument)) {
        throw new InvalidArgumentException('$argument cannot be ' . strval($argument));
    }

    //...
}

?>
8
  • 1
    Would declaring a type be appropriate for your situation? function (string $argument) Commented Jul 31, 2020 at 18:04
  • You used strval($string) but nowhere declared $string? Commented Jul 31, 2020 at 18:05
  • (string)true === '1' and (string)false === ''. Welcome to PHP. Commented Jul 31, 2020 at 18:06
  • 2
    However, you can json_encode($argument) or gettype($argument) for a textual depiction or description of the value or type. Commented Jul 31, 2020 at 18:06
  • Typo: $argeument vs $arguement? Also swap '...'pair to a "...." pair? Commented Jul 31, 2020 at 18:07

3 Answers 3

1

Best way to convert most value types to string is using json_encode.

An array for example would become ["a","b"].

Just json_encode($argument)

Sign up to request clarification or add additional context in comments.

2 Comments

Note, this works for serializable content to some degree (a large array might be counter-productive to include). I would still recommend a type declaration for the function argument, enabling static checking for many scenarios, and omit defensive programming, a different discussion than the question asks..
Thank you, this does the job.
0

I would use gettype and/or var_export:

throw new InvalidArgumentException('$argeument cannot be ' . gettype($argeument) . ' ' . var_export($argeument, true));

Or buffer output of var_dump and you can see it all:

ob_start();
var_dump($argeument);
$text = ob_get_clean();
throw new InvalidArgumentException('$argeument cannot be ' . $text);

Comments

0

Perhaps (in lieu of function (string $value), which I recommend)?

class InvalidArgumentType extends InvalidArgumentException
{
    public function __construct(int $position, string $requiredType, $value)
    {
        $encoded = json_encode($value);
        
        if (strlen($encoded) > 10) {
            $encoded = sprintf('%s ... %s', substr($encoded, 0, 10), substr($encoded, -1));
        }
        
        $inferredType = is_object($value) ? get_class($value) : gettype($value);
        
        parent::__construct(sprintf(
            'Argument %d value %s should be %s but is %s.',
            $position,
            $encoded,
            $requiredType,
            $inferredType
        ));
    }
}

class Foo {}

echo (new InvalidArgumentType(1, 'string', new Foo()))->getMessage().PHP_EOL;
echo (new InvalidArgumentType(2, 'string', [1,2,3,4,5,6,7,8,9,10]))->getMessage().PHP_EOL;
echo (new InvalidArgumentType(3, 'string', 123))->getMessage().PHP_EOL;
echo (new InvalidArgumentType(2, 'string', null))->getMessage();

Gives:

Argument 1 value {} should be string but is Foo.
Argument 2 value [1,2,3,4,5 ... ] should be string but is array.
Argument 3 value 123 should be string but is integer.
Argument 2 value null should be string but is NULL.

https://3v4l.org/ko7o1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.