Skip to content

Commit

Permalink
Merge copnflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
philipobenito committed Mar 13, 2024
1 parent 4f57589 commit 43cf67d
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 99 deletions.
5 changes: 1 addition & 4 deletions src/Argument/ArgumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@

interface ArgumentInterface
{
/**
* @return mixed
*/
public function getValue();
public function getValue(): mixed;
}
10 changes: 2 additions & 8 deletions src/Argument/DefaultValueArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,12 @@

class DefaultValueArgument extends ResolvableArgument implements DefaultValueInterface
{
protected $defaultValue;

public function __construct(string $value, $defaultValue = null)
public function __construct(string $value, protected mixed $defaultValue = null)
{
$this->defaultValue = $defaultValue;
parent::__construct($value);
}

/**
* @return mixed|null
*/
public function getDefaultValue()
public function getDefaultValue(): mixed
{
return $this->defaultValue;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Argument/DefaultValueInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@

interface DefaultValueInterface extends ArgumentInterface
{
/**
* @return mixed
*/
public function getDefaultValue();
public function getDefaultValue(): mixed;
}
12 changes: 3 additions & 9 deletions src/Argument/LiteralArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ class LiteralArgument implements LiteralArgumentInterface
public const TYPE_OBJECT = 'object';
public const TYPE_STRING = 'string';

/**
* @var mixed
*/
protected $value;
protected mixed $value;

public function __construct($value, string $type = null)
public function __construct(mixed $value, string $type = null)
{
if (
null === $type
Expand All @@ -38,10 +35,7 @@ public function __construct($value, string $type = null)
}
}

/**
* {@inheritdoc}
*/
public function getValue()
public function getValue(): mixed
{
return $this->value;
}
Expand Down
5 changes: 1 addition & 4 deletions src/Argument/ResolvableArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@

class ResolvableArgument implements ResolvableArgumentInterface
{
protected $value;

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

public function getValue(): string
Expand Down
98 changes: 33 additions & 65 deletions src/Definition/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,18 @@ class Definition implements ArgumentResolverInterface, DefinitionInterface
use ArgumentResolverTrait;
use ContainerAwareTrait;

/**
* @var string
*/
protected $alias;

/**
* @var mixed
*/
protected $concrete;

/**
* @var boolean
*/
protected $shared = false;

/**
* @var array
*/
protected $tags = [];

/**
* @var array
*/
protected $arguments = [];

/**
* @var array
*/
protected $methods = [];

/**
* @var mixed
*/
protected $resolved;

/**
* @var bool
*/
protected $isAlreadySearched = false;

/**
* @param string $id
* @param mixed|null $concrete
*/
public function __construct(string $id, $concrete = null)
{
$concrete ??= $id;
$this->alias = $id;
$this->concrete = $concrete;
protected mixed $resolved = null;
protected array $recursiveCheck = [];

public function __construct(
protected string $id,
protected mixed $concrete = null,
protected bool $shared = false,
protected array $arguments = [],
protected array $methods = [],
protected array $tags = [],
) {
$this->concrete ??= $this->id;
}

public function addTag(string $tag): DefinitionInterface
Expand All @@ -83,15 +46,25 @@ public function hasTag(string $tag): bool
return isset($this->tags[$tag]);
}

public function setAlias(string $id): DefinitionInterface
public function setId(string $id): DefinitionInterface
{
$this->alias = $id;
$this->id = $id;
return $this;
}

public function getId(): string
{
return $this->id;
}

public function setAlias(string $id): DefinitionInterface
{
return $this->setId($id);
}

public function getAlias(): string
{
return $this->alias;
return $this->getId();
}

public function setShared(bool $shared = true): DefinitionInterface
Expand Down Expand Up @@ -135,7 +108,7 @@ public function addArguments(array $args): DefinitionInterface
public function addMethodCall(string $method, array $args = []): DefinitionInterface
{
$this->methods[] = [
'method' => $method,
'method' => $method,
'arguments' => $args
];

Expand All @@ -151,7 +124,7 @@ public function addMethodCalls(array $methods = []): DefinitionInterface
return $this;
}

public function resolve()
public function resolve(): mixed
{
if (null !== $this->resolved && $this->isShared()) {
return $this->resolved;
Expand All @@ -160,7 +133,7 @@ public function resolve()
return $this->resolveNew();
}

public function resolveNew()
public function resolveNew(): mixed
{
$concrete = $this->concrete;

Expand Down Expand Up @@ -192,28 +165,23 @@ public function resolveNew()
}

// stop recursive resolving
if ($this->isAlreadySearched) {
throw new NotFoundException(
sprintf('Alias or class "%s" did not found.', $concrete)
);
if (is_string($concrete) && in_array($concrete, $this->recursiveCheck)) {
$this->resolved = $concrete;
return $concrete;
}

// if we still have a string, try to pull it from the container
// this allows for `alias -> alias -> ... -> concrete
if (is_string($concrete) && $container instanceof ContainerInterface && $container->has($concrete)) {
$this->isAlreadySearched = true;
$this->recursiveCheck[] = $concrete;
$concrete = $container->get($concrete);
}

$this->resolved = $concrete;
return $concrete;
}

/**
* @param callable $concrete
* @return mixed
*/
protected function resolveCallable(callable $concrete)
protected function resolveCallable(callable $concrete): mixed
{
$resolved = $this->resolveArguments($this->arguments);
return call_user_func_array($concrete, $resolved);
Expand Down
8 changes: 3 additions & 5 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,12 @@ public function testContainerAwareCannotBeUsedWithoutImplementingInterface(): vo
$class->setContainer($container);
}

public function testNonExistentClassCausesException(): void
public function testNonExistentClassResolvesAsString(): void
{
$container = new Container();
$container->add(NonExistent::class);

self::assertTrue($container->has(NonExistent::class));

$this->expectException(NotFoundException::class);
$container->get(NonExistent::class);
$this->assertTrue($container->has(NonExistent::class));
$this->assertSame(NonExistent::class, $container->get(NonExistent::class));
}
}

0 comments on commit 43cf67d

Please sign in to comment.