| Conditions | 9 |
| Paths | 16 |
| Total Lines | 24 |
| Code Lines | 14 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 27 | public static function nextGreaterElements2(array $nums): array |
||
| 28 | { |
||
| 29 | if (empty($nums)) { |
||
| 30 | return []; |
||
| 31 | } |
||
| 32 | $n = count($nums); |
||
| 33 | [$ans, $stack] = [array_fill(0, $n, -1), []]; |
||
| 34 | for ($i = 0; $i < $n; $i++) { |
||
| 35 | while ($stack && $nums[end($stack)] < $nums[$i]) { |
||
| 36 | $ans[array_pop($stack)] = $nums[$i]; |
||
| 37 | } |
||
| 38 | array_push($stack, $i); |
||
| 39 | } |
||
| 40 | |||
| 41 | for ($i = 0; $i < $n; $i++) { |
||
| 42 | while ($stack && $nums[end($stack)] < $nums[$i]) { |
||
| 43 | $ans[array_pop($stack)] = $nums[$i]; |
||
| 44 | } |
||
| 45 | if (empty($stack)) { |
||
| 46 | break; |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | return $ans; |
||
| 51 | } |
||
| 53 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.