Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
systemson committed Sep 3, 2019
1 parent b02e206 commit 9395cdc
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 64 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"sniff": "phpcs --standard=PSR12 ./src ./tests",
"fix-cs": "phpcbf --standard=PSR12 ./src ./tests",
"speed": "php ./tests/Benchmark.php",
"type-hint": "psalm --show-info=false",
"pull": [
"git pull",
"composer update -vvv"
Expand Down
54 changes: 54 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>

<issueHandlers>
<LessSpecificReturnType errorLevel="info" />

<MissingClosureParamType errorLevel="info" />

<!-- level 3 issues - slightly lazy code writing, but provably low false-negatives -->

<!--<DeprecatedMethod errorLevel="info" />
<DeprecatedProperty errorLevel="info" />
<DeprecatedClass errorLevel="info" />
<DeprecatedConstant errorLevel="info" />
<DeprecatedInterface errorLevel="info" />
<DeprecatedTrait errorLevel="info" />
<InternalMethod errorLevel="info" />
<InternalProperty errorLevel="info" />
<InternalClass errorLevel="info" />
<MissingClosureReturnType errorLevel="info" />
<MissingReturnType errorLevel="info" />
<MissingPropertyType errorLevel="info" />
<InvalidDocblock errorLevel="info" />
<MisplacedRequiredParam errorLevel="info" />
<PropertyNotSetInConstructor errorLevel="info" />
<MissingConstructor errorLevel="info" />
<MissingParamType errorLevel="info" />
<RedundantCondition errorLevel="info" />
<DocblockTypeContradiction errorLevel="info" />
<RedundantConditionGivenDocblockType errorLevel="info" />
<UnresolvableInclude errorLevel="info" />
<RawObjectIteration errorLevel="info" />
<InvalidStringClass errorLevel="info" />-->
</issueHandlers>
</psalm>
10 changes: 10 additions & 0 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@

use Amber\Utils\Implementations\AbstractWrapper;

/**
* @deprecated
*/
class App extends AbstractWrapper
{
/**
* @var Container
*/
protected static $accessor = Container::class;

/**
* @var mixed
*/
protected static $instance;
}
10 changes: 5 additions & 5 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected function getArguments(ServiceClass $service, string $method = '__const

// Then tries to get the argument from the service itself or from the container
try {
$arguments[$key] = $this->getArgumentFromService($service, $key) ?? $this->get($key);
$arguments[$key] = $this->getArgumentFromService($method, $service, $key) ?? $this->get($key);
} catch (NotFoundException $e) {
// If the parameter is not optional throws an exception.
if (!$param->isOptional()) {
Expand Down Expand Up @@ -250,13 +250,13 @@ protected function getArguments(ServiceClass $service, string $method = '__const
*
* @return mixed The argument's value.
*/
protected function getArgumentFromService(ServiceClass $service, string $key)
protected function getArgumentFromService(string $method, ServiceClass $service, string $key)
{
if (!$service->hasArgument($key)) {
if (!$service->hasArgument($method, $key)) {
return;
}

$subService = $service->getArgument($key);
$subService = $service->getArgument($method, $key);

if (!$subService instanceof ServiceClass) {
return $subService;
Expand Down Expand Up @@ -393,7 +393,7 @@ public function getClosureFor(string $class, string $method, array $binds = []):
}

$service = $this->locate($class)
->setArguments($binds)
->setArguments($method, $binds)
;

$args = $this->getArguments($service, $method);
Expand Down
86 changes: 62 additions & 24 deletions src/Service/ArgumentsHandlerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,69 @@

use Amber\Validator\Validator;
use Amber\Container\Container;
use ReflectionClass;
use Amber\Container\Exception\InvalidArgumentException;

trait ArgumentsHandlerTrait
{
/**
* @var array The arguments for the service constructor.
* @var \ReflectionClass.
*/
protected $reflection;

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

/**
* @var array The parameters required for the class constructor.
* @var array
*/
protected $parameters = [];

/**
* Gets the method paramaters for the current class.
* @var array
*/
protected $methods = [];

/**
* Gets an instance of the ReflectionClass for the current class.
*
* @return array The parameters for the class method.
* @return \ReflectionClass
*/
public function getParameters(string $method = '__construct')
public function getReflection(): \ReflectionClass
{
if ($this->reflection instanceof \ReflectionClass) {
return $this->reflection;
}

return $this->reflection = new \ReflectionClass($this->class);
}

public function getMethod(string $method): ?ServiceMethod
{
$reflection = $this->getReflection();
if (isset($this->methods[$method])) {
return $this->methods[$method];
}

if ($this->getReflection()->hasMethod($method)) {
return $this->methods[$method] = new ServiceMethod($method, $this->getReflection()->getMethod($method));
}

if ($reflection->hasMethod($method)) {
$methodReflection = $reflection->getMethod($method);
return null;
}

return $methodReflection->getParameters() ?? [];
/**
* Gets the method paramaters.
*
* @return array
*/
public function getParameters(string $method = '__construct'): array
{
if (($method = $this->getMethod($method)) != null) {
return $method->getParameters();
}

return [];
}

/**
Expand All @@ -45,17 +79,17 @@ public function getParameters(string $method = '__construct')
*
* @return self The current service.
*/
public function setArgument(string $key, $value = null): self
public function setArgument(string $method, string $key, $value = null): self
{
/* Throws an InvalidArgumentException on invalid type. */
if (is_null($value) && !$this->isClass($key)) {
InvalidArgumentException::identifierMustBeClass($key);
}

if ($this->isClass($value ?? $key)) {
$this->arguments[$key] = new ServiceClass($value ?? $key);
$this->getMethod($method)->setArgument($key, new ServiceClass($value ?? $key));
} else {
$this->arguments[$key] = $value;
$this->getMethod($method)->setArgument($key, $value);
}

return $this;
Expand All @@ -66,23 +100,23 @@ public function setArgument(string $key, $value = null): self
*
* @param string $key The argument key.
*
* @return bool.
* @return bool
*/
public function hasArgument(string $key): bool
public function hasArgument(string $method, string $key): bool
{
return isset($this->arguments[$key]);
return $this->getMethod($method)->hasArgument($key);
}

/**
* Gets a Service argument by its key.
*
* @param string $key The argument key.
*
* @return array.
* @return array
*/
public function getArgument(string $key)
public function getArgument(string $method, string $key)
{
$value = $this->arguments[$key];
$value = $this->getMethod($method)->getArgument($key);

if ($value instanceof \Closure) {
return $value();
Expand All @@ -98,10 +132,10 @@ public function getArgument(string $key)
*
* @return self The current service.
*/
public function setArguments(array $arguments = []): self
public function setArguments(string $method, array $arguments = []): self
{
foreach ($arguments as $key => $value) {
$this->setArgument($key, $value);
$this->setArgument($method, $key, $value);
}

return $this;
Expand All @@ -110,10 +144,14 @@ public function setArguments(array $arguments = []): self
/**
* Gets the Service arguments.
*
* @return array.
* @return array
*/
public function getArguments(): array
public function getArguments(string $method): array
{
return $this->arguments;
if (($methodRelection = $this->getMethod($method)) != null) {
return $methodRelection->getArguments();
}

return [];
}
}
19 changes: 1 addition & 18 deletions src/Service/ServiceClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use Amber\Validator\ValidatorTrait;
use Amber\Container\Container;
use ReflectionClass;
use Amber\Container\Exception\InvalidArgumentException;
use BadMethodCallException;

class ServiceClass
{
Expand All @@ -22,11 +20,6 @@ class ServiceClass
*/
protected $instance;

/**
* @var ReflectionClass The class reflection.
*/
protected $reflection;

/**
* @var bool Singleton condition for the class.
*/
Expand All @@ -47,16 +40,6 @@ public function __construct(string $class)
$this->class = $class;
}

/**
* Gets an instance of the ReflectionClass for the current class.
*
* @return ReflectionClass
*/
public function getReflection(): ReflectionClass
{
return new ReflectionClass($this->class);
}

/**
* Sets an instance for the Service.
*
Expand Down Expand Up @@ -183,7 +166,7 @@ public function afterConstruct(string $method, ...$args): self
$methods = get_class_methods($this->class);

if (!in_array($method, $methods)) {
throw new BadMethodCallException("Method [{$this->class}::{$method}()] does not exists.");
throw new \BadMethodCallException("Method [{$this->class}::{$method}()] does not exists.");
}

$this->callback[] = (object) [
Expand Down
Loading

0 comments on commit 9395cdc

Please sign in to comment.