Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential bug with composing and composed dependencies both aliased with interfaces #265

Open
elazar opened this issue Oct 26, 2024 · 2 comments

Comments

@elazar
Copy link
Contributor

elazar commented Oct 26, 2024

This may admittedly be a misunderstanding on my part regarding the expected behavior of this library.

See this branch. I've tried running the test case that it adds against the 4.x branch, the 4.2.3 tag, and the branch from #259 (leading me to believe this isn't an issue with recursive dependency resolution), but all yield the same result. See the test failure output against my branch (which is based on the 4.x branch) shown below.

➜  container git:(nested-interface-dependencies) ./vendor/bin/phpunit --filter testContainerAddsAndGetsNestedInterfaceDependency tests/ContainerTest.php

PHPUnit 8.5.40 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.24 with Xdebug 3.3.1
Configuration: container/phpunit.xml
Error:         This version of PHPUnit does not support code coverage on PHP 8

E                                                                   1 / 1 (100%)

Time: 106 ms, Memory: 6.00 MB

There was 1 error:

1) League\Container\Test\ContainerTest::testContainerAddsAndGetsNestedInterfaceDependency
ArgumentCountError: Too few arguments to function League\Container\Test\Asset\Bay::__construct(), 0 passed and exactly 1 expected

container/tests/Asset/Bay.php:11
container/src/Definition/Definition.php:212
container/src/Definition/Definition.php:175
container/src/Definition/Definition.php:154
container/src/Definition/DefinitionAggregate.php:79
container/src/Container.php:161
container/src/Container.php:111
container/tests/ContainerTest.php:33

If I pass the $container variable in the test to var_dump() immediately before the call to its get() method that causes the test failure, this is the output.

container/tests/ContainerTest.php:32:
class League\Container\Container#324 (5) {
  protected $defaultToShared =>
  bool(false)
  protected $definitions =>
  class League\Container\Definition\DefinitionAggregate#296 (2) {
    protected $definitions =>
    array(3) {
      [0] =>
      class League\Container\Definition\Definition#16 (8) {
        protected $alias =>
        string(40) "League\Container\Test\Asset\BayInterface"
        protected $concrete =>
        string(31) "League\Container\Test\Asset\Bay"
        protected $shared =>
        bool(false)
        protected $tags =>
        array(0) {
        }
        protected $arguments =>
        array(0) {
        }
        protected $methods =>
        array(0) {
        }
        protected $resolved =>
        NULL
        protected $container =>
        NULL
      }
      [1] =>
      class League\Container\Definition\Definition#17 (8) {
        protected $alias =>
        string(40) "League\Container\Test\Asset\BazInterface"
        protected $concrete =>
        string(31) "League\Container\Test\Asset\Baz"
        protected $shared =>
        bool(false)
        protected $tags =>
        array(0) {
        }
        protected $arguments =>
        array(0) {
        }
        protected $methods =>
        array(0) {
        }
        protected $resolved =>
        NULL
        protected $container =>
        NULL
      }
      [2] =>
      class League\Container\Definition\Definition#23 (8) {
        protected $alias =>
        string(31) "League\Container\Test\Asset\Bay"
        protected $concrete =>
        string(31) "League\Container\Test\Asset\Bay"
        protected $shared =>
        bool(false)
        protected $tags =>
        array(0) {
        }
        protected $arguments =>
        array(1) {
          [0] =>
          string(40) "League\Container\Test\Asset\BazInterface"
        }
        protected $methods =>
        array(0) {
        }
        protected $resolved =>
        NULL
        protected $container =>
        NULL
      }
    }
    protected $container =>
          ...

  }
  protected $providers =>
  class League\Container\ServiceProvider\ServiceProviderAggregate#25 (3) {
    protected $providers =>
    array(0) {
    }
    protected $registered =>
    array(0) {
    }
    protected $container =>
          ...

  }
  protected $inflectors =>
  class League\Container\Inflector\InflectorAggregate#22 (2) {
    protected $inflectors =>
    array(0) {
    }
    protected $container =>
          ...

  }
  protected $delegates =>
  array(0) {
  }
}

This behavior appears to be specific to these circumstances.

  1. One concrete class that implements an interface has a required constructor parameter that implements another interface.
  2. The container includes aliases from each of the two interfaces to their respective concrete class implementations and the argument of the composing class constructor.
@philipobenito
Copy link
Member

philipobenito commented Oct 26, 2024 via email

@spider-mane
Copy link

spider-mane commented Oct 29, 2024

@philipobenito I'm not sure I follow here. The pull request #254 that removed support for recursive resolution had the stated intention of preventing infinite loops when provided with a nonexistent class. Before that PR, recursive resolution was fully supported and I've had to fix my projects to version 4.2.0 in order to avoid problems. The pull request that I submitted #259 restores support for recursion while also preventing infinite loops in the event that the class provided does not exist. I don't quite get the rationale you've provided here against recursion, but either way #254 on its own introduced breaking changes that I don't think should have been included in a patch release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants