1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* HiPanel core package |
5
|
|
|
* |
6
|
|
|
* @link https://hipanel.com/ |
7
|
|
|
* @package hipanel-core |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2014-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hipanel\base; |
13
|
|
|
|
14
|
|
|
class Err |
15
|
|
|
{ |
16
|
|
|
public static function is($data, $eq = null) |
17
|
|
|
{ |
18
|
|
|
return is_null($eq) ? (is_array($data) ? array_key_exists('_error', $data) : !$data) : (isset($data['_error']) && $data['_error'] === $eq); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public static function not($data, $eq = null) |
22
|
|
|
{ |
23
|
|
|
return !static::is($data, $eq); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public static function get($data, $df = null) |
27
|
|
|
{ |
28
|
|
|
return isset($data['_error']) ? $data['_error'] : $df; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function set($data, $code, $ops = null) |
32
|
|
|
{ |
33
|
|
|
if (!is_array($data)) { |
34
|
|
|
$data = ['_error' => $code]; |
35
|
|
|
} else { |
36
|
|
|
$data['_error'] = $code; |
37
|
|
|
} |
38
|
|
|
if ($ops) { |
39
|
|
|
$data['_error_ops'] = $ops; |
40
|
|
|
} |
41
|
|
|
return $data; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function setifnot($data, $default = 'unknown error') |
45
|
|
|
{ |
46
|
|
|
if (static::not($data)) { |
47
|
|
|
return $data; |
48
|
|
|
} |
49
|
|
|
if (!static::get($data)) { |
50
|
|
|
static::set($data, $default); |
51
|
|
|
} |
52
|
|
|
return $data; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public static function reduce($res) |
56
|
|
|
{ |
57
|
|
|
if (!count($res)) { |
58
|
|
|
return static::set($res, 'nothing was done'); |
59
|
|
|
} |
60
|
|
|
$errors = []; |
61
|
|
|
foreach ($res as $k => $v) { |
62
|
|
|
if (static::is($v)) { |
63
|
|
|
$errors[$k] = static::get($v); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
$row_num = count($res); |
67
|
|
|
$err_num = count($errors); |
68
|
|
|
if ($err_num) { |
69
|
|
|
$res['_error'] = $row_num < 2 ? reset($errors) : (($row_num > $err_num) ? 'partially' : 'completely') . " failed ($err_num/$row_num): " . implode(', ', array_unique($errors)); |
70
|
|
|
} |
71
|
|
|
/* if ($set_nums) { |
|
|
|
|
72
|
|
|
$res['_error_num'] = $err_num; |
73
|
|
|
$res['_total_num'] = $row_num; |
74
|
|
|
}; */ |
75
|
|
|
return $res; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public static function clean($data) |
79
|
|
|
{ |
80
|
|
|
if (is_array($data)) { |
81
|
|
|
foreach ($data as $k => $v) { |
82
|
|
|
if (substr($k, 0, 6) === '_error') { |
83
|
|
|
unset($data[$k]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
return $data; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public static function isError($data, $eq = null) |
91
|
|
|
{ |
92
|
|
|
return is_null($eq) ? (is_array($data) ? array_key_exists('_error', $data) : !$data) : (isset($data['_error']) && $data['_error'] === $eq); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public static function getError($data, $df = null) |
96
|
|
|
{ |
97
|
|
|
return isset($data['_error']) ? $data['_error'] : $df; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.