Skip to content

Commit

Permalink
Merge a34a3e3 into 0ca3e76
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Oct 12, 2020
2 parents 0ca3e76 + a34a3e3 commit 590dbb4
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .github/pull_request_template.md
@@ -0,0 +1,5 @@
<!--
Please explain the motivation behind the changes.
In other words, explain **WHY** instead of **WHAT**.
-->
88 changes: 88 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,88 @@
name: CI

on:
push:
branches: ['master']
pull_request:
branches: ['*']
schedule:
- cron: '0 0 * * *'

jobs:

tests:
name: Tests - PHP ${{ matrix.php }} ${{ matrix.dependency-version }}
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
matrix:
php: [ '7.2', '7.3', '7.4', '8.0' ]
dependency-version: [ '' ]
include:
- php: '7.2'
dependency-version: '--prefer-lowest'
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer
coverage: none
- name: Cache Composer dependencies
uses: actions/cache@v2
with:
path: ~/.composer/cache
key: php-${{ matrix.php }}-composer-locked-${{ hashFiles('composer.lock') }}
restore-keys: php-${{ matrix.php }}-composer-locked-
- name: Install PHP dependencies
run: composer update ${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-progress --no-suggest
- name: PHPUnit
run: vendor/bin/phpunit

cs:
name: Coding standards
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
tools: composer, cs2pr
coverage: none
- name: Cache Composer dependencies
uses: actions/cache@v2
with:
path: ~/.composer/cache
key: php-composer-locked-${{ hashFiles('composer.lock') }}
restore-keys: php-composer-locked-
- name: Install PHP dependencies
run: composer install --no-interaction --no-progress --no-suggest
- name: PHP CS Fixer
run: vendor/bin/php-cs-fixer --format=checkstyle | cs2pr

phpstan:
name: PHPStan
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
tools: composer, cs2pr
coverage: none
- name: Cache Composer dependencies
uses: actions/cache@v2
with:
path: ~/.composer/cache
key: php-composer-locked-${{ hashFiles('composer.lock') }}
restore-keys: php-composer-locked-
- name: Install PHP dependencies
run: composer install --no-interaction --no-progress --no-suggest
- name: PHPStan
run: vendor/bin/phpstan analyse --error-format=checkstyle | cs2pr
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -32,7 +32,7 @@
"opis/closure": "^3.5.5"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
"phpunit/phpunit": "^8.5|^9.0",
"mnapoli/phpunit-easymock": "^1.2",
"doctrine/annotations": "~1.2",
"ocramius/proxy-manager": "~2.0.2",
Expand Down
6 changes: 3 additions & 3 deletions src/Definition/Source/AnnotationBasedAutowiring.php
Expand Up @@ -237,9 +237,9 @@ private function getMethodParameter($parameterIndex, ReflectionParameter $parame
}

// Try to use the type-hinting
$parameterClass = $parameter->getClass();
if ($parameterClass) {
return $parameterClass->getName();
$parameterType = $parameter->getType();
if ($parameterType && !$parameterType->isBuiltin() && $parameterType instanceof ReflectionNamedType) {
return $parameterType->getName();
}

// Last resort, look for @param tag
Expand Down
19 changes: 15 additions & 4 deletions src/Definition/Source/ReflectionBasedAutowiring.php
Expand Up @@ -7,6 +7,7 @@
use DI\Definition\ObjectDefinition;
use DI\Definition\ObjectDefinition\MethodInjection;
use DI\Definition\Reference;
use ReflectionNamedType;

/**
* Reads DI class definitions using reflection.
Expand Down Expand Up @@ -62,11 +63,21 @@ private function getParametersDefinition(\ReflectionFunctionAbstract $constructo
continue;
}

$parameterClass = $parameter->getClass();

if ($parameterClass) {
$parameters[$index] = new Reference($parameterClass->getName());
$parameterType = $parameter->getType();
if (!$parameterType) {
// No type
continue;
}
if ($parameterType->isBuiltin()) {
// Primitive types are not supported
continue;
}
if (!$parameterType instanceof ReflectionNamedType) {
// Union types are not supported
continue;
}

$parameters[$index] = new Reference($parameterType->getName());
}

return $parameters;
Expand Down
25 changes: 18 additions & 7 deletions src/Invoker/FactoryParameterResolver.php
Expand Up @@ -7,6 +7,7 @@
use Invoker\ParameterResolver\ParameterResolver;
use Psr\Container\ContainerInterface;
use ReflectionFunctionAbstract;
use ReflectionNamedType;

/**
* Inject the container, the definition or any other service using type-hints.
Expand Down Expand Up @@ -42,19 +43,29 @@ public function getParameters(
}

foreach ($parameters as $index => $parameter) {
$parameterClass = $parameter->getClass();

if (!$parameterClass) {
$parameterType = $parameter->getType();
if (!$parameterType) {
// No type
continue;
}
if ($parameterType->isBuiltin()) {
// Primitive types are not supported
continue;
}
if (!$parameterType instanceof ReflectionNamedType) {
// Union types are not supported
continue;
}

$parameterClass = $parameterType->getName();

if ($parameterClass->name === 'Psr\Container\ContainerInterface') {
if ($parameterClass === 'Psr\Container\ContainerInterface') {
$resolvedParameters[$index] = $this->container;
} elseif ($parameterClass->name === 'DI\Factory\RequestedEntry') {
} elseif ($parameterClass === 'DI\Factory\RequestedEntry') {
// By convention the second parameter is the definition
$resolvedParameters[$index] = $providedParameters[1];
} elseif ($this->container->has($parameterClass->name)) {
$resolvedParameters[$index] = $this->container->get($parameterClass->name);
} elseif ($this->container->has($parameterClass)) {
$resolvedParameters[$index] = $this->container->get($parameterClass);
}
}

Expand Down
5 changes: 1 addition & 4 deletions tests/IntegrationTest/Definitions/AutowireDefinitionTest.php
Expand Up @@ -317,6 +317,7 @@ public function test_autowire_lazy_object(ContainerBuilder $builder)

/**
* @dataProvider provideContainer
* @requires PHP < 8
*/
public function test_optional_parameter_followed_by_required_parameters(ContainerBuilder $builder)
{
Expand All @@ -333,10 +334,6 @@ public function test_optional_parameter_followed_by_required_parameters(Containe
*/
public function test_php71_nullable_typehint(ContainerBuilder $builder)
{
if (PHP_VERSION_ID < 70100) {
$this->markTestSkipped('This test cannot run on PHP 7');
}

$container = $builder->build();

$object = $container->get(Php71::class);
Expand Down
1 change: 1 addition & 0 deletions tests/IntegrationTest/ErrorMessages/ErrorMessagesTest.php
Expand Up @@ -167,6 +167,7 @@ public function test_factory_not_callable(ContainerBuilder $builder)

/**
* @dataProvider provideContainer
* @requires PHP < 8
*/
public function test_internal_class_default_parameter_value(ContainerBuilder $builder)
{
Expand Down
Expand Up @@ -65,12 +65,12 @@ public function testUnguessableProperty()
(new AnnotationBasedAutowiring)->autowire(AnnotationFixture4::class);
}

/**
* Typed properties support requires PHP 7.4
* @requires PHP 7.4
*/
public function testTypedProperty()
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Typed properties support requires PHP7.4');
}

$definition = (new AnnotationBasedAutowiring)->autowire(AnnotationFixtureTypedProperties::class);

$this->assertNotHasPropertyInjection($definition, 'typeAndNoInject');
Expand All @@ -79,12 +79,12 @@ public function testTypedProperty()
$this->assertHasPropertyInjection($definition, 'typedAndNamed', 'name');
}

/**
* Typed properties support requires PHP 7.4
* @requires PHP 7.4
*/
public function testScalarTypedPropertiesFail()
{
if (PHP_VERSION_ID < 70400) {
$this->markTestSkipped('Typed properties support requires PHP7.4');
}

$this->expectException(\DI\Definition\Exception\InvalidAnnotation::class);
(new AnnotationBasedAutowiring)->autowire(AnnotationFixtureScalarTypedProperty::class);
}
Expand Down

0 comments on commit 590dbb4

Please sign in to comment.