Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: [ '8.0', '8.1', '8.2', '8.3', '8.4' ]
php: [ '8.2', '8.3', '8.4', '8.5' ]

steps:
- uses: actions/checkout@master
Expand Down
12 changes: 3 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
.PHONY: tests

tests: vendor
php -d zend.assertions=1 -d assert.exception=1 vendor/bin/peridot ./specs
php vendor/bin/pest

vendor: composer.json composer.phar
php composer.phar install
vendor: composer.json
composer install
touch vendor

composer.phar:
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'.PHP_EOL; } else { echo 'Installer corrupt'.PHP_EOL; unlink('composer-setup.php'); exit(1); }"
php composer-setup.php
php -r "unlink('composer-setup.php');"
15 changes: 10 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
"auto-wiring"
],
"require": {
"php": "^8.0",
"php": "^8.2",
"psr/container": "^2.0",
"technically/null-container": "^2.0",
"technically/callable-reflection": "^0.4.2"
"technically/callable-reflection": "^0.5.0"
},
"require-dev": {
"peridot-php/peridot": "^1.19",
"technically/array-container": "^2.0"
"technically/array-container": "^2.0",
"pestphp/pest": "^2.0 || ^3.0 || ^4.0"
},
"license": "MIT",
"authors": [
Expand All @@ -33,7 +33,12 @@
},
"autoload-dev": {
"psr-4": {
"Technically\\DependencyResolver\\Specs\\": "specs"
"Technically\\DependencyResolver\\Tests\\": "tests"
}
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
17 changes: 17 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix=".spec.php">./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
8 changes: 0 additions & 8 deletions specs/Fixtures/MyAbstractClass.php

This file was deleted.

13 changes: 0 additions & 13 deletions specs/Fixtures/MySelfDependencyService.php

This file was deleted.

10 changes: 6 additions & 4 deletions src/Contracts/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ interface DependencyResolver
* It can be either found in the container or constructed on the fly,
* recursively auto-resolving the required parameters.
*
* @param class-string $className
* @return mixed
* @template T
* @param class-string<T> $className
* @return T
*
* @throws InvalidArgumentException If the class does not exist.
* @throws ContainerExceptionInterface If an error occurs while retrieving the existing entry from the container.
Expand All @@ -33,9 +34,10 @@ public function resolve(string $className): mixed;
* Even if the container already has the instance bound,
* it will still be instantiated.
*
* @param class-string $className
* @template T
* @param class-string<T> $className
* @param array<string,mixed> $bindings
* @return mixed
* @return T
*
* @throws ClassCannotBeInstantiated
* @throws CannotAutowireDependencyArgument
Expand Down
15 changes: 8 additions & 7 deletions src/DependencyResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ public function __construct(?ContainerInterface $container = null)
}

/**
* @param class-string $className
* @return mixed
* @template T
* @param class-string<T> $className
* @return T
*
* @throws InvalidArgumentException If class does not exist.
* @throws ContainerExceptionInterface If error occurs while retrieving the existing entry from the container.
Expand All @@ -47,9 +48,10 @@ public function resolve(string $className): mixed
}

/**
* @param class-string $className
* @template T
* @param class-string<T> $className
* @param array<string,mixed> $bindings
* @return mixed
* @return T
*
* @throws ClassCannotBeInstantiated
* @throws CannotAutowireDependencyArgument
Expand Down Expand Up @@ -125,7 +127,6 @@ private function resolveParameters(array $parameters, array $bindings = []): arr
}

/**
* @param ParameterReflection $parameter
* @return mixed|null
*
* @throws CannotAutowireArgument
Expand All @@ -136,7 +137,7 @@ private function resolveParameter(ParameterReflection $parameter): mixed
if ($type->isClassRequirement() && $this->container->has($class = $type->getClassRequirement())) {
try {
return $this->container->get($class);
} catch (ContainerExceptionInterface $exception) {
} catch (ContainerExceptionInterface) {
throw new CannotAutowireArgument($parameter->getName());
}
}
Expand All @@ -155,7 +156,7 @@ private function resolveParameter(ParameterReflection $parameter): mixed
$class = $type->getClassRequirement();
try {
return $this->construct($class);
} catch (DependencyResolutionException $exception) {
} catch (DependencyResolutionException) {
// try another one
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/CannotAutowireArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class CannotAutowireArgument extends DependencyResolutionException
* @param string $argumentName
* @param Throwable|null $previous
*/
public function __construct(string $argumentName, Throwable $previous = null)
public function __construct(string $argumentName, Throwable | null $previous = null)
{
$this->argumentName = $argumentName;

Expand Down
4 changes: 2 additions & 2 deletions src/Exceptions/CannotAutowireDependencyArgument.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ final class CannotAutowireDependencyArgument extends DependencyResolutionExcepti
* @param string $argumentName
* @param Throwable|null $previous
*/
public function __construct(string $dependencyName, string $argumentName, Throwable $previous = null)
public function __construct(string $dependencyName, string $argumentName, Throwable | null $previous = null)
{
$this->dependencyName = $dependencyName;
$this->argumentName = $argumentName;

parent::__construct("Could not autowire argument `{$argumentName}` for `${dependencyName}`.", 0, $previous);
parent::__construct("Could not autowire argument `{$argumentName}` for `{$dependencyName}`.", 0, $previous);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ClassCannotBeInstantiated.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct(string $className)
{
$this->className = $className;

parent::__construct("Class (${className}) cannot be instantiated.");
parent::__construct("Class ({$className}) cannot be instantiated.");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
it('should instantiate', function () {
$resolver = new DependencyResolver();

assert($resolver instanceof DependencyResolver);
expect($resolver)->toBeInstanceOf(DependencyResolver::class);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
use Psr\Container\ContainerInterface;
use Technically\ArrayContainer\ArrayContainer;
use Technically\DependencyResolver\DependencyResolver;
use Technically\DependencyResolver\Specs\Fixtures\MyInstanceMethodService;
use Technically\DependencyResolver\Specs\Fixtures\MyInvokableService;
use Technically\DependencyResolver\Specs\Fixtures\MyStaticMethodService;
use Technically\DependencyResolver\Tests\Fixtures\MyInstanceMethodService;
use Technically\DependencyResolver\Tests\Fixtures\MyInvokableService;
use Technically\DependencyResolver\Tests\Fixtures\MyStaticMethodService;
use Technically\NullContainer\NullContainer;

describe('DependencyResolver::call()', function () {
Expand All @@ -16,23 +16,23 @@

$value = $resolver->call('my_global_function', ['value' => $resolver]);

assert($value === true);
expect($value)->toBeTrue();
});

it('should call Closures resolving dependencies', function () {
$container = new ArrayContainer();
$resolver = new DependencyResolver($container);
$container->set(DependencyResolver::class, $resolver);

[$r, $m, $n] = $resolver->call(function (DependencyResolver $resolver, string $message, int $else = null) {
[$r, $m, $n] = $resolver->call(function (DependencyResolver $resolver, string $message, ?int $else = null) {
return [$resolver, $message, $else];
}, [
'message' => 'Hello'
]);

assert($r === $resolver);
assert($m === 'Hello');
assert($n === null);
expect($r)->toBe($resolver);
expect($m)->toBe('Hello');
expect($n)->toBeNull();
});

it('should call static method array resolving dependencies', function () {
Expand All @@ -44,9 +44,9 @@
'message' => 'Hello'
]);

assert($r === $resolver);
assert($m === 'Hello');
assert($n === null);
expect($r)->toBe($resolver);
expect($m)->toBe('Hello');
expect($n)->toBeNull();
});

it('should call static method string resolving dependencies', function () {
Expand All @@ -58,9 +58,9 @@
'message' => 'Hello'
]);

assert($r === $resolver);
assert($m === 'Hello');
assert($n === null);
expect($r)->toBe($resolver);
expect($m)->toBe('Hello');
expect($n)->toBeNull();
});

it('should call instance methods resolving dependencies', function () {
Expand All @@ -72,9 +72,9 @@
'message' => 'Hello'
]);

assert($r === $resolver);
assert($m === 'Hello');
assert($n === null);
expect($r)->toBe($resolver);
expect($m)->toBe('Hello');
expect($n)->toBeNull();
});

it('should call invokable objects resolving dependencies', function () {
Expand All @@ -86,9 +86,9 @@
'message' => 'Hello'
]);

assert($r === $resolver);
assert($m === 'Hello');
assert($n === null);
expect($r)->toBe($resolver);
expect($m)->toBe('Hello');
expect($n)->toBeNull();
});

it('should resolve typed variadic parameters', function () {
Expand All @@ -100,8 +100,8 @@
return $containers;
});

assert(is_array($ret));
assert(count($ret) === 1);
expect($ret)->toBeArray();
expect($ret)->toHaveCount(1);
});

it('should call function by passing variadic parameter value explicitly', function () {
Expand All @@ -117,8 +117,8 @@
'containers' => [$null],
]);

assert(is_array($ret));
assert(count($ret) === 1);
assert($ret === [$null]);
expect($ret)->toBeArray();
expect($ret)->toHaveCount(1);
expect($ret)->toBe([$null]);
});
});
Loading