Completed
Push — master ( 704abe...89f38d )
by Alexander
18s
created
YaLinqo/Utils.php 1 patch
Braces   +33 added lines, -26 removed lines patch added patch discarded remove patch
@@ -56,16 +56,20 @@  discard block
 block discarded – undo
56 56
     public static function createLambda($closure, string $closureArgs, $default = null)
57 57
     {
58 58
         if ($closure === null) {
59
-            if ($default === null)
60
-                throw new \InvalidArgumentException(self::ERROR_CLOSURE_NULL);
59
+            if ($default === null) {
60
+                            throw new \InvalidArgumentException(self::ERROR_CLOSURE_NULL);
61
+            }
61 62
             return $default;
62 63
         }
63
-        if ($closure instanceof \Closure)
64
-            return $closure;
65
-        if (is_string($closure) && ($function = self::createLambdaFromString($closure, $closureArgs)))
66
-            return $function;
67
-        if (is_callable($closure))
68
-            return $closure;
64
+        if ($closure instanceof \Closure) {
65
+                    return $closure;
66
+        }
67
+        if (is_string($closure) && ($function = self::createLambdaFromString($closure, $closureArgs))) {
68
+                    return $function;
69
+        }
70
+        if (is_callable($closure)) {
71
+                    return $closure;
72
+        }
69 73
         throw new \InvalidArgumentException(self::ERROR_CLOSURE_NOT_CALLABLE);
70 74
     }
71 75
 
@@ -83,8 +87,7 @@  discard block
 block discarded – undo
83 87
         if ($closure === null) {
84 88
             $isReversed = false;
85 89
             return $sortOrder === SORT_DESC ? Functions::$compareStrictReversed : Functions::$compareStrict;
86
-        }
87
-        elseif (is_int($closure)) {
90
+        } elseif (is_int($closure)) {
88 91
             switch ($closure) {
89 92
                 case SORT_REGULAR:
90 93
                     return Functions::$compareStrict;
@@ -116,14 +119,16 @@  discard block
 block discarded – undo
116 119
      */
117 120
     public static function lambdaToSortFlagsAndOrder($closure, &$sortOrder)
118 121
     {
119
-        if ($sortOrder !== SORT_ASC && $sortOrder !== SORT_DESC)
120
-            $sortOrder = $sortOrder ? SORT_DESC : SORT_ASC;
121
-        if (is_int($closure))
122
-            return $closure;
123
-        elseif (($closure === null || is_string($closure)) && isset(self::$compareFunctionToSortFlags[$closure]))
124
-            return self::$compareFunctionToSortFlags[$closure];
125
-        else
126
-            return null;
122
+        if ($sortOrder !== SORT_ASC && $sortOrder !== SORT_DESC) {
123
+                    $sortOrder = $sortOrder ? SORT_DESC : SORT_ASC;
124
+        }
125
+        if (is_int($closure)) {
126
+                    return $closure;
127
+        } elseif (($closure === null || is_string($closure)) && isset(self::$compareFunctionToSortFlags[$closure])) {
128
+                    return self::$compareFunctionToSortFlags[$closure];
129
+        } else {
130
+                    return null;
131
+        }
127 132
     }
128 133
 
129 134
     /**
@@ -137,23 +142,25 @@  discard block
 block discarded – undo
137 142
     {
138 143
         $posDollar = strpos($closure, '$');
139 144
         if ($posDollar !== false) {
140
-            if (isset(self::$lambdaCache[$closure][$closureArgs]))
141
-                return self::$lambdaCache[$closure][$closureArgs];
145
+            if (isset(self::$lambdaCache[$closure][$closureArgs])) {
146
+                            return self::$lambdaCache[$closure][$closureArgs];
147
+            }
142 148
             $posArrow = strpos($closure, '==>', $posDollar);
143 149
             if ($posArrow !== false) {
144 150
                 $args = trim(substr($closure, 0, $posArrow), "() \r\n\t");
145 151
                 $code = substr($closure, $posArrow + 3);
146
-            }
147
-            else {
152
+            } else {
148 153
                 $args = '$' . str_replace(',', '=null,$', $closureArgs) . '=null';
149 154
                 $code = $closure;
150 155
             }
151 156
             $code = trim($code, " \r\n\t");
152
-            if (strlen($code) > 0 && $code[0] != '{')
153
-                $code = "return {$code};";
157
+            if (strlen($code) > 0 && $code[0] != '{') {
158
+                            $code = "return {$code};";
159
+            }
154 160
             $fun = eval("return function($args) { $code };");
155
-            if (!$fun)
156
-                throw new \InvalidArgumentException(self::ERROR_CANNOT_PARSE_LAMBDA);
161
+            if (!$fun) {
162
+                            throw new \InvalidArgumentException(self::ERROR_CANNOT_PARSE_LAMBDA);
163
+            }
157 164
             self::$lambdaCache[$closure][$closureArgs] = $fun;
158 165
             return $fun;
159 166
         }
Please login to merge, or discard this patch.
YaLinqo/OrderedEnumerable.php 1 patch
Braces   +47 added lines, -38 removed lines patch added patch discarded remove patch
@@ -54,8 +54,10 @@  discard block
 block discarded – undo
54 54
     private function getSingleComparer()
55 55
     {
56 56
         $comparer = $this->comparer;
57
-        if ($this->isReversed)
58
-            $comparer = function($a, $b) use ($comparer) { return -$comparer($a, $b); };
57
+        if ($this->isReversed) {
58
+                    $comparer = function($a, $b) use ($comparer) { return -$comparer($a, $b);
59
+        }
60
+        };
59 61
         return $comparer;
60 62
     }
61 63
 
@@ -116,8 +118,9 @@  discard block
 block discarded – undo
116 118
         $array = $this->source->tryGetArrayCopy();
117 119
 
118 120
         $it = $this->trySortBySingleField($array, $canMultisort);
119
-        if ($it !== null)
120
-            return $it;
121
+        if ($it !== null) {
122
+                    return $it;
123
+        }
121 124
 
122 125
         return $this->sortByMultipleFields($array, $canMultisort);
123 126
     }
@@ -126,24 +129,23 @@  discard block
 block discarded – undo
126 129
     {
127 130
         if ($this->parent !== null || $array === null) {
128 131
             return null;
129
-        }
130
-        elseif ($this->keySelector === Functions::$value) {
131
-            if (!$canMultisort)
132
-                uasort($array, $this->getSingleComparer());
133
-            elseif ($this->sortOrder == SORT_ASC)
134
-                asort($array, $this->sortFlags);
135
-            else
136
-                arsort($array, $this->sortFlags);
137
-        }
138
-        elseif ($this->keySelector === Functions::$key) {
139
-            if (!$canMultisort)
140
-                uksort($array, $this->getSingleComparer());
141
-            elseif ($this->sortOrder == SORT_ASC)
142
-                ksort($array, $this->sortFlags);
143
-            else
144
-                krsort($array, $this->sortFlags);
145
-        }
146
-        else {
132
+        } elseif ($this->keySelector === Functions::$value) {
133
+            if (!$canMultisort) {
134
+                            uasort($array, $this->getSingleComparer());
135
+            } elseif ($this->sortOrder == SORT_ASC) {
136
+                            asort($array, $this->sortFlags);
137
+            } else {
138
+                            arsort($array, $this->sortFlags);
139
+            }
140
+        } elseif ($this->keySelector === Functions::$key) {
141
+            if (!$canMultisort) {
142
+                            uksort($array, $this->getSingleComparer());
143
+            } elseif ($this->sortOrder == SORT_ASC) {
144
+                            ksort($array, $this->sortFlags);
145
+            } else {
146
+                            krsort($array, $this->sortFlags);
147
+            }
148
+        } else {
147 149
             return null;
148 150
         }
149 151
         return new \ArrayIterator($array);
@@ -154,14 +156,16 @@  discard block
 block discarded – undo
154 156
         $orders = [];
155 157
         for ($order = $this; $order !== null; $order = $order->parent) {
156 158
             $orders[] = $order;
157
-            if ($order->sortFlags === null)
158
-                $canMultisort = false;
159
+            if ($order->sortFlags === null) {
160
+                            $canMultisort = false;
161
+            }
159 162
         }
160 163
         $orders = array_reverse($orders);
161 164
 
162 165
         $it = $this->trySortArrayWithMultisort($array, $orders, $canMultisort);
163
-        if ($it !== null)
164
-            return $it;
166
+        if ($it !== null) {
167
+                    return $it;
168
+        }
165 169
 
166 170
         return $this->sortIterator($orders, $canMultisort);
167 171
     }
@@ -169,20 +173,23 @@  discard block
 block discarded – undo
169 173
     private function sortIterator(array $orders, bool $canMultisort)
170 174
     {
171 175
         $enum = [];
172
-        if ($canMultisort)
173
-            $this->sortIteratorWithMultisort($enum, $orders);
174
-        else
175
-            $this->sortIteratorWithUsort($enum, $orders);
176
+        if ($canMultisort) {
177
+                    $this->sortIteratorWithMultisort($enum, $orders);
178
+        } else {
179
+                    $this->sortIteratorWithUsort($enum, $orders);
180
+        }
176 181
 
177
-        foreach ($enum as $pair)
178
-            yield $pair[0] => $pair[1];
182
+        foreach ($enum as $pair) {
183
+                    yield $pair[0] => $pair[1];
184
+        }
179 185
     }
180 186
 
181 187
     private function trySortArrayWithMultisort($array, array $orders, bool $canMultisort)
182 188
     {
183 189
         /** @var $order OrderedEnumerable */
184
-        if ($array === null || !$canMultisort)
185
-            return null;
190
+        if ($array === null || !$canMultisort) {
191
+                    return null;
192
+        }
186 193
 
187 194
         $args = [];
188 195
         foreach ($orders as $order) {
@@ -205,8 +212,9 @@  discard block
 block discarded – undo
205 212
     private function sortIteratorWithMultisort(&$enum, array $orders)
206 213
     {
207 214
         /** @var $order OrderedEnumerable */
208
-        foreach ($this->source as $k => $v)
209
-            $enum[] = [ $k, $v ];
215
+        foreach ($this->source as $k => $v) {
216
+                    $enum[] = [ $k, $v ];
217
+        }
210 218
 
211 219
         $args = [];
212 220
         foreach ($orders as $order) {
@@ -243,8 +251,9 @@  discard block
 block discarded – undo
243 251
                 $order = $orders[$i];
244 252
                 $comparer = $order->comparer;
245 253
                 $diff = $comparer($a[$i + 2], $b[$i + 2]);
246
-                if ($diff != 0)
247
-                    return $order->isReversed ? -$diff : $diff;
254
+                if ($diff != 0) {
255
+                                    return $order->isReversed ? -$diff : $diff;
256
+                }
248 257
             }
249 258
             return 0;
250 259
         });
Please login to merge, or discard this patch.