Giter VIP home page Giter VIP logo

definitions's Issues

Incorrect resolving of a spreaded parameter

What steps will reproduce the problem?

  1. Define a spreaded parameter in constructor like this: __construct(...$test)
  2. Define its value in config: ['__construct()' => ['test' => [1, 2]]]

What is the expected result?

$test in constructor contains [1, 2] value

What do you get instead?

$test in constructor contains [0 => [1, 2]] value

Additional info

Q A
Version dev-master
PHP version 8.1
Operating system Alpine

[Bug] All exceptions are caught for optional dependencies

What steps will reproduce the problem?

Having the sample code:

class A
{
    public function __construct()
    {
        throw new RuntimeException('Something happened on initialization');
    }
}

class B
{
    public function __construct(private ?A $a = null)
    {
    }
}

$container->get(B::class);

What is the expected result?

The exception is thrown

What do you get instead?

Instance of B with a wrong dependency, and no exception is thrown. It' a hell to debug this situation.

Additional info

This code is responsible for the specified behavior. I'm pretty sure container definitions must not have a deal with application-level exceptions. In my opinion in the case when a dependency has a definition, and an exception is thrown on its resolving, this exception must not be catched by container/definition resolver, and doesn't matter if this dependency is optional or not.

Is it correct return of DefinitionExtractor::fromClass()?

return $dependencies;

At the beginnig of the function u r checking defined dependencies for specified class (it's ok):

if (isset(self::$dependencies[$class])) {
        return self::$dependencies[$class];
}

but at the end of function u r return all dependencies.

self::$dependencies[$class] = $dependencies;

return $dependencies;

I have not watched use of this method, but looks like bug.

Remove `var_export` usage

I'd remove var_export usage because it's insecure to disclose array's values into the world/logs.
Better to use keys or variable types.

Throw an exception if unknown parameter passed to a method

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

Optimizing constructor and argument initialization

I propose to add an alternative syntax for a simplified notation of the constructor and its arguments.
For example:

use \Yiisoft\Definitions\ArrayDefinition;

$definition = ArrayDefinition::fromConfig([
    'class' => MyServiceInterface::class,
    '__construct()' => [42], 
    '$propertyName' => 'value',
    'setName()' => ['Alex'],
]);

So it can be written like this:

use \Yiisoft\Definitions\ArrayDefinition;

$definition = ArrayDefinition::fromConfig([
    MyServiceInterface::class,
    [42], 
    'propertyName' => 'value',
    'setName()' => ['Alex'],
]);

Here the unnamed first (0) parameter (string) is the name of the class.
The second unnamed (1) parameter (array, if present) is the constructor arguments.

Raise an error if unnamed parameters are present along with "__construct()" or "class".

Also, the '$' notation here may be redundant and looks like a developer error visually (lack of double quotes).

This improvement will simplify development and reduce the amount of code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.