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
48 changes: 0 additions & 48 deletions .github/workflows/cs.yml

This file was deleted.

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.1'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ composer.phar

# PHP CS Fixer
/.php-cs-fixer.cache
/.php-cs-fixer.php
17 changes: 16 additions & 1 deletion .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,24 @@
->exclude('Php8_4/');

return (new Config())
->setRiskyAllowed(true)
->setParallelConfig(ParallelConfigFactory::detect())
->setRules([
'@PER-CS2.0' => true,
'@PER-CS3.0' => 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);

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

## 3.4.2 under development

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

## 3.4.1 December 02, 2025

Expand Down
118 changes: 59 additions & 59 deletions src/ArrayDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,35 +84,6 @@ public static function fromPreparedData(string $class, array $constructorArgumen
return new self($class, $constructorArguments, $methodsAndProperties);
}

/**
* @psalm-param array<string, mixed> $config
*
* @psalm-return array<string, MethodOrPropertyItem>
*/
private static function getMethodsAndPropertiesFromConfig(array $config): array
{
$methodsAndProperties = [];

foreach ($config as $key => $value) {
if ($key === self::CONSTRUCTOR) {
continue;
}

/**
* @infection-ignore-all Explode limit does not affect the result.
*
* @see \Yiisoft\Definitions\Tests\Unit\Helpers\DefinitionValidatorTest::testIncorrectMethodName()
*/
if (count($methodArray = explode('()', $key, 2)) === 2) {
$methodsAndProperties[$key] = [self::TYPE_METHOD, $methodArray[0], $value];
} elseif (count($propertyArray = explode('$', $key)) === 2) {
$methodsAndProperties[$key] = [self::TYPE_PROPERTY, $propertyArray[1], $value];
}
}

return $methodsAndProperties;
}

/**
* @psalm-return class-string
*/
Expand Down Expand Up @@ -173,6 +144,65 @@ public function resolve(ContainerInterface $container): object
return $object;
}

/**
* Create a new definition that is merged from this definition and another definition.
*
* @param ArrayDefinition $other Definition to merge with.
*
* @return self New definition that is merged from this definition and another definition.
*/
public function merge(self $other): self
{
$new = clone $this;
$new->class = $other->class;
$new->constructorArguments = ArrayDefinitionHelper::mergeArguments($this->constructorArguments, $other->constructorArguments);

$methodsAndProperties = $this->methodsAndProperties;
foreach ($other->methodsAndProperties as $key => $item) {
if ($item[0] === self::TYPE_PROPERTY) {
$methodsAndProperties[$key] = $item;
} elseif ($item[0] === self::TYPE_METHOD) {
/** @psalm-suppress MixedArgument */
$arguments = isset($methodsAndProperties[$key])
? ArrayDefinitionHelper::mergeArguments($methodsAndProperties[$key][2], $item[2])
: $item[2];
$methodsAndProperties[$key] = [$item[0], $item[1], $arguments];
}
}
$new->methodsAndProperties = $methodsAndProperties;

return $new;
}

/**
* @psalm-param array<string, mixed> $config
*
* @psalm-return array<string, MethodOrPropertyItem>
*/
private static function getMethodsAndPropertiesFromConfig(array $config): array
{
$methodsAndProperties = [];

foreach ($config as $key => $value) {
if ($key === self::CONSTRUCTOR) {
continue;
}

/**
* @infection-ignore-all Explode limit does not affect the result.
*
* @see \Yiisoft\Definitions\Tests\Unit\Helpers\DefinitionValidatorTest::testIncorrectMethodName()
*/
if (count($methodArray = explode('()', $key, 2)) === 2) {
$methodsAndProperties[$key] = [self::TYPE_METHOD, $methodArray[0], $value];
} elseif (count($propertyArray = explode('$', $key)) === 2) {
$methodsAndProperties[$key] = [self::TYPE_PROPERTY, $propertyArray[1], $value];
}
}

return $methodsAndProperties;
}

/**
* @param array<string,ParameterDefinition> $dependencies
*
Expand Down Expand Up @@ -267,34 +297,4 @@ private function isIntegerIndexed(array $arguments): bool

return $hasIntegerIndex;
}

/**
* Create a new definition that is merged from this definition and another definition.
*
* @param ArrayDefinition $other Definition to merge with.
*
* @return self New definition that is merged from this definition and another definition.
*/
public function merge(self $other): self
{
$new = clone $this;
$new->class = $other->class;
$new->constructorArguments = ArrayDefinitionHelper::mergeArguments($this->constructorArguments, $other->constructorArguments);

$methodsAndProperties = $this->methodsAndProperties;
foreach ($other->methodsAndProperties as $key => $item) {
if ($item[0] === self::TYPE_PROPERTY) {
$methodsAndProperties[$key] = $item;
} elseif ($item[0] === self::TYPE_METHOD) {
/** @psalm-suppress MixedArgument */
$arguments = isset($methodsAndProperties[$key])
? ArrayDefinitionHelper::mergeArguments($methodsAndProperties[$key][2], $item[2])
: $item[2];
$methodsAndProperties[$key] = [$item[0], $item[1], $arguments];
}
}
$new->methodsAndProperties = $methodsAndProperties;

return $new;
}
}
4 changes: 2 additions & 2 deletions src/Helpers/DefinitionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public static function ensureResolvable(mixed $value): array|ReferenceInterface|

if ($value instanceof DefinitionInterface) {
throw new InvalidConfigException(
'Only references are allowed in constructor arguments, a definition object was provided: ' .
var_export($value, true),
'Only references are allowed in constructor arguments, a definition object was provided: '
. var_export($value, true),
);
}

Expand Down
6 changes: 4 additions & 2 deletions src/Helpers/DefinitionValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use function is_string;
use function sprintf;

use const PHP_VERSION_ID;

/**
* Definition validator checks if definition is valid.
*/
Expand Down Expand Up @@ -282,8 +284,8 @@ private static function validateConstructor(mixed $value): void
foreach ($value as $argument) {
if (is_object($argument) && !self::isValidObject($argument)) {
throw new InvalidConfigException(
'Only references are allowed in constructor arguments, a definition object was provided: ' .
var_export($argument, true),
'Only references are allowed in constructor arguments, a definition object was provided: '
. var_export($argument, true),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/ExceptionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public static function incorrectArrayDefinitionMethodArguments(string $key, mixe
{
return new InvalidConfigException(
sprintf(
'Invalid definition: incorrect method "%s" arguments. Expected array, got "%s". ' .
'Probably you should wrap them into square brackets.',
'Invalid definition: incorrect method "%s" arguments. Expected array, got "%s". '
. 'Probably you should wrap them into square brackets.',
$key,
get_debug_type($value),
),
Expand Down
4 changes: 2 additions & 2 deletions src/Helpers/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public static function normalize(mixed $definition, ?string $class = null): Defi
if (is_string($definition)) {
// Current class
if (
$class === $definition ||
($class === null && class_exists($definition))
$class === $definition
|| ($class === null && class_exists($definition))
) {
/** @psalm-var class-string $definition */
return ArrayDefinition::fromPreparedData($definition);
Expand Down
8 changes: 4 additions & 4 deletions src/ParameterDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ private function resolveVariadicOrBuiltinOrIntersectionOrNonTyped(): mixed
if ($type === null) {
throw new NotInstantiableException(
sprintf(
'Can not determine value of the "%s" parameter without type when instantiating "%s". ' .
'Please specify argument explicitly.',
'Can not determine value of the "%s" parameter without type when instantiating "%s". '
. 'Please specify argument explicitly.',
$this->parameter->getName(),
$this->getCallable(),
),
Expand All @@ -120,8 +120,8 @@ private function resolveVariadicOrBuiltinOrIntersectionOrNonTyped(): mixed

throw new NotInstantiableException(
sprintf(
'Can not determine value of the "%s" parameter of type "%s" when instantiating "%s". ' .
'Please specify argument explicitly.',
'Can not determine value of the "%s" parameter of type "%s" when instantiating "%s". '
. 'Please specify argument explicitly.',
$this->parameter->getName(),
$type,
$this->getCallable(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Yiisoft\Definitions\Tests\Support\DefinitionStorage;

use NonExisting;

final class ServiceWithNonExistingDependency
{
public function __construct(\NonExisting $nonExisting) {}
public function __construct(NonExisting $nonExisting) {}
}
5 changes: 4 additions & 1 deletion tests/Support/Notebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

namespace Yiisoft\Definitions\Tests\Support;

use NotExist1;
use NotExist2;

final class Notebook
{
public function __construct(
public \NotExist1|\NotExist2 $notExist,
public NotExist1|NotExist2 $notExist,
) {}
}
5 changes: 2 additions & 3 deletions tests/Support/Phone.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

final class Phone
{
public bool $dev = false;
public ?string $codeName = null;
private ?string $id = null;
private array $colors;
private array $apps = [];
private ?string $author = null;
private ?string $country = null;

public bool $dev = false;
public ?string $codeName = null;

public function __construct(
private ?string $name = null,
private ?string $version = null,
Expand Down
Loading