Skip to content

Commit 0b494cb

Browse files
authored
Update the PHP CS Fixer configuration (#112)
1 parent 38a1c86 commit 0b494cb

18 files changed

+147
-139
lines changed

.github/workflows/cs.yml

Lines changed: 0 additions & 48 deletions
This file was deleted.

.github/workflows/rector-cs.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Rector + PHP CS Fixer
2+
3+
on:
4+
pull_request_target:
5+
paths:
6+
- 'src/**'
7+
- 'tests/**'
8+
- '.github/workflows/rector-cs.yml'
9+
- 'composer.json'
10+
- 'rector.php'
11+
- '.php-cs-fixer.dist.php'
12+
13+
permissions:
14+
contents: read
15+
16+
concurrency:
17+
group: ${{ github.workflow }}-${{ github.ref }}
18+
cancel-in-progress: true
19+
20+
jobs:
21+
rector:
22+
uses: yiisoft/actions/.github/workflows/rector-cs.yml@master
23+
secrets:
24+
token: ${{ secrets.YIISOFT_GITHUB_TOKEN }}
25+
with:
26+
repository: ${{ github.event.pull_request.head.repo.full_name }}
27+
php: '8.1'

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ composer.phar
2929

3030
# PHP CS Fixer
3131
/.php-cs-fixer.cache
32+
/.php-cs-fixer.php

.php-cs-fixer.dist.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,24 @@
1414
->exclude('Php8_4/');
1515

1616
return (new Config())
17+
->setRiskyAllowed(true)
1718
->setParallelConfig(ParallelConfigFactory::detect())
1819
->setRules([
19-
'@PER-CS2.0' => true,
20+
'@PER-CS3.0' => true,
2021
'no_unused_imports' => true,
22+
'ordered_class_elements' => true,
23+
'class_attributes_separation' => ['elements' => ['method' => 'one']],
24+
'declare_strict_types' => true,
25+
'native_function_invocation' => true,
26+
'native_constant_invocation' => true,
27+
'fully_qualified_strict_types' => [
28+
'import_symbols' => true
29+
],
30+
'global_namespace_import' => [
31+
'import_classes' => true,
32+
'import_constants' => true,
33+
'import_functions' => true,
34+
],
2135
])
2236
->setFinder($finder);
37+

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 3.4.2 under development
44

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

77
## 3.4.1 December 02, 2025
88

src/ArrayDefinition.php

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -84,35 +84,6 @@ public static function fromPreparedData(string $class, array $constructorArgumen
8484
return new self($class, $constructorArguments, $methodsAndProperties);
8585
}
8686

87-
/**
88-
* @psalm-param array<string, mixed> $config
89-
*
90-
* @psalm-return array<string, MethodOrPropertyItem>
91-
*/
92-
private static function getMethodsAndPropertiesFromConfig(array $config): array
93-
{
94-
$methodsAndProperties = [];
95-
96-
foreach ($config as $key => $value) {
97-
if ($key === self::CONSTRUCTOR) {
98-
continue;
99-
}
100-
101-
/**
102-
* @infection-ignore-all Explode limit does not affect the result.
103-
*
104-
* @see \Yiisoft\Definitions\Tests\Unit\Helpers\DefinitionValidatorTest::testIncorrectMethodName()
105-
*/
106-
if (count($methodArray = explode('()', $key, 2)) === 2) {
107-
$methodsAndProperties[$key] = [self::TYPE_METHOD, $methodArray[0], $value];
108-
} elseif (count($propertyArray = explode('$', $key)) === 2) {
109-
$methodsAndProperties[$key] = [self::TYPE_PROPERTY, $propertyArray[1], $value];
110-
}
111-
}
112-
113-
return $methodsAndProperties;
114-
}
115-
11687
/**
11788
* @psalm-return class-string
11889
*/
@@ -173,6 +144,65 @@ public function resolve(ContainerInterface $container): object
173144
return $object;
174145
}
175146

147+
/**
148+
* Create a new definition that is merged from this definition and another definition.
149+
*
150+
* @param ArrayDefinition $other Definition to merge with.
151+
*
152+
* @return self New definition that is merged from this definition and another definition.
153+
*/
154+
public function merge(self $other): self
155+
{
156+
$new = clone $this;
157+
$new->class = $other->class;
158+
$new->constructorArguments = ArrayDefinitionHelper::mergeArguments($this->constructorArguments, $other->constructorArguments);
159+
160+
$methodsAndProperties = $this->methodsAndProperties;
161+
foreach ($other->methodsAndProperties as $key => $item) {
162+
if ($item[0] === self::TYPE_PROPERTY) {
163+
$methodsAndProperties[$key] = $item;
164+
} elseif ($item[0] === self::TYPE_METHOD) {
165+
/** @psalm-suppress MixedArgument */
166+
$arguments = isset($methodsAndProperties[$key])
167+
? ArrayDefinitionHelper::mergeArguments($methodsAndProperties[$key][2], $item[2])
168+
: $item[2];
169+
$methodsAndProperties[$key] = [$item[0], $item[1], $arguments];
170+
}
171+
}
172+
$new->methodsAndProperties = $methodsAndProperties;
173+
174+
return $new;
175+
}
176+
177+
/**
178+
* @psalm-param array<string, mixed> $config
179+
*
180+
* @psalm-return array<string, MethodOrPropertyItem>
181+
*/
182+
private static function getMethodsAndPropertiesFromConfig(array $config): array
183+
{
184+
$methodsAndProperties = [];
185+
186+
foreach ($config as $key => $value) {
187+
if ($key === self::CONSTRUCTOR) {
188+
continue;
189+
}
190+
191+
/**
192+
* @infection-ignore-all Explode limit does not affect the result.
193+
*
194+
* @see \Yiisoft\Definitions\Tests\Unit\Helpers\DefinitionValidatorTest::testIncorrectMethodName()
195+
*/
196+
if (count($methodArray = explode('()', $key, 2)) === 2) {
197+
$methodsAndProperties[$key] = [self::TYPE_METHOD, $methodArray[0], $value];
198+
} elseif (count($propertyArray = explode('$', $key)) === 2) {
199+
$methodsAndProperties[$key] = [self::TYPE_PROPERTY, $propertyArray[1], $value];
200+
}
201+
}
202+
203+
return $methodsAndProperties;
204+
}
205+
176206
/**
177207
* @param array<string,ParameterDefinition> $dependencies
178208
*
@@ -267,34 +297,4 @@ private function isIntegerIndexed(array $arguments): bool
267297

268298
return $hasIntegerIndex;
269299
}
270-
271-
/**
272-
* Create a new definition that is merged from this definition and another definition.
273-
*
274-
* @param ArrayDefinition $other Definition to merge with.
275-
*
276-
* @return self New definition that is merged from this definition and another definition.
277-
*/
278-
public function merge(self $other): self
279-
{
280-
$new = clone $this;
281-
$new->class = $other->class;
282-
$new->constructorArguments = ArrayDefinitionHelper::mergeArguments($this->constructorArguments, $other->constructorArguments);
283-
284-
$methodsAndProperties = $this->methodsAndProperties;
285-
foreach ($other->methodsAndProperties as $key => $item) {
286-
if ($item[0] === self::TYPE_PROPERTY) {
287-
$methodsAndProperties[$key] = $item;
288-
} elseif ($item[0] === self::TYPE_METHOD) {
289-
/** @psalm-suppress MixedArgument */
290-
$arguments = isset($methodsAndProperties[$key])
291-
? ArrayDefinitionHelper::mergeArguments($methodsAndProperties[$key][2], $item[2])
292-
: $item[2];
293-
$methodsAndProperties[$key] = [$item[0], $item[1], $arguments];
294-
}
295-
}
296-
$new->methodsAndProperties = $methodsAndProperties;
297-
298-
return $new;
299-
}
300300
}

src/Helpers/DefinitionResolver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public static function ensureResolvable(mixed $value): array|ReferenceInterface|
7878

7979
if ($value instanceof DefinitionInterface) {
8080
throw new InvalidConfigException(
81-
'Only references are allowed in constructor arguments, a definition object was provided: ' .
82-
var_export($value, true),
81+
'Only references are allowed in constructor arguments, a definition object was provided: '
82+
. var_export($value, true),
8383
);
8484
}
8585

src/Helpers/DefinitionValidator.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
use function is_string;
2121
use function sprintf;
2222

23+
use const PHP_VERSION_ID;
24+
2325
/**
2426
* Definition validator checks if definition is valid.
2527
*/
@@ -282,8 +284,8 @@ private static function validateConstructor(mixed $value): void
282284
foreach ($value as $argument) {
283285
if (is_object($argument) && !self::isValidObject($argument)) {
284286
throw new InvalidConfigException(
285-
'Only references are allowed in constructor arguments, a definition object was provided: ' .
286-
var_export($argument, true),
287+
'Only references are allowed in constructor arguments, a definition object was provided: '
288+
. var_export($argument, true),
287289
);
288290
}
289291
}

src/Helpers/ExceptionHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public static function incorrectArrayDefinitionMethodArguments(string $key, mixe
3737
{
3838
return new InvalidConfigException(
3939
sprintf(
40-
'Invalid definition: incorrect method "%s" arguments. Expected array, got "%s". ' .
41-
'Probably you should wrap them into square brackets.',
40+
'Invalid definition: incorrect method "%s" arguments. Expected array, got "%s". '
41+
. 'Probably you should wrap them into square brackets.',
4242
$key,
4343
get_debug_type($value),
4444
),

src/Helpers/Normalizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ public static function normalize(mixed $definition, ?string $class = null): Defi
5656
if (is_string($definition)) {
5757
// Current class
5858
if (
59-
$class === $definition ||
60-
($class === null && class_exists($definition))
59+
$class === $definition
60+
|| ($class === null && class_exists($definition))
6161
) {
6262
/** @psalm-var class-string $definition */
6363
return ArrayDefinition::fromPreparedData($definition);

0 commit comments

Comments
 (0)