Since this is getting a bit extensive for the comments, here's an answer.
Firstly, let me stress that only clear syntax is good syntax. Syntax that looks like one thing but does something else entirely is just confusing. One of the strengths of (good) LaTeX is that syntax always reflects meaning; \emph emphasizes text (no matter what this means typographically), \begin starts an environment, \end ends it.
Thus, while possible in theory, I would strongly discourage building a syntax that looks like two separate environments where the second environment is really an optional argument. Instead, there are some alternatives you could choose from. (I will from now on call the first environment head and the second one tail.)
You could really make the environments separate. If you want to pass some information from head to tail, make it available outside of head. This has the disadvantage of "leaking" macros and you have little control over where after head the user puts tail. It will however allow you to use the syntax you desire.
You could use the second environment inside the first. This makes sense semantically (tail is an optional part of head) and makes designing the environments rather easy, too (you can make tail and the information needed by it unavailable outside of head naturally by the grouping provided by \begin and end).
You could design a macro for use inside of head that marks everything up to \end{head} as part of tail. This is basically the same as the previous option with a slightly different syntax.
I would favor option 2 since it is the most straight forward.
Here's an example of all three options. I didn't dive deep into your link, but it seemed you wanted to store something for later output, so my head just typesets something and my tail collects its content with references to the corresponding head, which is then printed by \printtails.
All of the definitions could of course be improved depending on the use case, but I think the concept becomes clear here.
\documentclass{article}
\usepackage{etoolbox}
\usepackage{calc}
\usepackage{collect}
\usepackage{hyperref}
\makeatletter
\newcounter{head}
\newcounter{tail}
\def\@printtails{}
\def\printtails{\@printtails\def\@printtails{}}
\newenvironment{headA}[2]{%
\par\vskip 1em\noindent%
\refstepcounter{head}%
\protected@edef\@currentlabelname{#2}%
\label{#1}%
% make information public
\xdef\@lasthead@label{#1}%
\xdef\@lasthead@title{#1}%
\centering
{\Large\bfseries Head~\thehead : #2\par}%
}{%
\par\vskip 1em%
}
\newenvironment{headB}[2]{%
\par\vskip 1em\noindent%
\refstepcounter{head}%
\protected@edef\@currentlabelname{#2}%
\label{#1}%
% make {tail} available
\def\tailB{\@tailB{#1}{#2}}%
\let\endtailB\end@tailB
\centering
{\Large\bfseries Head~\thehead : #2\par}%
}{%
\par\vskip 1em%
}
\newenvironment{headC}[2]{%
\par\vskip 1em\noindent%
\refstepcounter{head}%
\protected@edef\@currentlabelname{#2}%
\label{#1}%
% reset the \end{tail} hook
\let\@maybeendtail\relax
% make {tail} available
\def\tailC{%
\let\@maybeendtail\end@tailB
\@tailB{#1}{#2}%
}%
\centering
{\Large\bfseries Head~\thehead : #2\par}%
}{%
% use the \end{tail} hook
\@maybeendtail
\par\vskip 1em%
}
\newenvironment{@printtail}[2]{%
\par\vskip 1em\noindent
\setcounter{tail}{\expandafter\expandafter\expandafter\@firstoffive\csname r@#1\endcsname-1}%
\refstepcounter{tail}%
\protected@edef\@currentlabelname{The Tail of Head~\ref*{#1} ({\slshape\nameref*{#1}})}%
\label{tailof:#1}%
\begin{tabular}{|p{\textwidth-2\tabcolsep}|}%
{\bfseries The Tail of Head~\ref{#1} ({\slshape\nameref{#1}})\quad}%
}{%
\end{tabular}%
\par\vskip 1em%
}
\newenvironment{tailA}{%
\xappto\@printtails{%
\noexpand\begin{@printtail}{\@lasthead@label}{\@lasthead@title}%
\expandafter\noexpand\csname @tailcontent@\@lasthead@label\endcsname
\noexpand\end{@printtail}%
}%
\collectinmacro\@temp@tailcontent{}{}%
}{%
\endcollectinmacro
\expandafter\expandafter\expandafter
\gdef
\expandafter\expandafter
\csname @tailcontent@\@lasthead@label\endcsname
\expandafter
{\@temp@tailcontent}%
}
\newenvironment{@tailB}[2]{%
\xappto\@printtails{%
\noexpand\begin{@printtail}{#1}{#2}%
\expandafter\noexpand\csname @tailcontent@#1\endcsname
\noexpand\end{@printtail}%
}%
\def\@temp@headlabel{#1}%
\collectinmacro\@temp@tailcontent{}{}%
}{%
\endcollectinmacro
\expandafter\expandafter\expandafter
\gdef
\expandafter\expandafter
\csname @tailcontent@\@temp@headlabel\endcsname
\expandafter
{\@temp@tailcontent}%
}
\makeatother
\begin{document}
Have a look at tail~\ref{tailof:head:lorem} with the title \nameref{tailof:head:lorem}.
Or maybe consider tail~\ref{tailof:head:die} with the title \nameref{tailof:head:die}.
\begin{headA}{head:wine}{Better Than Grapes}
In vino veritas.
\end{headA}
\begin{headA}{head:lorem}{This Is a Test}
Lorem ipsum dolor sit amet.
\end{headA}
\begin{tailA}
Well, it looks like Latin, but it doesn't actually mean anything, does it?
Anyhow, people won't care if we just use it as filler.
\end{tailA}
\begin{headB}{head:something-else}{Something Else}
Haec non sunt verba.
\begin{tailB}
Okay, then.
\end{tailB}
\end{headB}
\begin{headC}{head:die}{Roll the die}
Alea iacta est.
\tailC
Yes, I'm running out of sentences.
\end{headC}
\printtails
\end{document}

restatableandproofenvironments as desired already? – schtandard May 02 '19 at 10:49proofAtEndtoproof(while keeping them distinct), just make it available outside ofproofAtEnd. If you want to have one construct handling both, make the syntax reflect that. You could e.g. have one environment in which you can use an optional command to indicate that everything after belongs to the proof. Or just nest two environments. – schtandard May 02 '19 at 12:12