Skip to content

Commit

Permalink
Improve exception messages for argument positions to match PHP 8+ format
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jan 29, 2025
1 parent eedc82f commit 5f1cba2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ private static function parameterError(\ReflectionParameter $parameter): string
$name = explode("\0", $class->getName())[0] . '::' . $name;
}

return 'Argument ' . ($parameter->getPosition() + 1) . ' ($' . $parameter->getName() . ') of ' . $name . '()';
return 'Argument #' . ($parameter->getPosition() + 1) . ' ($' . $parameter->getName() . ') of ' . $name . '()';
}

/**
Expand Down
10 changes: 5 additions & 5 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1529,25 +1529,25 @@ public function provideInvalidClasses(): \Generator

yield [
InvalidConstructorUntyped::class,
'Argument 1 ($value) of %s::__construct() has no type'
'Argument #1 ($value) of %s::__construct() has no type'
];

yield [
InvalidConstructorInt::class,
'Argument 1 ($value) of %s::__construct() expects unsupported type int'
'Argument #1 ($value) of %s::__construct() expects unsupported type int'
];

if (PHP_VERSION_ID >= 80000) {
yield [
InvalidConstructorUnion::class,
'Argument 1 ($value) of %s::__construct() expects unsupported type int|float'
'Argument #1 ($value) of %s::__construct() expects unsupported type int|float'
];
}

if (PHP_VERSION_ID >= 80100) {
yield [
InvalidConstructorIntersection::class,
'Argument 1 ($value) of %s::__construct() expects unsupported type Traversable&ArrayAccess'
'Argument #1 ($value) of %s::__construct() expects unsupported type Traversable&ArrayAccess'
];
}

Expand All @@ -1558,7 +1558,7 @@ public function provideInvalidClasses(): \Generator

yield [
InvalidConstructorSelf::class,
'Argument 1 ($value) of %s::__construct() is recursive'
'Argument #1 ($value) of %s::__construct() is recursive'
];
}

Expand Down
14 changes: 7 additions & 7 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ public function __construct(\stdClass $data)
$callable = $container->callable(get_class($controller));

$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Argument 1 ($username) of {closure:' . __FILE__ . ':' . $line .'}() is not defined');
$this->expectExceptionMessage('Argument #1 ($username) of {closure:' . __FILE__ . ':' . $line .'}() is not defined');
$callable($request);
}

Expand Down Expand Up @@ -1641,7 +1641,7 @@ public function __construct(string $name)
$callable = $container->callable(get_class($controller));

$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Argument 1 ($name) of class@anonymous::__construct() expects unsupported type string');
$this->expectExceptionMessage('Argument #1 ($name) of class@anonymous::__construct() expects unsupported type string');
$callable($request);
}

Expand Down Expand Up @@ -1772,7 +1772,7 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryRequiresUntypedA
$callable = $container->callable(\stdClass::class);

$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Argument 1 ($undefined) of {closure:' . __FILE__ . ':' . $line .'}() has no type');
$this->expectExceptionMessage('Argument #1 ($undefined) of {closure:' . __FILE__ . ':' . $line .'}() has no type');
$callable($request);
}

Expand All @@ -1791,7 +1791,7 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryRequiresUndefine
$callable = $container->callable(\stdClass::class);

$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Argument 1 ($undefined) of {closure:' . __FILE__ . ':' . $line .'}() is not defined');
$this->expectExceptionMessage('Argument #1 ($undefined) of {closure:' . __FILE__ . ':' . $line .'}() is not defined');
$callable($request);
}

Expand All @@ -1807,7 +1807,7 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryRequiresRecursiv
$callable = $container->callable(\stdClass::class);

$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Argument 1 ($data) of {closure:' . __FILE__ . ':' . $line .'}() is recursive');
$this->expectExceptionMessage('Argument #1 ($data) of {closure:' . __FILE__ . ':' . $line .'}() is recursive');
$callable($request);
}

Expand Down Expand Up @@ -2101,7 +2101,7 @@ public function testGetEnvThrowsWhenFactoryUsesBuiltInFunctionThatReferencesUnkn
]);

$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Argument 1 ($extension) of extension_loaded() is not defined');
$this->expectExceptionMessage('Argument #1 ($extension) of extension_loaded() is not defined');
$container->getEnv('X_FOO');
}

Expand All @@ -2119,7 +2119,7 @@ public function foo(string $bar): string
]);

$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Argument 1 ($bar) of class@anonymous::foo() is not defined');
$this->expectExceptionMessage('Argument #1 ($bar) of class@anonymous::foo() is not defined');
$container->getEnv('X_FOO');
}

Expand Down

0 comments on commit 5f1cba2

Please sign in to comment.