1
0
mirror of https://github.com/danog/PHPStruct.git synced 2024-11-30 04:19:08 +01:00

Merge pull request #2 from danog/analysis-z4wNP1

Applied fixes from StyleCI
This commit is contained in:
Daniil Gentili 2016-07-25 12:44:13 +02:00 committed by GitHub
commit ed97d0d401
2 changed files with 64 additions and 55 deletions

View File

@ -192,35 +192,35 @@ class Struct
'ENDIANNESS' => $this->LITTLE_ENDIAN_TABLE, 'ENDIANNESS' => $this->LITTLE_ENDIAN_TABLE,
'SIZE' => $this->SIZE, 'SIZE' => $this->SIZE,
'FORMATS' => $this->FORMATS, 'FORMATS' => $this->FORMATS,
'TYPE' => $this->TYPE 'TYPE' => $this->TYPE,
], ],
'>' => [ '>' => [
'BIG_ENDIAN' => true, 'BIG_ENDIAN' => true,
'ENDIANNESS' => $this->BIG_ENDIAN_TABLE, 'ENDIANNESS' => $this->BIG_ENDIAN_TABLE,
'SIZE' => $this->SIZE, 'SIZE' => $this->SIZE,
'FORMATS' => $this->FORMATS, 'FORMATS' => $this->FORMATS,
'TYPE' => $this->TYPE 'TYPE' => $this->TYPE,
], ],
'!' => [ '!' => [
'BIG_ENDIAN' => true, 'BIG_ENDIAN' => true,
'ENDIANNESS' => $this->BIG_ENDIAN_TABLE, 'ENDIANNESS' => $this->BIG_ENDIAN_TABLE,
'SIZE' => $this->SIZE, 'SIZE' => $this->SIZE,
'FORMATS' => $this->FORMATS, 'FORMATS' => $this->FORMATS,
'TYPE' => $this->TYPE 'TYPE' => $this->TYPE,
], ],
'=' => [ '=' => [
'BIG_ENDIAN' => $this->BIG_ENDIAN, 'BIG_ENDIAN' => $this->BIG_ENDIAN,
'ENDIANNESS' => $this->NATIVE_ENDIAN_TABLE, 'ENDIANNESS' => $this->NATIVE_ENDIAN_TABLE,
'SIZE' => $this->SIZE, 'SIZE' => $this->SIZE,
'FORMATS' => $this->FORMATS, 'FORMATS' => $this->FORMATS,
'TYPE' => $this->TYPE 'TYPE' => $this->TYPE,
], ],
'@' => [ '@' => [
'BIG_ENDIAN' => $this->BIG_ENDIAN, 'BIG_ENDIAN' => $this->BIG_ENDIAN,
'ENDIANNESS' => $this->NATIVE_ENDIAN_TABLE, 'ENDIANNESS' => $this->NATIVE_ENDIAN_TABLE,
'SIZE' => $this->NATIVE_SIZE, 'SIZE' => $this->NATIVE_SIZE,
'FORMATS' => $this->NATIVE_FORMATS, 'FORMATS' => $this->NATIVE_FORMATS,
'TYPE' => $this->NATIVE_TYPE 'TYPE' => $this->NATIVE_TYPE,
], ],
]; ];
} }
@ -236,7 +236,7 @@ class Struct
if (error_reporting() === 0) { if (error_reporting() === 0) {
return true; // return true to continue through the others error handlers return true; // return true to continue through the others error handlers
} }
throw new StructException($errstr . " on line " . $errline, $errno); throw new StructException($errstr.' on line '.$errline, $errno);
} }
/** /**
@ -318,7 +318,7 @@ class Struct
$curresult = strrev($curresult); $curresult = strrev($curresult);
} // Reverse if wrong endianness } // Reverse if wrong endianness
if (strlen($curresult) != $command['modifiers']['SIZE'] * $command['count']) { if (strlen($curresult) != $command['modifiers']['SIZE'] * $command['count']) {
throw new StructException("Size of packed data from format char " . $command['format'] ." (".strlen($curresult).") isn't equal to expected size (".$command['modifiers']['SIZE'] * $command['count'].")."); throw new StructException('Size of packed data from format char '.$command['format'].' ('.strlen($curresult).") isn't equal to expected size (".$command['modifiers']['SIZE'] * $command['count'].').');
} }
/* /*
if (strlen($curresult) > $command['modifiers']['SIZE'] * $command['count']) { if (strlen($curresult) > $command['modifiers']['SIZE'] * $command['count']) {
@ -533,7 +533,7 @@ class Struct
'BIG_ENDIAN' => $modifier['BIG_ENDIAN'], 'BIG_ENDIAN' => $modifier['BIG_ENDIAN'],
'FORMAT_ENDIANNESS' => $modifier['ENDIANNESS'][$currentformatchar], 'FORMAT_ENDIANNESS' => $modifier['ENDIANNESS'][$currentformatchar],
'SIZE' => $modifier['SIZE'][$currentformatchar], 'SIZE' => $modifier['SIZE'][$currentformatchar],
'TYPE' => $modifier['TYPE'][$currentformatchar] 'TYPE' => $modifier['TYPE'][$currentformatchar],
]; ];
if ($unpack) { if ($unpack) {
if ($arraycount[$datarraycount] != $result[$formatcharcount]['count'] * $result[$formatcharcount]['modifiers']['SIZE']) { if ($arraycount[$datarraycount] != $result[$formatcharcount]['count'] * $result[$formatcharcount]['modifiers']['SIZE']) {
@ -642,6 +642,7 @@ class Struct
return $count; return $count;
} }
/** /**
* num_pack_unsigned. * num_pack_unsigned.
* *
@ -656,7 +657,7 @@ class Struct
* *
* @return Byte string * @return Byte string
**/ **/
public function num_pack($n, $blocksize = 1, $unsigned) public function num_pack($n, $blocksize, $unsigned)
{ {
$bitnumber = $blocksize * 8; $bitnumber = $blocksize * 8;
if ($unsigned) { if ($unsigned) {
@ -667,7 +668,7 @@ class Struct
$min = -pow(2, $bitnumber - 1); $min = -pow(2, $bitnumber - 1);
} }
if ($n < $min || $n > $max) { if ($n < $min || $n > $max) {
trigger_error("Number is not within required range (".$min." <= number <= ".$max.")."); trigger_error('Number is not within required range ('.$min.' <= number <= '.$max.').');
} }
if (!$unsigned && $n < 0) { if (!$unsigned && $n < 0) {
$n = (-($n) ^ (pow(2, $bitnumber) - 1)) + 1; $n = (-($n) ^ (pow(2, $bitnumber) - 1)) + 1;
@ -679,23 +680,25 @@ class Struct
} }
$break = true; $break = true;
foreach ($this->range(strlen($s)) as $i) { foreach ($this->range(strlen($s)) as $i) {
if ($s[$i] != pack("@")[0]) { if ($s[$i] != pack('@')[0]) {
$break = false; $break = false;
break; break;
} }
} }
if ($break) { if ($break) {
$s = pack("@1"); $s = pack('@1');
$i = 0; $i = 0;
} }
$s = substr($s, $i); $s = substr($s, $i);
if (strlen($s) < $blocksize) { if (strlen($s) < $blocksize) {
$s = pack('@'.($blocksize - strlen($s))).$s; $s = pack('@'.($blocksize - strlen($s))).$s;
} elseif (strlen($s) > $blocksize) { } elseif (strlen($s) > $blocksize) {
trigger_error("Generated data length (".strlen($s).") is bigger than required length (".$blocksize.")."); trigger_error('Generated data length ('.strlen($s).') is bigger than required length ('.$blocksize.').');
} }
return $s; return $s;
} }
/** /**
* num_unpack. * num_unpack.
* *
@ -706,14 +709,14 @@ class Struct
* @param $blocksize Block size * @param $blocksize Block size
* @param $unsigned Boolean that determines whether to work in signed or unsigned mode * @param $unsigned Boolean that determines whether to work in signed or unsigned mode
* *
* @return Float or int with the unpack value * @return float or int with the unpack value
**/ **/
public function num_unpack($s, $blocksize, $unsigned) public function num_unpack($s, $blocksize, $unsigned)
{ {
$length = strlen($s); $length = strlen($s);
$bitnumber = $blocksize * 8; $bitnumber = $blocksize * 8;
if ($length != $blocksize) { if ($length != $blocksize) {
trigger_error("Given data length (".$length.") is different from the required length (".$blocksize.")."); trigger_error('Given data length ('.$length.') is different from the required length ('.$blocksize.').');
} }
$acc = 0; $acc = 0;
foreach ($this->range(0, $length, 1) as $i) { foreach ($this->range(0, $length, 1) as $i) {
@ -722,8 +725,10 @@ class Struct
if (!$unsigned && $acc > (pow(2, ($bitnumber) - 1) - 1)) { if (!$unsigned && $acc > (pow(2, ($bitnumber) - 1) - 1)) {
$acc = -((($acc) ^ (pow(2, $bitnumber) - 1)) + 1); $acc = -((($acc) ^ (pow(2, $bitnumber) - 1)) + 1);
} }
return $acc; return $acc;
} }
/** /**
* range. * range.
* *
@ -732,9 +737,10 @@ class Struct
* @param $start Beginning of the range (or stop if no other params are specified) * @param $start Beginning of the range (or stop if no other params are specified)
* @param $stop End of the range * @param $stop End of the range
* @param $step Step to use in range * @param $step Step to use in range
*
* @return array with the range * @return array with the range
**/ **/
function range($start, $stop = null, $step = 1) public function range($start, $stop = null, $step = 1)
{ {
if ($stop === null) { if ($stop === null) {
$stop = $start; $stop = $start;
@ -743,6 +749,7 @@ class Struct
if ($stop <= $start && $step < 0) { if ($stop <= $start && $step < 0) {
$arr = range($stop, $start, -$step); $arr = range($stop, $start, -$step);
array_pop($arr); array_pop($arr);
return array_reverse($arr, false); return array_reverse($arr, false);
} }
if ($step > 1 && $step > ($stop - $start)) { if ($step > 1 && $step > ($stop - $start)) {
@ -751,8 +758,10 @@ class Struct
$arr = range($start, $stop, $step); $arr = range($start, $stop, $step);
array_pop($arr); array_pop($arr);
} }
return $arr; return $arr;
} }
/** /**
* count. * count.
* *