| Total Complexity | 40 |
| Total Lines | 278 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 0 | Features | 1 |
Complex classes like Mailcode_Factory often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Mailcode_Factory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Mailcode_Factory |
||
| 22 | { |
||
| 23 | const ERROR_INVALID_COMMAND_CREATED = 50001; |
||
| 24 | const ERROR_UNEXPECTED_COMMAND_TYPE = 50002; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Mailcode_Factory_CommandSets |
||
| 28 | */ |
||
| 29 | private static $commandSets; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Creates a ShowVariable command. |
||
| 33 | * |
||
| 34 | * @param string $variableName A variable name, with or without the $ sign prepended. |
||
| 35 | * @return Mailcode_Commands_Command_ShowVariable |
||
| 36 | */ |
||
| 37 | public static function showVar(string $variableName) : Mailcode_Commands_Command_ShowVariable |
||
| 38 | { |
||
| 39 | return self::$commandSets->show()->showVar($variableName); |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Creates a ShowDate command, used to display date variables and |
||
| 44 | * format the date using date format strings. |
||
| 45 | * |
||
| 46 | * @param string $variableName A variable name, with or without the $ sign prepended. |
||
| 47 | * @param string $formatString A date format string, or empty string for default. |
||
| 48 | * @return Mailcode_Commands_Command_ShowDate |
||
| 49 | */ |
||
| 50 | public static function showDate(string $variableName, string $formatString="") : Mailcode_Commands_Command_ShowDate |
||
| 51 | { |
||
| 52 | return self::$commandSets->show()->showDate($variableName, $formatString); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Creates a ShowSnippet command. |
||
| 57 | * |
||
| 58 | * @param string $snippetName A snippet name, with or without the $ sign prepended. |
||
| 59 | * @return Mailcode_Commands_Command_ShowSnippet |
||
| 60 | */ |
||
| 61 | public static function showSnippet(string $snippetName) : Mailcode_Commands_Command_ShowSnippet |
||
| 62 | { |
||
| 63 | return self::$commandSets->show()->showSnippet($snippetName); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Creates a SetVariable command. |
||
| 68 | * |
||
| 69 | * @param string $variableName A variable name, with or without the $ sign prepended. |
||
| 70 | * @param string $value |
||
| 71 | * @param bool $quoteValue Whether to treat the value as a string literal, and add quotes to it. |
||
| 72 | * @return Mailcode_Commands_Command_SetVariable |
||
| 73 | * @throws Mailcode_Factory_Exception |
||
| 74 | * |
||
| 75 | * @see Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED |
||
| 76 | */ |
||
| 77 | public static function setVar(string $variableName, string $value, bool $quoteValue=true) : Mailcode_Commands_Command_SetVariable |
||
| 78 | { |
||
| 79 | return self::$commandSets->set()->setVar($variableName, $value, $quoteValue); |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Like setVar(), but treats the value as a string literal |
||
| 84 | * and automatically adds quotes to it. |
||
| 85 | * |
||
| 86 | * @param string $variableName |
||
| 87 | * @param string $value |
||
| 88 | * @return Mailcode_Commands_Command_SetVariable |
||
| 89 | */ |
||
| 90 | public static function setVarString(string $variableName, string $value) : Mailcode_Commands_Command_SetVariable |
||
| 91 | { |
||
| 92 | return self::$commandSets->set()->setVar($variableName, $value, true); |
||
| 93 | } |
||
| 94 | |||
| 95 | public static function comment(string $comments) : Mailcode_Commands_Command_Comment |
||
| 96 | { |
||
| 97 | return self::$commandSets->misc()->comment($comments); |
||
| 98 | } |
||
| 99 | |||
| 100 | public static function else() : Mailcode_Commands_Command_Else |
||
| 101 | { |
||
| 102 | return self::$commandSets->if()->else(); |
||
| 103 | } |
||
| 104 | |||
| 105 | public static function end() : Mailcode_Commands_Command_End |
||
| 106 | { |
||
| 107 | return self::$commandSets->if()->end(); |
||
| 108 | } |
||
| 109 | |||
| 110 | public static function if(string $condition, string $type='') : Mailcode_Commands_Command_If |
||
| 111 | { |
||
| 112 | return self::$commandSets->if()->if($condition, $type); |
||
| 113 | } |
||
| 114 | |||
| 115 | public static function ifVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
||
| 116 | { |
||
| 117 | return self::$commandSets->if()->ifVar($variable, $operand, $value, $quoteValue); |
||
| 118 | } |
||
| 119 | |||
| 120 | public static function ifVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_If_Variable |
||
| 121 | { |
||
| 122 | return self::$commandSets->if()->ifVarString($variable, $operand, $value); |
||
| 123 | } |
||
| 124 | |||
| 125 | public static function ifVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
||
| 126 | { |
||
| 127 | return self::$commandSets->if()->ifVarEquals($variable, $value, $quoteValue); |
||
| 128 | } |
||
| 129 | |||
| 130 | public static function ifVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If |
||
| 131 | { |
||
| 132 | return self::$commandSets->if()->ifVarEqualsString($variable, $value); |
||
| 133 | } |
||
| 134 | |||
| 135 | public static function ifVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
||
| 136 | { |
||
| 137 | return self::$commandSets->if()->ifVarNotEquals($variable, $value, $quoteValue); |
||
| 138 | } |
||
| 139 | |||
| 140 | public static function ifVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If_Variable |
||
| 141 | { |
||
| 142 | return self::$commandSets->if()->ifVarNotEqualsString($variable, $value); |
||
| 143 | } |
||
| 144 | |||
| 145 | public static function elseIf(string $condition, string $type='') : Mailcode_Commands_Command_ElseIf |
||
| 146 | { |
||
| 147 | return self::$commandSets->elseIf()->elseIf($condition, $type); |
||
| 148 | } |
||
| 149 | |||
| 150 | public static function elseIfEmpty(string $variable) : Mailcode_Commands_Command_ElseIf_Empty |
||
| 151 | { |
||
| 152 | return self::$commandSets->elseIf()->elseIfEmpty($variable); |
||
| 153 | } |
||
| 154 | |||
| 155 | public static function elseIfNotEmpty(string $variable) : Mailcode_Commands_Command_ElseIf_NotEmpty |
||
| 156 | { |
||
| 157 | return self::$commandSets->elseIf()->elseIfNotEmpty($variable); |
||
| 158 | } |
||
| 159 | |||
| 160 | public static function elseIfVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 161 | { |
||
| 162 | return self::$commandSets->elseIf()->elseIfVar($variable, $operand, $value, $quoteValue); |
||
| 163 | } |
||
| 164 | |||
| 165 | public static function elseIfVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 166 | { |
||
| 167 | return self::$commandSets->elseIf()->elseIfVarString($variable, $operand, $value); |
||
| 168 | } |
||
| 169 | |||
| 170 | public static function elseIfVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 171 | { |
||
| 172 | return self::$commandSets->elseIf()->elseIfVarEquals($variable, $value, $quoteValue); |
||
| 173 | } |
||
| 174 | |||
| 175 | public static function elseIfVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 178 | } |
||
| 179 | |||
| 180 | public static function elseIfVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 181 | { |
||
| 182 | return self::$commandSets->elseIf()->elseIfVarNotEquals($variable, $value, $quoteValue); |
||
| 183 | } |
||
| 184 | |||
| 185 | public static function elseIfVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 188 | } |
||
| 189 | |||
| 190 | public static function ifBeginsWith(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_BeginsWith |
||
| 191 | { |
||
| 192 | return self::$commandSets->if()->ifBeginsWith($variable, $search, $caseInsensitive); |
||
| 193 | } |
||
| 194 | |||
| 195 | public static function ifEndsWith(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_EndsWith |
||
| 196 | { |
||
| 197 | return self::$commandSets->if()->ifEndsWith($variable, $search, $caseInsensitive); |
||
| 198 | } |
||
| 199 | |||
| 200 | public static function elseIfBeginsWith(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_BeginsWith |
||
| 201 | { |
||
| 202 | return self::$commandSets->elseIf()->elseIfBeginsWith($variable, $search, $caseInsensitive); |
||
| 203 | } |
||
| 204 | |||
| 205 | public static function elseIfEndsWith(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_EndsWith |
||
| 206 | { |
||
| 208 | } |
||
| 209 | |||
| 210 | public static function ifContains(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_Contains |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Creates if contains command, with several search terms. |
||
| 217 | * |
||
| 218 | * @param string $variable |
||
| 219 | * @param string[] $searchTerms List of search terms. Do not add surrounding quotes. |
||
| 220 | * @param bool $caseInsensitive |
||
| 221 | * @return Mailcode_Commands_Command_If_Contains |
||
| 222 | */ |
||
| 223 | public static function ifContainsAny(string $variable, array $searchTerms, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_Contains |
||
| 224 | { |
||
| 225 | return self::$commandSets->if()->ifContains($variable, $searchTerms, $caseInsensitive); |
||
| 226 | } |
||
| 227 | |||
| 228 | public static function elseIfContains(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_Contains |
||
| 229 | { |
||
| 230 | return self::$commandSets->elseIf()->elseIfContains($variable, array($search), $caseInsensitive); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Creates else if contains command, with several search terms. |
||
| 235 | * |
||
| 236 | * @param string $variable |
||
| 237 | * @param string[] $searchTerms List of search terms. Do not add surrounding quotes. |
||
| 238 | * @param bool $caseInsensitive |
||
| 239 | * @return Mailcode_Commands_Command_ElseIf_Contains |
||
| 240 | */ |
||
| 241 | public static function elseIfContainsAny(string $variable, array $searchTerms, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_Contains |
||
| 244 | } |
||
| 245 | |||
| 246 | public static function ifEmpty(string $variable) : Mailcode_Commands_Command_If_Empty |
||
| 247 | { |
||
| 248 | return self::$commandSets->if()->ifEmpty($variable); |
||
| 249 | } |
||
| 250 | |||
| 251 | public static function ifNotEmpty(string $variable) : Mailcode_Commands_Command_If_NotEmpty |
||
| 254 | } |
||
| 255 | |||
| 256 | public static function for(string $sourceVariable, string $loopVariable) : Mailcode_Commands_Command_For |
||
| 257 | { |
||
| 258 | return self::$commandSets->misc()->for($sourceVariable, $loopVariable); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Creates a renderer instance, which can be used to easily |
||
| 263 | * create and convert commands to strings. |
||
| 264 | * |
||
| 265 | * @return Mailcode_Renderer |
||
| 266 | */ |
||
| 267 | public static function createRenderer() : Mailcode_Renderer |
||
| 268 | { |
||
| 269 | return new Mailcode_Renderer(); |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Creates a printer instance, which works like the renderer, |
||
| 274 | * but outputs the generated strings to standard output. |
||
| 275 | * |
||
| 276 | * @return Mailcode_Printer |
||
| 277 | */ |
||
| 278 | public static function createPrinter() : Mailcode_Printer |
||
| 279 | { |
||
| 280 | return new Mailcode_Printer(); |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Gets/creates the global instance of the date format info |
||
| 285 | * class, used to handle date formatting aspects. |
||
| 286 | * |
||
| 287 | * @return Mailcode_Date_FormatInfo |
||
| 288 | */ |
||
| 289 | public static function createDateInfo() : Mailcode_Date_FormatInfo |
||
| 290 | { |
||
| 291 | return Mailcode_Date_FormatInfo::getInstance(); |
||
| 292 | } |
||
| 293 | |||
| 294 | public static function init() : void |
||
| 299 | } |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | Mailcode_Factory::init(); |
||
| 304 |