Completed
Push — master ( 73f6e1...42bf17 )
by Sergey
04:22
created

Binary::length()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 2
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 10 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace ButterAMQP;
4
5 1
Binary::init();
6
7
/**
8
 * Simple set of static methods to polyfill some PHP functions.
9
 */
10
class Binary
11
{
12
    /**
13
     * Check if machine endian format is big endian format.
14
     *
15
     * @var bool
16
     */
17
    private static $isBigEndian = false;
18
19
    /**
20
     * Initialize flags.
21
     */
22 1
    public static function init()
23
    {
24 1
        self::$isBigEndian = unpack('S', "\x00\x01")[1] == 1;
25 1
    }
26
27
    /**
28
     * Pack a value.
29
     *
30
     * @param string $format
31
     * @param string $value
32
     *
33
     * @return string
34
     */
35 41
    public static function pack($format, $value)
36
    {
37 41
        return pack($format, $value);
38
    }
39
40
    /**
41
     * Pack a value and enforce big endian format.
42
     *
43
     * @param string $format
44
     * @param string $value
45
     *
46
     * @return string
47
     */
48 46
    public static function packbe($format, $value)
49
    {
50 46
        return self::$isBigEndian ? pack($format, $value) : strrev(pack($format, $value));
51
    }
52
53
    /**
54
     * Unpack a value.
55
     *
56
     * @param string $format
57
     * @param string $data
58
     *
59
     * @return string
60
     */
61 41
    public static function unpack($format, $data)
62
    {
63 41
        return unpack($format, $data)[1];
64
    }
65
66
    /**
67
     * Unpack a value and enforce big endian format.
68
     *
69
     * @param string $format
70
     * @param string $data
71
     *
72
     * @return string
73
     */
74 42
    public static function unpackbe($format, $data)
75
    {
76 42
        return self::$isBigEndian ? unpack($format, $data)[1] : unpack($format, strrev($data))[1];
77
    }
78
}
79