Test Setup Failed
Pull Request — master (#478)
by Mohamed
13:16
created

processParallel()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
c 1
b 0
f 0
nc 6
nop 4
dl 0
loc 25
rs 9.4222
1
<?php
2
3
function get_l_name($name){
4
    $name = trim($name);
5
    $last_name = (strpos($name,' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
6
    return $last_name;
7
}
8
9
// Gen name with initials with help of fullname
10
function genNameWithInitials($fullname = null){
11
    $names = explode(' ', $fullname);
12
    $length  = count($names);
13
    $Initials = '';
14
    if($length > 1){
15
        for ($i = 0; ($length-1) > $i; $i++) {
16
            $Initials = $Initials . '' . mb_substr($names[$i], 0, 1, "UTF-8");
17
        }
18
        $nameWithInitials = $Initials . ' ' . $names[$length - 1];
19
    }else{
20
        $nameWithInitials = $fullname;
21
    }
22
    return $nameWithInitials;
23
}
24
25
//check the array of keys exists in the given array
26
function array_keys_exists(array $keys, array $arr)
27
{
28
    return !array_diff_key(array_flip($keys), $arr);
29
}
30
31
function clean($string) {
32
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
33
34
   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
35
}
36
37
38
function getMatchingKeys($array){
39
    $keys = [];
40
    foreach ($array as $key => $value){
41
        if(strstr($key , 'option'))
42
            $keys[] = $key;
43
    }
44
    return $keys;
45
}
46
47
function is_sha1($str) {
48
    return (bool) preg_match('/^[0-9a-f]{40}$/i', $str);
49
}
50
51
function isEmpty($value){
52
    return $value['institution_optional_subject'] !== null;
53
}
54
55
function unique_multidim_array(array $array, $key) {
56
    $temp_array = array();
57
    $i = 0;
58
    $key_array = array();
59
60
    foreach($array as $val) {
61
        if (!in_array($val[$key], $key_array)) {
62
            $key_array[$i] = $val[$key];
63
            $temp_array[$i] = $val;
64
        }
65
        $i++;
66
    }
67
    return $temp_array;
68
}
69
70
71
72
function merge_two_arrays($array1,$array2) {
73
74
    $data = array();
75
    $arrayAB = array_merge($array1,$array2);
76
77
    foreach ($arrayAB as $value) {
78
        dd($arrayAB);
79
        $id = $value['row'];
80
        if (!isset($data[$id])) {
81
            $data[$id] = array();
82
        }
83
        $data[$id] = array_merge($data[$id],$value);
84
    }
85
    return $data;
86
}
87
88
function array_value_recursive($key, array $arr){
89
    $val = array();
90
    array_walk_recursive($arr, function($v, $k) use($key, &$val){
91
        if($k == $key) array_push($val, $v);
92
    });
93
    return count($val) > 1 ? $val : array_pop($val);
94
}
95
96
97
function merge_error_by_row($errors,$key){
98
    $temp_array = array();
99
    $i = 0;
100
101
    foreach($errors as $keys => $val) {
102
        if (!in_array($val[$key], $temp_array)) {
103
            $temp_array[$keys]['errors'][] = $val;
104
        }
105
        $i++;
106
    }
107
    return $temp_array;
108
}
109
110
/**
111
 * @param $error
112
 * @param $count
113
 * @param $reader
114
 * bind error messages to the excel file
115
 */
116
117
function append_errors_to_excel($error, $count, $reader){
0 ignored issues
show
Unused Code introduced by
The parameter $count is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

117
function append_errors_to_excel($error, /** @scrutinizer ignore-unused */ $count, $reader){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
    $active_sheet = $reader->getActiveSheet();
119
    $prev_value = $active_sheet->getCell('A'.$error['row'])->getValue();
120
    $active_sheet->setCellValue('A'. ($error['row']) ,  $prev_value.','.implode(',',$error['errors']));
121
    $active_sheet->getStyle('A'. ($error['row']))->getAlignment()->setWrapText(true);
122
    $columns = Illuminate\Support\Facades\Config::get('excel.columns');
123
124
    $column = array_keys($columns,$error['attribute']);
125
    if(!empty($column)){
126
        $column = $column[0]+1;
127
        $selectedCells = $active_sheet->setSelectedCellByColumnAndRow($column,$error['row']);
128
        $active_cell = ($selectedCells->getActiveCell());
129
130
        $active_sheet->getStyle($active_cell)
131
            ->getFill()
132
            ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
133
            ->getStartColor()
134
            ->setARGB('FF0000');
135
136
        }
137
}
138
139
function rows($error){
140
    return $error['row'];
141
}
142
143
function rowIndex($row){
144
    return $row->getRowIndex();
145
}
146
147
function removeRows($row,$param){
148
    if(in_array($row,$param['rows'])){
149
        $param['reader']->getActiveSheet()->removeRow($row);
150
    }
151
}
152
153
function colorizeCell($column,$error,$active_sheet){
154
    $column = array_keys($column,$error['attribute']);
155
    $selectedCells = $active_sheet->setSelectedCellByColumnAndRow($column,$error['row']);
156
    $active_cell = ($selectedCells->getActiveCell());
157
158
    $active_sheet->getStyle($active_cell)
159
        ->getFill()
160
        ->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)
161
        ->getStartColor()
162
        ->setARGB('FF0000');
163
164
}
165
166
167
function errors_unique_array($item,$key){
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

167
function errors_unique_array($item,/** @scrutinizer ignore-unused */ $key){

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
168
169
        $search = array_filter($item,function ($data) use ($item){
170
            return isset($data['row']) &&  ($data['row']  == $item->row());
171
        });
172
173
        if($search){
0 ignored issues
show
Bug Best Practice introduced by
The expression $search of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
174
            array_push($search[0]['errors'],implode(',',$item->errors()));
175
            $errors = $search;
176
        }
177
178
        return $errors;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $errors does not seem to be defined for all execution paths leading up to this point.
Loading history...
179
}
180
181
function isEmptyRow($row) {
182
    foreach($row as $cell){
183
        if (null !== $cell) return false;
184
    }
185
    return true;
186
}
187
188
function sig_handler($signo){
189
    global $child;
190
    switch($signo){
191
        case 'SIFCLD':
192
    }
193
194
}
195
function processParallel($func ,array $arr, $procs = 4,$params =[])
196
    {
197
        // Break array up into $procs chunks.
198
        $chunks   = array_chunk($arr, ceil((count($arr) / $procs)));
0 ignored issues
show
Bug introduced by
ceil(count($arr) / $procs) of type double is incompatible with the type integer expected by parameter $size of array_chunk(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

198
        $chunks   = array_chunk($arr, /** @scrutinizer ignore-type */ ceil((count($arr) / $procs)));
Loading history...
199
        $pid      = -1;
0 ignored issues
show
Unused Code introduced by
The assignment to $pid is dead and can be removed.
Loading history...
200
        $children = array();
201
        foreach ($chunks as $items) {
202
            $pid = pcntl_fork();
203
            if ($pid === -1) {
204
                die('could not fork');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
205
            } else if ($pid === 0) {
206
                // We are the child process. Pass a chunk of items to process.
207
                echo('['.getmypid().']This Process executed at'.date("F d, Y h:i:s A")."\n") ;
208
                array_walk($items, $func,$params);
209
                exit(0);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
210
            } else {
211
                // We are the parent.
212
                echo('['.getmypid().']This Process executed at'.date("F d, Y h:i:s A")."\n") ;
213
                $children[] = $pid;
214
            }
215
        }
216
        // Wait for children to finish.
217
        foreach ($children as $pid) {
218
            // We are still the parent.
219
            pcntl_waitpid($pid, $status);
220
        }
221
    }
222