Passed
Push — master ( 75ee28...758f58 )
by Sebastian
04:00
created

Mailcode_Factory   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 321
Duplicated Lines 0 %

Importance

Changes 10
Bugs 0 Features 1
Metric Value
eloc 55
c 10
b 0
f 1
dl 0
loc 321
rs 8.64
wmc 47

46 Methods

Rating   Name   Duplication   Size   Complexity  
A showVar() 0 3 1
A showDate() 0 3 1
A elseIfVarEqualsString() 0 3 1
A elseIfContainsAny() 0 3 1
A init() 0 5 2
A ifVarEqualsString() 0 3 1
A ifBiggerThan() 0 3 1
A elseIfEndsWith() 0 3 1
A ifVarNotEquals() 0 3 1
A elseIfVar() 0 3 1
A elseIfEmpty() 0 3 1
A elseIfBiggerThan() 0 3 1
A elseIfVarNotEqualsString() 0 3 1
A elseIfNotEmpty() 0 3 1
A elseIf() 0 3 1
A ifNotEmpty() 0 3 1
A ifSmallerThan() 0 3 1
A ifVarString() 0 3 1
A ifVarEquals() 0 3 1
A createPrinter() 0 3 1
A comment() 0 3 1
A setVarString() 0 3 1
A ifVarEqualsNumber() 0 3 1
A showNumber() 0 3 1
A ifVarNotEqualsString() 0 3 1
A createRenderer() 0 3 1
A elseIfContains() 0 3 1
A if() 0 3 1
A else() 0 3 1
A elseIfBeginsWith() 0 3 1
A createDateInfo() 0 3 1
A setVar() 0 3 1
A elseIfVarString() 0 3 1
A elseIfVarEquals() 0 3 1
A ifEndsWith() 0 3 1
A showSnippet() 0 3 1
A ifContainsAny() 0 3 1
A ifContains() 0 3 1
A ifEmpty() 0 3 1
A elseIfVarEqualsNumber() 0 3 1
A elseIfSmallerThan() 0 3 1
A for() 0 3 1
A ifBeginsWith() 0 3 1
A ifVar() 0 3 1
A elseIfVarNotEquals() 0 3 1
A end() 0 3 1

How to fix   Complexity   

Complex Class

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
2
/**
3
 * File containing the {@see Mailcode_Factory} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Utilities
7
 * @see Mailcode_Factory
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Factory utility used to create commands.
16
 *
17
 * @package Mailcode
18
 * @subpackage Utilities
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
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 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
189
    {
190
        return self::$commandSets->elseIf()->elseIfVarEqualsString($variable, $value);
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
285
    {
286
        return self::$commandSets->elseIf()->elseIfContains($variable, $searchTerms, $caseInsensitive);
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
   /**
305
    * Creates a renderer instance, which can be used to easily
306
    * create and convert commands to strings.
307
    * 
308
    * @return Mailcode_Renderer
309
    */
310
    public static function createRenderer() : Mailcode_Renderer
311
    {
312
        return new Mailcode_Renderer();
313
    }
314
    
315
   /**
316
    * Creates a printer instance, which works like the renderer,
317
    * but outputs the generated strings to standard output.
318
    * 
319
    * @return Mailcode_Printer
320
    */
321
    public static function createPrinter() : Mailcode_Printer
322
    {
323
        return new Mailcode_Printer();
324
    }
325
    
326
   /**
327
    * Gets/creates the global instance of the date format info
328
    * class, used to handle date formatting aspects.
329
    * 
330
    * @return Mailcode_Date_FormatInfo
331
    */
332
    public static function createDateInfo() : Mailcode_Date_FormatInfo
333
    {
334
        return Mailcode_Date_FormatInfo::getInstance();
335
    }
336
337
    public static function init() : void
338
    {
339
        if(!isset(self::$commandSets))
340
        {
341
            self::$commandSets = new Mailcode_Factory_CommandSets();
342
        }
343
    }
344
}
345
346
Mailcode_Factory::init();
347