Skip to content

Commit

Permalink
Add fixes from mago
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadyita committed Jan 4, 2025
1 parent 9bf639e commit a354cea
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
}
56 changes: 56 additions & 0 deletions mago.toml
Original file line number Diff line number Diff line change
@@ -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"
14 changes: 6 additions & 8 deletions src/MMDB/AsyncMMDBClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
]);
Expand All @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions tests/MMDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array{int, int}> */
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);
}
}

0 comments on commit a354cea

Please sign in to comment.