From a9229a067b7b51c1fe4927fd8f7e910a72c2935e Mon Sep 17 00:00:00 2001 From: itismadness Date: Sat, 3 Oct 2020 18:47:30 +0000 Subject: [PATCH] move to PSR12 coding standard --- composer.json | 4 +- phpcs.xml | 41 +++--------------- src/Bencode.php | 51 +++++++++++----------- src/BencodeTorrent.php | 83 ++++++++++++++++++++++-------------- tests/BencodeTest.php | 24 +++++++---- tests/BencodeTorrentTest.php | 62 +++++++++++++++++---------- 6 files changed, 142 insertions(+), 123 deletions(-) diff --git a/composer.json b/composer.json index bff69bf..20cd886 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,8 @@ "ext-mbstring": "^7" }, "scripts": { - "test": "phpunit" + "test": "phpunit", + "lint": "phpcs", + "lint:fix": "phpcbf" } } diff --git a/phpcs.xml b/phpcs.xml index 8725c31..b7ebaf4 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -1,38 +1,7 @@ - - The coding standard for the Gazelle project + + src + tests - - - - - - - - - - - 0 - - - 0 - - - - - - - - - - - - - - - - - - - - \ No newline at end of file + + diff --git a/src/Bencode.php b/src/Bencode.php index 87e92ab..bb1e445 100644 --- a/src/Bencode.php +++ b/src/Bencode.php @@ -2,7 +2,8 @@ namespace OrpheusNET\BencodeTorrent; -class Bencode { +class Bencode +{ protected $data = null; /** @@ -10,7 +11,8 @@ class Bencode { * @param mixed $data * @throws \RuntimeException */ - public function setData($data) { + public function setData($data) + { $this->data = $data; } @@ -19,7 +21,8 @@ class Bencode { * @param string $data * @throws \RuntimeException */ - public function decodeString(string $data) { + public function decodeString(string $data) + { $this->data = $this->decode($data); } @@ -29,7 +32,8 @@ class Bencode { * @param string $path * @throws \RuntimeException */ - public function decodeFile(string $path) { + public function decodeFile(string $path) + { $this->data = $this->decode(file_get_contents($path, FILE_BINARY)); } @@ -46,7 +50,8 @@ class Bencode { * @param int $pos * @return mixed */ - protected function decode(string $data, int &$pos = 0) { + protected function decode(string $data, int &$pos = 0) + { $start_decode = $pos === 0; if ($data[$pos] === 'd') { $pos++; @@ -58,14 +63,13 @@ class Bencode { break; } if (!is_string($key)) { - throw new \RuntimeException('Invalid key type, must be string: '.gettype($key)); + throw new \RuntimeException('Invalid key type, must be string: ' . gettype($key)); } $return[$key] = $value; } ksort($return); $pos++; - } - elseif ($data[$pos] === 'l') { + } elseif ($data[$pos] === 'l') { $pos++; $return = []; while ($data[$pos] !== 'e') { @@ -73,8 +77,7 @@ class Bencode { $return[] = $value; } $pos++; - } - elseif ($data[$pos] === 'i') { + } elseif ($data[$pos] === 'i') { $pos++; $digits = strpos($data, 'e', $pos) - $pos; $return = substr($data, $pos, $digits); @@ -87,13 +90,12 @@ class Bencode { $return = substr($return, 1); } if (!ctype_digit($return)) { - $msg = 'Cannot have non-digit values in integer number: '.$return; + $msg = 'Cannot have non-digit values in integer number: ' . $return; throw new \RuntimeException($msg); } $return = $multiplier * ((int) $return); $pos += $digits + 1; - } - else { + } else { $digits = strpos($data, ':', $pos) - $pos; $len = (int) substr($data, $pos, $digits); $pos += ($digits + 1); @@ -112,14 +114,16 @@ class Bencode { * Get the internal data array * @return mixed */ - public function getData() { + public function getData() + { return $this->data; } /** * @throws \RuntimeException */ - protected function hasData() { + protected function hasData() + { if ($this->data === null) { throw new \RuntimeException('Must decode proper bencode string first'); } @@ -128,7 +132,8 @@ class Bencode { /** * @return string */ - public function getEncode() : string { + public function getEncode(): string + { $this->hasData(); return $this->encodeVal($this->data); } @@ -137,7 +142,8 @@ class Bencode { * @param mixed $data * @return string */ - protected function encodeVal($data) : string { + protected function encodeVal($data): string + { if (is_array($data)) { $return = ''; $check = -1; @@ -154,8 +160,7 @@ class Bencode { foreach ($data as $value) { $return .= $this->encodeVal($value); } - } - else { + } else { $return .= 'd'; foreach ($data as $key => $value) { $return .= $this->encodeVal(strval($key)); @@ -163,11 +168,9 @@ class Bencode { } } $return .= 'e'; - } - elseif (is_integer($data)) { - $return = 'i'.$data.'e'; - } - else { + } elseif (is_integer($data)) { + $return = 'i' . $data . 'e'; + } else { $return = strlen($data) . ':' . $data; } return $return; diff --git a/src/BencodeTorrent.php b/src/BencodeTorrent.php index 5a6d7b1..97f20ee 100644 --- a/src/BencodeTorrent.php +++ b/src/BencodeTorrent.php @@ -24,11 +24,13 @@ namespace OrpheusNET\BencodeTorrent; * For Gazelle, this also acts as a unification of the two original BEncode implementations * which were both used in separate areas of the codebase. */ -class BencodeTorrent extends Bencode { - const FILELIST_DELIM = 0xF7; +class BencodeTorrent extends Bencode +{ + public const FILELIST_DELIM = 0xF7; private static $utf8_filelist_delim = null; - public function __construct() { + public function __construct() + { $this->setDelim(); } @@ -37,7 +39,8 @@ class BencodeTorrent extends Bencode { * and char to set a class constant or variable, so we wait till the class is initialized * for the first time to set it. */ - private function setDelim() { + private function setDelim() + { if (self::$utf8_filelist_delim === null) { self::$utf8_filelist_delim = utf8_encode(chr(self::FILELIST_DELIM)); } @@ -48,7 +51,8 @@ class BencodeTorrent extends Bencode { * @param array $data * @throws \RuntimeException */ - public function setData($data) { + public function setData($data) + { parent::setData($data); $this->validate(); } @@ -58,7 +62,8 @@ class BencodeTorrent extends Bencode { * @param string $data * @throws \RuntimeException */ - public function decodeString(string $data) { + public function decodeString(string $data) + { parent::decodeString($data); $this->validate(); } @@ -69,7 +74,8 @@ class BencodeTorrent extends Bencode { * @param string $path * @throws \RuntimeException */ - public function decodeFile(string $path) { + public function decodeFile(string $path) + { parent::decodeFile($path); $this->validate(); } @@ -78,7 +84,8 @@ class BencodeTorrent extends Bencode { * Validates that the internal data array * @throws \RuntimeException */ - public function validate() { + public function validate() + { if (!is_array($this->data)) { throw new \TypeError('Data must be an array'); } @@ -111,7 +118,8 @@ class BencodeTorrent extends Bencode { * * @return bool flag to indicate if we altered the info dictionary */ - public function clean() : bool { + public function clean(): bool + { $this->cleanDataDictionary(); return $this->cleanInfoDictionary(); } @@ -121,7 +129,8 @@ class BencodeTorrent extends Bencode { * overwritten dynamically on any downloaded torrent (like announce or comment), so that we * store the smallest encoded string within the database and cuts down on potential waste. */ - public function cleanDataDictionary() { + public function cleanDataDictionary() + { $allowed_keys = array('encoding', 'info'); foreach ($this->data as $key => $value) { if (!in_array($key, $allowed_keys)) { @@ -144,7 +153,8 @@ class BencodeTorrent extends Bencode { * * @return bool */ - public function cleanInfoDictionary() : bool { + public function cleanInfoDictionary(): bool + { $cleaned = false; $allowed_keys = array('files', 'name', 'piece length', 'pieces', 'private', 'length', 'name.utf8', 'name.utf-8', 'md5sum', 'sha1', 'source', @@ -164,7 +174,8 @@ class BencodeTorrent extends Bencode { * * @return bool */ - public function isPrivate() : bool { + public function isPrivate(): bool + { $this->hasData(); return isset($this->data['info']['private']) && $this->data['info']['private'] === 1; } @@ -178,7 +189,8 @@ class BencodeTorrent extends Bencode { * * @return bool */ - public function makePrivate() : bool { + public function makePrivate(): bool + { $this->hasData(); if ($this->isPrivate()) { return false; @@ -199,7 +211,8 @@ class BencodeTorrent extends Bencode { * * @return bool true if the source was set/changed, false if no change */ - public function setSource(string $source) : bool { + public function setSource(string $source): bool + { $this->hasData(); if (isset($this->data['info']['source']) && $this->data['info']['source'] === $source) { return false; @@ -212,7 +225,8 @@ class BencodeTorrent extends Bencode { return true; } - public function getSource(): ?string { + public function getSource(): ?string + { $this->hasData(); return $this->data['info']['source'] ?? null; } @@ -225,7 +239,8 @@ class BencodeTorrent extends Bencode { * * @param array $array */ - public function setValue(array $array) { + public function setValue(array $array) + { foreach ($array as $key => $value) { if (is_array($value)) { ksort($value); @@ -258,19 +273,22 @@ class BencodeTorrent extends Bencode { * * @return string */ - public function getInfoHash() : string { + public function getInfoHash(): string + { $this->hasData(); return sha1($this->encodeVal($this->data['info'])); } - public function getHexInfoHash(): string { + public function getHexInfoHash(): string + { return pack('H*', $this->getInfoHash()); } /** * @return string */ - public function getName() { + public function getName() + { if (isset($this->data['info']['name.utf-8'])) { return $this->data['info']['name.utf-8']; } @@ -284,12 +302,12 @@ class BencodeTorrent extends Bencode { * * @return int */ - public function getSize() : int { + public function getSize(): int + { $cur_size = 0; if (!isset($this->data['info']['files'])) { $cur_size = $this->data['info']['length']; - } - else { + } else { foreach ($this->data['info']['files'] as $file) { $cur_size += $file['length']; } @@ -306,7 +324,8 @@ class BencodeTorrent extends Bencode { * * @return array */ - public function getFileList() : array { + public function getFileList(): array + { $files = []; if (!isset($this->data['info']['files'])) { // Single-file torrent @@ -315,8 +334,7 @@ class BencodeTorrent extends Bencode { $this->data['info']['name']); $size = $this->data['info']['length']; $files[] = ['path' => $name, 'size' => $size]; - } - else { + } else { $size = 0; foreach ($this->data['info']['files'] as $file) { $size += $file['length']; @@ -333,11 +351,13 @@ class BencodeTorrent extends Bencode { return array('total_size' => $size, 'files' => $files); } - public function hasFiles(): bool { + public function hasFiles(): bool + { return isset($this->data['info']['files']); } - public function hasEncryptedFiles(): bool { + public function hasEncryptedFiles(): bool + { return isset($this->data['encrypted_files']); } @@ -351,7 +371,8 @@ class BencodeTorrent extends Bencode { * * @return array */ - public function getGazelleFileList() : array { + public function getGazelleFileList(): array + { $files = []; foreach ($this->getFileList()['files'] as $file) { $path = $file['path']; @@ -373,7 +394,8 @@ class BencodeTorrent extends Bencode { * * @return string */ - private function makeUTF8(string $str) : string { + private function makeUTF8(string $str): string + { if (preg_match('//u', $str)) { $encoding = 'UTF-8'; } @@ -388,8 +410,7 @@ class BencodeTorrent extends Bencode { // @codeCoverageIgnoreEnd if ($encoding === 'UTF-8') { return $str; - } - else { + } else { return @mb_convert_encoding($str, 'UTF-8', $encoding); } } diff --git a/tests/BencodeTest.php b/tests/BencodeTest.php index ae1809a..9a6fdb4 100644 --- a/tests/BencodeTest.php +++ b/tests/BencodeTest.php @@ -2,8 +2,10 @@ namespace OrpheusNET\BencodeTorrent; -class BencodeTest extends \PHPUnit\Framework\TestCase { - public function dataProvider() { +class BencodeTest extends \PHPUnit\Framework\TestCase +{ + public function dataProvider() + { return [ ['i0e', 0], ['i-1e', -1], @@ -21,27 +23,31 @@ class BencodeTest extends \PHPUnit\Framework\TestCase { * @param string $bencoded_string * @param mixed $expected */ - public function testDecodeEncode($bencoded_string, $expected) { + public function testDecodeEncode($bencoded_string, $expected) + { $bencode = new Bencode(); $bencode->decodeString($bencoded_string); $this->assertEquals($expected, $bencode->getData()); $this->assertEquals($bencoded_string, $bencode->getEncode()); } - public function testEmptyDict() { + public function testEmptyDict() + { $bencode = new Bencode(); $bencode->decodeString('de'); $this->assertEquals([], $bencode->getData()); $this->assertEquals('le', $bencode->getEncode()); } - public function testInvalidDictionaryKey() { + public function testInvalidDictionaryKey() + { $bencode = new Bencode(); $this->expectException(\RuntimeException::class); $bencode->decodeString('di1e5:valuee'); } - public function invalidIntegers() { + public function invalidIntegers() + { return [['-0'], ['a'], ['1.0']]; } @@ -49,13 +55,15 @@ class BencodeTest extends \PHPUnit\Framework\TestCase { * @param string $value * @dataProvider invalidIntegers */ - public function testInvalidInteger(string $value) { + public function testInvalidInteger(string $value) + { $bencode = new Bencode(); $this->expectException(\RuntimeException::class); $bencode->decodeString("i{$value}e"); } - public function testInvalidString() { + public function testInvalidString() + { $bencode = new Bencode(); $this->expectException(\RuntimeException::class); $bencode->decodeString('i1e0e'); diff --git a/tests/BencodeTorrentTest.php b/tests/BencodeTorrentTest.php index 634135f..84693c0 100644 --- a/tests/BencodeTorrentTest.php +++ b/tests/BencodeTorrentTest.php @@ -2,13 +2,14 @@ namespace OrpheusNET\BencodeTorrent; -class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { - public function testLoadTorrent() { +class BencodeTorrentTest extends \PHPUnit\Framework\TestCase +{ + public function testLoadTorrent() + { $bencode = new BencodeTorrent(); try { - $bencode->decodeFile(__DIR__.'/data/test_1.torrent'); - } - catch (\Exception $exc) { + $bencode->decodeFile(__DIR__ . '/data/test_1.torrent'); + } catch (\Exception $exc) { $this->fail('Decode should not have thrown exception'); } $data = $bencode->getData(); @@ -83,11 +84,11 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { $this->assertEquals(16020, strlen($data['info']['pieces'])); $this->assertEquals(1, $data['info']['private']); $this->assertEquals('APL', $data['info']['source']); - $this->assertStringEqualsFile(__DIR__.'/data/test_1.torrent', $bencode->getEncode()); + $this->assertStringEqualsFile(__DIR__ . '/data/test_1.torrent', $bencode->getEncode()); $this->assertEquals(104916260, $bencode->getSize()); $this->assertTrue($bencode->isPrivate()); $bencode->decodeString($bencode->getEncode()); - $this->assertStringEqualsFile(__DIR__.'/data/test_1.torrent', $bencode->getEncode()); + $this->assertStringEqualsFile(__DIR__ . '/data/test_1.torrent', $bencode->getEncode()); $this->assertEquals('1f830103427029a88dd5fde85be74e622ee07951', $bencode->getInfoHash()); $this->assertEquals($bencode->getInfoHash(), unpack('H*', $bencode->getHexInfoHash())[1]); $this->assertFalse($bencode->hasEncryptedFiles()); @@ -143,7 +144,8 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { $this->assertEquals($file_list, $bencode->getFileList()); } - public function testSetData() { + public function testSetData() + { $data = [ 'encoding' => 'UTF8', 'info' => [ @@ -175,7 +177,8 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { ); } - public function testEmptyDictionary() { + public function testEmptyDictionary() + { $expected = [ 'encoding' => 'UTF8', 'info' => [ @@ -187,13 +190,14 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { ]; $bencode = new BencodeTorrent(); $bencode->decodeString( - 'd8:encoding4:UTF84:infod4:name4:test6:lengthi1213134e'. + 'd8:encoding4:UTF84:infod4:name4:test6:lengthi1213134e' . '6:pieces18:fake pieces stringe4:test0:e' ); $this->assertEquals($expected, $bencode->getData()); } - public function testClean() { + public function testClean() + { $data = [ 'encoding' => 'UTF8', 'info' => [ @@ -226,7 +230,8 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { $this->assertEquals($data, $bencode->getData()); } - public function testSetPrivate() { + public function testSetPrivate() + { $data = [ 'encoding' => 'UTF8', 'info' => [ @@ -246,7 +251,8 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { $this->assertEquals(1, $actual['info']['private']); } - public function testSetAlreadyPrivate() { + public function testSetAlreadyPrivate() + { $data = [ 'encoding' => 'UTF8', 'info' => [ @@ -260,7 +266,8 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { $this->assertFalse($bencode->makePrivate()); } - public function testSetSource() { + public function testSetSource() + { $data = [ 'encoding' => 'UTF8', 'info' => [ @@ -281,7 +288,8 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { $this->assertNotContains('x_cross_seed', $actual['info']); } - public function testSetAlreadySourced() { + public function testSetAlreadySourced() + { $data = [ 'encoding' => 'UTF8', 'info' => [ @@ -299,7 +307,8 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { $this->assertEquals($data, $bencode->getData()); } - public function testSetDifferentSource() { + public function testSetDifferentSource() + { $data = [ 'encoding' => 'UTF8', 'info' => [ @@ -319,7 +328,8 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { $this->assertNotContains('x_cross_seed', $actual['info']); } - public function testSetValue() { + public function testSetValue() + { $data = [ 'encoding' => 'UTF8', 'info' => [ @@ -376,7 +386,8 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { $this->assertTrue($bencode->hasFiles()); } - public function testGetUtf8Name() { + public function testGetUtf8Name() + { $data = [ 'encoding' => 'UTF8', 'info' => [ @@ -405,7 +416,8 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { $this->assertEquals(['. s12345s test!! รท'], $bencode->getGazelleFileList()); } - public function testIso88591Name() { + public function testIso88591Name() + { $data = [ 'info' => [ 'length' => 1234, @@ -418,14 +430,16 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { $this->assertFalse($bencode->hasFiles()); } - public function testInvalidSet() { + public function testInvalidSet() + { $data = ['encoding' => 'UTF8', 'announce' => 'http://localhost:8080/announce']; $bencode = new BencodeTorrent(); $this->expectException(\RuntimeException::class); $bencode->setData($data); } - public function testInvalidSetValue() { + public function testInvalidSetValue() + { $data = [ 'encoding' => 'UTF8', 'info' => [ @@ -441,13 +455,15 @@ class BencodeTorrentTest extends \PHPUnit\Framework\TestCase { $bencode->setValue(['info' => []]); } - public function testGetEncodeNoData() { + public function testGetEncodeNoData() + { $bencode = new BencodeTorrent(); $this->expectException(\RuntimeException::class); $bencode->getEncode(); } - public function testInvalidPath() { + public function testInvalidPath() + { $data = [ 'encoding' => 'UTF8', 'info' => [