Skip to content

Commit

Permalink
Refactor: remove redundant assertions and improve array handling
Browse files Browse the repository at this point in the history
Removed unnecessary assertions such as `assert` and `property_exists` to make the tests cleaner and more efficient. Adjusted array handling by casting `bindings` to an array for better compatibility and added necessary code comments to suppress static analysis warnings where applicable. These changes improve the maintainability and readability of the codebase without affecting the functionality.
  • Loading branch information
koriym committed Nov 29, 2024
1 parent 8e263f5 commit e085bda
Show file tree
Hide file tree
Showing 7 changed files with 5 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/di/Bind.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private function getStringName(array $name): string
* @param array-key $key
*/
static function (array $carry, $key) use ($name): array {
if (! is_string($key)) {
if (! is_string($key)) { // @phpstan-ignore-line
throw new InvalidToConstructorNameParameter((string) $key);
}

Expand Down
2 changes: 0 additions & 2 deletions src/di/MultiBinding/LazyProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Ray\Di\InjectorInterface;
use Ray\Di\ProviderInterface;

use function assert;

/**
* @template T of ProviderInterface
*/
Expand Down
2 changes: 2 additions & 0 deletions src/di/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,11 @@ private function parseName(string $name): array
if (isset($exploded[1])) {
[$key, $value] = $exploded;
if (isset($key[0]) && $key[0] === '$') {
assert(is_string($key)); // @phpstan-ignore-line
$key = substr($key, 1);
}

/** @psalm-suppress MixedArgument */
$names[trim($key)] = trim($value);
}
}
Expand Down
2 changes: 0 additions & 2 deletions tests/di/ArgumentsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use ReflectionMethod;
use ReflectionParameter;

use function assert;
use function is_object;
use function spl_object_hash;

class ArgumentsTest extends TestCase
Expand Down
2 changes: 1 addition & 1 deletion tests/di/DependencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function testInjectInterceptor(): void
$isWeave = (new ReflectionClass($instance))->implementsInterface(WeavedInterface::class);
$this->assertTrue($isWeave);
assert(property_exists($instance, 'bindings'));
$this->assertArrayHasKey('returnSame', $instance->bindings);
$this->assertArrayHasKey('returnSame', (array) $instance->bindings);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions tests/di/GrapherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@

use function assert;
use function file_get_contents;
use function is_object;
use function is_string;
use function passthru;
use function property_exists;
use function unserialize;

class GrapherTest extends TestCase
Expand All @@ -28,7 +26,6 @@ public function testGetInstanceWithArgs(): void
$grapher = new Grapher(new FakeUntargetModule(), __DIR__ . '/tmp');
$instance = $grapher->newInstanceArgs(FakeUntargetChild::class, ['1']);
$this->assertInstanceOf(FakeUntargetChild::class, $instance);
assert(property_exists($instance, 'val'));
$this->assertSame('1', $instance->val);
}

Expand Down
7 changes: 1 addition & 6 deletions tests/di/InjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use function file_get_contents;
use function is_string;
use function passthru;
use function property_exists;
use function serialize;
use function spl_object_hash;
use function unlink;
Expand Down Expand Up @@ -184,7 +183,6 @@ public function testAnnotationBasedInjection(): Injector
$this->assertInstanceOf(FakeMirrorInterface::class, $car->spareMirror);
$this->assertSame(spl_object_hash($car->rightMirror), spl_object_hash($car->spareMirror));
$this->assertInstanceOf(FakeHandle::class, $car->handle);
assert($car->handle instanceof FakeHandle);
$this->assertSame($car->handle->logo, 'momo');

return $injector;
Expand Down Expand Up @@ -242,7 +240,6 @@ public function testSerializeBuiltinBinding(): void
$injector = unserialize(serialize(new Injector()));
assert($injector instanceof InjectorInterface);
$instance = $injector->getInstance(FakeBuiltin::class);
assert(property_exists($instance, 'injector'));
$this->assertInstanceOf(Injector::class, $instance->injector);
}

Expand Down Expand Up @@ -430,9 +427,7 @@ protected function configure()
$instance = $injector->getInstance(FakeAop::class);
$result = $instance->returnSame(2);
$this->assertSame(2, $result);
assert(property_exists($instance, 'bindings'));
assert(isset($instance->bindings['returnSame'][0]));
$this->assertInstanceOf(NullInterceptor::class, $instance->bindings['returnSame'][0]);
$this->assertInstanceOf(NullInterceptor::class, $instance->bindings['returnSame'][0]); // @phpstan-ignore-line
}

public function testModuleArray(): void
Expand Down

0 comments on commit e085bda

Please sign in to comment.