Passed
Push — 0.8.x ( 54626d...382f48 )
by Alexander
06:05 queued 02:55
created

Helper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 90
rs 10
c 2
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A length() 0 9 2
B formatTime() 0 25 7
A substr() 0 9 2
A width() 0 9 2
1
<?php
2
3
/**
4
 * Lenevor Framework
5
 *
6
 * LICENSE
7
 *
8
 * This source file is subject to the new BSD license that is bundled
9
 * with this package in the file license.md.
10
 * It is also available through the world-wide-web at this URL:
11
 * https://lenevor.com/license
12
 * If you did not receive a copy of the license and are unable to
13
 * obtain it through the world-wide-web, please send an email
14
 * to [email protected] so we can send you a copy immediately.
15
 *
16
 * @package     Lenevor
17
 * @subpackage  Base
18
 * @link        https://lenevor.com
19
 * @copyright   Copyright (c) 2019 - 2023 Alexander Campo <[email protected]>
20
 * @license     https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md
21
 */
22
23
namespace Syscodes\Components\Console\Helper;
24
25
/**
26
 * Helper is the base class for all helper classes.
27
 */
28
class Helper
29
{
30
    /**
31
     * The width is how many characters positions the string will use.
32
     * 
33
     * @param  string|null  $value
34
     * 
35
     * @return int
36
     */
37
    public static function width(?string $value): int
38
    {
39
        $value ?? '';
40
        
41
        if (false === $encoding = mb_detect_encoding($value, null, true)) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_detect_encoding() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

41
        if (false === $encoding = mb_detect_encoding(/** @scrutinizer ignore-type */ $value, null, true)) {
Loading history...
42
            return strlen($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

42
            return strlen(/** @scrutinizer ignore-type */ $value);
Loading history...
43
        }
44
        
45
        return mb_strwidth($value, $encoding);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_strwidth() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

45
        return mb_strwidth(/** @scrutinizer ignore-type */ $value, $encoding);
Loading history...
46
    }
47
    
48
    /**
49
     * The length is related to how many bytes the string will use.
50
     * 
51
     * @param  string|null  $value
52
     * 
53
     * @return int
54
     */
55
    public static function length(?string $value): int
56
    {
57
        $value ?? '';
58
        
59
        if (false === $encoding = mb_detect_encoding($value, null, true)) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_detect_encoding() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        if (false === $encoding = mb_detect_encoding(/** @scrutinizer ignore-type */ $value, null, true)) {
Loading history...
60
            return strlen($value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

60
            return strlen(/** @scrutinizer ignore-type */ $value);
Loading history...
61
        }
62
        
63
        return mb_strlen($value, $encoding);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

63
        return mb_strlen(/** @scrutinizer ignore-type */ $value, $encoding);
Loading history...
64
    }
65
66
    /**
67
     * Returns the subset of a string, using mb_substr if it is available.
68
     * 
69
     * @param  string|null  $value
70
     * @param  int  $from
71
     * @param  int|null  $length
72
     * 
73
     * @return string
74
     */
75
    public static function substr(?string $value, int $from, int $length = null): string
76
    {
77
        $value ?? '';
78
        
79
        if (false === $encoding = mb_detect_encoding($value, null, true)) {
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_detect_encoding() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

79
        if (false === $encoding = mb_detect_encoding(/** @scrutinizer ignore-type */ $value, null, true)) {
Loading history...
80
            return substr($value, $from, $length);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

80
            return substr(/** @scrutinizer ignore-type */ $value, $from, $length);
Loading history...
81
        }
82
        
83
        return mb_substr($value, $from, $length, $encoding);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $string of mb_substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

83
        return mb_substr(/** @scrutinizer ignore-type */ $value, $from, $length, $encoding);
Loading history...
84
    }
85
    
86
    /**
87
     * Get the format for time of an item result to response time in console.
88
     * 
89
     * @param  int|float  $seconds
90
     * 
91
     * @return string
92
     */
93
    public static function formatTime(int|float $secs)
94
    {
95
        static $timeFormats = [
96
            [0, '< 1 sec'],
97
            [1, '1 sec'],
98
            [2, 'secs', 1],
99
            [60, '1 min'],
100
            [120, 'mins', 60],
101
            [3600, '1 hr'],
102
            [7200, 'hrs', 3600],
103
            [86400, '1 day'],
104
            [172800, 'days', 86400],
105
        ];
106
107
        foreach ($timeFormats as $index => $format) {
108
            if ($secs >= $format[0]) {
109
                if ((isset($timeFormats[$index + 1]) && 
110
                           $secs < $timeFormats[$index + 1][0]) ||
111
                           $index == \count($timeFormats) - 1
112
                ) {
113
                    if (2 == \count($format)) {
114
                        return $format[1];
115
                    }
116
117
                    return floor($secs / $format[2]).' '.$format[1];
118
                }
119
            }
120
        }
121
    }
122
}