Skip to content

Commit f882651

Browse files
Thomas Kerinafk11
authored andcommitted
PHP7 support
1 parent 5ac7578 commit f882651

52 files changed

Lines changed: 456 additions & 387 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.travis.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
language: php
22

33
php:
4-
- 5.6
54
- 7
6-
- hhvm
75
- 7.1
6+
- 7.2
87
- nightly
98

109
dist: trusty
@@ -17,11 +16,11 @@ install:
1716

1817
script:
1918
- php vendor/bin/phpunit
20-
- php vendor/bin/phpcs -n --standard=PSR1,PSR2 --report=full src/
19+
- php vendor/bin/phpcs -n --standard=PSR1,PSR2 --report=full src/ tests/ examples/
2120

2221
after_success:
2322
- wget https://scrutinizer-ci.com/ocular.phar
24-
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" && "$TRAVIS_PHP_VERSION" !== "nightly" ] && [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover build/docs/clover.xml; fi;'
23+
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ] && [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover build/docs/clover.xml; fi;'
2524

2625
matrix:
2726
fast_finish: true

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
}
2424
},
2525
"require": {
26-
"mdanter/ecc": "~0.4"
26+
"mdanter/ecc": "v0.5.0"
2727
},
2828
"require-dev": {
2929
"phpunit/phpunit": "~5.0",

examples/usingParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020

2121
foreach ($set as $item) {
2222
echo $item->getBinary() . PHP_EOL;
23-
}
23+
}

examples/usingTemplates.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
// a structure-specific parser reads the internal structure (not needed for writing)
2929

3030
$vector = (new TemplateFactory())
31-
->vector(function () {}) // can be null, since we're writing
31+
->vector(function () {
32+
}) // can be null, since we're writing
3233
->getTemplate()
3334
->write([$set]);
3435

3536
echo $vector->getHex() . PHP_EOL;
3637

3738
echo $vector->getBinary() . PHP_EOL;
38-

examples/usingTemplates2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
// Use the template to read resulting Buffer
3333
$parsed = $template->parse(new Parser($binary));
3434

35-
print_r($parsed);
35+
print_r($parsed);

phpunit.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
<directory>./tests/</directory>
88
</testsuite>
99
</testsuites>
10+
<filter>
11+
<whitelist>
12+
<directory>src</directory>
13+
</whitelist>
14+
</filter>
1015
<logging>
1116
<log type="coverage-html" target="build/docs/code-coverage"/>
1217
<log type="coverage-clover" target="build/docs/clover.xml"/>

src/Buffertools/Buffer.php

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace BitWasp\Buffertools;
46

57
use Mdanter\Ecc\EccFactory;
@@ -17,20 +19,14 @@ class Buffer implements BufferInterface
1719
*/
1820
protected $buffer;
1921

20-
/**
21-
* @var GmpMathInterface
22-
*/
23-
protected $math;
24-
2522
/**
2623
* @param string $byteString
2724
* @param null|integer $byteSize
2825
* @param GmpMathInterface $math
2926
* @throws \Exception
3027
*/
31-
public function __construct($byteString = '', $byteSize = null, GmpMathInterface $math = null)
28+
public function __construct(string $byteString = '', int $byteSize = null, GmpMathInterface $math = null)
3229
{
33-
$this->math = $math ?: EccFactory::getAdapter();
3430
if ($byteSize !== null) {
3531
// Check the integer doesn't overflow its supposed size
3632
if (strlen($byteString) > $byteSize) {
@@ -64,7 +60,7 @@ public function __debugInfo()
6460
* @return Buffer
6561
* @throws \Exception
6662
*/
67-
public static function hex($hexString = '', $byteSize = null, GmpMathInterface $math = null)
63+
public static function hex(string $hexString = '', int $byteSize = null, GmpMathInterface $math = null): Buffer
6864
{
6965
if (strlen($hexString) > 0 && !ctype_xdigit($hexString)) {
7066
throw new \InvalidArgumentException('Buffer::hex: non-hex character passed');
@@ -81,7 +77,7 @@ public static function hex($hexString = '', $byteSize = null, GmpMathInterface $
8177
* @param GmpMathInterface|null $math
8278
* @return Buffer
8379
*/
84-
public static function int($integer, $byteSize = null, GmpMathInterface $math = null)
80+
public static function int($integer, $byteSize = null, GmpMathInterface $math = null): Buffer
8581
{
8682
if ($integer < 0) {
8783
throw new \InvalidArgumentException('Negative integers not supported by Buffer::int. This could be an application error, or you should be using templates.');
@@ -95,10 +91,10 @@ public static function int($integer, $byteSize = null, GmpMathInterface $math =
9591
/**
9692
* @param integer $start
9793
* @param integer|null $end
98-
* @return Buffer
94+
* @return BufferInterface
9995
* @throws \Exception
10096
*/
101-
public function slice($start, $end = null)
97+
public function slice(int $start, int $end = null): BufferInterface
10298
{
10399
if ($start > $this->getSize()) {
104100
throw new \Exception('Start exceeds buffer length');
@@ -118,15 +114,15 @@ public function slice($start, $end = null)
118114
}
119115

120116
$length = strlen($string);
121-
return new self($string, $length, $this->math);
117+
return new self($string, $length);
122118
}
123119

124120
/**
125121
* Get the size of the buffer to be returned
126122
*
127123
* @return int
128124
*/
129-
public function getSize()
125+
public function getSize(): int
130126
{
131127
return $this->size;
132128
}
@@ -136,15 +132,15 @@ public function getSize()
136132
*
137133
* @return int
138134
*/
139-
public function getInternalSize()
135+
public function getInternalSize(): int
140136
{
141137
return strlen($this->buffer);
142138
}
143139

144140
/**
145141
* @return string
146142
*/
147-
public function getBinary()
143+
public function getBinary(): string
148144
{
149145
// if a size is specified we'll make sure the value returned is that size
150146
if ($this->size !== null) {
@@ -161,15 +157,15 @@ public function getBinary()
161157
/**
162158
* @return string
163159
*/
164-
public function getHex()
160+
public function getHex(): string
165161
{
166162
return unpack("H*", $this->getBinary())[1];
167163
}
168164

169165
/**
170166
* @return \GMP
171167
*/
172-
public function getGmp()
168+
public function getGmp(): \GMP
173169
{
174170
$gmp = gmp_init($this->getHex(), 16);
175171
return $gmp;
@@ -178,24 +174,26 @@ public function getGmp()
178174
/**
179175
* @return int|string
180176
*/
181-
public function getInt()
177+
public function getInt(): string
182178
{
183179
return gmp_strval($this->getGmp(), 10);
184180
}
185181

186182
/**
187-
* @return BufferInterface
183+
* @return Buffer
188184
*/
189-
public function flip()
185+
public function flip(): Buffer
190186
{
191-
return Buffertools::flipBytes($this);
187+
/** @var Buffer $buffer */
188+
$buffer = Buffertools::flipBytes($this);
189+
return $buffer;
192190
}
193191

194192
/**
195193
* @param BufferInterface $other
196194
* @return bool
197195
*/
198-
public function equals(BufferInterface $other)
196+
public function equals(BufferInterface $other): bool
199197
{
200198
return ($other->getSize() === $this->getSize()
201199
&& $other->getBinary() === $this->getBinary());
Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace BitWasp\Buffertools;
46

57
interface BufferInterface
@@ -10,49 +12,50 @@ interface BufferInterface
1012
* @return BufferInterface
1113
* @throws \Exception
1214
*/
13-
public function slice($start, $end = null);
15+
public function slice(int $start, int $end = null): BufferInterface;
1416

1517
/**
1618
* Get the size of the buffer to be returned
1719
*
1820
* @return int
1921
*/
20-
public function getSize();
22+
public function getSize(): int;
2123

2224
/**
2325
* Get the size of the value stored in the buffer
2426
*
2527
* @return int
2628
*/
27-
public function getInternalSize();
29+
public function getInternalSize(): int;
2830

2931
/**
3032
* @return string
3133
*/
32-
public function getBinary();
34+
public function getBinary(): string;
3335

3436
/**
3537
* @return string
3638
*/
37-
public function getHex();
39+
public function getHex(): string;
3840

3941
/**
4042
* @return int|string
4143
*/
42-
public function getInt();
44+
public function getInt(): string;
4345

4446
/**
4547
* @return \GMP
4648
*/
47-
public function getGmp();
49+
public function getGmp(): \GMP;
4850

4951
/**
5052
* @return Buffer
5153
*/
52-
public function flip();
54+
public function flip(): Buffer;
5355

5456
/**
57+
* @param BufferInterface $other
5558
* @return bool
5659
*/
57-
public function equals(BufferInterface $other);
60+
public function equals(BufferInterface $other): bool;
5861
}

src/Buffertools/Buffertools.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace BitWasp\Buffertools;
46

57
class Buffertools
@@ -9,7 +11,7 @@ class Buffertools
911
* @return string
1012
* @throws \Exception
1113
*/
12-
public static function numToVarIntBin($decimal)
14+
public static function numToVarIntBin(int $decimal): string
1315
{
1416
if ($decimal < 0xfd) {
1517
$bin = chr($decimal);
@@ -31,10 +33,10 @@ public static function numToVarIntBin($decimal)
3133
* Convert a decimal number into a VarInt Buffer
3234
*
3335
* @param integer $decimal
34-
* @return Buffer
36+
* @return BufferInterface
3537
* @throws \Exception
3638
*/
37-
public static function numToVarInt($decimal)
39+
public static function numToVarInt(int $decimal): BufferInterface
3840
{
3941
return new Buffer(static::numToVarIntBin($decimal));
4042
}
@@ -67,7 +69,7 @@ public static function flipBytes($bytes)
6769
* @param int $size
6870
* @return BufferInterface
6971
*/
70-
public static function concat(BufferInterface $buffer1, BufferInterface $buffer2, $size = null)
72+
public static function concat(BufferInterface $buffer1, BufferInterface $buffer2, int $size = null)
7173
{
7274
return new Buffer($buffer1->getBinary() . $buffer2->getBinary(), $size);
7375
}
@@ -89,7 +91,7 @@ public static function concat(BufferInterface $buffer1, BufferInterface $buffer2
8991
* @param callable $convertToBuffer
9092
* @return array
9193
*/
92-
public static function sort(array $items, callable $convertToBuffer = null)
94+
public static function sort(array $items, callable $convertToBuffer = null): array
9395
{
9496
if (null == $convertToBuffer) {
9597
$convertToBuffer = function ($value) {

src/Buffertools/ByteOrder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace BitWasp\Buffertools;
46

57
class ByteOrder

0 commit comments

Comments
 (0)