Skip to content

Commit 77f778b

Browse files
Php84 support (#8)
* Add PHP 8.4 support * fixed compact_nullable_typehint deprecation * Fix nullable types for PHP 8.4 * update readme * update year * fixed PHP_CS_FIXER_IGNORE_ENV deprecation * update composer test:coverage script * removed PHP 7 code * added autowire returns null test * added resolveparameters return empty array when method is null test * cs and sniffer fix
1 parent d14df58 commit 77f778b

File tree

8 files changed

+71
-35
lines changed

8 files changed

+71
-35
lines changed

.cs.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
'array_syntax' => ['syntax' => 'short'],
2020
'cast_spaces' => ['space' => 'none'],
2121
'concat_space' => ['spacing' => 'one'],
22-
'compact_nullable_typehint' => true,
22+
'compact_nullable_type_declaration' => true,
2323
'declare_equal_normalize' => ['space' => 'single'],
2424
'general_phpdoc_annotation_remove' => [
2525
'annotations' => [
@@ -36,7 +36,11 @@
3636
'phpdoc_order' => true, // psr-5
3737
'phpdoc_no_useless_inheritdoc' => false,
3838
'protected_to_private' => false,
39-
'yoda_style' => false,
39+
'yoda_style' => [
40+
'equal' => false,
41+
'identical' => false,
42+
'less_and_greater' => false
43+
],
4044
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
4145
'ordered_imports' => [
4246
'sort_algorithm' => 'alpha',

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2023 odan
3+
Copyright (c) 2025 odan
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
[![Latest Version on Packagist](https://img.shields.io/github/release/selective-php/container.svg)](https://packagist.org/packages/selective/container)
44
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)
55
[![Build Status](https://github.com/selective-php/container/workflows/build/badge.svg)](https://github.com/selective-php/container/actions)
6-
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/selective-php/container.svg)](https://scrutinizer-ci.com/g/selective-php/container/code-structure)
7-
[![Quality Score](https://img.shields.io/scrutinizer/quality/g/selective-php/container.svg)](https://scrutinizer-ci.com/g/selective-php/container/?branch=master)
86
[![Total Downloads](https://img.shields.io/packagist/dt/selective/container.svg)](https://packagist.org/packages/selective/container/stats)
97

108
## Description
@@ -13,11 +11,11 @@ A PSR-11 container implementation with optional **autowiring**.
1311

1412
## Requirements
1513

16-
* PHP 8.1+
14+
* PHP 8.1 - 8.4
1715

1816
## Installation
1917

20-
```
18+
```bash
2119
composer require selective/container
2220
```
2321

composer.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
"psr-11"
88
],
99
"require": {
10-
"php": "^8.1",
10+
"php": "8.1.* || 8.2.* || 8.3.* || 8.4.*",
1111
"psr/container": "^2.0"
1212
},
1313
"require-dev": {
1414
"friendsofphp/php-cs-fixer": "^3",
15-
"phpstan/phpstan": "^1",
15+
"phpstan/phpstan": "^2",
1616
"phpunit/phpunit": "^10",
1717
"squizlabs/php_codesniffer": "^3",
1818
"symfony/event-dispatcher": "^5 || 6.0.*"
@@ -32,12 +32,10 @@
3232
},
3333
"scripts": {
3434
"cs:check": [
35-
"@putenv PHP_CS_FIXER_IGNORE_ENV=1",
36-
"php-cs-fixer fix --dry-run --format=txt --verbose --diff --config=.cs.php --ansi"
35+
"php-cs-fixer fix --dry-run --format=txt --verbose --diff --config=.cs.php --ansi --allow-unsupported-php-version=yes"
3736
],
3837
"cs:fix": [
39-
"@putenv PHP_CS_FIXER_IGNORE_ENV=1",
40-
"php-cs-fixer fix --config=.cs.php --ansi --verbose"
38+
"php-cs-fixer fix --config=.cs.php --ansi --verbose --allow-unsupported-php-version=yes"
4139
],
4240
"sniffer:check": "phpcs --standard=phpcs.xml",
4341
"sniffer:fix": "phpcbf --standard=phpcs.xml",
@@ -49,6 +47,9 @@
4947
"@stan",
5048
"@test"
5149
],
52-
"test:coverage": "php -d xdebug.mode=coverage -r \"require 'vendor/bin/phpunit';\" -- --configuration phpunit.xml --do-not-cache-result --colors=always --coverage-clover build/logs/clover.xml --coverage-html build/coverage"
50+
"test:coverage": [
51+
"@putenv XDEBUG_MODE=coverage",
52+
"phpunit --configuration phpunit.xml --do-not-cache-result --colors=always --display-warnings --display-deprecations --coverage-clover build/coverage/clover.xml --coverage-html build/coverage --coverage-text"
53+
]
5354
}
5455
}

src/Exceptions/InvalidDefinitionException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class InvalidDefinitionException extends Exception implements ContainerException
1919
*
2020
* @return self
2121
*/
22-
public static function create(string $message, Throwable $previous = null): self
22+
public static function create(string $message, ?Throwable $previous = null): self
2323
{
2424
return new self($message, 0, $previous);
2525
}

src/Resolver/ConstructorResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public function resolve(string $id)
7878
*
7979
* @return array<mixed> The resolved parameters
8080
*/
81-
private function resolveParameters(string $id, ReflectionMethod $method = null): array
81+
private function resolveParameters(string $id, ?ReflectionMethod $method = null): array
8282
{
8383
if ($method === null) {
8484
return [];

tests/TestCase/ContainerTest.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Selective\Container\Exceptions\InvalidDefinitionException;
1313
use Selective\Container\Exceptions\NotFoundException;
1414
use Selective\Container\Resolver\ConstructorResolver;
15+
use Selective\Container\Resolver\DefinitionResolverInterface;
1516
use Selective\Container\Test\TestCase\Service\MyAbstractService;
1617
use Selective\Container\Test\TestCase\Service\MyService;
1718
use Selective\Container\Test\TestCase\Service\MyServiceA;
@@ -290,16 +291,7 @@ public function testAutowireWithInvalidInternalClass(): void
290291
$container = new Container();
291292
$container->addResolver(new ConstructorResolver($container));
292293

293-
// https://3v4l.org/1AXpr
294-
if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
295-
// PHP 8+
296-
$this->assertInstanceOf(Exception::class, $container->get(Exception::class));
297-
} else {
298-
// PHP 7.x
299-
// Cannot determine default value for internal functions
300-
$this->expectException(InvalidDefinitionException::class);
301-
$container->get(Exception::class);
302-
}
294+
$this->assertInstanceOf(Exception::class, $container->get(Exception::class));
303295
}
304296

305297
/**
@@ -312,15 +304,7 @@ public function testAutowireWithInvalidInternalInterface(): void
312304
$container = new Container();
313305
$container->addResolver(new ConstructorResolver($container));
314306

315-
if (version_compare(PHP_VERSION, '8.0.0') >= 0) {
316-
// PHP 8+
317-
$this->assertInstanceOf(NotFoundException::class, $container->get(NotFoundException::class));
318-
} else {
319-
// PHP 7.x
320-
// Cannot determine default value for internal functions
321-
$this->expectException(InvalidDefinitionException::class);
322-
$container->get(NotFoundException::class);
323-
}
307+
$this->assertInstanceOf(NotFoundException::class, $container->get(NotFoundException::class));
324308
}
325309

326310
/**
@@ -353,4 +337,25 @@ public function testAutowireWithNotExistingClass(): void
353337
$container->addResolver(new ConstructorResolver($container));
354338
$container->get('Nada\Foo');
355339
}
340+
341+
/**
342+
* Test.
343+
*
344+
* @return void
345+
*/
346+
public function testAutowireReturnsNull(): void
347+
{
348+
$container = new Container();
349+
350+
$resolver = $this->createMock(DefinitionResolverInterface::class);
351+
$resolver->method('isResolvable')->willReturn(false);
352+
$resolver->expects($this->once())
353+
->method('resolve')
354+
->with(stdClass::class)
355+
->willReturn(null);
356+
357+
$container->addResolver($resolver);
358+
359+
$this->assertNull($container->get(stdClass::class));
360+
}
356361
}

tests/TestCase/Resolver/ConstructorResolverTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
namespace Selective\Container\Test\Resolver\TestCase;
44

55
use PHPUnit\Framework\TestCase;
6+
use ReflectionMethod;
67
use Selective\Container\Container;
78
use Selective\Container\Exceptions\InvalidDefinitionException;
89
use Selective\Container\Resolver\ConstructorResolver;
10+
use stdClass;
911

1012
final class ConstructorResolverTest extends TestCase
1113
{
14+
/**
15+
* Test.
16+
*
17+
* @return void
18+
*/
1219
public function testResolveOnInvalidDefinition(): void
1320
{
1421
$this->expectException(InvalidDefinitionException::class);
@@ -19,4 +26,25 @@ public function testResolveOnInvalidDefinition(): void
1926
$constructorResolver = new ConstructorResolver($container);
2027
$constructorResolver->resolve($invalidId);
2128
}
29+
30+
/**
31+
* Resolve parameters returns empty array when method is null.
32+
*
33+
* @return void
34+
*/
35+
public function testResolveParametersReturnsEmptyArrayWhenMethodIsNull(): void
36+
{
37+
$container = new Container();
38+
$resolver = new ConstructorResolver($container);
39+
40+
$ref = new ReflectionMethod(
41+
ConstructorResolver::class,
42+
'resolveParameters'
43+
);
44+
$ref->setAccessible(true);
45+
46+
$result = $ref->invoke($resolver, stdClass::class, null);
47+
48+
$this->assertSame([], $result);
49+
}
2250
}

0 commit comments

Comments
 (0)