Conditions | 22 |
Paths | 21 |
Total Lines | 46 |
Code Lines | 41 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
26 | public static function factory($value) { |
||
27 | if (is_object($value) && $value instanceof TerminalExpression) { |
||
28 | return $value; |
||
29 | } elseif (is_numeric($value)) { |
||
30 | return new Number($value); |
||
31 | } elseif (preg_match('/^\$?[a-z]+$/', $value)) { |
||
32 | return new Variable($value); |
||
33 | } |
||
34 | |||
35 | switch ($value) { |
||
36 | case '+': |
||
37 | return new Addition($value); |
||
38 | case '-': |
||
39 | return new Subtraction($value); |
||
40 | case '*': |
||
41 | return new Multiplication($value); |
||
42 | case '/': |
||
43 | return new Division($value); |
||
44 | case '%': |
||
45 | return new Modulo($value); |
||
46 | case '?': |
||
47 | case ':': |
||
48 | return new Ternary($value); |
||
49 | case '(': |
||
50 | case ')': |
||
51 | return new Parenthesis($value); |
||
52 | case '==': |
||
53 | return new ComparisonEQ($value); |
||
54 | case '<': |
||
55 | return new ComparisonLT($value); |
||
56 | case '>': |
||
57 | return new ComparisonGT($value); |
||
58 | case '<=': |
||
59 | return new ComparisonLTE($value); |
||
60 | case '>=': |
||
61 | return new ComparisonGTE($value); |
||
62 | case '!=': |
||
63 | return new ComparisonNE($value); |
||
64 | case '||': |
||
65 | return new OperatorOr($value); |
||
66 | case '&&': |
||
67 | return new OperatorAnd($value); |
||
68 | } |
||
69 | |||
70 | throw new \Exception('Undefined Value ' . $value); |
||
71 | } |
||
72 | |||
87 |