Skip to content

Commit

Permalink
Code modernizations for PHP 8.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayesh committed Dec 30, 2023
1 parent ad3a49f commit 4c89a1d
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 25 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"php": "^8.3"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
"phpunit/phpunit": "^10.5.5"
},
"license": "MIT",
"authors": [
Expand Down
12 changes: 7 additions & 5 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" executionOrder="depends,defects" forceCoversAnnotation="false" beStrictAboutCoversAnnotation="true" beStrictAboutOutputDuringTests="true" beStrictAboutTodoAnnotatedTests="true" cacheResult="true" colors="true" verbose="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="vendor/autoload.php" executionOrder="depends,defects" beStrictAboutOutputDuringTests="true" cacheResult="true" colors="true" cacheDirectory=".phpunit.cache" requireCoverageMetadata="false" beStrictAboutCoverageMetadata="true">
<coverage>
<report>
<html outputDirectory="build/coverage-html"/>
<text outputFile="php://stdout" showUncoveredFiles="false"/>
Expand All @@ -15,4 +12,9 @@
</testsuite>
</testsuites>
<logging/>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>
25 changes: 13 additions & 12 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PHPWatch\SimpleContainer\Exception\NotFoundException;
use Psr\Container\ContainerInterface;
use function array_key_exists;
use function gettype;
use function is_callable;
use function sprintf;

Expand All @@ -20,7 +21,7 @@ public function __construct(array $definitions = []) {
$this->definitions = $definitions;
}

private function getService(string $id) {
private function getService(string $id): mixed {
if (!$this->has($id)) {
throw new NotFoundException(sprintf('Container key "%s" is not defined', $id));
}
Expand All @@ -40,7 +41,7 @@ private function getService(string $id) {
return $this->generated[$id] = $this->definitions[$id]($this);
}

public function set(string $id, $value): void {
public function set(string $id, mixed $value): void {
if (array_key_exists($id, $this->definitions)) {
unset($this->generated[$id], $this->factories[$id], $this->protected[$id]);
}
Expand Down Expand Up @@ -70,32 +71,32 @@ private function getDefaultDefinition(string $id, string $exception_message): ca
throw new BadMethodCallException($exception_message);
}
if (!is_callable($this->definitions[$id])) {
throw new BadMethodCallException(sprintf('Definition for "%s" expected to be a callable, "%s" found', $id, \gettype($this->definitions[$id])));
throw new BadMethodCallException(sprintf('Definition for "%s" expected to be a callable, "%s" found', $id, gettype($this->definitions[$id])));
}

return $this->definitions[$id];
}

public function offsetSet($id, $value): void {
$this->set($id, $value);
public function offsetSet(mixed $offset, mixed $value): void {
$this->set($offset, $value);
}

public function offsetUnset($id): void {
unset($this->definitions[$id], $this->generated[$id], $this->factories[$id], $this->protected[$id]);
public function offsetUnset(mixed $offset): void {
unset($this->definitions[$offset], $this->generated[$offset], $this->factories[$offset], $this->protected[$offset]);
}

public function offsetExists(mixed $id): bool {
return array_key_exists($id, $this->definitions);
public function offsetExists(mixed $offset): bool {
return array_key_exists($offset, $this->definitions);
}

public function offsetGet(mixed $id): mixed {
return $this->getService($id);
public function offsetGet(mixed $offset): mixed {
return $this->getService($offset);
}

/**
* @inheritDoc
*/
public function get(string $id) {
public function get(string $id): mixed {
return $this->getService($id);
}

Expand Down
11 changes: 10 additions & 1 deletion tests/ContainerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
use PHPWatch\SimpleContainer\Container;
use PHPUnit\Framework\TestCase;

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

use const PHP_INT_MAX;

class ContainerFactoryTest extends TestCase {
/**
* @throws NotFoundExceptionInterface
* @throws ContainerExceptionInterface
*/
public function testFactoryReturnsDifferentValues(): void {
$random = fn() => random_int(43, \PHP_INT_MAX);
$random = static fn() => random_int(43, PHP_INT_MAX);
$container = new Container(['foo' => $random]);

$this->assertGreaterThan(42, $container->get('foo'));
Expand Down
2 changes: 1 addition & 1 deletion tests/ContainerProtectedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class ContainerProtectedTest extends TestCase {
public function testProtectedFromDefinition(): void {
$item = fn() => 42;
$item = static fn() => 42;
$container = new Container(['foo' => $item, 'bar' => 42]);

$this->assertSame(42, $container['foo']);
Expand Down
7 changes: 5 additions & 2 deletions tests/ContainerServiceCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;

use const PHP_INT_MAX;
use const PHP_INT_MIN;

class ContainerServiceCacheTest extends TestCase {
public function testStandardServiceResolutionInInit(): void {
$container = new Container(
Expand All @@ -23,7 +26,7 @@ public function testStandardServiceResolutionInInit(): void {

public function testStandardServiceResolution(): void {
$container = new Container();
$random = fn() => random_int(\PHP_INT_MIN, \PHP_INT_MAX);
$random = fn() => random_int(PHP_INT_MIN, PHP_INT_MAX);
$container->set('bar', $random);

$value = $container['bar'];
Expand All @@ -38,7 +41,7 @@ public function testStandardServiceResolution(): void {
}

public function testServiceOverrides(): void {
$random = fn() => random_int(43, \PHP_INT_MAX);
$random = static fn() => random_int(43, PHP_INT_MAX);
$static = 42;

$container = new Container(['foo' => $random]);
Expand Down
3 changes: 2 additions & 1 deletion tests/ContainerStaticValuesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use PHPWatch\SimpleContainer\Container;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use stdClass;

class ContainerStaticValuesTest extends TestCase {
Expand Down Expand Up @@ -74,7 +75,7 @@ public function testIssetWorks(): void {
public function testIssetDoesNotExecute(): void {
$container = new Container();
$container->set('kill', static function() {
throw new Exception('Must not execute');
throw new RuntimeException('Must not execute');
});

$this->assertTrue(isset($container['kill']));
Expand Down
2 changes: 1 addition & 1 deletion tests/Exception/BadMethodCallExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function testMarkProtectedWithoutClosure(): void {
$container->setProtected('foo');
}

public function testMarkFactorydWithoutClosure(): void {
public function testMarkFactoryWithoutClosure(): void {
$container = new Container();
$this->expectException(BadMethodCallException::class);
$container->setFactory('foo');
Expand Down
2 changes: 1 addition & 1 deletion tests/Exception/NotFoundExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testNulledValuesDoNotThrow(): void {

public function testClosureNullReturnsDoNotThrow(): void {
$container = new Container();
$container['foo'] = fn(): ?string => null;
$container['foo'] = static fn(): ?string => null;
$this->assertNull($container['foo']);
$this->assertNull($container['foo']); // Trigger cached result

Expand Down

0 comments on commit 4c89a1d

Please sign in to comment.