• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / core-library / 5185546495

pending completion
5185546495

push

github

webeweb
Fix unit tests

49100 of 49247 relevant lines covered (99.7%)

6.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.67
/src/types/Helper/IntegerHelper.php
1
<?php
2

3
/*
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace WBW\Library\Types\Helper;
13

14
use WBW\Library\Types\Exception\IntegerArgumentException;
15

16
/**
17
 * Integer helper.
18
 *
19
 * @author webeweb <https://github.com/webeweb>
20
 * @package WBW\Library\Types\Helper
21
 */
22
class IntegerHelper {
23

24
    /**
25
     * Factorial.
26
     *
27
     * @param int|null $n The number.
28
     * @return float|null Returns the factorial.
29
     */
30
    public static function factorial(?int $n): ?float {
31

32
        if (null === $n || $n < 0) {
2✔
33
            return null;
2✔
34
        }
35

36
        if (0 === $n) {
1✔
37
            return 1;
1✔
38
        }
39

40
        $result = $n;
1✔
41
        while (1 < --$n) {
1✔
42
            $result *= $n;
1✔
43
        }
44

45
        return $result;
1✔
46
    }
47

48
    /**
49
     * Determine if a value is an integer.
50
     *
51
     * @param mixed $value The value.
52
     * @return void
53
     * @throws IntegerArgumentException Throws an integer argument exception if the value is not of expected type.
54
     */
55
    public static function isInteger($value): void {
56
        if (false === is_integer($value)) {
2✔
57
            throw new IntegerArgumentException($value);
1✔
58
        }
59
    }
60

61
    /**
62
     * Parse a boolean.
63
     *
64
     * @param bool|null $value The boolean value.
65
     * @return int Returns 1 in case of success, 0 otherwise.
66
     */
67
    public static function parseBoolean(?bool $value): int {
68
        return $value === true ? 1 : 0;
4✔
69
    }
70

71
    /**
72
     * Parse a string.
73
     *
74
     * @param string|null $value The string value.
75
     * @return int|null Returns the integer in case of success, null otherwise.
76
     * @throws IntegerArgumentException Throws an integer argument exception if the string value does not represent an integer.
77
     */
78
    public static function parseString(?string $value): ?int {
79

80
        if (null === $value) {
7✔
81
            return null;
1✔
82
        }
83

84
        if (0 === preg_match("/^-?[0-9]+$/", $value)) {
7✔
85
            throw new IntegerArgumentException($value);
1✔
86
        }
87

88
        return intval($value);
6✔
89
    }
90

91
    /**
92
     * Summation.
93
     *
94
     * @param int|null $n The number.
95
     * @return float|null Returns the summation.
96
     */
97
    public static function summation(?int $n): ?float {
98

99
        if (null === $n) {
1✔
100
            return null;
×
101
        }
102

103
        $q = $n * ($n + 1);
1✔
104

105
        return $q / 2;
1✔
106
    }
107

108
    /**
109
     * Usort callback.
110
     *
111
     * @param bool $asc ASC ?
112
     * @return callable Returns the usort callback.
113
     */
114
    public static function usortCallback(bool $asc = true): callable {
115

116
        return function(?int $int1, ?int $int2) use ($asc): int {
4✔
117

118
            $result = null;
4✔
119

120
            if ($int1 < $int2) {
4✔
121
                $result = -1;
4✔
122
            }
123
            if ($int1 === $int2) {
4✔
124
                $result = 0;
4✔
125
            }
126
            if ($int1 > $int2) {
4✔
127
                $result = 1;
4✔
128
            }
129

130
            return true === $asc ? $result : -$result;
4✔
131
        };
4✔
132
    }
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc