\documentclass{article}
\usepackage{ulem}
\usepackage{graphicx}
\usepackage{hyperref}
\pagestyle{headings}
\begin{document}
\part{Instantiate Application Object Refactoring}
This is a refactoring.  It is informed by some experience, but is by no means complete.

\section{Intent}
Remove coupling with global state to make application reusable in different contexts.

\section{Motivation}
\begin{itemize}
\item Allows for subclassing applications, which benefits testing


\item Provides for multiple, differently-configured applications, desirable for virtual hosting environments


\item Aesthetics, ease of understanding


\end{itemize}
\section{Mechanics}
Presuming you are working on an app named "flarg":

\begin{itemize}
\item If there is a global \texttt{\$flarg} variable which is an instance of some kind of driver, rename it to \texttt{\$flarg\_driver}


\item In \texttt{lib/base.php}, create a global \texttt{\$flarg} which is an instance of \texttt{Flarg::}


\item For each static method in \texttt{Flarg::}
\begin{itemize}
\item Find all calls of the method and replace \texttt{Flarg::method()} with \texttt{\$flarg->method()}.  (\textbf{Possible exception:} if the method is truly static, meaning it will always produce the same output given the same parameters, you might want to keep it static.)


\end{itemize}

\item For each global variable (\texttt{\$flarg\_driver} is used as an example):
\begin{itemize}
\item Add a \texttt{getDriver()} method to \texttt{Flarg::}


\item Make \texttt{getDriver()} return the global variable's value


\item Find references to the global variable, and replace each with a call to \texttt{\$flarg->getDriver()}


\item When all references are replaced, do \textbf{one} of the following:
\begin{itemize}
\item Move initialization code for the value into the method


\item Introduce a field, move initialization code into constructor


\item Introduce a field which defaults to \texttt{null}, move initialization code into getter, but only execute when the field is \texttt{null} (Note, beware of introducing cycles.)


\end{itemize}

\end{itemize}

\end{itemize}
When application class gets too heavy to work with, you can use <a href="https://wiki.horde.org/ExtractClass">ExtractClass</a> and/or <a href="https://wiki.horde.org/MoveMethod">MoveMethod</a> to split up responsibilities.

Ideally, we will eventually end up with an application object which takes all configuration as constructor parameters, does all of the application's work (probably most via delegation - <a href="https://wiki.horde.org/FacadePattern">FacadePattern</a>), but does not contain any UI code.  Ideal for use from command-line scripts and unit tests.

\section{TODO}
\begin{itemize}
\item Open discussion from Horde developers


\item Check for core developer support for this pattern.


\item When/how to write tests and run tests for this refactoring?


\item "Motivation" section needs some work.


\item ...


\end{itemize}
\end{document}
