public function leaveContext(?Context $currentContext = null): void
    {
        if (!$this->context) {
            throw LeavingContextFailed::noContext();
        }

        // Not in expected context. Just quit.
        if ($currentContext && !$currentContext->match($this->context)) {
            return;
        }

        // Get last know context which was used before current context.
        $context = array_pop($this->contextStack);

        if ($context === null) {
            throw LeavingContextFailed::inContext($this->context);
        }

        $this->switchContext($context);
    }