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 |
||
45 | public static function factory($value) { |
||
46 | if (is_object($value) && $value instanceof Expression) { |
||
47 | return $value; |
||
48 | } elseif (is_numeric($value)) { |
||
49 | return new Expressions\Number($value); |
||
50 | } elseif (preg_match('/^\$?[a-z]+$/', $value)) { |
||
51 | return new Expressions\Variable($value); |
||
52 | } |
||
53 | |||
54 | switch ($value) { |
||
55 | case '+': |
||
56 | return new Expressions\Addition($value); |
||
57 | case '-': |
||
58 | return new Expressions\Subtraction($value); |
||
59 | case '*': |
||
60 | return new Expressions\Multiplication($value); |
||
61 | case '/': |
||
62 | return new Expressions\Division($value); |
||
63 | case '%': |
||
64 | return new Expressions\Modulo($value); |
||
65 | case '?': |
||
66 | case ':': |
||
67 | return new Expressions\Ternary($value); |
||
68 | case '(': |
||
69 | case ')': |
||
70 | return new Expressions\Parenthesis($value); |
||
71 | case '==': |
||
72 | return new Expressions\ComparisonEQ($value); |
||
73 | case '<': |
||
74 | return new Expressions\ComparisonLT($value); |
||
75 | case '>': |
||
76 | return new Expressions\ComparisonGT($value); |
||
77 | case '<=': |
||
78 | return new Expressions\ComparisonLTE($value); |
||
79 | case '>=': |
||
80 | return new Expressions\ComparisonGTE($value); |
||
81 | case '!=': |
||
82 | return new Expressions\ComparisonNE($value); |
||
83 | case '||': |
||
84 | return new Expressions\OperatorOr($value); |
||
85 | case '&&': |
||
86 | return new Expressions\OperatorAnd($value); |
||
87 | } |
||
88 | |||
89 | throw new \Exception('Undefined Value ' . $value); |
||
90 | } |
||
91 | |||
106 |