From b4f16e669c54cf883c7c15d124d96e037e84fc7e Mon Sep 17 00:00:00 2001 From: Dmitriy Efremov Date: Wed, 1 Nov 2017 18:17:43 +0700 Subject: [PATCH] added strict scalar typing in constructor --- src/Arguments/ArgumentsResolver.php | 16 ++-- tests/src/Container/ConstructorTest.php | 14 --- tests/src/Container/DependencyTest.php | 2 +- tests/src/Container/ScalarTest.php | 121 ++++++++++++++++++++++++ tests/src/Fixtures/ScalarDependency.php | 19 +--- tests/src/Fixtures/StrictDependency.php | 11 --- tests/src/Fixtures/StringDependency.php | 18 ++++ 7 files changed, 154 insertions(+), 47 deletions(-) create mode 100644 tests/src/Container/ScalarTest.php delete mode 100644 tests/src/Fixtures/StrictDependency.php create mode 100644 tests/src/Fixtures/StringDependency.php diff --git a/src/Arguments/ArgumentsResolver.php b/src/Arguments/ArgumentsResolver.php index c77955b..e4c3b16 100644 --- a/src/Arguments/ArgumentsResolver.php +++ b/src/Arguments/ArgumentsResolver.php @@ -52,8 +52,7 @@ public static function parseDefinitionsByMethodSignature( ContainerInterface $container, array $definitions, \ReflectionMethod $method = null - ): array - { + ): array { $numberOfArguments = $method ? $method->getNumberOfParameters() : 0; if (count($definitions) > $numberOfArguments) { $numberOfRedundantly = count($definitions) - $numberOfArguments; @@ -69,20 +68,23 @@ public static function parseDefinitionsByMethodSignature( $result = []; foreach ($parameters as $parameter) { $class = $parameter->getClass(); - $className = $class ? $class->getName() : null; switch (true) { // match by position case array_key_exists($parameter->getPosition(), $definitions): - $result[] = static::parseDefinition($container, $definitions[$parameter->getPosition()]); + $result[] = $class == null && $parameter->getType() !== null + ? new ValueArgument($definitions[$parameter->getPosition()]) + : static::parseDefinition($container, $definitions[$parameter->getPosition()]); break; // match by name case array_key_exists($parameter->getName(), $definitions): - $result[] = static::parseDefinition($container, $definitions[$parameter->getName()]); + $result[] = $class == null && $parameter->getType() !== null + ? new ValueArgument($definitions[$parameter->getName()]) + : static::parseDefinition($container, $definitions[$parameter->getName()]); break; // autowiring - case $class !== null && $container->has($className): - $result[] = new ContainerArgument($className); + case $class !== null && $container->has($class->getName()): + $result[] = new ContainerArgument($class->getName()); break; // skip optional parameters case $parameter->isOptional(): diff --git a/tests/src/Container/ConstructorTest.php b/tests/src/Container/ConstructorTest.php index 4ad8847..9bb0b8f 100644 --- a/tests/src/Container/ConstructorTest.php +++ b/tests/src/Container/ConstructorTest.php @@ -76,18 +76,4 @@ public function testInterface() $this->assertInstanceOf(InterfaceDependency::class, $entry); $this->assertInstanceOf(Foo::class, $entry->getFoo()); } - - public function testScalar() - { - $container = new Container([ - ScalarDependency::class => [ - 'args' => [1] - ], - ]); - // act - /** @var ScalarDependency $entry */ - $entry = $container->get(ScalarDependency::class); - // assert - $this->assertInternalType('int', $entry->getNumber()); - } } diff --git a/tests/src/Container/DependencyTest.php b/tests/src/Container/DependencyTest.php index 6070b31..49b5c91 100644 --- a/tests/src/Container/DependencyTest.php +++ b/tests/src/Container/DependencyTest.php @@ -56,7 +56,7 @@ public function testInterfaceDependency() /** * @expectedException \Psr\Container\ContainerExceptionInterface - * @expectedExceptionMessage Unable to create instance of entry `Zp\PHPWire\Tests\Fixtures\ScalarDependency`: Unable to invoke arguments to constructor: Please provide definition for argument `number` + * @expectedExceptionMessage Unable to create instance of entry `Zp\PHPWire\Tests\Fixtures\ScalarDependency`: Unable to invoke arguments to constructor: Please provide definition for argument `value` */ public function testUnresolvedDependencies() { diff --git a/tests/src/Container/ScalarTest.php b/tests/src/Container/ScalarTest.php new file mode 100644 index 0000000..111a17c --- /dev/null +++ b/tests/src/Container/ScalarTest.php @@ -0,0 +1,121 @@ + [ + 'args' => [1] + ], + ]); + // act + /** @var ScalarDependency $entry */ + $entry = $container->get(ScalarDependency::class); + // assert + $this->assertEquals(1, $entry->getValue()); + } + + public function testMixedNoService() + { + $container = new Container([ + ScalarDependency::class => [ + 'args' => [Foo::class] + ], + ]); + // act + /** @var ScalarDependency $entry */ + $entry = $container->get(ScalarDependency::class); + // assert + $this->assertEquals(Foo::class, $entry->getValue()); + } + + public function testMixedWithService() + { + $container = new Container([ + Foo::class => [], + ScalarDependency::class => [ + 'args' => [Foo::class] + ], + ]); + // act + /** @var ScalarDependency $entry */ + $entry = $container->get(ScalarDependency::class); + // assert + $this->assertInstanceOf(Foo::class, $entry->getValue()); + } + + public function testMixedWithAlias() + { + $container = new Container([ + 'foo' => [ + 'class' => Foo::class, + ], + ScalarDependency::class => [ + 'args' => ['$foo'] + ], + ]); + // act + /** @var ScalarDependency $entry */ + $entry = $container->get(ScalarDependency::class); + // assert + $this->assertInstanceOf(Foo::class, $entry->getValue()); + } + + public function testStringNoService() + { + $container = new Container([ + StringDependency::class => [ + 'args' => [Foo::class] + ], + ]); + // act + /** @var ScalarDependency $entry */ + $entry = $container->get(StringDependency::class); + // assert + $this->assertEquals(Foo::class, $entry->getValue()); + } + + public function testStringWithService() + { + $container = new Container([ + Foo::class => [], + StringDependency::class => [ + 'args' => [Foo::class] + ], + ]); + // act + /** @var ScalarDependency $entry */ + $entry = $container->get(StringDependency::class); + // assert + $this->assertEquals(Foo::class, $entry->getValue()); + } + + public function testStringWithAlias() + { + $container = new Container([ + 'foo' => [ + 'class' => Foo::class, + ], + StringDependency::class => [ + 'args' => ['$foo'] + ], + ]); + // act + /** @var ScalarDependency $entry */ + $entry = $container->get(StringDependency::class); + // assert + $this->assertEquals('$foo', $entry->getValue()); + } +} diff --git a/tests/src/Fixtures/ScalarDependency.php b/tests/src/Fixtures/ScalarDependency.php index 3a93ac7..fd1204f 100644 --- a/tests/src/Fixtures/ScalarDependency.php +++ b/tests/src/Fixtures/ScalarDependency.php @@ -4,24 +4,15 @@ class ScalarDependency { - /** - * @var int - */ - private $number; + private $value; - /** - * @param $number - */ - public function __construct($number) + public function __construct($value) { - $this->number = $number; + $this->value = $value; } - /** - * @return int - */ - public function getNumber() + public function getValue() { - return $this->number; + return $this->value; } } diff --git a/tests/src/Fixtures/StrictDependency.php b/tests/src/Fixtures/StrictDependency.php deleted file mode 100644 index abcdcd7..0000000 --- a/tests/src/Fixtures/StrictDependency.php +++ /dev/null @@ -1,11 +0,0 @@ -value = $value; + } + + public function getValue() + { + return $this->value; + } +}