/** * Constructor. * * @param string $expected Expected type. * @param mixed $actual Actual argument given. * @param int $number [optional] Argument number. * @param bool $hideCaller [optional] Should the function that has thrown this exception be hidden? Default: `false`. */ public function __construct($expected, $actual, $number = 1, $hideCaller = false) { $trace = Debugger::getPrettyTrace(debug_backtrace()); $type = Debugger::getType($actual); $type = $type === 'string' ? $type . ' ("' . StringUtils::truncate($actual, 50) . '")' : $type; if (!$hideCaller && isset($trace[1])) { $message = $trace[1]['function'] . ' expected argument ' . $number . ' to be ' . $expected . ', ' . $type . ' given.'; } else { $message = 'Expected argument ' . $number . ' to be ' . $expected . ', ' . $type . ' given.'; } parent::__construct($message); }
/** * Parses arguments of a method call. * * @param array $arguments Arguments. * * @return array */ private function parseCallArguments(array $arguments) { $args = []; foreach ($arguments as $argument) { if (is_object($argument)) { $args[] = '<comment>(object)</comment> ' . StringUtils::truncate(Debugger::getClass($argument), 32, '...', StringUtils::TRUNCATE_MIDDLE); } elseif (is_array($argument)) { $args[] = '<comment>(array)</comment> ' . StringUtils::truncate(json_encode($argument), 32, '...', StringUtils::TRUNCATE_MIDDLE); } elseif (is_string($argument)) { $args[] = sprintf("'%s'", StringUtils::truncate($argument, 32, '...', StringUtils::TRUNCATE_MIDDLE)); } else { $args[] = $argument; } } return implode(', ', $args); }
/** * @dataProvider provideTruncateStrings */ public function testTruncatePositions($string, $expected, $length, $position) { $this->assertEquals($expected, StringUtils::truncate($string, $length, '...', $position)); }