From a354ceac3616e5893fbeab1701518f77f84440d1 Mon Sep 17 00:00:00 2001 From: Nadyita Date: Sat, 4 Jan 2025 10:33:40 +0100 Subject: [PATCH] Add fixes from mago --- composer.json | 3 +- mago.toml | 56 ++++++++++++++++++++++++++++++++++++ src/MMDB/AsyncMMDBClient.php | 14 ++++----- src/Package.php | 2 +- src/Parser.php | 4 +-- tests/MMDBTest.php | 20 +++++++++++++ 6 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 mago.toml diff --git a/composer.json b/composer.json index ce3e168..3f8660c 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,8 @@ "tests": [ "phpunit -c phpunit.xml", "phpstan analyse --memory-limit 512M --no-progress --no-ansi", - "phpcs --no-colors --report=checkstyle -q src tests" + "phpcs --no-colors --report=checkstyle -q src tests", + "(command -v mago && mago lint) || true" ] } } diff --git a/mago.toml b/mago.toml new file mode 100644 index 0000000..6528b3f --- /dev/null +++ b/mago.toml @@ -0,0 +1,56 @@ +[source] +paths = ["src"] +excludes = [ ] +includes = ["vendor"] +extensions = ["php", "php81"] + +[linter] +level = "Error" + +[[linter.rules]] +name = "best-practices/no-unused-parameter" +level = "Off" + +[[linter.rules]] +name = "best-practices/loop-does-not-iterate" +level = "Off" + +[[linter.rules]] +name = "naming/interface" +psr = false + +[[linter.rules]] +name = "naming/class" +psr = false + +[[linter.rules]] +name = "strictness/no-assignment-in-condition" +level = "Off" + +[[linter.rules]] +name = "strictness/require-constant-type" +level = "Off" + +[[linter.rules]] +name = "strictness/missing-assert-description" +level = "Off" + +[[linter.rules]] +name = "safety/no-suppressed-expression" +level = "Off" + +[[linter.rules]] +name = "comment/docblock-syntax" +level = "Note" + +[[linter.rules]] +name = "deprecation/optional-parameter-before-required" +level = "Off" + +[[linter.rules]] +name = "best-practices/no-multi-assignments" +level = "Off" + +[[linter.rules]] +name = "migration/readonly-class-promotion" +level = "Off" diff --git a/src/MMDB/AsyncMMDBClient.php b/src/MMDB/AsyncMMDBClient.php index 121cc19..ff90ceb 100644 --- a/src/MMDB/AsyncMMDBClient.php +++ b/src/MMDB/AsyncMMDBClient.php @@ -92,12 +92,11 @@ public function findAllInstancesInCategory(int $categoryId): ?array { // find all instances $instances = []; $instance = $this->readEntry(); - $previousInstance = null; - while ($previousInstance == null || $instance->id > $previousInstance->id) { + do { $instances[] = $instance; $previousInstance = $instance; $instance = $this->readEntry(); - } + } while ($instance->id > $previousInstance->id); return $instances; } @@ -110,12 +109,11 @@ public function getCategories(): ?array { // find all categories $categories = []; $category = $this->readEntry(); - $previousCategory = null; - while ($previousCategory == null || $category->id > $previousCategory->id) { + do { $categories[] = $category; $previousCategory = $category; $category = $this->readEntry(); - } + } while ($category->id > $previousCategory->id); return $categories; } @@ -134,10 +132,10 @@ private function findEntry(int $id, int $offset): ?MMDBEntry { $previousEntry = $entry; $entry = $this->readEntry(); - if ($previousEntry != null && $entry->id < $previousEntry->id) { + if ($previousEntry !== null && $entry->id < $previousEntry->id) { return null; } - } while ($id != $entry->id); + } while ($id !== $entry->id); return $entry; } diff --git a/src/Package.php b/src/Package.php index 47d7b3e..7b6cbf6 100644 --- a/src/Package.php +++ b/src/Package.php @@ -16,7 +16,7 @@ abstract class Package implements Stringable { public function __construct(public readonly PackageType $type) { } - public function __toString() { + public function __toString(): string { $values = []; $refClass = new ReflectionClass($this); $props = get_object_vars($this); diff --git a/src/Parser.php b/src/Parser.php index dad4926..e8bf249 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -47,7 +47,7 @@ public function parseBinaryPackage(BinaryPackage $package): Package { assert(is_int($args[1])); assert(is_string($args[2])); /* Hack to support extended messages */ - if ($args[1] === 0 && substr($args[2], 0, 2) == '~&') { + if ($args[1] === 0 && substr($args[2], 0, 2) === '~&') { $this->logger?->debug('Extended message {message} found', [ 'message' => $args[2], ]); @@ -59,7 +59,7 @@ public function parseBinaryPackage(BinaryPackage $package): Package { } break; case In\SystemMessage::class: - assert(count($args) == 4); + assert(count($args) === 4); assert(is_int($args[2])); assert(is_string($args[3])); $categoryId = 20_000; diff --git a/tests/MMDBTest.php b/tests/MMDBTest.php index 75a9eab..d4f8b24 100644 --- a/tests/MMDBTest.php +++ b/tests/MMDBTest.php @@ -31,4 +31,24 @@ public function testMessages(int $categoryId, int $messageId, string $expected): $mmdb = AsyncMMDBClient::createDefault(); $this->assertSame($expected, $mmdb->getMessageString($categoryId, $messageId)); } + + /** @return list */ + public static function exampleMMDBInstanceCounts(): array { + return [ + [20_000, 38], + ]; + } + + #[DataProvider('exampleMMDBInstanceCounts')] + public function testFindAllInstances(int $categoryId, int $expectedCount): void { + $mmdb = AsyncMMDBClient::createDefault(); + $this->assertCount($expectedCount, $mmdb->findAllInstancesInCategory($categoryId)); + } + + public function testGetCategories(): void { + $mmdb = AsyncMMDBClient::createDefault(); + $foundCategories = $mmdb->getCategories(); + $this->assertNotNull($foundCategories); + $this->assertCount(52, $foundCategories); + } }