Passed
Branch master (c73d10)
by Stefan
02:51 queued 55s
created

Math::toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\PNServer\Utils;
5
6
/*
7
 * extracted required classes and functions from package
8
 *		spomky-labs/jose
9
 *		https://github.com/Spomky-Labs/Jose 
10
 *
11
 * @package PNServer
12
 * @version 1.0.0
13
 * @copyright MIT License - see the copyright below and LICENSE file for details
14
 */
15
16
/*
17
 * *********************************************************************
18
 * Copyright (c) 2014-2016 Spomky-Labs
19
 *
20
 * Permission is hereby granted, free of charge, to any person obtaining
21
 * a copy of this software and associated documentation files (the "Software"),
22
 * to deal in the Software without restriction, including without limitation
23
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
24
 * and/or sell copies of the Software, and to permit persons to whom the
25
 * Software is furnished to do so, subject to the following conditions:
26
 *
27
 * The above copyright notice and this permission notice shall be included
28
 * in all copies or substantial portions of the Software.
29
 *
30
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
31
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
33
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
34
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
35
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
36
 * OTHER DEALINGS IN THE SOFTWARE.
37
 * ***********************************************************************
38
 */
39
40
/**
41
 * @internal
42
 */
43
class Math
44
{
45
    public static function cmp(\GMP $first, \GMP $other) : int
46
    {
47
        return \gmp_cmp($first, $other);
48
    }
49
50
    public static function equals(\GMP $first, \GMP $other) : bool
51
    {
52
        return 0 === \gmp_cmp($first, $other);
53
    }
54
55
    public static function mod(\GMP $number, \GMP $modulus) : \GMP
56
    {
57
        return \gmp_mod($number, $modulus);
1 ignored issue
show
Bug Best Practice introduced by
The expression return gmp_mod($number, $modulus) could return the type resource which is incompatible with the type-hinted return GMP. Consider adding an additional type-check to rule them out.
Loading history...
58
    }
59
60
    public static function add(\GMP $augend, \GMP $addend) : \GMP
61
    {
62
        return \gmp_add($augend, $addend);
1 ignored issue
show
Bug Best Practice introduced by
The expression return gmp_add($augend, $addend) could return the type resource which is incompatible with the type-hinted return GMP. Consider adding an additional type-check to rule them out.
Loading history...
63
    }
64
65
    public static function sub(\GMP $minuend, \GMP $subtrahend) : \GMP
66
    {
67
        return \gmp_sub($minuend, $subtrahend);
1 ignored issue
show
Bug Best Practice introduced by
The expression return gmp_sub($minuend, $subtrahend) could return the type resource which is incompatible with the type-hinted return GMP. Consider adding an additional type-check to rule them out.
Loading history...
68
    }
69
70
    public static function mul(\GMP $multiplier, \GMP $multiplicand) : \GMP
71
    {
72
        return \gmp_mul($multiplier, $multiplicand);
1 ignored issue
show
Bug Best Practice introduced by
The expression return gmp_mul($multiplier, $multiplicand) could return the type resource which is incompatible with the type-hinted return GMP. Consider adding an additional type-check to rule them out.
Loading history...
73
    }
74
75
    public static function pow(\GMP $base, int $exponent) : \GMP 
76
    {
77
        return \gmp_pow($base, $exponent);
1 ignored issue
show
Bug Best Practice introduced by
The expression return gmp_pow($base, $exponent) could return the type resource which is incompatible with the type-hinted return GMP. Consider adding an additional type-check to rule them out.
Loading history...
78
    }
79
80
    public static function bitwiseAnd(\GMP $first, \GMP $other) : \GMP
81
    {
82
        return \gmp_and($first, $other);
1 ignored issue
show
Bug Best Practice introduced by
The expression return gmp_and($first, $other) could return the type resource which is incompatible with the type-hinted return GMP. Consider adding an additional type-check to rule them out.
Loading history...
83
    }
84
85
    public static function bitwiseXor(\GMP $first, \GMP $other) : \GMP
86
    {
87
        return \gmp_xor($first, $other);
1 ignored issue
show
Bug Best Practice introduced by
The expression return gmp_xor($first, $other) could return the type resource which is incompatible with the type-hinted return GMP. Consider adding an additional type-check to rule them out.
Loading history...
88
    }
89
90
    public static function toString(\GMP  $value) : string
91
    {
92
        return \gmp_strval($value);
93
    }
94
95
    /**
96
     * @param \GMP $a
97
     * @param \GMP $m
98
     * @return \GMP|bool
99
     */
100
    public static function inverseMod(\GMP $a, \GMP $m)
101
    {
102
        return \gmp_invert($a, $m);
0 ignored issues
show
Bug Best Practice introduced by
The expression return gmp_invert($a, $m) also could return the type resource which is incompatible with the documented return type GMP|boolean.
Loading history...
103
    }
104
105
    /**
106
     * @param int|string $number
107
     * @param int $from
108
     * @param int $to
109
     * @return string
110
     */
111
    public static function baseConvert($number, int $from, int $to) : string
112
    {
113
        return \gmp_strval(\gmp_init($number, $from), $to);
114
    }
115
116
    public static function rightShift(\GMP $number, int $positions) : \GMP 
117
    {
118
        // when using \gmp_div, phpStan says: Method SKien\PNServer\Utils\Math::rightShift() should return GMP but returns resource. ?
119
        return \gmp_div_q($number, \gmp_pow(\gmp_init(2, 10), $positions));
1 ignored issue
show
Bug Best Practice introduced by
The expression return gmp_div_q($number...it(2, 10), $positions)) could return the type resource which is incompatible with the type-hinted return GMP. Consider adding an additional type-check to rule them out.
Loading history...
120
    }
121
    
122
    public static function modSub(\GMP $minuend, \GMP $subtrahend, \GMP $modulus) : \GMP
123
   	{
124
   		return self::mod(self::sub($minuend, $subtrahend), $modulus);
125
   	}
126
    
127
   	public static function modMul(\GMP $multiplier, \GMP $muliplicand, \GMP $modulus) : \GMP
128
   	{
129
   		return self::mod(self::mul($multiplier, $muliplicand), $modulus);
130
   	}
131
    
132
   	public static function modDiv(\GMP $dividend, \GMP $divisor, \GMP $modulus) : \GMP
133
   	{
134
   		return self::mul($dividend, self::inverseMod($divisor, $modulus));
135
   	}
136
}
137