| Total Complexity | 49 |
| Total Lines | 332 |
| Duplicated Lines | 0 % |
| Changes | 10 | ||
| 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 |
||
| 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 |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Creates a ShowNumber command, used to display numeric variables |
||
| 57 | * and format the numbers to a specific format. |
||
| 58 | * |
||
| 59 | * @param string $variableName A variable name, with or without the $ sign prepended. |
||
| 60 | * @param string $formatString A number format string, or empty string for default. |
||
| 61 | * @return Mailcode_Commands_Command_ShowNumber |
||
| 62 | */ |
||
| 63 | public static function showNumber(string $variableName, string $formatString="") : Mailcode_Commands_Command_ShowNumber |
||
| 64 | { |
||
| 65 | return self::$commandSets->show()->showNumber($variableName, $formatString); |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Creates a ShowSnippet command. |
||
| 70 | * |
||
| 71 | * @param string $snippetName A snippet name, with or without the $ sign prepended. |
||
| 72 | * @return Mailcode_Commands_Command_ShowSnippet |
||
| 73 | */ |
||
| 74 | public static function showSnippet(string $snippetName) : Mailcode_Commands_Command_ShowSnippet |
||
| 75 | { |
||
| 76 | return self::$commandSets->show()->showSnippet($snippetName); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Creates a SetVariable command. |
||
| 81 | * |
||
| 82 | * @param string $variableName A variable name, with or without the $ sign prepended. |
||
| 83 | * @param string $value |
||
| 84 | * @param bool $quoteValue Whether to treat the value as a string literal, and add quotes to it. |
||
| 85 | * @return Mailcode_Commands_Command_SetVariable |
||
| 86 | * @throws Mailcode_Factory_Exception |
||
| 87 | * |
||
| 88 | * @see Mailcode_Factory::ERROR_INVALID_COMMAND_CREATED |
||
| 89 | */ |
||
| 90 | public static function setVar(string $variableName, string $value, bool $quoteValue=true) : Mailcode_Commands_Command_SetVariable |
||
| 91 | { |
||
| 92 | return self::$commandSets->set()->setVar($variableName, $value, $quoteValue); |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Like setVar(), but treats the value as a string literal |
||
| 97 | * and automatically adds quotes to it. |
||
| 98 | * |
||
| 99 | * @param string $variableName |
||
| 100 | * @param string $value |
||
| 101 | * @return Mailcode_Commands_Command_SetVariable |
||
| 102 | */ |
||
| 103 | public static function setVarString(string $variableName, string $value) : Mailcode_Commands_Command_SetVariable |
||
| 104 | { |
||
| 105 | return self::$commandSets->set()->setVar($variableName, $value, true); |
||
| 106 | } |
||
| 107 | |||
| 108 | public static function comment(string $comments) : Mailcode_Commands_Command_Comment |
||
| 109 | { |
||
| 110 | return self::$commandSets->misc()->comment($comments); |
||
| 111 | } |
||
| 112 | |||
| 113 | public static function else() : Mailcode_Commands_Command_Else |
||
| 114 | { |
||
| 115 | return self::$commandSets->if()->else(); |
||
| 116 | } |
||
| 117 | |||
| 118 | public static function end() : Mailcode_Commands_Command_End |
||
| 119 | { |
||
| 120 | return self::$commandSets->if()->end(); |
||
| 121 | } |
||
| 122 | |||
| 123 | public static function if(string $condition, string $type='') : Mailcode_Commands_Command_If |
||
| 124 | { |
||
| 125 | return self::$commandSets->if()->if($condition, $type); |
||
| 126 | } |
||
| 127 | |||
| 128 | public static function ifVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
||
| 129 | { |
||
| 130 | return self::$commandSets->if()->ifVar($variable, $operand, $value, $quoteValue); |
||
| 131 | } |
||
| 132 | |||
| 133 | public static function ifVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_If_Variable |
||
| 134 | { |
||
| 135 | return self::$commandSets->if()->ifVarString($variable, $operand, $value); |
||
| 136 | } |
||
| 137 | |||
| 138 | public static function ifVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
||
| 139 | { |
||
| 140 | return self::$commandSets->if()->ifVarEquals($variable, $value, $quoteValue); |
||
| 141 | } |
||
| 142 | |||
| 143 | public static function ifVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If |
||
| 144 | { |
||
| 145 | return self::$commandSets->if()->ifVarEqualsString($variable, $value); |
||
| 146 | } |
||
| 147 | |||
| 148 | public static function ifVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_If_Variable |
||
| 149 | { |
||
| 150 | return self::$commandSets->if()->ifVarNotEquals($variable, $value, $quoteValue); |
||
| 151 | } |
||
| 152 | |||
| 153 | public static function ifVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_If_Variable |
||
| 154 | { |
||
| 155 | return self::$commandSets->if()->ifVarNotEqualsString($variable, $value); |
||
| 156 | } |
||
| 157 | |||
| 158 | public static function elseIf(string $condition, string $type='') : Mailcode_Commands_Command_ElseIf |
||
| 159 | { |
||
| 160 | return self::$commandSets->elseIf()->elseIf($condition, $type); |
||
| 161 | } |
||
| 162 | |||
| 163 | public static function elseIfEmpty(string $variable) : Mailcode_Commands_Command_ElseIf_Empty |
||
| 164 | { |
||
| 165 | return self::$commandSets->elseIf()->elseIfEmpty($variable); |
||
| 166 | } |
||
| 167 | |||
| 168 | public static function elseIfNotEmpty(string $variable) : Mailcode_Commands_Command_ElseIf_NotEmpty |
||
| 169 | { |
||
| 170 | return self::$commandSets->elseIf()->elseIfNotEmpty($variable); |
||
| 171 | } |
||
| 172 | |||
| 173 | public static function elseIfVar(string $variable, string $operand, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 174 | { |
||
| 175 | return self::$commandSets->elseIf()->elseIfVar($variable, $operand, $value, $quoteValue); |
||
| 176 | } |
||
| 177 | |||
| 178 | public static function elseIfVarString(string $variable, string $operand, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 179 | { |
||
| 180 | return self::$commandSets->elseIf()->elseIfVarString($variable, $operand, $value); |
||
| 181 | } |
||
| 182 | |||
| 183 | public static function elseIfVarEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 184 | { |
||
| 185 | return self::$commandSets->elseIf()->elseIfVarEquals($variable, $value, $quoteValue); |
||
| 186 | } |
||
| 187 | |||
| 188 | public static function elseIfVarEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 191 | } |
||
| 192 | |||
| 193 | public static function elseIfVarNotEquals(string $variable, string $value, bool $quoteValue=false) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 194 | { |
||
| 195 | return self::$commandSets->elseIf()->elseIfVarNotEquals($variable, $value, $quoteValue); |
||
| 196 | } |
||
| 197 | |||
| 198 | public static function elseIfVarNotEqualsString(string $variable, string $value) : Mailcode_Commands_Command_ElseIf_Variable |
||
| 199 | { |
||
| 200 | return self::$commandSets->elseIf()->elseIfVarNotEqualsString($variable, $value); |
||
| 201 | } |
||
| 202 | |||
| 203 | public static function ifBeginsWith(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_BeginsWith |
||
| 204 | { |
||
| 205 | return self::$commandSets->if()->ifBeginsWith($variable, $search, $caseInsensitive); |
||
| 206 | } |
||
| 207 | |||
| 208 | public static function ifBiggerThan(string $variable, string $number) : Mailcode_Commands_Command_If_BiggerThan |
||
| 209 | { |
||
| 210 | return self::$commandSets->if()->ifBiggerThan($variable, $number); |
||
| 211 | } |
||
| 212 | |||
| 213 | public static function ifSmallerThan(string $variable, string $number) : Mailcode_Commands_Command_If_SmallerThan |
||
| 214 | { |
||
| 215 | return self::$commandSets->if()->ifSmallerThan($variable, $number); |
||
| 216 | } |
||
| 217 | |||
| 218 | public static function ifVarEqualsNumber(string $variable, string $number) : Mailcode_Commands_Command_If_EqualsNumber |
||
| 219 | { |
||
| 220 | return self::$commandSets->if()->ifVarEqualsNumber($variable, $number); |
||
| 221 | } |
||
| 222 | |||
| 223 | public static function ifEndsWith(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_EndsWith |
||
| 224 | { |
||
| 225 | return self::$commandSets->if()->ifEndsWith($variable, $search, $caseInsensitive); |
||
| 226 | } |
||
| 227 | |||
| 228 | public static function elseIfBeginsWith(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_BeginsWith |
||
| 229 | { |
||
| 230 | return self::$commandSets->elseIf()->elseIfBeginsWith($variable, $search, $caseInsensitive); |
||
| 231 | } |
||
| 232 | |||
| 233 | public static function elseIfEndsWith(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_EndsWith |
||
| 234 | { |
||
| 235 | return self::$commandSets->elseIf()->elseIfEndsWith($variable, $search, $caseInsensitive); |
||
| 236 | } |
||
| 237 | |||
| 238 | public static function ifContains(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_Contains |
||
| 239 | { |
||
| 240 | return self::$commandSets->if()->ifContains($variable, array($search), $caseInsensitive); |
||
| 241 | } |
||
| 242 | |||
| 243 | public static function elseIfBiggerThan(string $variable, string $number) : Mailcode_Commands_Command_ElseIf_BiggerThan |
||
| 244 | { |
||
| 245 | return self::$commandSets->elseIf()->elseIfBiggerThan($variable, $number); |
||
| 246 | } |
||
| 247 | |||
| 248 | public static function elseIfSmallerThan(string $variable, string $number) : Mailcode_Commands_Command_ElseIf_SmallerThan |
||
| 249 | { |
||
| 250 | return self::$commandSets->elseIf()->elseIfSmallerThan($variable, $number); |
||
| 251 | } |
||
| 252 | |||
| 253 | public static function elseIfVarEqualsNumber(string $variable, string $number) : Mailcode_Commands_Command_ElseIf_EqualsNumber |
||
| 254 | { |
||
| 255 | return self::$commandSets->elseIf()->elseIfVarEqualsNumber($variable, $number); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Creates if contains command, with several search terms. |
||
| 260 | * |
||
| 261 | * @param string $variable |
||
| 262 | * @param string[] $searchTerms List of search terms. Do not add surrounding quotes. |
||
| 263 | * @param bool $caseInsensitive |
||
| 264 | * @return Mailcode_Commands_Command_If_Contains |
||
| 265 | */ |
||
| 266 | public static function ifContainsAny(string $variable, array $searchTerms, bool $caseInsensitive=false) : Mailcode_Commands_Command_If_Contains |
||
| 267 | { |
||
| 268 | return self::$commandSets->if()->ifContains($variable, $searchTerms, $caseInsensitive); |
||
| 269 | } |
||
| 270 | |||
| 271 | public static function elseIfContains(string $variable, string $search, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_Contains |
||
| 272 | { |
||
| 273 | return self::$commandSets->elseIf()->elseIfContains($variable, array($search), $caseInsensitive); |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Creates else if contains command, with several search terms. |
||
| 278 | * |
||
| 279 | * @param string $variable |
||
| 280 | * @param string[] $searchTerms List of search terms. Do not add surrounding quotes. |
||
| 281 | * @param bool $caseInsensitive |
||
| 282 | * @return Mailcode_Commands_Command_ElseIf_Contains |
||
| 283 | */ |
||
| 284 | public static function elseIfContainsAny(string $variable, array $searchTerms, bool $caseInsensitive=false) : Mailcode_Commands_Command_ElseIf_Contains |
||
| 287 | } |
||
| 288 | |||
| 289 | public static function ifEmpty(string $variable) : Mailcode_Commands_Command_If_Empty |
||
| 290 | { |
||
| 291 | return self::$commandSets->if()->ifEmpty($variable); |
||
| 292 | } |
||
| 293 | |||
| 294 | public static function ifNotEmpty(string $variable) : Mailcode_Commands_Command_If_NotEmpty |
||
| 295 | { |
||
| 296 | return self::$commandSets->if()->ifNotEmpty($variable); |
||
| 297 | } |
||
| 298 | |||
| 299 | public static function for(string $sourceVariable, string $loopVariable) : Mailcode_Commands_Command_For |
||
| 300 | { |
||
| 301 | return self::$commandSets->misc()->for($sourceVariable, $loopVariable); |
||
| 302 | } |
||
| 303 | |||
| 304 | public static function break() : Mailcode_Commands_Command_Break |
||
| 305 | { |
||
| 306 | return self::$commandSets->misc()->break(); |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Creates a renderer instance, which can be used to easily |
||
| 311 | * create and convert commands to strings. |
||
| 312 | * |
||
| 313 | * @return Mailcode_Renderer |
||
| 314 | */ |
||
| 315 | public static function createRenderer() : Mailcode_Renderer |
||
| 316 | { |
||
| 317 | return new Mailcode_Renderer(); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Creates a printer instance, which works like the renderer, |
||
| 322 | * but outputs the generated strings to standard output. |
||
| 323 | * |
||
| 324 | * @return Mailcode_Printer |
||
| 325 | */ |
||
| 326 | public static function createPrinter() : Mailcode_Printer |
||
| 327 | { |
||
| 328 | return new Mailcode_Printer(); |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Gets/creates the global instance of the date format info |
||
| 333 | * class, used to handle date formatting aspects. |
||
| 334 | * |
||
| 335 | * @return Mailcode_Date_FormatInfo |
||
| 336 | */ |
||
| 337 | public static function createDateInfo() : Mailcode_Date_FormatInfo |
||
| 338 | { |
||
| 339 | return Mailcode_Date_FormatInfo::getInstance(); |
||
| 340 | } |
||
| 341 | |||
| 342 | public static function init() : void |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | public static function code(string $language) : Mailcode_Commands_Command_Code |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | Mailcode_Factory::init(); |
||
| 357 |