Conditions | 19 |
Paths | 82 |
Total Lines | 79 |
Code Lines | 48 |
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 |
||
83 | public function onAndPossibility($field, $operator, $rule, array $allOperandsByField) |
||
84 | { |
||
85 | if ($rule instanceof EqualRule) { |
||
86 | $value = $rule->getValue(); |
||
87 | } |
||
88 | elseif ($rule instanceof InRule) { |
||
89 | $value = $rule->getPossibilities(); |
||
90 | } |
||
91 | elseif ($rule instanceof NotInRule) { |
||
92 | $operator = 'NOT IN'; |
||
93 | $value = $rule->getPossibilities(); |
||
94 | } |
||
95 | elseif ($rule instanceof AboveRule) { |
||
96 | $value = $rule->getLowerLimit(); |
||
97 | } |
||
98 | elseif ($rule instanceof BelowRule) { |
||
99 | $value = $rule->getUpperLimit(); |
||
100 | } |
||
101 | elseif ($rule instanceof AboveOrEqualRule) { |
||
102 | $value = $rule->getMinimum(); |
||
103 | } |
||
104 | elseif ($rule instanceof BelowOrEqualRule) { |
||
105 | $value = $rule->getMaximum(); |
||
106 | } |
||
107 | elseif ($rule instanceof NotEqualRule) { |
||
108 | $value = $rule->getValue(); |
||
109 | } |
||
110 | elseif ($rule instanceof RegexpRule) { |
||
111 | $value = RegexpRule::php2mariadbPCRE( $rule->getPattern() ); |
||
112 | } |
||
113 | else { |
||
114 | throw new \InvalidArgumentException( |
||
115 | "Unhandled operator '$operator' during SQL query generation" |
||
116 | ); |
||
117 | } |
||
118 | |||
119 | if (gettype($value) == 'integer') { |
||
120 | } |
||
121 | elseif (gettype($value) == 'double') { |
||
122 | // TODO disable locale to handle separators |
||
123 | } |
||
124 | elseif ($value instanceof \DateTime) { |
||
125 | $value = "'" . $value->format('Y-m-d H:i:s') . "'"; |
||
126 | } |
||
127 | elseif (gettype($value) == 'string') { |
||
128 | $value = $this->addParameter($value); |
||
129 | } |
||
130 | elseif (gettype($value) == 'array') { |
||
131 | $sql_part = []; |
||
132 | foreach ($value as $possibility) { |
||
133 | $sql_part[] = $this->addParameter($possibility); |
||
134 | } |
||
135 | $value = '(' . implode(', ', $sql_part) . ')'; |
||
136 | } |
||
137 | elseif ($value === null) { |
||
138 | $value = "NULL"; |
||
139 | if ($rule instanceof EqualRule) { |
||
140 | $operator = 'IS'; |
||
141 | } |
||
142 | elseif ($rule instanceof NotEqualRule) { |
||
143 | $operator = 'IS NOT'; |
||
144 | } |
||
145 | else { |
||
146 | throw new \InvalidArgumentException( |
||
147 | "NULL is only handled for equality / difference" |
||
148 | ); |
||
149 | } |
||
150 | } |
||
151 | else { |
||
152 | throw new \InvalidArgumentException( |
||
153 | "Unhandled type of value: ".gettype($value). ' | ' .var_export($value, true) |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | $operator = strtoupper($operator); |
||
158 | |||
159 | $new_rule = "$field $operator $value"; |
||
160 | |||
161 | $this->appendToLastOrOperandKey($new_rule); |
||
162 | } |
||
183 |