Issues (2963)

includes/helpers.php (1 issue)

1
<?php
2
/**
3
 * helpers.php
4
 *
5
 * Functions available in both Laravel and Legacy code (must not call any other legacy functions)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 *
20
 * @link       https://www.librenms.org
21
 *
22
 * @copyright  2018 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
use LibreNMS\Util\Debug;
27
use LibreNMS\Util\Laravel;
28
29
if (! function_exists('d_echo')) {
30
    /**
31
     * Legacy convenience function - please use this instead of 'if (Debug::isEnabled()) { echo ...; }'
32
     * Use Log directly in pure Laravel code!
33
     *
34
     * @param  string|array  $text  The error message or array to print
35
     * @param  string  $no_debug_text  Text to print if debug is disabled
36
     */
37
    function d_echo($text, $no_debug_text = null)
38
    {
39
        if (Laravel::isBooted()) {
40
            \Log::debug(is_string($text) ? rtrim($text) : $text);
41
        } elseif (Debug::isEnabled()) {
42
            print_r($text);
43
        }
44
45
        if (! Debug::isEnabled() && $no_debug_text) {
46
            echo "$no_debug_text";
47
        }
48
    }
49
}
50
51
if (! function_exists('array_pairs')) {
52
    /**
53
     * Get all consecutive pairs of values in an array.
54
     * [1,2,3,4] -> [[1,2],[2,3],[3,4]]
55
     *
56
     * @param  array  $array
57
     * @return array
58
     */
59
    function array_pairs($array)
60
    {
61
        $pairs = [];
62
63
        for ($i = 1; $i < count($array); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
64
            $pairs[] = [$array[$i - 1], $array[$i]];
65
        }
66
67
        return $pairs;
68
    }
69
}
70
71
/**
72
 * Cast string to int or float.
73
 * Returns 0 if string is not numeric
74
 *
75
 * @param  string  $number
76
 * @return float|int
77
 */
78
function cast_number($number)
79
{
80
    return \LibreNMS\Util\Number::cast($number);
81
}
82
83
if (! function_exists('trans_fb')) {
84
    /**
85
     * Translate the given message with a fallback string if none exists.
86
     *
87
     * @param  string  $key
88
     * @param  string  $fallback
89
     * @param  array  $replace
90
     * @param  string  $locale
91
     * @return \Symfony\Component\Translation\TranslatorInterface|string
92
     */
93
    function trans_fb($key, $fallback, $replace = [], $locale = null)
94
    {
95
        return ($key === ($translation = trans($key, $replace, $locale))) ? $fallback : $translation;
96
    }
97
}
98