Eloquent\Phony\Hook\Exception\FunctionHookGenerationFailedException::__construct PHP Method

__construct() public method

Construct a mock generation failed exception.
public __construct ( string $functionName, callable $callback, string $source, array $error = null, Exceptio\Exception | Erro\Error | null $cause = null )
$functionName string The function name.
$callback callable The callback.
$source string The generated source code.
$error array
$cause Exceptio\Exception | Erro\Error | null The cause, if available.
    public function __construct($functionName, $callback, $source, array $error = null, $cause = null)
    {
        $this->functionName = $functionName;
        $this->callback = $callback;
        $this->source = $source;
        $this->error = $error;
        $lines = explode(PHP_EOL, $source);
        if (null === $error) {
            $message = sprintf('Function hook %s generation failed.%sRelevant lines:%%s', $functionName, PHP_EOL);
            $errorLineNumber = null;
        } else {
            $errorLineNumber = $error['line'];
            $startLine = $errorLineNumber - 4;
            $contextLineCount = 7;
            if ($startLine < 0) {
                $contextLineCount += $startLine;
                $startLine = 0;
            }
            $lines = array_slice($lines, $startLine, $contextLineCount, true);
            $message = sprintf('Function hook %s generation failed: ' . '%s in generated code on line %d.%s' . 'Relevant lines:%%s', $functionName, $error['message'], $errorLineNumber, PHP_EOL);
        }
        end($lines);
        $lineNumber = key($lines);
        $padSize = strlen($lineNumber + 1) + 4;
        $renderedLines = '';
        foreach ($lines as $lineNumber => $line) {
            if (null !== $errorLineNumber) {
                $highlight = $lineNumber + 1 === $errorLineNumber;
            } else {
                $highlight = false;
            }
            $renderedLines .= sprintf('%s%s%s %s', PHP_EOL, str_pad($lineNumber + 1, $padSize, ' ', STR_PAD_LEFT), $highlight ? ':' : ' ', $line);
        }
        parent::__construct(sprintf($message, $renderedLines), 0, $cause);
    }