Passed
Push — trunk ( 9d93ee...00dc82 )
by SuperNova.WS
03:42
created

unserializeOrJsonDecode()   B

Complexity

Conditions 4
Paths 7

Size

Total Lines 34
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 14
c 1
b 0
f 1
nc 7
nop 2
dl 0
loc 34
rs 8.5806
1
<?php
2
/**
3
 * Created by Gorlum 04.12.2017 4:25
4
 */
5
6
/**
7
 * Format $value to ID
8
 *
9
 * @param     $value
10
 * @param int $default
11
 *
12
 * @return float|int
13
 */
14
function idval($value, $default = 0) {
15
  $value = floatval($value);
16
17
  return preg_match('#^(\d*)#', $value, $matches) && $matches[1] ? floatval($matches[1]) : $default;
18
}
19
20
21
/**
22
 * @param int|float      $number
23
 * @param true|int|float $compareTo
24
 *    true             - compare to zero from above i.e. resources amount ($number > 0 for positive)
25
 *    numeric positive - compare to $compareTo from below i.e. price with resource amount ($number < $compareTo for positive)
26
 *    numeric negative - compare to -$compareTo from above i.e. resource amount with price ($n > -$compareTo for positive)
27
 *
28
 * @return string
29
 */
30
function prettyNumberGetClass($number, $compareTo) {
31
  $n = floor($number);
32
33
  if ($compareTo === true) {
34
    $class = $n == 0 ? 'zero' : ($n > 0 ? 'positive' : 'negative');
35
  } elseif ($compareTo >= 0) {
36
    $class = $n == $compareTo ? 'zero' : ($n < $compareTo ? 'positive' : 'negative');
37
  } else {
38
    $class = ($n == -$compareTo) ? 'zero' : ($n < -$compareTo ? 'negative' : 'positive');
39
  }
40
41
  return $class;
42
}
43
44
/**
45
 * Return number floored, formatted and styled with "span"
46
 *
47
 * @param int|float $number
48
 *
49
 * @return string
50
 * @see PtlVariableDecorator
51
 * // TODO - this should be made in templates
52
 */
53
function prettyNumberStyledDefault($number) {
54
  return prettyNumberStyledCompare($number, true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer|double expected by parameter $compareTo of prettyNumberStyledCompare(). ( Ignorable by Annotation )

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

54
  return prettyNumberStyledCompare($number, /** @scrutinizer ignore-type */ true);
Loading history...
55
}
56
57
/**
58
 * @param int|float $number
59
 * @param int|float $compareTo
60
 *
61
 * @return string
62
 *
63
 * // TODO - this should be made in templates
64
 */
65
function prettyNumberStyledCompare($number, $compareTo) {
66
  return
67
    '<span class="' . prettyNumberGetClass($number, $compareTo) . '">' .
68
    HelperString::numberFloorAndFormat($number) .
69
    '</span>';
70
}
71
72
// ----------------------------------------------------------------------------------------------------------------
73
function pretty_time($seconds) {
74
  $day = floor($seconds / (24 * 3600));
75
76
  return sprintf('%s%02d:%02d:%02d', $day ? $day . SN::$lang['sys_day_short'] . ' ' : '', floor($seconds / 3600 % 24), floor($seconds / 60 % 60), floor($seconds / 1 % 60));
77
}
78
79
function sys_time_human($time, $full = false) {
80
  global $lang;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
81
82
  $seconds = $time % 60;
83
  $time = floor($time / 60);
84
  $minutes = $time % 60;
85
  $time = floor($time / 60);
86
  $hours = $time % 24;
87
  $time = floor($time / 24);
88
89
  return
90
    ($full || $time ? "{$time} {$lang['sys_day']}&nbsp;" : '') .
91
    ($full || $hours ? "{$hours} {$lang['sys_hrs']}&nbsp;" : '') .
92
    ($full || $minutes ? "{$minutes} {$lang['sys_min']}&nbsp;" : '') .
93
    ($full || !$time || $seconds ? "{$seconds} {$lang['sys_sec']}" : '');
94
}
95
96
function sys_time_human_system($time) {
97
  return $time ? date(FMT_DATE_TIME_SQL, $time) . " ({$time}), " . sys_time_human(SN_TIME_NOW - $time) : '{NEVER}';
98
}
99
100
if (!function_exists('strptime')) {
101
  function strptime($date, $format) {
102
    $masks = array(
103
      '%d' => '(?P<d>[0-9]{2})',
104
      '%m' => '(?P<m>[0-9]{2})',
105
      '%Y' => '(?P<Y>[0-9]{4})',
106
      '%H' => '(?P<H>[0-9]{2})',
107
      '%M' => '(?P<M>[0-9]{2})',
108
      '%S' => '(?P<S>[0-9]{2})',
109
      // usw..
110
    );
111
112
    $rexep = "#" . strtr(preg_quote($format), $masks) . "#";
113
    if (preg_match($rexep, $date, $out)) {
114
      $ret = array(
115
        "tm_sec"  => (int)$out['S'],
116
        "tm_min"  => (int)$out['M'],
117
        "tm_hour" => (int)$out['H'],
118
        "tm_mday" => (int)$out['d'],
119
        "tm_mon"  => $out['m'] ? $out['m'] - 1 : 0,
120
        "tm_year" => $out['Y'] > 1900 ? $out['Y'] - 1900 : 0,
121
      );
122
    } else {
123
      $ret = false;
124
    }
125
126
    return $ret;
127
  }
128
}
129
130
131
function js_safe_string($string) {
132
  return str_replace(array("\r", "\n"), array('\r', '\n'), addslashes($string));
133
}
134
135
function sys_safe_output($string) {
136
  return str_replace(array("&", "\"", "<", ">", "'"), array("&amp;", "&quot;", "&lt;", "&gt;", "&apos;"), $string);
137
}
138
139
function str_raw2unsafe($raw) {
140
  return trim(strip_tags($raw));
141
}
142
143
function ip2longu($ip) {
144
  return sprintf('%u', floatval(ip2long($ip)));
145
}
146
147
/**
148
 * @param int $fullDate
149
 *
150
 * @return int
151
 */
152
function datePart($fullDate) {
153
  return (int)strtotime(date('Y-m-d', $fullDate));
154
}
155
156
157
/**
158
 * Fall-back function to support old, serialize()-d data
159
 *
160
 * @param string $var
161
 *
162
 * @return mixed|string
163
 * @deprecated
164
 */
165
function unserializeOrJsonDecode($var, $asArray = true) {
166
  // Variable is not a string - returning it back
167
  if (!is_string($var)) {
0 ignored issues
show
introduced by
The condition is_string($var) is always true.
Loading history...
168
    return $var;
169
  }
170
171
  set_error_handler(
172
  /**
173
   * @param $errno
174
   * @param $errstr
175
   * @param $errfile
176
   * @param $errline
177
   *
178
   * @return bool
179
   * @throws ErrorException
180
   */
181
    function ($errno, $errstr, $errfile, $errline) {
182
      throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
183
    }
184
  );
185
186
  try {
187
    $result = unserialize($var);
188
  } catch (ErrorException $e) {
189
    // String is not a serialized variable. May be it's a json?
190
    $result = json_decode($var, $asArray);
191
    if (JSON_ERROR_NONE != json_last_error()) {
192
      $result = $var;
193
    }
194
  } finally {
195
    restore_error_handler();
196
  }
197
198
  return $result;
199
}
200