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
27 changes: 27 additions & 0 deletions .github/workflows/rector-cs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Rector + PHP CS Fixer

on:
pull_request_target:
paths:
- 'src/**'
- 'tests/**'
- '.github/workflows/rector-cs.yml'
- 'composer.json'
- 'rector.php'
- '.php-cs-fixer.dist.php'

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
rector:
uses: yiisoft/actions/.github/workflows/rector-cs.yml@master
secrets:
token: ${{ secrets.YIISOFT_GITHUB_TOKEN }}
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
php: '8.0'
24 changes: 0 additions & 24 deletions .github/workflows/rector.yml

This file was deleted.

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ phpunit.phar
# phpunit cache
.phpunit.result.cache


# PHP CS Fixer
/.php-cs-fixer.cache
/.php-cs-fixer.php
40 changes: 40 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Config;
use PhpCsFixer\Finder;
use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;

$finder = (new Finder())->in([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// TODO: Update the configuration after raising the minimum PHP version
return (new Config())
->setRiskyAllowed(true)
->setParallelConfig(ParallelConfigFactory::detect())
->setRules([
'@PER-CS2.0' => true,
'nullable_type_declaration' => true,
'operator_linebreak' => true,
'ordered_types' => ['null_adjustment' => 'always_last', 'sort_algorithm' => 'none'],
'single_class_element_per_statement' => true,
'types_spaces' => true,
'no_unused_imports' => true,
'ordered_class_elements' => true,
'class_attributes_separation' => ['elements' => ['method' => 'one']],
'declare_strict_types' => true,
'native_function_invocation' => true,
'native_constant_invocation' => true,
'fully_qualified_strict_types' => [
'import_symbols' => true
],
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],
])
->setFinder($finder);
85 changes: 0 additions & 85 deletions .styleci.yml

This file was deleted.

2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 1.3.2 under development

- no changes in this release.
- Enh #220: Explicitly import functions in "use" section (@mspirkov)

## 1.3.1 December 02, 2025

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.8.3",
"friendsofphp/php-cs-fixer": "^3.66",
"maglnet/composer-require-checker": "^4.4",
"phpunit/phpunit": "^9.6.22",
"rector/rector": "^2.0.10",
Expand Down
34 changes: 17 additions & 17 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public function __construct(
?ContainerInterface $container = null,
array $definitions = [],
private bool $validate = true
private bool $validate = true,
) {
$this->validateDefinitions($definitions);
$this->internalContainer = new FactoryInternalContainer($container, $definitions);
Expand All @@ -47,28 +47,13 @@
*/
public function withDefinitions(array $definitions): self
{
$this->validateDefinitions($definitions);

Check warning on line 50 in src/Factory.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": @@ @@ */ public function withDefinitions(array $definitions): self { - $this->validateDefinitions($definitions); + $new = clone $this; $new->internalContainer = $this->internalContainer->withDefinitions($definitions); return $new;

$new = clone $this;
$new->internalContainer = $this->internalContainer->withDefinitions($definitions);
return $new;
}

/**
* @param array $definitions Definitions to validate.
* @psalm-param array<string, mixed> $definitions
*
* @throws InvalidConfigException
*/
private function validateDefinitions(array $definitions): void
{
if ($this->validate) {
foreach ($definitions as $id => $definition) {
DefinitionValidator::validate($definition, $id);
}
}
}

/**
* Creates a new object using the given configuration.
*
Expand Down Expand Up @@ -140,6 +125,21 @@
return $this->internalContainer->create($definition);
}

/**
* @param array $definitions Definitions to validate.
* @psalm-param array<string, mixed> $definitions
*
* @throws InvalidConfigException
*/
private function validateDefinitions(array $definitions): void
{
if ($this->validate) {
foreach ($definitions as $id => $definition) {
DefinitionValidator::validate($definition, $id);
}
}
}

/**
* @throws InvalidConfigException
*/
Expand All @@ -155,7 +155,7 @@
) {
$definition = $this->mergeDefinitions(
$containerDefinition,
$definition
$definition,
);
}

Expand Down
16 changes: 8 additions & 8 deletions src/FactoryInternalContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use function array_key_exists;
use function is_object;
use function is_string;
use function sprintf;

/**
* Factory's primary container.
Expand All @@ -42,16 +43,15 @@
*/
public function __construct(
private ?ContainerInterface $container,
private array $definitions
) {
}
private array $definitions,
) {}

/**
* @param array<string, mixed> $definitions Definitions to create objects with.
*/
public function withDefinitions(array $definitions): self
{
$new = clone $this;

Check warning on line 54 in src/FactoryInternalContainer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": @@ @@ */ public function withDefinitions(array $definitions): self { - $new = clone $this; + $new = $this; $new->definitions = $definitions; $new->definitionInstances = []; $new->creatingIds = [];
$new->definitions = $definitions;
$new->definitionInstances = [];
$new->creatingIds = [];
Expand All @@ -69,7 +69,7 @@
return $this->build($id);
}

if ($this->container?->has($id)) {

Check warning on line 72 in src/FactoryInternalContainer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "NullSafeMethodCall": @@ @@ if ($this->hasDefinition($id)) { return $this->build($id); } - if ($this->container?->has($id)) { + if ($this->container->has($id)) { return $this->container->get($id); } throw new NotInstantiableClassException($id);
return $this->container->get($id);
}

Expand All @@ -78,16 +78,16 @@

public function has($id): bool
{
return $this->hasDefinition($id) || $this->container?->has($id);

Check warning on line 81 in src/FactoryInternalContainer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "NullSafeMethodCall": @@ @@ } public function has($id): bool { - return $this->hasDefinition($id) || $this->container?->has($id); + return $this->hasDefinition($id) || $this->container->has($id); } public function create(DefinitionInterface $definition): mixed {
}

public function create(DefinitionInterface $definition): mixed
{
if ($definition instanceof ArrayDefinition) {
$this->creatingIds[$definition->getClass()] = 1;

Check warning on line 87 in src/FactoryInternalContainer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": @@ @@ public function create(DefinitionInterface $definition): mixed { if ($definition instanceof ArrayDefinition) { - $this->creatingIds[$definition->getClass()] = 1; + $this->creatingIds[$definition->getClass()] = 2; } try { $result = $definition->resolve($this);
}

try {

Check warning on line 90 in src/FactoryInternalContainer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "UnwrapFinally": @@ @@ if ($definition instanceof ArrayDefinition) { $this->creatingIds[$definition->getClass()] = 1; } - try { - $result = $definition->resolve($this); - return is_object($result) ? clone $result : $result; - } finally { - if ($definition instanceof ArrayDefinition) { - unset($this->creatingIds[$definition->getClass()]); - } + $result = $definition->resolve($this); + return is_object($result) ? clone $result : $result; + if ($definition instanceof ArrayDefinition) { + unset($this->creatingIds[$definition->getClass()]); } } /**
$result = $definition->resolve($this);
return is_object($result) ? clone $result : $result;
} finally {
Expand All @@ -106,8 +106,8 @@
{
if (!isset($this->definitionInstances[$id])) {
if (isset($this->definitions[$id])) {
if (is_object($this->definitions[$id]) && !($this->definitions[$id] instanceof ReferenceInterface)) {

Check warning on line 109 in src/FactoryInternalContainer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndAllSubExprNegation": @@ @@ { if (!isset($this->definitionInstances[$id])) { if (isset($this->definitions[$id])) { - if (is_object($this->definitions[$id]) && !$this->definitions[$id] instanceof ReferenceInterface) { + if (!is_object($this->definitions[$id]) && $this->definitions[$id] instanceof ReferenceInterface) { return Normalizer::normalize($this->definitions[$id], $id); } if (is_string($this->definitions[$id]) && $this->hasDefinition($this->definitions[$id]) && $this->definitions[$id] !== $this->definitions[$this->definitions[$id]]) {

Check warning on line 109 in src/FactoryInternalContainer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "LogicalNot": @@ @@ { if (!isset($this->definitionInstances[$id])) { if (isset($this->definitions[$id])) { - if (is_object($this->definitions[$id]) && !$this->definitions[$id] instanceof ReferenceInterface) { + if (is_object($this->definitions[$id]) && $this->definitions[$id] instanceof ReferenceInterface) { return Normalizer::normalize($this->definitions[$id], $id); } if (is_string($this->definitions[$id]) && $this->hasDefinition($this->definitions[$id]) && $this->definitions[$id] !== $this->definitions[$this->definitions[$id]]) {

Check warning on line 109 in src/FactoryInternalContainer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "InstanceOf_": @@ @@ { if (!isset($this->definitionInstances[$id])) { if (isset($this->definitions[$id])) { - if (is_object($this->definitions[$id]) && !$this->definitions[$id] instanceof ReferenceInterface) { + if (is_object($this->definitions[$id]) && $this->definitions[$id] instanceof ReferenceInterface) { return Normalizer::normalize($this->definitions[$id], $id); } if (is_string($this->definitions[$id]) && $this->hasDefinition($this->definitions[$id]) && $this->definitions[$id] !== $this->definitions[$this->definitions[$id]]) {
return Normalizer::normalize($this->definitions[$id], $id);

Check warning on line 110 in src/FactoryInternalContainer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "ReturnRemoval": @@ @@ if (!isset($this->definitionInstances[$id])) { if (isset($this->definitions[$id])) { if (is_object($this->definitions[$id]) && !$this->definitions[$id] instanceof ReferenceInterface) { - return Normalizer::normalize($this->definitions[$id], $id); + } if (is_string($this->definitions[$id]) && $this->hasDefinition($this->definitions[$id]) && $this->definitions[$id] !== $this->definitions[$this->definitions[$id]]) { $this->definitionInstances[$id] = $this->getDefinition($this->definitions[$id]);
}

if (
Expand All @@ -117,14 +117,14 @@
) {
$this->definitionInstances[$id] = $this->getDefinition($this->definitions[$id]);
} else {
$this->definitionInstances[$id] =
(is_string($this->definitions[$id]) && class_exists($this->definitions[$id]))
$this->definitionInstances[$id]
= (is_string($this->definitions[$id]) && class_exists($this->definitions[$id]))
? ArrayDefinition::fromPreparedData($this->definitions[$id])
: Normalizer::normalize($this->definitions[$id], $id);
}
} else {
throw new LogicException(
sprintf('No definition found for "%s".', $id)
sprintf('No definition found for "%s".', $id),
);
}
}
Expand Down Expand Up @@ -157,8 +157,8 @@
sprintf(
'Circular reference to "%s" detected while creating: %s.',
$id,
implode(', ', array_keys($this->creatingIds))
)
implode(', ', array_keys($this->creatingIds)),
),
);
}

Expand Down
4 changes: 3 additions & 1 deletion src/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Exception;
use Psr\Container\NotFoundExceptionInterface;

use function sprintf;

/**
* NotFoundException is thrown when no definition or class was found in the factory for a given ID.
*/
Expand All @@ -16,7 +18,7 @@ final class NotFoundException extends Exception implements NotFoundExceptionInte
* @param string $id ID of the definition or name of the class that was not found.
*/
public function __construct(
private string $id
private string $id,
) {
parent::__construct(sprintf('No definition or class found or resolvable for %s.', $id));
}
Expand Down
2 changes: 1 addition & 1 deletion src/StrictFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function create(string $id): mixed
}

return $this->internalContainer->create(
$this->internalContainer->getDefinition($id)
$this->internalContainer->getDefinition($id),
);
}

Expand Down
6 changes: 3 additions & 3 deletions tests/FactoryCircularReferencesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testCircularClassDependency(): void
[
Chicken::class => Chicken::class,
Egg::class => Egg::class,
]
],
);

$this->expectException(CircularReferenceException::class);
Expand All @@ -63,12 +63,12 @@ public function testCircularReferences(): void
'engine-1' => Reference::to('engine-2'),
'engine-2' => Reference::to('engine-3'),
'engine-3' => Reference::to('engine-1'),
]
],
);

$this->expectException(CircularReferenceException::class);
$this->expectExceptionMessage(
'Circular reference to "engine-2" detected while creating: engine-2, engine-3, engine-1.'
'Circular reference to "engine-2" detected while creating: engine-2, engine-3, engine-1.',
);
$factory->create('engine-1');
}
Expand Down
Loading
Loading