yiisoft / yiisoft/definitions

Throw an exception if unknown parameter passed to a method

Open
#96 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
15
Forks
6
Avg merge
1h 1m
Merged PRs (30d)
1

Description

What steps will reproduce the problem?
class Foo
{
    public function __construct(public Bar $bar)
    {
    }
}

class Bar
{
    public function __construct(public string $value = 'default')
    {
    }
}

$config = ContainerConfig::create()
    ->withDefinitions([
        Foo::class => [
            'class' => Foo::class,
            '__construct()' => [
                'baz' => Reference::to('bar'), // Typo here
            ],
        ],
        'bar' => [
            'class' => Bar::class,
            '__construct()' => [
                'value' => 'custom',
            ],
        ],
    ]);

$container = new Container($config);

var_dump($container->get(Foo::class));
What is the expected result?

Exception with message, like:
Unknown parameter $baz in Foo::__construct() method. Available paraemeters: $bar

What do you get instead?

New instance of Foo with wrong Bar

class Foo#11 (1) {
  public Bar $bar =>
  class Bar#10 (1) {
    public string $value =>
    string(7) "default"
  }
}
Additional info
Q A
Version master
PHP version N/A
Operating system N/A

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at Container::get() and trace how the Foo definition's __construct() parameters are matched to the Foo constructor. Reproduce the example with the unknown baz parameter, then add coverage showing that an exception identifies the unknown parameter and available parameters instead of silently using Bar's default value.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.