Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Lets you round the numeric elements of an array to integers while preserving their sum. |
||
| 5 | * |
||
| 6 | * Usage: |
||
| 7 | * |
||
| 8 | * Jetpack_Constrained_Array_Rounding::get_rounded_constrained_array( $bound_array ) |
||
| 9 | * if a specific sum doesn't need to be specified for the bound array |
||
| 10 | * |
||
| 11 | * Jetpack_Constrained_Array_Rounding::get_rounded_constrained_array( $bound_array, $sum ) |
||
| 12 | * If the sum of $bound_array must equal $sum after rounding. |
||
| 13 | * |
||
| 14 | * If $sum is less than the sum of the floor of the elements of the array, the class defaults to using the sum of the array elements. |
||
| 15 | */ |
||
| 16 | class Jetpack_Constrained_Array_Rounding { |
||
| 17 | public static function get_rounded_constrained_array( $bound_array, $sum = false ) { |
||
| 18 | // Convert associative arrays before working with them and convert them back before returning the values |
||
| 19 | $keys = array_keys( $bound_array ); |
||
| 20 | $bound_array = array_values( $bound_array ); |
||
| 21 | |||
| 22 | $bound_array_int = self::get_int_floor_array( $bound_array ); |
||
| 23 | |||
| 24 | $lower_sum = array_sum( wp_list_pluck( $bound_array_int, 'floor' ) ); |
||
| 25 | if ( ! $sum || ( $sum < $lower_sum ) ) { |
||
| 26 | // If value of sum is not supplied or is invalid, calculate the sum that the returned array is constrained to match |
||
| 27 | $sum = array_sum( $bound_array ); |
||
| 28 | } |
||
| 29 | $diff_sum = $sum - $lower_sum; |
||
| 30 | |||
| 31 | self::adjust_constrained_array( $bound_array_int, $diff_sum ); |
||
| 32 | |||
| 33 | $bound_array_fin = wp_list_pluck( $bound_array_int, 'floor' ); |
||
| 34 | return array_combine( $keys, $bound_array_fin ); |
||
| 35 | } |
||
| 36 | |||
| 37 | private static function get_int_floor_array( $bound_array ) { |
||
| 38 | $bound_array_int_floor = array(); |
||
| 39 | foreach ( $bound_array as $i => $value ){ |
||
| 40 | $bound_array_int_floor[$i] = array( |
||
| 41 | 'floor' => (int) floor( $value ), |
||
| 42 | 'fraction' => $value - floor( $value ), |
||
| 43 | 'index' => $i, |
||
| 44 | ); |
||
| 45 | } |
||
| 46 | |||
| 47 | return $bound_array_int_floor; |
||
| 48 | } |
||
| 49 | |||
| 50 | private static function adjust_constrained_array( &$bound_array_int, $adjustment ) { |
||
| 51 | usort( $bound_array_int, array( 'self', 'cmp_desc_fraction' ) ); |
||
| 52 | |||
| 53 | $start = 0; |
||
| 54 | $end = $adjustment - 1; |
||
| 55 | $length = count( $bound_array_int ); |
||
| 56 | |||
| 57 | for ( $i = $start; $i <= $end; $i++ ) { |
||
| 58 | $bound_array_int[ $i % $length ]['floor']++; |
||
| 59 | } |
||
| 60 | |||
| 61 | usort( $bound_array_int, array( 'self', 'cmp_asc_index' ) ); |
||
| 62 | } |
||
| 63 | |||
| 64 | private static function cmp_desc_fraction( $a, $b ) { |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 65 | if ( $a['fraction'] == $b['fraction'] ) |
||
| 66 | return 0; |
||
| 67 | return $a['fraction'] > $b['fraction'] ? -1 : 1; |
||
| 68 | } |
||
| 69 | |||
| 70 | private static function cmp_asc_index( $a, $b ) { |
||
| 71 | if ( $a['index'] == $b['index'] ) |
||
| 72 | return 0; |
||
| 73 | return $a['index'] < $b['index'] ? -1 : 1; |
||
| 74 | } |
||
| 75 | } |
||
| 76 |