Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
added strict scalar typing in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
arren-ru authored and idr0id committed Dec 1, 2017
1 parent 5b79d47 commit b4f16e6
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 47 deletions.
16 changes: 9 additions & 7 deletions src/Arguments/ArgumentsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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():
Expand Down
14 changes: 0 additions & 14 deletions tests/src/Container/ConstructorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion tests/src/Container/DependencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
121 changes: 121 additions & 0 deletions tests/src/Container/ScalarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

namespace Zp\PHPWire\Tests\Container;

use PHPUnit\Framework\TestCase;
use Zp\PHPWire\Container;
use Zp\PHPWire\Tests\Fixtures\ClassDependency;
use Zp\PHPWire\Tests\Fixtures\InterfaceDependency;
use Zp\PHPWire\Tests\Fixtures\ScalarDependency;
use Zp\PHPWire\Tests\Fixtures\Foo;
use Zp\PHPWire\Tests\Fixtures\FooInterface;
use Zp\PHPWire\Tests\Fixtures\StringDependency;

class ScalarTest extends TestCase
{
public function testScalar()
{
$container = new Container([
ScalarDependency::class => [
'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());
}
}
19 changes: 5 additions & 14 deletions tests/src/Fixtures/ScalarDependency.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
11 changes: 0 additions & 11 deletions tests/src/Fixtures/StrictDependency.php

This file was deleted.

18 changes: 18 additions & 0 deletions tests/src/Fixtures/StringDependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Zp\PHPWire\Tests\Fixtures;

class StringDependency
{
private $value;

public function __construct(string $value)
{
$this->value = $value;
}

public function getValue()
{
return $this->value;
}
}

0 comments on commit b4f16e6

Please sign in to comment.