Skip to content

Commit

Permalink
Merge pull request #737 from PHP-DI/php8
Browse files Browse the repository at this point in the history
Support PHP 8
  • Loading branch information
mnapoli committed Oct 12, 2020
2 parents 0ca3e76 + 68b3417 commit 955cace
Show file tree
Hide file tree
Showing 18 changed files with 150 additions and 104 deletions.
2 changes: 0 additions & 2 deletions .coveralls.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# .gitattributes
.github/ export-ignore
tests/ export-ignore
website/ export-ignore
doc/ export-ignore
Expand Down
5 changes: 5 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!--
Please explain the motivation behind the changes.
In other words, explain **WHY** instead of **WHAT**.
-->
95 changes: 95 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
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
if: matrix.php != '8.0'
run: composer update ${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-progress --no-suggest
- name: 'Install PHP dependencies on PHP 8 (TODO: remove that)'
if: matrix.php == '8.0'
run: |
# Install Composer 2
composer self-update --snapshot
composer update --ignore-platform-reqs ${{ 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 fix --dry-run --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
17 changes: 0 additions & 17 deletions .styleci.yml

This file was deleted.

40 changes: 0 additions & 40 deletions .travis.yml

This file was deleted.

3 changes: 0 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Contributing

[![Build Status](https://travis-ci.org/PHP-DI/PHP-DI.png?branch=master)](https://travis-ci.org/PHP-DI/PHP-DI) [![Coverage Status](https://coveralls.io/repos/PHP-DI/PHP-DI/badge.png?branch=master)](https://coveralls.io/r/PHP-DI/PHP-DI?branch=master)

PHP-DI is licensed under the MIT License.


Expand Down Expand Up @@ -39,7 +37,6 @@ Read the [How it works](doc/how-it-works.md) documentation.

## What to do?

- Add tests: pick up uncovered situations in the [code coverage report](https://coveralls.io/r/PHP-DI/PHP-DI)
- Resolve issues: [issue list](https://github.com/PHP-DI/PHP-DI/issues)
- Improve the documentation
-
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ layout: home

[![](doc/img.png)](https://php-di.org/)

[![Build Status](https://img.shields.io/travis/PHP-DI/PHP-DI/master.svg?style=flat-square)](https://travis-ci.org/PHP-DI/PHP-DI)
[![Coverage Status](https://img.shields.io/coveralls/PHP-DI/PHP-DI/master.svg?style=flat-square)](https://coveralls.io/r/PHP-DI/PHP-DI?branch=master)
[![Downloads per months](https://img.shields.io/packagist/dm/PHP-DI/PHP-DI.svg?style=flat-square)](https://packagist.org/packages/PHP-DI/PHP-DI)
[![Total downloads](https://img.shields.io/packagist/dt/PHP-DI/PHP-DI.svg?style=flat-square)](https://packagist.org/packages/PHP-DI/PHP-DI)

[![PrettyCI Status](https://hc4rcprbe1.execute-api.eu-west-1.amazonaws.com/dev?name=php-di/php-di)](https://prettyci.com/)

[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/PHP-DI/PHP-DI.svg)](http://isitmaintained.com/project/PHP-DI/PHP-DI "Average time to resolve an issue")
[![Percentage of issues still open](http://isitmaintained.com/badge/open/PHP-DI/PHP-DI.svg)](http://isitmaintained.com/project/PHP-DI/PHP-DI "Percentage of issues still open")

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
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
1 change: 1 addition & 0 deletions src/Definition/Resolver/ObjectCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private function createProxy(ObjectDefinition $definition, array $parameters) :
function (& $wrappedObject, $proxy, $method, $params, & $initializer) use ($definition, $parameters) {
$wrappedObject = $this->createInstance($definition, $parameters);
$initializer = null; // turning off further lazy initialization

return true;
}
);
Expand Down
6 changes: 3 additions & 3 deletions src/Definition/Source/AnnotationBasedAutowiring.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion website/default.twig
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
|
Website generated with
<a href="http://couscous.io" title="Static website generator for software documentation">Couscous</a>,
supported by <a href="https://prettyci.com/" title="Continuous integration for PHP coding standards">PrettyCI.com</a>.
supported by <a href="https://null.tc/">null</a>.
</p>
</div>
</footer>
Expand Down
10 changes: 0 additions & 10 deletions website/home.twig
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,3 @@
</div>

{% endblock %}

{% block footerTop %}
<p>
Developed with care -
<a href='https://travis-ci.org/PHP-DI/PHP-DI'>
<img src="https://img.shields.io/travis/PHP-DI/PHP-DI.svg?style=flat-square"></a>
<a href='https://coveralls.io/r/PHP-DI/PHP-DI?branch=master'>
<img src="https://img.shields.io/coveralls/PHP-DI/PHP-DI/master.svg?style=flat-square" alt='Coverage Status'></a>
</p>
{% endblock %}

0 comments on commit 955cace

Please sign in to comment.