repo stringlengths 7 63 | file_url stringlengths 81 284 | file_path stringlengths 5 200 | content stringlengths 0 32.8k | language stringclasses 1
value | license stringclasses 7
values | commit_sha stringlengths 40 40 | retrieved_at stringdate 2026-01-04 15:02:33 2026-01-05 05:24:06 | truncated bool 2
classes |
|---|---|---|---|---|---|---|---|---|
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Tokenizer/Transformer/NullableTypeTransformerTest.php | tests/Tokenizer/Transformer/NullableTypeTransformerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tokenizer\CT;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Transformer\NullableTypeTransformer
*
* @phpstan-import-type _TransformerTestExpectedKindsUnderIndex from AbstractTransformerTestCase
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NullableTypeTransformerTest extends AbstractTransformerTestCase
{
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcessCases
*/
public function testProcess(string $source, array $expectedTokens = []): void
{
$this->doTest(
$source,
$expectedTokens,
[
CT::T_NULLABLE_TYPE,
],
);
}
/**
* @return iterable<int, array{0: string, 1?: _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcessCases(): iterable
{
yield [
'<?php function foo(?Barable $barA, ?Barable $barB): ?Fooable {}',
[
5 => CT::T_NULLABLE_TYPE,
11 => CT::T_NULLABLE_TYPE,
18 => CT::T_NULLABLE_TYPE,
],
];
yield [
'<?php interface Fooable { function foo(): ?Fooable; }',
[
14 => CT::T_NULLABLE_TYPE,
],
];
yield [
'<?php
$a = 1 ? "aaa" : "bbb";
$b = 1 ? fnc() : [];
$c = 1 ?: [];
$a instanceof static ? "aaa" : "bbb";
',
];
yield [
'<?php class Foo { private ?string $foo; }',
[
9 => CT::T_NULLABLE_TYPE,
],
];
yield [
'<?php class Foo { protected ?string $foo; }',
[
9 => CT::T_NULLABLE_TYPE,
],
];
yield [
'<?php class Foo { public ?string $foo; }',
[
9 => CT::T_NULLABLE_TYPE,
],
];
yield [
'<?php class Foo { var ?string $foo; }',
[
9 => CT::T_NULLABLE_TYPE,
],
];
yield [
'<?php class Foo { var ? Foo\Bar $foo; }',
[
9 => CT::T_NULLABLE_TYPE,
],
];
yield [
'<?php fn(?Barable $barA, ?Barable $barB): ?Fooable => null;',
[
3 => CT::T_NULLABLE_TYPE,
9 => CT::T_NULLABLE_TYPE,
16 => CT::T_NULLABLE_TYPE,
],
];
yield [
'<?php class Foo { public ?array $foo; public static ?array $bar; }',
[
9 => CT::T_NULLABLE_TYPE,
19 => CT::T_NULLABLE_TYPE,
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess80Cases
*
* @requires PHP 8.0
*/
public function testProcess80(array $expectedTokens, string $source): void
{
$this->testProcess($source, $expectedTokens);
}
/**
* @return iterable<int, array{_TransformerTestExpectedKindsUnderIndex, string}>
*/
public static function provideProcess80Cases(): iterable
{
yield [
[
17 => CT::T_NULLABLE_TYPE,
29 => CT::T_NULLABLE_TYPE,
41 => CT::T_NULLABLE_TYPE,
],
'<?php
class Foo
{
public function __construct(
private ?string $foo = null,
protected ?string $bar = null,
public ?string $xyz = null,
) {
}
}
',
];
yield [
[
10 => CT::T_NULLABLE_TYPE,
],
'<?php
function test(#[TestAttribute] ?User $user) {}
',
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess81Cases
*
* @requires PHP 8.1
*/
public function testProcess81(array $expectedTokens, string $source): void
{
$this->testProcess($source, $expectedTokens);
}
/**
* @return iterable<int, array{_TransformerTestExpectedKindsUnderIndex, string}>
*/
public static function provideProcess81Cases(): iterable
{
yield [
[
19 => CT::T_NULLABLE_TYPE,
33 => CT::T_NULLABLE_TYPE,
47 => CT::T_NULLABLE_TYPE,
],
'<?php
class Foo
{
public function __construct(
private readonly ?string $foo = null,
protected readonly ?string $bar = null,
public readonly ?string $xyz = null,
) {
}
}
',
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess83Cases
*
* @requires PHP 8.3
*/
public function testProcess83(array $expectedTokens, string $source): void
{
$this->testProcess($source, $expectedTokens);
}
/**
* @return iterable<string, array{_TransformerTestExpectedKindsUnderIndex, string}>
*/
public static function provideProcess83Cases(): iterable
{
yield 'nullable class constant' => [
[
12 => CT::T_NULLABLE_TYPE,
],
'<?php
class Foo
{
public const ?string FOO = null;
}
',
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess84Cases
*
* @requires PHP 8.4
*/
public function testProcess84(array $expectedTokens, string $source): void
{
$this->testProcess($source, $expectedTokens);
}
/**
* @return iterable<string, array{_TransformerTestExpectedKindsUnderIndex, string}>
*/
public static function provideProcess84Cases(): iterable
{
yield 'asymmetric visibility' => [
[
18 => CT::T_NULLABLE_TYPE,
28 => CT::T_NULLABLE_TYPE,
38 => CT::T_NULLABLE_TYPE,
],
<<<'PHP'
<?php
class Foo {
public function __construct(
public public(set) ?Bar $x,
public protected(set) ?Bar $y,
public private(set) ?Bar $z,
) {}
}
PHP,
];
yield 'abstract properties' => [
[
13 => CT::T_NULLABLE_TYPE,
29 => CT::T_NULLABLE_TYPE,
45 => CT::T_NULLABLE_TYPE,
61 => CT::T_NULLABLE_TYPE,
77 => CT::T_NULLABLE_TYPE,
93 => CT::T_NULLABLE_TYPE,
],
<<<'PHP'
<?php
abstract class Foo {
abstract public ?bool $b1 { set; }
public abstract ?bool $b2 { set; }
abstract protected ?int $i1 { set; }
protected abstract ?int $i2 { set; }
abstract private ?string $s1 { set; }
private abstract ?string $s2 { set; }
}
PHP,
];
yield 'final properties' => [
[
11 => CT::T_NULLABLE_TYPE,
31 => CT::T_NULLABLE_TYPE,
51 => CT::T_NULLABLE_TYPE,
71 => CT::T_NULLABLE_TYPE,
91 => CT::T_NULLABLE_TYPE,
111 => CT::T_NULLABLE_TYPE,
],
<<<'PHP'
<?php
class Foo {
final public ?bool $b1 { get => 0; }
public final ?bool $b2 { get => 0; }
final protected ?int $i1 { get => 0; }
protected final ?int $i2 { get => 0; }
final private ?string $s1 { get => 0; }
private final ?string $s2 { get => 0; }
}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Tokenizer/Transformer/NameQualifiedTransformerTest.php | tests/Tokenizer/Transformer/NameQualifiedTransformerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tests\Test\Assert\AssertTokensTrait;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\FCT;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Tokenizer\Tokens;
/**
* @internal
*
* @requires PHP 8.0
*
* @covers \PhpCsFixer\Tokenizer\Transformer\NameQualifiedTransformer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NameQualifiedTransformerTest extends AbstractTransformerTestCase
{
use AssertTokensTrait;
/**
* @param list<Token> $expected
* @param null|list<Token> $input
*
* @dataProvider provideProcessCases
*
* @requires PHP 8.0
*/
public function testProcess(array $expected, ?array $input = null): void
{
$expectedTokens = Tokens::fromArray($expected);
$tokens = null === $input
? Tokens::fromArray($expected)
: Tokens::fromArray($input);
$tokenCount = \count($tokens);
for ($i = 0; $i < $tokenCount; ++$i) {
$this->transformer->process($tokens, $tokens[$i], $i);
}
self::assertTokens($expectedTokens, $tokens);
if (null === $input) {
self::assertFalse($tokens->isChanged());
} else {
self::testProcess($expected);
self::assertTrue($tokens->isChanged());
}
}
/**
* @return iterable<string, array{0: list<Token>, 1?: list<Token>}>
*/
public static function provideProcessCases(): iterable
{
yield 'string' => [
[
new Token([\T_OPEN_TAG, "<?php\n"]),
new Token([\T_STRING, 'Foo']),
new Token(';'),
],
];
yield 'relative 1' => [
[
new Token([\T_OPEN_TAG, "<?php\n"]),
new Token([\T_NAMESPACE, 'namespace']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Transformer']),
new Token(';'),
],
[
new Token([\T_OPEN_TAG, "<?php\n"]),
new Token([FCT::T_NAME_RELATIVE, 'namespace\Transformer']),
new Token(';'),
],
];
yield 'relative 2' => [
[
new Token([\T_OPEN_TAG, "<?php\n"]),
new Token([\T_NAMESPACE, 'namespace']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Transformer']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Foo']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar']),
new Token(';'),
],
[
new Token([\T_OPEN_TAG, "<?php\n"]),
new Token([FCT::T_NAME_RELATIVE, 'namespace\Transformer\Foo\Bar']),
new Token(';'),
],
];
yield 'name fully qualified 1' => [
[
new Token([\T_OPEN_TAG, "<?php\n"]),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Foo']),
new Token(';'),
],
[
new Token([\T_OPEN_TAG, "<?php\n"]),
new Token([FCT::T_NAME_FULLY_QUALIFIED, '\Foo']),
new Token(';'),
],
];
yield 'name qualified 1' => [
[
new Token([\T_OPEN_TAG, "<?php\n"]),
new Token([\T_STRING, 'Foo']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar']),
new Token(';'),
],
[
new Token([\T_OPEN_TAG, "<?php\n"]),
new Token([FCT::T_NAME_QUALIFIED, 'Foo\Bar']),
new Token(';'),
],
];
yield 'name qualified 2' => [
[
new Token([\T_OPEN_TAG, "<?php\n"]),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Foo']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar']),
new Token(';'),
],
[
new Token([\T_OPEN_TAG, "<?php\n"]),
new Token([FCT::T_NAME_QUALIFIED, '\Foo\Bar']),
new Token(';'),
],
];
}
/**
* @param list<Token> $expected
*
* @dataProvider providePriorityCases
*/
public function testPriority(array $expected, string $source): void
{
// Parse `$source` before tokenizing `$expected`, so source has priority to generate collection
// over blindly re-taking cached collection created on top of `$expected`.
$tokens = Tokens::fromCode($source);
self::assertTokens(Tokens::fromArray($expected), $tokens);
}
/**
* @return iterable<int, array{list<Token>, string}>
*/
public static function providePriorityCases(): iterable
{
yield [
[
new Token([\T_OPEN_TAG, '<?php ']),
new Token([\T_STRING, 'Foo']),
new Token(';'),
new Token([\T_STRING, 'Bar']),
new Token(';'),
],
'<?php Foo;Bar;',
];
yield [
[
new Token([\T_OPEN_TAG, '<?php ']),
new Token([\T_STRING, 'Foo']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar1']),
new Token(';'),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Foo']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar2']),
new Token(';'),
new Token([\T_STRING, 'Foo']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar3']),
new Token(';'),
],
'<?php Foo\Bar1;\Foo\Bar2;Foo\Bar3;',
];
yield [
[
new Token([\T_OPEN_TAG, '<?php ']),
new Token([\T_STRING, 'Foo']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar1']),
new Token(';'),
new Token([\T_STRING, 'Foo']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar2']),
new Token(';'),
new Token([\T_STRING, 'Foo']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar3']),
new Token(';'),
],
'<?php Foo\Bar1;Foo\Bar2;Foo\Bar3;',
];
yield [
[
new Token([\T_OPEN_TAG, '<?php ']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Foo']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar1']),
new Token(';'),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Foo']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar2']),
new Token(';'),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Foo']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar3']),
new Token(';'),
],
'<?php \Foo\Bar1;\Foo\Bar2;\Foo\Bar3;',
];
yield [
[
new Token([\T_OPEN_TAG, '<?php ']),
new Token([CT::T_NAMESPACE_OPERATOR, 'namespace']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Transformer']),
new Token(';'),
],
'<?php namespace\Transformer;',
];
yield [
[
new Token([\T_OPEN_TAG, '<?php ']),
new Token([CT::T_NAMESPACE_OPERATOR, 'namespace']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Foo']),
new Token(';'),
new Token([CT::T_NAMESPACE_OPERATOR, 'namespace']),
new Token([\T_NS_SEPARATOR, '\\']),
new Token([\T_STRING, 'Bar']),
new Token(';'),
],
'<?php namespace\Foo;namespace\Bar;',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Tokenizer/Transformer/BraceTransformerTest.php | tests/Tokenizer/Transformer/BraceTransformerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Tokens;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Transformer\BraceTransformer
*
* @phpstan-import-type _TransformerTestExpectedKindsUnderIndex from AbstractTransformerTestCase
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class BraceTransformerTest extends AbstractTransformerTestCase
{
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcessCases
*/
public function testProcess(string $source, array $expectedTokens = []): void
{
$this->doTest(
$source,
$expectedTokens,
[
\T_CURLY_OPEN,
CT::T_CURLY_CLOSE,
\T_DOLLAR_OPEN_CURLY_BRACES,
CT::T_DOLLAR_CLOSE_CURLY_BRACES,
CT::T_DYNAMIC_PROP_BRACE_OPEN,
CT::T_DYNAMIC_PROP_BRACE_CLOSE,
CT::T_DYNAMIC_VAR_BRACE_OPEN,
CT::T_DYNAMIC_VAR_BRACE_CLOSE,
CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN,
CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
CT::T_GROUP_IMPORT_BRACE_OPEN,
CT::T_GROUP_IMPORT_BRACE_CLOSE,
CT::T_PROPERTY_HOOK_BRACE_OPEN,
CT::T_PROPERTY_HOOK_BRACE_CLOSE,
],
);
}
/**
* @return iterable<string, array{0: string, 1?: _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcessCases(): iterable
{
yield 'curly open/close I' => [
'<?php echo "This is {$great}";',
[
5 => \T_CURLY_OPEN,
7 => CT::T_CURLY_CLOSE,
],
];
yield 'curly open/close II' => [
'<?php $a = "a{$b->c()}d";',
[
7 => \T_CURLY_OPEN,
13 => CT::T_CURLY_CLOSE,
],
];
yield 'dynamic var brace open/close' => [
'<?php echo "I\'d like an {${beers::$ale}}\n";',
[
5 => \T_CURLY_OPEN,
7 => CT::T_DYNAMIC_VAR_BRACE_OPEN,
11 => CT::T_DYNAMIC_VAR_BRACE_CLOSE,
12 => CT::T_CURLY_CLOSE,
],
];
yield 'dollar curly brace open/close' => [
'<?php echo "This is ${great}";',
[
5 => \T_DOLLAR_OPEN_CURLY_BRACES,
7 => CT::T_DOLLAR_CLOSE_CURLY_BRACES,
],
];
yield 'dynamic property brace open/close' => [
'<?php $foo->{$bar};',
[
3 => CT::T_DYNAMIC_PROP_BRACE_OPEN,
5 => CT::T_DYNAMIC_PROP_BRACE_CLOSE,
],
];
yield 'dynamic variable brace open/close' => [
'<?php ${$bar};',
[
2 => CT::T_DYNAMIC_VAR_BRACE_OPEN,
4 => CT::T_DYNAMIC_VAR_BRACE_CLOSE,
],
];
yield 'mixed' => [
'<?php echo "This is {$great}";
$a = "a{$b->c()}d";
echo "I\'d like an {${beers::$ale}}\n";
',
[
5 => \T_CURLY_OPEN,
7 => CT::T_CURLY_CLOSE,
17 => \T_CURLY_OPEN,
23 => CT::T_CURLY_CLOSE,
32 => \T_CURLY_OPEN,
34 => CT::T_DYNAMIC_VAR_BRACE_OPEN,
38 => CT::T_DYNAMIC_VAR_BRACE_CLOSE,
39 => CT::T_CURLY_CLOSE,
],
];
yield 'do not touch' => [
'<?php if (1) {} class Foo{ } function bar(){ }',
];
yield 'dynamic property with string with variable' => [
'<?php $object->{"set_{$name}"}(42);',
[
3 => CT::T_DYNAMIC_PROP_BRACE_OPEN,
6 => \T_CURLY_OPEN,
8 => CT::T_CURLY_CLOSE,
10 => CT::T_DYNAMIC_PROP_BRACE_CLOSE,
],
];
yield 'group import' => [
'<?php use some\a\{ClassA, ClassB, ClassC as C};',
[
7 => CT::T_GROUP_IMPORT_BRACE_OPEN,
19 => CT::T_GROUP_IMPORT_BRACE_CLOSE,
],
];
yield 'nested curly open + close' => [
'<?php echo "{$foo->{"{$bar}"}}";',
[
4 => \T_CURLY_OPEN,
7 => CT::T_DYNAMIC_PROP_BRACE_OPEN,
9 => \T_CURLY_OPEN,
11 => CT::T_CURLY_CLOSE,
13 => CT::T_DYNAMIC_PROP_BRACE_CLOSE,
14 => CT::T_CURLY_CLOSE,
],
];
yield 'functions "set" and "get" (like property hooks, but not)' => [
<<<'PHP'
<?php if ($x) {
set();
} elseif ($y) {
SET();
} else {
get();
}
PHP,
[],
];
yield 'method "get" aliased in trait import' => [
<<<'PHP'
<?php class Foo
{
use Bar {
get as private otherGet;
}
}
PHP,
[],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess80Cases
*
* @requires PHP 8.0
*/
public function testProcess80(string $source, array $expectedTokens = []): void
{
$this->testProcess($source, $expectedTokens);
}
/**
* @return iterable<string, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcess80Cases(): iterable
{
yield 'dynamic nullable property brace open/close' => [
'<?php $foo?->{$bar};',
[
3 => CT::T_DYNAMIC_PROP_BRACE_OPEN,
5 => CT::T_DYNAMIC_PROP_BRACE_CLOSE,
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider providePre84ProcessCases
*
* @requires PHP <8.4
*/
public function testPre84Process(string $source, array $expectedTokens = []): void
{
$this->doTest(
$source,
$expectedTokens,
[
\T_CURLY_OPEN,
CT::T_CURLY_CLOSE,
\T_DOLLAR_OPEN_CURLY_BRACES,
CT::T_DOLLAR_CLOSE_CURLY_BRACES,
CT::T_DYNAMIC_PROP_BRACE_OPEN,
CT::T_DYNAMIC_PROP_BRACE_CLOSE,
CT::T_DYNAMIC_VAR_BRACE_OPEN,
CT::T_DYNAMIC_VAR_BRACE_CLOSE,
CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN,
CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
CT::T_GROUP_IMPORT_BRACE_OPEN,
CT::T_GROUP_IMPORT_BRACE_CLOSE,
CT::T_PROPERTY_HOOK_BRACE_OPEN,
CT::T_PROPERTY_HOOK_BRACE_CLOSE,
],
);
}
/**
* @return iterable<string, array{string, array<int, int>}>
*/
public static function providePre84ProcessCases(): iterable
{
yield 'array index curly brace open/close' => [
'<?php
echo $arr{$index};
echo $arr[$index];
if (1) {}
',
[
5 => CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN,
7 => CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
],
];
yield 'array index curly brace open/close, after square index' => [
'<?php $b = [1]{0};
',
[
8 => CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN,
10 => CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
],
];
yield 'array index curly brace open/close, nested' => [
'<?php
echo $nestedArray{$index}{$index2}[$index3]{$index4};
',
[
5 => CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN,
7 => CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
8 => CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN,
10 => CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
14 => CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN,
16 => CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
],
];
yield 'array index curly brace open/close, repeated' => [
'<?php
echo $array{0}->foo;
echo $collection->items{1}->property;
',
[
5 => CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN,
7 => CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
17 => CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN,
19 => CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
],
];
yield 'array index curly brace open/close, minimal' => [
'<?php
echo [1]{0};
echo array(1){0};
',
[
7 => CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN,
9 => CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
18 => CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN,
20 => CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideStarting84ProcessCases
*
* @requires PHP 8.4
*/
public function testStarting84Process(string $source, array $expectedTokens = []): void
{
$this->doTest(
$source,
$expectedTokens,
[
\T_CURLY_OPEN,
CT::T_CURLY_CLOSE,
\T_DOLLAR_OPEN_CURLY_BRACES,
CT::T_DOLLAR_CLOSE_CURLY_BRACES,
CT::T_DYNAMIC_PROP_BRACE_OPEN,
CT::T_DYNAMIC_PROP_BRACE_CLOSE,
CT::T_DYNAMIC_VAR_BRACE_OPEN,
CT::T_DYNAMIC_VAR_BRACE_CLOSE,
CT::T_ARRAY_INDEX_CURLY_BRACE_OPEN,
CT::T_ARRAY_INDEX_CURLY_BRACE_CLOSE,
CT::T_GROUP_IMPORT_BRACE_OPEN,
CT::T_GROUP_IMPORT_BRACE_CLOSE,
CT::T_PROPERTY_HOOK_BRACE_OPEN,
CT::T_PROPERTY_HOOK_BRACE_CLOSE,
],
);
}
/**
* @return iterable<string, array{string, array<int, int>}>
*/
public static function provideStarting84ProcessCases(): iterable
{
yield 'property hooks: property without default value' => [
<<<'PHP'
<?php
class PropertyHooks
{
public string $bar { // << this one
set(string $value) {
$this->bar = strtolower($value);
}
} // << this one
}
PHP,
[
13 => CT::T_PROPERTY_HOOK_BRACE_OPEN,
40 => CT::T_PROPERTY_HOOK_BRACE_CLOSE,
],
];
yield 'property hooks: property with default value (string)' => [
<<<'PHP'
<?php
class PropertyHooks
{
public string $bar = "example" { // << this one
set(string $value) {
$this->bar = strtolower($value);
}
} // << this one
}
PHP,
[
17 => CT::T_PROPERTY_HOOK_BRACE_OPEN,
44 => CT::T_PROPERTY_HOOK_BRACE_CLOSE,
],
];
yield 'property hooks: property with default value (array)' => [
<<<'PHP'
<?php
class PropertyHooks
{
public $bar = [1,2,3] { // << this one
set($value) {
$this->bar = $value;
}
} // << this one
}
PHP,
[
21 => CT::T_PROPERTY_HOOK_BRACE_OPEN,
43 => CT::T_PROPERTY_HOOK_BRACE_CLOSE,
],
];
yield 'property hooks: property with default value (namespaced)' => [
<<<'PHP'
<?php
class PropertyHooks
{
public $bar = DateTimeInterface::ISO8601 { // << this one
set($value) {
$this->bar = $value;
}
} // << this one
}
PHP,
[
17 => CT::T_PROPERTY_HOOK_BRACE_OPEN,
39 => CT::T_PROPERTY_HOOK_BRACE_CLOSE,
],
];
yield 'property hooks: property with setter attributes' => [
<<<'PHP'
<?php
class PropertyHooks
{
public string $bar { // << this one
#[A]
#[B]
set(string $value) {
$this->bar = strtolower($value);
}
} // << this one
}
PHP,
[
13 => CT::T_PROPERTY_HOOK_BRACE_OPEN,
48 => CT::T_PROPERTY_HOOK_BRACE_CLOSE,
],
];
yield 'property hooks: property with short setter' => [
<<<'PHP'
<?php
class PropertyHooks
{
public string $bar { // << this one
set {
$this->bar = strtolower($value);
}
} // << this one
}
PHP,
[
13 => CT::T_PROPERTY_HOOK_BRACE_OPEN,
35 => CT::T_PROPERTY_HOOK_BRACE_CLOSE,
],
];
yield 'property hooks: property with short getter' => [
<<<'PHP'
<?php
class PropertyHooks
{
public string $bar { // << this one
get => ucwords(mb_strtolower($this->bar));
} // << this one
}
PHP,
[
13 => CT::T_PROPERTY_HOOK_BRACE_OPEN,
32 => CT::T_PROPERTY_HOOK_BRACE_CLOSE,
],
];
yield 'property hooks: some more curly braces within hook' => [
<<<'PHP'
<?php
class PropertyHooks
{
public $callable { // << this one
set($value) {
if (is_callable($value)) {
$this->callable = $value;
} else {
$this->callable = static function (): void {
$foo = new class implements \Stringable {
public function __toString(): string {
echo 'Na';
}
};
for ($i = 0; $i < 8; $i++) {
echo (string) $foo;
}
};
}
}
} // << this one
}
PHP,
[
11 => CT::T_PROPERTY_HOOK_BRACE_OPEN,
143 => CT::T_PROPERTY_HOOK_BRACE_CLOSE,
],
];
yield 'property hooks: casing' => [
<<<'PHP'
<?php
class PropertyHooks
{
public string $bar { // << this one
Get => strtolower($this->bar);
SET => strtoupper($value);
} // << this one
}
PHP,
[
13 => CT::T_PROPERTY_HOOK_BRACE_OPEN,
39 => CT::T_PROPERTY_HOOK_BRACE_CLOSE,
],
];
}
/**
* @dataProvider provideNotDynamicClassConstantFetchCases
*/
public function testNotDynamicClassConstantFetch(string $source): void
{
Tokens::clearCache();
$tokens = Tokens::fromCode($source);
self::assertFalse(
$tokens->isAnyTokenKindsFound(
[
CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
],
),
);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideNotDynamicClassConstantFetchCases(): iterable
{
yield 'negatives' => [
'<?php
namespace B {$b = Z::B;};
echo $c::{$static_method}();
echo Foo::{$static_method}();
echo Foo::${static_property};
echo Foo::${$static_property};
echo Foo::class;
echo $foo::$bar;
echo $foo::bar();
echo foo()::A();
{$z = A::C;}
',
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideDynamicClassConstantFetchCases
*
* @requires PHP 8.3
*/
public function testDynamicClassConstantFetch(array $expectedTokens, string $source): void
{
$this->doTest(
$source,
$expectedTokens,
[
CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
],
);
}
/**
* @return iterable<string, array{_TransformerTestExpectedKindsUnderIndex, string}>
*/
public static function provideDynamicClassConstantFetchCases(): iterable
{
yield 'simple' => [
[
5 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
7 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
],
'<?php echo Foo::{$bar};',
];
yield 'long way of writing `Bar::class`' => [
[
5 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
7 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
],
"<?php echo Bar::{'class'};",
];
yield 'variable variable wrapped, close tag' => [
[
5 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
10 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
],
'<?php echo Foo::{${$var}}?>',
];
yield 'variable variable, comment' => [
[
5 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
8 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
],
'<?php echo Foo::{$$var}/* */;?>',
];
yield 'static, self' => [
[
37 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
39 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
46 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
48 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
55 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
57 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
],
'<?php
class Foo
{
private const X = 1;
public function Bar($var): void
{
echo self::{$var};
echo static::{$var};
echo static::{"X"};
}
}
',
];
yield 'chained' => [
[
5 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
7 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
9 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
11 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
],
"<?php echo Foo::{'BAR'}::{'BLA'}::{static_method}(1,2) ?>",
];
yield 'mixed chain' => [
[
21 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
23 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
25 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
27 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
],
'<?php echo Foo::{\'static_method\'}()::{$$a}()["const"]::{some_const}::{$other_const}::{$last_static_method}();',
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideDynamicClassConstantFetchPhp83Cases
*
* @requires PHP ~8.3.0
*/
public function testDynamicClassConstantFetchPhp83(array $expectedTokens, string $source): void
{
$this->doTest(
$source,
$expectedTokens,
[
CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
],
);
}
/**
* @return iterable<string, array{array<int, int>, string}>
*/
public static function provideDynamicClassConstantFetchPhp83Cases(): iterable
{
yield 'static method var, string' => [
[
10 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
12 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
],
"<?php echo Foo::{\$static_method}(){'XYZ'};",
];
yield 'mixed chain' => [
[
17 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
19 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
21 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
23 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
25 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_OPEN,
27 => CT::T_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE_CLOSE,
],
'<?php echo Foo::{\'static_method\'}()::{$$a}(){"const"}::{some_const}::{$other_const}::{$last_static_method}();',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Tokenizer/Transformer/ArrayTypehintTransformerTest.php | tests/Tokenizer/Transformer/ArrayTypehintTransformerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tokenizer\CT;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Transformer\ArrayTypehintTransformer
*
* @phpstan-import-type _TransformerTestExpectedKindsUnderIndex from AbstractTransformerTestCase
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ArrayTypehintTransformerTest extends AbstractTransformerTestCase
{
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcessCases
*/
public function testProcess(string $source, array $expectedTokens = []): void
{
$this->doTest(
$source,
$expectedTokens,
[
\T_ARRAY,
CT::T_ARRAY_TYPEHINT,
],
);
}
/**
* @return iterable<int, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcessCases(): iterable
{
yield [
'<?php
$a = array(1, 2, 3);
function foo (array /** @type array */ $bar)
{
}',
[
5 => \T_ARRAY,
22 => CT::T_ARRAY_TYPEHINT,
],
];
yield [
'<?php
$a = array(1, 2, 3);
$fn = fn(array /** @type array */ $bar) => null;',
[
5 => \T_ARRAY,
23 => CT::T_ARRAY_TYPEHINT,
],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Tokenizer/Transformer/AttributeTransformerTest.php | tests/Tokenizer/Transformer/AttributeTransformerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Tokens;
/**
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Transformer\AttributeTransformer
*
* @phpstan-import-type _TransformerTestExpectedKindsUnderIndex from AbstractTransformerTestCase
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class AttributeTransformerTest extends AbstractTransformerTestCase
{
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcessCases
*
* @requires PHP 8.0
*/
public function testProcess(string $source, array $expectedTokens): void
{
$this->doTest($source, $expectedTokens);
}
/**
* @return iterable<int, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcessCases(): iterable
{
yield ['<?php class Foo {
#[Listens(ProductCreatedEvent::class)]
public $foo;
}
',
[
14 => CT::T_ATTRIBUTE_CLOSE,
],
];
yield ['<?php class Foo {
#[Required]
public $bar;
}',
[
9 => CT::T_ATTRIBUTE_CLOSE,
],
];
yield [
'<?php function foo(
#[MyAttr([1, 2])] Type $myParam,
) {}',
[
16 => CT::T_ATTRIBUTE_CLOSE,
],
];
yield [
'<?php class Foo {
#[ORM\Column("string", ORM\Column::UNIQUE)]
#[Assert\Email(["message" => "The email {{ value }} is not a valid email."])]
private $email;
}',
[
21 => CT::T_ATTRIBUTE_CLOSE,
36 => CT::T_ATTRIBUTE_CLOSE,
],
];
yield [
'<?php
#[ORM\Id]
#[ConditionalDeclare(PHP_VERSION_ID < 70000+1**2-1>>9+foo(a)+foo((bool)$b))] // gets removed from AST when >= 7.0
#[IgnoreRedeclaration] // throws no error when already declared, removes the redeclared thing
function intdiv(int $numerator, int $divisor) {
}
#[
Attr1("foo"),Attr2("bar"),
]
#[PhpAttribute(self::IS_REPEATABLE)]
class Route
{
}
',
[
5 => CT::T_ATTRIBUTE_CLOSE,
35 => CT::T_ATTRIBUTE_CLOSE,
41 => CT::T_ATTRIBUTE_CLOSE,
76 => CT::T_ATTRIBUTE_CLOSE,
85 => CT::T_ATTRIBUTE_CLOSE,
],
];
yield [
'<?php
#[Jit]
function foo() {}
class Foo
{
#[ExampleAttribute]
public const FOO = "foo";
#[ExampleAttribute]
public function foo(#[ExampleAttribute] Type $bar) {}
}
$object = new #[ExampleAttribute] class () {};
$f1 = #[ExampleAttribute] function () {};
$f2 = #[ExampleAttribute] fn() => 1;
',
[
3 => CT::T_ATTRIBUTE_CLOSE,
22 => CT::T_ATTRIBUTE_CLOSE,
37 => CT::T_ATTRIBUTE_CLOSE,
47 => CT::T_ATTRIBUTE_CLOSE,
67 => CT::T_ATTRIBUTE_CLOSE,
84 => CT::T_ATTRIBUTE_CLOSE,
101 => CT::T_ATTRIBUTE_CLOSE,
],
];
yield [
'<?php
#[
ORM\Entity,
ORM\Table("user")
]
class User
{
#[ORM\Id, ORM\Column("integer"), ORM\GeneratedValue]
private $id;
#[ORM\Column("string", ORM\Column::UNIQUE)]
#[Assert\Email(["message" => "The email \'{{ value }}\' is not a valid email."])]
private $email;
#[\Doctrine\ORM\ManyToMany(
targetEntity: User::class,
joinColumn: "group_id",
inverseJoinColumn: "user_id",
cascade: array("persist", "remove")
)]
#[Assert\Valid]
#[JMSSerializer\XmlList(inline: true, entry: "user")]
public $users;
}
',
[
15 => CT::T_ATTRIBUTE_CLOSE,
40 => CT::T_ATTRIBUTE_CLOSE,
61 => CT::T_ATTRIBUTE_CLOSE,
76 => CT::T_ATTRIBUTE_CLOSE,
124 => CT::T_ATTRIBUTE_CLOSE,
130 => CT::T_ATTRIBUTE_CLOSE,
148 => CT::T_ATTRIBUTE_CLOSE,
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess85Cases
*
* @requires PHP 8.5
*/
public function testProcess85(string $source, array $expectedTokens): void
{
$this->doTest($source, $expectedTokens);
}
/**
* @return iterable<int, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcess85Cases(): iterable
{
yield [
<<<'PHP'
<?php
#[Foo([static function (#[SensitiveParameter] $a) {
return [fn (#[Bar([1, 2])] $b) => [$b[1]]];
}])]
class Baz {}
PHP,
[
12 => CT::T_ATTRIBUTE_CLOSE,
35 => CT::T_ATTRIBUTE_CLOSE,
54 => CT::T_ATTRIBUTE_CLOSE,
],
];
}
/**
* @dataProvider provideNotChangeCases
*/
public function testNotChange(string $source): void
{
Tokens::clearCache();
foreach (Tokens::fromCode($source) as $token) {
self::assertFalse($token->isGivenKind([
CT::T_ATTRIBUTE_CLOSE,
]));
}
}
/**
* @return iterable<int, array{string}>
*/
public static function provideNotChangeCases(): iterable
{
yield [
'<?php
$foo = [];
$a[] = $b[1];
$c = $d[2];
// [$e] = $f;',
];
yield [
'<?php [$e] = $f;',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Tokenizer/Transformer/ClassConstantTransformerTest.php | tests/Tokenizer/Transformer/ClassConstantTransformerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tokenizer\CT;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Transformer\ClassConstantTransformer
*
* @phpstan-import-type _TransformerTestExpectedKindsUnderIndex from AbstractTransformerTestCase
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ClassConstantTransformerTest extends AbstractTransformerTestCase
{
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcessCases
*/
public function testProcess(string $source, array $expectedTokens = []): void
{
$this->doTest(
$source,
$expectedTokens,
[
CT::T_CLASS_CONSTANT,
],
);
}
/**
* @return iterable<int, array{0: string, 1?: _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcessCases(): iterable
{
yield [
'<?php echo X::class;',
[
5 => CT::T_CLASS_CONSTANT,
],
];
yield [
'<?php echo X::cLaSS;',
[
5 => CT::T_CLASS_CONSTANT,
],
];
yield [
'<?php echo X::bar;',
];
yield [
'<?php class X{}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Tokenizer/Transformer/TypeColonTransformerTest.php | tests/Tokenizer/Transformer/TypeColonTransformerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tokenizer\CT;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Transformer\TypeColonTransformer
*
* @phpstan-import-type _TransformerTestExpectedKindsUnderIndex from AbstractTransformerTestCase
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class TypeColonTransformerTest extends AbstractTransformerTestCase
{
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcessCases
*/
public function testProcess(string $source, array $expectedTokens = []): void
{
$this->doTest(
$source,
$expectedTokens,
[
CT::T_TYPE_COLON,
],
);
}
/**
* @return iterable<int, array{0: string, 1?: _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcessCases(): iterable
{
yield [
'<?php function foo(): array { return []; }',
[
6 => CT::T_TYPE_COLON,
],
];
yield [
'<?php function & foo(): array { return []; }',
[
8 => CT::T_TYPE_COLON,
],
];
yield [
'<?php interface F { public function foo(): array; }',
[
14 => CT::T_TYPE_COLON,
],
];
yield [
'<?php $a=1; $f = function () : array {};',
[
15 => CT::T_TYPE_COLON,
],
];
yield [
'<?php $a=1; $f = function () use($a) : array {};',
[
20 => CT::T_TYPE_COLON,
],
];
yield [
'<?php
$a = 1 ? [] : [];
$b = 1 ? fnc() : [];
$c = 1 ?: [];
',
];
yield [
'<?php fn(): array => [];',
[
4 => CT::T_TYPE_COLON,
],
];
yield [
'<?php fn & (): array => [];',
[
7 => CT::T_TYPE_COLON,
],
];
yield [
'<?php $a=1; $f = fn () : array => [];',
[
15 => CT::T_TYPE_COLON,
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess81Cases
*
* @requires PHP 8.1
*/
public function testProcess81(string $source, array $expectedTokens = []): void
{
$this->testProcess($source, $expectedTokens);
}
/**
* @return iterable<int, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcess81Cases(): iterable
{
yield [
'<?php enum Foo: int {}',
[
4 => CT::T_TYPE_COLON,
],
];
yield [
'<?php enum Foo /** */ : int {}',
[
7 => CT::T_TYPE_COLON,
],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Tokenizer/Transformer/TypeIntersectionTransformerTest.php | tests/Tokenizer/Transformer/TypeIntersectionTransformerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\FCT;
/**
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Transformer\TypeIntersectionTransformer
*
* @phpstan-import-type _TransformerTestExpectedKindsUnderIndex from AbstractTransformerTestCase
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class TypeIntersectionTransformerTest extends AbstractTransformerTestCase
{
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcessCases
*
* @requires PHP 8.1
*/
public function testProcess(string $source, array $expectedTokens = []): void
{
$this->doTest(
$source,
$expectedTokens,
[
CT::T_TYPE_INTERSECTION,
],
);
}
/**
* @return iterable<array{0: string, 1?: _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcessCases(): iterable
{
yield 'do not fix cases' => [
'<?php
echo 2 & 4;
echo "aaa" & "bbb";
echo F_OK & F_ERR;
echo foo(F_OK & F_ERR);
foo($A&&$b);
foo($A&$b);
// try {} catch (ExceptionType1 & ExceptionType2) {}
$a = function(){};
$x = ($y&$z);
function foo(){}
$a = $b&$c;
$a &+ $b;
const A1 = B&C;
const B1 = D::X & C;
',
];
yield 'arrow function' => [
'<?php $a = fn(int&null $item): int&null => $item * 2;',
[
8 => CT::T_TYPE_INTERSECTION,
16 => CT::T_TYPE_INTERSECTION,
],
];
yield 'static function' => [
'<?php
$a = static function (A&B&int $a):int&null {};
',
[
11 => CT::T_TYPE_INTERSECTION,
13 => CT::T_TYPE_INTERSECTION,
20 => CT::T_TYPE_INTERSECTION,
],
];
yield 'static function with use' => [
'<?php
$a = static function () use ($a) : int&null {};
',
[
21 => CT::T_TYPE_INTERSECTION,
],
];
yield 'function variable unions' => [
'<?php
function Bar1(A&B&int $a) {
}
',
[
6 => CT::T_TYPE_INTERSECTION,
8 => CT::T_TYPE_INTERSECTION,
],
];
yield 'class method variable unions' => [
'<?php
class Foo
{
public function Bar1(A&B&int $a) {}
public function Bar2(A\B&\A\Z $a) {}
public function Bar3(int $a) {}
}
',
[
// Bar1
14 => CT::T_TYPE_INTERSECTION,
16 => CT::T_TYPE_INTERSECTION,
// Bar2
34 => CT::T_TYPE_INTERSECTION,
],
];
yield 'class method return unions' => [
'<?php
class Foo
{
public function Bar(): A&B&int {}
}
',
[
17 => CT::T_TYPE_INTERSECTION,
19 => CT::T_TYPE_INTERSECTION,
],
];
yield 'class attribute var + union' => [
'<?php
class Number
{
var int&float&null $number;
}
',
[
10 => CT::T_TYPE_INTERSECTION,
12 => CT::T_TYPE_INTERSECTION,
],
];
yield 'class attributes visibility + unions' => [
'<?php
class Number
{
public array $numbers;
/** */
public int&float $number1;
// 2
protected int&float&null $number2;
# - foo 3
private int&float&string&null $number3;
/* foo 4 */
private \Foo\Bar\A&null $number4;
private \Foo&Bar $number5;
private ?Bar $number6; // ? cannot be part of a union in PHP8
}
',
[
// number 1
19 => CT::T_TYPE_INTERSECTION,
// number 2
30 => CT::T_TYPE_INTERSECTION,
32 => CT::T_TYPE_INTERSECTION,
// number 3
43 => CT::T_TYPE_INTERSECTION,
45 => CT::T_TYPE_INTERSECTION,
47 => CT::T_TYPE_INTERSECTION,
// number 4
63 => CT::T_TYPE_INTERSECTION,
// number 5
73 => CT::T_TYPE_INTERSECTION,
],
];
yield 'typed static properties' => [
'<?php
class Foo {
private static int & null $bar;
private static int & float & string & null $baz;
}',
[
14 => CT::T_TYPE_INTERSECTION,
27 => CT::T_TYPE_INTERSECTION,
31 => CT::T_TYPE_INTERSECTION,
35 => CT::T_TYPE_INTERSECTION,
],
];
yield 'array as first element of types' => [
'<?php function foo(array&bool&null $foo) {}',
[
6 => CT::T_TYPE_INTERSECTION,
8 => CT::T_TYPE_INTERSECTION,
],
];
yield 'array as middle element of types' => [
'<?php function foo(null&array&bool $foo) {}',
[
6 => CT::T_TYPE_INTERSECTION,
8 => CT::T_TYPE_INTERSECTION,
],
];
yield 'array as last element of types' => [
'<?php function foo(null&bool&array $foo) {}',
[
6 => CT::T_TYPE_INTERSECTION,
8 => CT::T_TYPE_INTERSECTION,
],
];
yield 'multiple function parameters' => [
'<?php function foo(A&B $x, C&D $y, E&F $z) {};',
[
6 => CT::T_TYPE_INTERSECTION,
13 => CT::T_TYPE_INTERSECTION,
20 => CT::T_TYPE_INTERSECTION,
],
];
yield 'function calls and function definitions' => [
'<?php
f1(CONST_A&CONST_B);
function f2(A&B $x, C&D $y, E&F $z) {};
f3(CONST_A&CONST_B);
function f4(A&B $x, C&D $y, E&F $z) {};
f5(CONST_A&CONST_B);
$x = ($y&$z);
',
[
15 => CT::T_TYPE_INTERSECTION,
22 => CT::T_TYPE_INTERSECTION,
29 => CT::T_TYPE_INTERSECTION,
52 => CT::T_TYPE_INTERSECTION,
59 => CT::T_TYPE_INTERSECTION,
66 => CT::T_TYPE_INTERSECTION,
],
];
yield 'static function with alternation' => [
'<?php
$a = static function (A&B&int $a):int|null {};
',
[
11 => CT::T_TYPE_INTERSECTION,
13 => CT::T_TYPE_INTERSECTION,
],
];
yield 'promoted properties' => [
'<?php class Foo {
public function __construct(
public readonly int&string $a,
protected readonly int&string $b,
private readonly int&string $c
) {}
}',
[
19 => CT::T_TYPE_INTERSECTION,
30 => CT::T_TYPE_INTERSECTION,
41 => CT::T_TYPE_INTERSECTION,
],
];
yield 'callable type' => [
'<?php
function f1(array&callable $x) {};
function f2(callable&array $x) {};
function f3(string&callable $x) {};
function f4(callable&string $x) {};
',
[
7 => CT::T_TYPE_INTERSECTION,
22 => CT::T_TYPE_INTERSECTION,
37 => CT::T_TYPE_INTERSECTION,
52 => CT::T_TYPE_INTERSECTION,
],
];
yield [
'<?php
use Psr\Log\LoggerInterface;
function f( #[Target(\'xxx\')] LoggerInterface&A $logger) {}
',
[
24 => CT::T_TYPE_INTERSECTION,
],
];
yield [
'<?php
use Psr\Log\LoggerInterface;
function f( #[Target(\'a\')] #[Target(\'b\')] #[Target(\'c\')] #[Target(\'d\')] LoggerInterface&X $logger) {}
',
[
45 => CT::T_TYPE_INTERSECTION,
],
];
yield 'parameters by reference' => [
'<?php
f(FOO|BAR|BAZ&$x);
function f1(FOO|BAR|BAZ&$x) {}
function f2(FOO&BAR&BAZ&$x) {} // Intersection found
f(FOO&BAR|BAZ&$x);
f(FOO|BAR&BAZ&$x);
fn(FOO&BAR&BAZ&$x) => 0; // Intersection found
fn(FOO|BAR|BAZ&$x) => 0;
f(FOO&BAR&BAZ&$x);
',
[
35 => CT::T_TYPE_INTERSECTION,
37 => CT::T_TYPE_INTERSECTION,
75 => CT::T_TYPE_INTERSECTION,
77 => CT::T_TYPE_INTERSECTION,
],
];
yield 'self as type' => [
'<?php class Foo {
function f1(bool&self&int $x): void {}
function f2(): self&\stdClass {}
}',
[
12 => CT::T_TYPE_INTERSECTION,
14 => CT::T_TYPE_INTERSECTION,
34 => CT::T_TYPE_INTERSECTION,
],
];
yield 'static as type' => [
'<?php class Foo {
function f1(): static&TypeA {}
function f2(): TypeA&static&TypeB {}
function f3(): TypeA&static {}
}',
[
15 => CT::T_TYPE_INTERSECTION,
29 => CT::T_TYPE_INTERSECTION,
31 => CT::T_TYPE_INTERSECTION,
45 => CT::T_TYPE_INTERSECTION,
],
];
yield 'splat operator' => [
'<?php class Foo {
function f1(bool&int ... $x) {}
function f2(bool&int $x, TypeA&\Bar\Baz&TypeB ...$y) {}
}',
[
12 => CT::T_TYPE_INTERSECTION,
28 => CT::T_TYPE_INTERSECTION,
35 => CT::T_TYPE_INTERSECTION,
40 => CT::T_TYPE_INTERSECTION,
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess81Cases
*
* @requires PHP 8.1
*/
public function testProcess81(string $source, array $expectedTokens): void
{
$this->doTest($source, $expectedTokens);
}
/**
* @return iterable<string, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcess81Cases(): iterable
{
yield 'ensure T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG is not modified' => [
'<?php $a = $b&$c;',
[
6 => FCT::T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG,
],
];
yield 'do not fix, close/open' => [
'<?php fn() => 0 ?><?php $a = FOO|BAR|BAZ&$x;',
[
20 => FCT::T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG,
],
];
yield 'do not fix, foreach' => [
'<?php while(foo()){} $a = FOO|BAR|BAZ&$x;',
[
19 => FCT::T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG,
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess82Cases
*
* @requires PHP 8.2
*/
public function testProcess82(string $source, array $expectedTokens): void
{
$this->doTest($source, $expectedTokens);
}
/**
* @return iterable<string, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcess82Cases(): iterable
{
yield 'disjunctive normal form types parameter' => [
'<?php function foo((A&B)|D $x): void {}',
[
7 => CT::T_TYPE_INTERSECTION,
],
];
yield 'disjunctive normal form types return' => [
'<?php function foo(): (A&B)|D {}',
[
10 => CT::T_TYPE_INTERSECTION,
],
];
yield 'disjunctive normal form types parameters' => [
'<?php function foo(
(A&B)|C|D $x,
A|(B&C)|D $y,
(A&B)|(C&D) $z,
): void {}',
[
8 => CT::T_TYPE_INTERSECTION,
23 => CT::T_TYPE_INTERSECTION,
34 => CT::T_TYPE_INTERSECTION,
40 => CT::T_TYPE_INTERSECTION,
],
];
yield 'lambda with lots of DNF parameters and some others' => [
'<?php
$a = function(
(X&Y)|C $a,
$b = array(1,2),
(\X&\Y)|C $c,
array $d = [1,2],
(\X&\Y)|C $e,
$x, $y, $z, P|(H&J) $uu,
) {};
function foo (array $a = array(66,88, $d = [99,44],array()), $e = [99,44],(C&V)|G|array $f = array()){};
return new static();
',
[
10 => CT::T_TYPE_INTERSECTION, // $a
34 => CT::T_TYPE_INTERSECTION, // $c
60 => CT::T_TYPE_INTERSECTION, // $e
83 => CT::T_TYPE_INTERSECTION, // $uu
142 => CT::T_TYPE_INTERSECTION, // $f
],
];
yield 'bigger set of multiple DNF properties' => [
'<?php
class Dnf
{
public A|(C&D) $a;
protected (C&D)|B $b;
private (C&D)|(E&F)|(G&H) $c;
static (C&D)|Z $d;
public /* */ (C&D)|X $e;
public function foo($a, $b) {
return
$z|($A&$B)|(A::z&B\A::x)
|| A::b|($A&$B)
;
}
}
',
[
13 => CT::T_TYPE_INTERSECTION,
24 => CT::T_TYPE_INTERSECTION,
37 => CT::T_TYPE_INTERSECTION,
43 => CT::T_TYPE_INTERSECTION,
49 => CT::T_TYPE_INTERSECTION,
60 => CT::T_TYPE_INTERSECTION,
75 => CT::T_TYPE_INTERSECTION,
],
];
yield 'arrow function with DNF types' => [
'<?php
$f1 = fn (): A|(B&C) => new Foo();
$f2 = fn ((A&B)|C $x, A|(B&C) $y): (A&B&C)|D|(E&F) => new Bar();
',
[
16 => CT::T_TYPE_INTERSECTION,
38 => CT::T_TYPE_INTERSECTION,
51 => CT::T_TYPE_INTERSECTION,
61 => CT::T_TYPE_INTERSECTION,
63 => CT::T_TYPE_INTERSECTION,
71 => CT::T_TYPE_INTERSECTION,
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess83Cases
*
* @requires PHP 8.3
*/
public function testProcess83(string $source, array $expectedTokens): void
{
$this->doTest($source, $expectedTokens);
}
/**
* @return iterable<string, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcess83Cases(): iterable
{
yield 'typed const DNF types 1' => [
'<?php class Foo { const (A&B)|Z Bar = 1;}',
[
11 => CT::T_TYPE_INTERSECTION,
],
];
yield 'typed const DNF types 2' => [
'<?php class Foo { const Z|(A&B) Bar = 1;}',
[
13 => CT::T_TYPE_INTERSECTION,
],
];
yield 'typed const DNF types 3' => [
'<?php class Foo { const Z|(A&B)|X Bar = 1;}',
[
13 => CT::T_TYPE_INTERSECTION,
],
];
yield 'typed const' => [
'<?php class Foo { const A&B Bar = 1;}',
[
10 => CT::T_TYPE_INTERSECTION,
],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Tokenizer/Transformer/TypeAlternationTransformerTest.php | tests/Tokenizer/Transformer/TypeAlternationTransformerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tokenizer\CT;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @covers \PhpCsFixer\Tokenizer\AbstractTypeTransformer
* @covers \PhpCsFixer\Tokenizer\Transformer\TypeAlternationTransformer
*
* @phpstan-import-type _TransformerTestExpectedKindsUnderIndex from AbstractTransformerTestCase
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class TypeAlternationTransformerTest extends AbstractTransformerTestCase
{
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcessCases
*/
public function testProcess(string $source, array $expectedTokens): void
{
$this->doTest(
$source,
$expectedTokens,
[
CT::T_TYPE_ALTERNATION,
],
);
}
/**
* @return iterable<string, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcessCases(): iterable
{
yield 'no namespace' => [
'<?php try {} catch (ExceptionType1 | ExceptionType2 | ExceptionType3 $e) {}',
[
11 => CT::T_TYPE_ALTERNATION,
15 => CT::T_TYPE_ALTERNATION,
],
];
yield 'comments & spacing' => [
"<?php try {/* 1 */} catch (/* 2 */ExceptionType1/* 3 */\t\n| \n\t/* 4 */\n\tExceptionType2 \$e) {}",
[
14 => CT::T_TYPE_ALTERNATION,
],
];
yield 'native namespace only' => [
'<?php try {} catch (\ExceptionType1 | \ExceptionType2 | \ExceptionType3 $e) {}',
[
12 => CT::T_TYPE_ALTERNATION,
17 => CT::T_TYPE_ALTERNATION,
],
];
yield 'namespaces' => [
'<?php try {} catch (A\ExceptionType1 | \A\ExceptionType2 | \A\B\C\ExceptionType3 $e) {}',
[
13 => CT::T_TYPE_ALTERNATION,
20 => CT::T_TYPE_ALTERNATION,
],
];
yield 'do not fix cases' => [
'<?php
echo 2 | 4;
echo "aaa" | "bbb";
echo F_OK | F_ERR;
echo foo(F_OK | F_ERR);
foo($A||$b);
foo($A|$b);
// try {} catch (ExceptionType1 | ExceptionType2) {}
$a = function(){};
$x = ($y|$z);
function foo(){}
$a = $b|$c;
const A = B|C;
const B = D::X|C ?>
',
[
6 => '|',
15 => '|',
24 => '|',
35 => '|',
52 => '|',
76 => '|',
94 => '|',
105 => '|',
118 => '|',
],
];
yield 'do not fix, close/open' => [
'<?php fn() => 0 ?><?php $a = FOO|BAR|BAZ&$x;',
[
16 => '|',
18 => '|',
],
];
yield 'do not fix, foreach' => [
'<?php while(foo()){} $a = FOO|BAR|BAZ&$x;',
[
15 => '|',
17 => '|',
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess80Cases
*
* @requires PHP 8.0
*/
public function testProcess80(string $source, array $expectedTokens): void
{
$this->doTest($source, $expectedTokens);
}
/**
* @return iterable<array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcess80Cases(): iterable
{
yield 'arrow function' => [
'<?php $a = fn(int|null $item): int|null => $item * 2;',
[
8 => CT::T_TYPE_ALTERNATION,
16 => CT::T_TYPE_ALTERNATION,
],
];
yield 'static function' => ['<?php
$a = static function (A|B|int $a):int|null {};
',
[
11 => CT::T_TYPE_ALTERNATION,
13 => CT::T_TYPE_ALTERNATION,
20 => CT::T_TYPE_ALTERNATION,
],
];
yield 'static function with use' => ['<?php
$a = static function () use ($a) : int|null {};
',
[
21 => CT::T_TYPE_ALTERNATION,
],
];
yield 'function variable unions' => ['<?php
function Bar1(A|B|int $a) {
}
',
[
6 => CT::T_TYPE_ALTERNATION,
8 => CT::T_TYPE_ALTERNATION,
],
];
yield 'class method variable unions' => ['<?php
class Foo
{
public function Bar1(A|B|int $a) {}
public function Bar2(A\B|\A\Z $a) {}
public function Bar3(int $a) {}
}
',
[
// Bar1
14 => CT::T_TYPE_ALTERNATION,
16 => CT::T_TYPE_ALTERNATION,
// Bar2
34 => CT::T_TYPE_ALTERNATION,
],
];
yield 'class method return unions' => ['<?php
class Foo
{
public function Bar(): A|B|int {}
}
',
[
17 => CT::T_TYPE_ALTERNATION,
19 => CT::T_TYPE_ALTERNATION,
],
];
yield 'class attribute var + union' => ['<?php
class Number
{
var int|float|null $number;
}
',
[
10 => CT::T_TYPE_ALTERNATION,
12 => CT::T_TYPE_ALTERNATION,
],
];
yield 'class attributes visibility + unions' => ['<?php
class Number
{
public array $numbers;
/** */
public int|float $number1;
// 2
protected int|float|null $number2;
# - foo 3
private int|float|string|null $number3;
/* foo 4 */
private \Foo\Bar\A|null $number4;
private \Foo|Bar $number5;
private ?Bar $number6; // ? cannot be part of a union in PHP8
}
',
[
// number 1
19 => CT::T_TYPE_ALTERNATION,
// number 2
30 => CT::T_TYPE_ALTERNATION,
32 => CT::T_TYPE_ALTERNATION,
// number 3
43 => CT::T_TYPE_ALTERNATION,
45 => CT::T_TYPE_ALTERNATION,
47 => CT::T_TYPE_ALTERNATION,
// number 4
63 => CT::T_TYPE_ALTERNATION,
// number 5
73 => CT::T_TYPE_ALTERNATION,
],
];
yield 'typed static properties' => [
'<?php
class Foo {
private static int | null $bar;
private static int | float | string | null $baz;
}',
[
14 => CT::T_TYPE_ALTERNATION,
27 => CT::T_TYPE_ALTERNATION,
31 => CT::T_TYPE_ALTERNATION,
35 => CT::T_TYPE_ALTERNATION,
],
];
yield 'array as first element of types' => [
'<?php function foo(array|bool|null $foo) {}',
[
6 => CT::T_TYPE_ALTERNATION,
8 => CT::T_TYPE_ALTERNATION,
],
];
yield 'array as middle element of types' => [
'<?php function foo(null|array|bool $foo) {}',
[
6 => CT::T_TYPE_ALTERNATION,
8 => CT::T_TYPE_ALTERNATION,
],
];
yield 'array as last element of types' => [
'<?php function foo(null|bool|array $foo) {}',
[
6 => CT::T_TYPE_ALTERNATION,
8 => CT::T_TYPE_ALTERNATION,
],
];
yield 'multiple function parameters' => [
'<?php function foo(A|B $x, C|D $y, E|F $z) {};',
[
6 => CT::T_TYPE_ALTERNATION,
13 => CT::T_TYPE_ALTERNATION,
20 => CT::T_TYPE_ALTERNATION,
],
];
yield 'function calls and function definitions' => [
'<?php
f1(CONST_A|CONST_B);
function f2(A|B $x, C|D $y, E|F $z) {};
f3(CONST_A|CONST_B);
function f4(A|B $x, C|D $y, E|F $z) {};
f5(CONST_A|CONST_B);
$x = ($y|$z);
',
[
15 => CT::T_TYPE_ALTERNATION,
22 => CT::T_TYPE_ALTERNATION,
29 => CT::T_TYPE_ALTERNATION,
52 => CT::T_TYPE_ALTERNATION,
59 => CT::T_TYPE_ALTERNATION,
66 => CT::T_TYPE_ALTERNATION,
],
];
yield 'callable type' => [
'<?php
function f1(array|callable $x) {};
function f2(callable|array $x) {};
function f3(string|callable $x) {};
function f4(callable|string $x) {};
',
[
7 => CT::T_TYPE_ALTERNATION,
22 => CT::T_TYPE_ALTERNATION,
37 => CT::T_TYPE_ALTERNATION,
52 => CT::T_TYPE_ALTERNATION,
],
];
yield 'promoted properties' => [
'<?php class Foo {
public function __construct(
public int|string $a,
protected int|string $b,
private int|string $c
) {}
}',
[
17 => CT::T_TYPE_ALTERNATION,
26 => CT::T_TYPE_ALTERNATION,
35 => CT::T_TYPE_ALTERNATION,
],
];
yield [
'<?php
use Psr\Log\LoggerInterface;
function f( #[Target(\'xxx\')] LoggerInterface|A $logger) {}
',
[
24 => CT::T_TYPE_ALTERNATION,
],
];
yield [
'<?php
use Psr\Log\LoggerInterface;
function f( #[Target(\'a\')] #[Target(\'b\')] #[Target(\'c\')] #[Target(\'d\')] LoggerInterface|X $logger) {}
',
[
45 => CT::T_TYPE_ALTERNATION,
],
];
yield 'self as type' => [
'<?php class Foo {
function f1(bool|self|int $x): void {}
function f2(): self|\stdClass {}
}',
[
12 => CT::T_TYPE_ALTERNATION,
14 => CT::T_TYPE_ALTERNATION,
34 => CT::T_TYPE_ALTERNATION,
],
];
yield 'static as type' => [
'<?php class Foo {
function f1(): static|TypeA {}
function f2(): TypeA|static|TypeB {}
function f3(): TypeA|static {}
}',
[
15 => CT::T_TYPE_ALTERNATION,
29 => CT::T_TYPE_ALTERNATION,
31 => CT::T_TYPE_ALTERNATION,
45 => CT::T_TYPE_ALTERNATION,
],
];
yield 'splat operator' => [
'<?php class Foo {
function f1(bool|int ... $x) {}
function f2(bool|int $x, TypeA|\Bar\Baz|TypeB ...$y) {}
}',
[
12 => CT::T_TYPE_ALTERNATION,
28 => CT::T_TYPE_ALTERNATION,
35 => CT::T_TYPE_ALTERNATION,
40 => CT::T_TYPE_ALTERNATION,
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(array $expectedTokens, string $source): void
{
$this->doTest(
$source,
$expectedTokens,
[
CT::T_TYPE_ALTERNATION,
],
);
}
/**
* @return iterable<string, array{_TransformerTestExpectedKindsUnderIndex, string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'readonly' => [
[
12 => CT::T_TYPE_ALTERNATION,
],
'<?php
class Foo
{
public readonly string|int $c;
}',
];
yield 'promoted properties' => [
[
19 => CT::T_TYPE_ALTERNATION,
30 => CT::T_TYPE_ALTERNATION,
41 => CT::T_TYPE_ALTERNATION,
],
'<?php class Foo {
public function __construct(
public readonly int|string $a,
protected readonly int|string $b,
private readonly int|string $c
) {}
}',
];
yield 'parameters by reference' => [
[
19 => CT::T_TYPE_ALTERNATION,
21 => CT::T_TYPE_ALTERNATION,
91 => CT::T_TYPE_ALTERNATION,
93 => CT::T_TYPE_ALTERNATION,
],
'<?php
f(FOO|BAR|BAZ&$x);
function f1(FOO|BAR|BAZ&$x) {} // Alternation found
function f2(FOO&BAR&BAZ&$x) {}
f(FOO&BAR|BAZ&$x);
f(FOO|BAR&BAZ&$x);
fn(FOO&BAR&BAZ&$x) => 0;
fn(FOO|BAR|BAZ&$x) => 0; // Alternation found
f(FOO&BAR&BAZ&$x);
',
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess81Cases
*
* @requires PHP 8.1
*/
public function testProcess81(string $source, array $expectedTokens): void
{
$this->doTest($source, $expectedTokens);
}
/**
* @return iterable<string, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcess81Cases(): iterable
{
yield 'arrow function with intersection' => [
'<?php $a = fn(int|null $item): int&null => $item * 2;',
[
8 => CT::T_TYPE_ALTERNATION,
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess82Cases
*
* @requires PHP 8.2
*/
public function testProcess82(string $source, array $expectedTokens): void
{
$this->doTest($source, $expectedTokens);
}
/**
* @return iterable<string, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcess82Cases(): iterable
{
yield 'disjunctive normal form types parameter' => [
'<?php function foo((A&B)|D $x): void {}',
[
10 => CT::T_TYPE_ALTERNATION,
],
];
yield 'disjunctive normal form types return' => [
'<?php function foo(): (A&B)|D {}',
[
13 => CT::T_TYPE_ALTERNATION,
],
];
yield 'disjunctive normal form types parameters' => [
'<?php function foo(
(A&B)|C|D $x,
A|(B&C)|D $y,
A|B|(C&D) $z,
): void {}',
[
11 => CT::T_TYPE_ALTERNATION,
13 => CT::T_TYPE_ALTERNATION,
20 => CT::T_TYPE_ALTERNATION,
26 => CT::T_TYPE_ALTERNATION,
33 => CT::T_TYPE_ALTERNATION,
35 => CT::T_TYPE_ALTERNATION,
],
];
yield 'bigger set of multiple DNF properties' => [
'<?php
class Dnf
{
public A|(C&D) $a;
protected (C&D)|B $b;
private (C&D)|(E&F)|(G&H) $c;
static (C&D)|Z $d;
public /* */ (C&D)|X $e;
public function foo($a, $b) {
return
$z|($A&$B)|(A::z&B\A::x)
|| A::b|($A&$B)
;
}
}
',
[
10 => CT::T_TYPE_ALTERNATION,
27 => CT::T_TYPE_ALTERNATION,
40 => CT::T_TYPE_ALTERNATION,
46 => CT::T_TYPE_ALTERNATION,
63 => CT::T_TYPE_ALTERNATION,
78 => CT::T_TYPE_ALTERNATION,
],
];
yield 'arrow function with DNF types' => [
'<?php
$f1 = fn (): A|(B&C) => new Foo();
$f2 = fn ((A&B)|C $x, A|(B&C) $y): (A&B&C)|D|(E&F) => new Bar();
',
[
13 => CT::T_TYPE_ALTERNATION,
41 => CT::T_TYPE_ALTERNATION,
48 => CT::T_TYPE_ALTERNATION,
66 => CT::T_TYPE_ALTERNATION,
68 => CT::T_TYPE_ALTERNATION,
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcess83Cases
*
* @requires PHP 8.3
*/
public function testProcess83(string $source, array $expectedTokens): void
{
$this->doTest($source, $expectedTokens);
}
/**
* @return iterable<string, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcess83Cases(): iterable
{
yield 'typed const alternate types' => [
'<?php class Foo { const A|B Bar = 1;}',
[
10 => CT::T_TYPE_ALTERNATION,
],
];
yield 'typed const mixed types' => [
'<?php
class Foo {
const A|\B|array/**/|(Z&V)|callable | X /* X*/ |D Bar = 1;
}
',
[
11 => CT::T_TYPE_ALTERNATION,
14 => CT::T_TYPE_ALTERNATION,
17 => CT::T_TYPE_ALTERNATION,
23 => CT::T_TYPE_ALTERNATION,
26 => CT::T_TYPE_ALTERNATION,
32 => CT::T_TYPE_ALTERNATION,
],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Tokenizer/Transformer/UseTransformerTest.php | tests/Tokenizer/Transformer/UseTransformerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tokenizer\CT;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Transformer\UseTransformer
*
* @phpstan-import-type _TransformerTestExpectedKindsUnderIndex from AbstractTransformerTestCase
* @phpstan-import-type _TransformerTestObservedKinds from AbstractTransformerTestCase
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class UseTransformerTest extends AbstractTransformerTestCase
{
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcessCases
*/
public function testProcess(string $source, array $expectedTokens = []): void
{
$this->doTest(
$source,
$expectedTokens,
[
\T_USE,
CT::T_USE_LAMBDA,
CT::T_USE_TRAIT,
],
);
}
/**
* @return iterable<array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcessCases(): iterable
{
yield [
'<?php use Foo;',
[
1 => \T_USE,
],
];
yield [
'<?php $foo = function() use ($bar) {};',
[
9 => CT::T_USE_LAMBDA,
],
];
yield [
'<?php class Foo { use Bar; }',
[
7 => CT::T_USE_TRAIT,
],
];
yield [
'<?php namespace Aaa; use Bbb; class Foo { use Bar; function baz() { $a=1; return function () use ($a) {}; } }',
[
6 => \T_USE,
17 => CT::T_USE_TRAIT,
42 => CT::T_USE_LAMBDA,
],
];
yield [
'<?php
namespace A {
class Foo {}
echo Foo::class;
}
namespace B {
use \stdClass;
echo 123;
}',
[
30 => \T_USE,
],
];
yield [
'<?php use Foo; $a = Bar::class;',
[
1 => \T_USE,
],
];
yield 'nested anonymous classes' => [
'<?php
namespace SomeWhereOverTheRainbow;
trait Foo {
public function test()
{
$a = time();
return function() use ($a) { echo $a; };
}
};
$a = new class(
new class() {
use Foo;
}
) {
public function __construct($bar)
{
$a = $bar->test();
$a();
}
};
',
[
38 => CT::T_USE_LAMBDA,
76 => CT::T_USE_TRAIT,
],
];
yield [
'<?php
use A\{B,};
use function D;
use C\{D,E,};
',
[
1 => \T_USE,
11 => \T_USE,
18 => \T_USE,
],
];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @requires PHP 8.1
*
* @dataProvider provideProcessPhp81Cases
*/
public function testProcessPhp81(string $source, array $expectedTokens = []): void
{
$this->doTest($source, $expectedTokens, [CT::T_USE_TRAIT]);
}
/**
* @return iterable<int, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcessPhp81Cases(): iterable
{
yield [
'<?php enum Foo: string
{
use Bar;
case Test1 = "a";
}
',
[
10 => CT::T_USE_TRAIT,
],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Tokenizer/Transformer/NamespaceOperatorTransformerTest.php | tests/Tokenizer/Transformer/NamespaceOperatorTransformerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tokenizer\CT;
/**
* @author Gregor Harlan <gharlan@web.de>
*
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Transformer\NamespaceOperatorTransformer
*
* @phpstan-import-type _TransformerTestExpectedKindsUnderIndex from AbstractTransformerTestCase
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NamespaceOperatorTransformerTest extends AbstractTransformerTestCase
{
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcessCases
*/
public function testProcess(string $source, array $expectedTokens): void
{
$this->doTest(
$source,
$expectedTokens,
[
\T_NAMESPACE,
CT::T_NAMESPACE_OPERATOR,
],
);
}
/**
* @return iterable<int, array{string, _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcessCases(): iterable
{
yield [
'<?php
namespace Foo;
namespace\Bar\baz();
',
[
1 => \T_NAMESPACE,
6 => CT::T_NAMESPACE_OPERATOR,
],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Tokenizer/Transformer/SquareBraceTransformerTest.php | tests/Tokenizer/Transformer/SquareBraceTransformerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Tokenizer\Transformer;
use PhpCsFixer\Tests\Test\AbstractTransformerTestCase;
use PhpCsFixer\Tokenizer\CT;
use PhpCsFixer\Tokenizer\Tokens;
use PhpCsFixer\Tokenizer\Transformer\SquareBraceTransformer;
/**
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @internal
*
* @covers \PhpCsFixer\Tokenizer\Transformer\SquareBraceTransformer
*
* @phpstan-import-type _TransformerTestExpectedKindsUnderIndex from AbstractTransformerTestCase
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SquareBraceTransformerTest extends AbstractTransformerTestCase
{
/**
* @param list<int> $inspectIndexes
*
* @dataProvider provideIsShortArrayCases
*/
public function testIsShortArray(string $source, array $inspectIndexes, bool $expected): void
{
$transformer = new SquareBraceTransformer();
$tokens = Tokens::fromCode($source);
foreach ($inspectIndexes as $index) {
self::assertTrue($tokens->offsetExists($index), \sprintf('Index %d does not exist.', $index));
}
foreach ($tokens as $index => $token) {
if (\in_array($index, $inspectIndexes, true)) {
self::assertSame('[', $token->getContent(), \sprintf('Token @ index %d must have content \']\'', $index));
$exp = $expected;
} elseif ('[' === $token->getContent()) {
$exp = !$expected;
} else {
continue;
}
self::assertSame(
$expected,
\Closure::bind(static fn (SquareBraceTransformer $transformer): bool => $transformer->isShortArray($tokens, $index), null, SquareBraceTransformer::class)($transformer),
\sprintf('Excepted token "%s" @ index %d %sto be detected as short array.', $token->toJson(), $index, $exp ? '' : 'not '),
);
}
}
/**
* @return iterable<int, array{string, list<int>, bool}>
*/
public static function provideIsShortArrayCases(): iterable
{
yield ['<?php $a=[];', [3], false];
yield ['<?php [$a] = [$b];', [7], false];
yield ['<?php [$a] = $b;', [1], false];
yield ['<?php [$a] = [$b] = [$b];', [1], false];
yield ['<?php function A(){}[$a] = [$b] = [$b];', [8], false];
yield ['<?php [$foo, $bar] = [$baz, $bat] = [$a, $b];', [10], false];
yield ['<?php [[$a, $b], [$c, $d]] = [[1, 2], [3, 4]];', [1], false];
yield ['<?php ["a" => $a, "b" => $b, "c" => $c] = $array;', [1], false];
yield ['<?php [$a, $b,, [$c, $d]] = $a;', [1, 9], false];
}
/**
* @param _TransformerTestExpectedKindsUnderIndex $expectedTokens
*
* @dataProvider provideProcessCases
*/
public function testProcess(string $source, array $expectedTokens = []): void
{
$this->doTest(
$source,
$expectedTokens,
[
CT::T_ARRAY_SQUARE_BRACE_OPEN,
CT::T_ARRAY_SQUARE_BRACE_CLOSE,
CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
);
}
/**
* @return iterable<array{0: string, 1?: _TransformerTestExpectedKindsUnderIndex}>
*/
public static function provideProcessCases(): iterable
{
yield 'Array offset only.' => [
'<?php $a = array(); $a[] = 0; $a[1] = 2;',
];
yield 'Short array construction.' => [
'<?php $b = [1, 2, 3];',
[
5 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
13 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php function foo(array $c = [ ]) {}',
[
11 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
13 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [];',
[
1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
2 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [1, "foo"];',
[
1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
6 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [[]];',
[
1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
2 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
3 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
4 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php ["foo", ["bar", "baz"]];',
[
1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
5 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
10 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
11 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php (array) [1, 2];',
[
3 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
8 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [1,2][$x];',
[
1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
5 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php $a[] = []?>',
[
7 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
8 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php $b = [1];',
[
5 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
7 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php $c[] = 2?>',
];
yield [
'<?php $d[3] = 4;',
];
yield [
'<?php $e = [];',
[
5 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
6 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php array();',
];
yield [
'<?php $x[] = 1;',
];
yield [
'<?php $x[1];',
];
yield [
'<?php $x [ 1 ];',
];
yield [
'<?php ${"x"}[1];',
];
yield [
'<?php FOO[1];',
];
yield [
'<?php array("foo")[1];',
];
yield [
'<?php foo()[1];',
];
yield [
'<?php "foo"[1];//[]',
];
yield [
'<?php
class Test
{
public function updateAttributeKey($key, $value)
{
$this->{camel_case($attributes)}[$key] = $value;
}
}',
];
yield [
'<?php [$a, $b, $c] = [1, 2, 3];',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
9 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
13 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
21 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php ["a" => $a, "b" => $b, "c" => $c] = $array;',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
21 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [$e] = $d; if ($a){}[$a, $b] = b();',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
3 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
17 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
22 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php $a = [$x] = [$y] = [$z] = [];', // this sample makes no sense, however is in valid syntax
[
5 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
7 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
11 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
13 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
17 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
19 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
23 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
24 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [$$a, $b] = $array;',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
7 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [$a, $b,, [$c, $d]] = $a;',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
9 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
14 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
15 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield 'nested I' => [
'<?php [$a[]] = $b;',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
5 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield 'nested II (with array offset)' => [
'<?php [$a[1]] = $b;',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
6 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield 'nested III' => [
'<?php [$a[1], [$b], $c[2]] = $d;',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
8 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
10 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
17 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [[[$a]/**/], $b[1], [/**/[$c]] /** */ ] = $d[1][2][3];',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
2 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
3 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
5 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
7 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
16 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
18 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
20 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
21 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
25 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php foreach ($z as [$a, $b]) {}',
[
8 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
13 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php foreach ($a as $key => [$x, $y]) {}',
[
12 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
17 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [$key => [$x, $y]];',
[
1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
6 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
11 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
12 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php array($key => [$x, $y]);',
[
7 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
12 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [$key => [$x, $y] = foo()];',
[
1 => CT::T_ARRAY_SQUARE_BRACE_OPEN,
6 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
11 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
18 => CT::T_ARRAY_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [&$a, $b] = $a;',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
7 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [$a, &$b] = $a;',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
7 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [&$a, &$b] = $a;',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
8 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
yield [
'<?php [[ [&$a, &$b], [&$c] ], [&$d/* */]] = $e;',
[
1 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
2 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
4 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
11 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
14 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
17 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
19 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
22 => CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN,
26 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
27 => CT::T_DESTRUCTURING_SQUARE_BRACE_CLOSE,
],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/NoSuperfluousElseifFixerTest.php | tests/Fixer/ControlStructure/NoSuperfluousElseifFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractNoUselessElseFixer
* @covers \PhpCsFixer\Fixer\ControlStructure\NoSuperfluousElseifFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoSuperfluousElseifFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoSuperfluousElseifFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
if ($some) { return 1; } if ($a == 6){ $test = false; } //',
'<?php
if ($some) { return 1; } elseif ($a == 6){ $test = false; } //',
];
yield [
'<?php
if ($foo) {
return 1;
}
if ($bar) {
return 2;
}
if ($baz) {
return 3;
} else {
return 4;
}',
'<?php
if ($foo) {
return 1;
} elseif ($bar) {
return 2;
} else if ($baz) {
return 3;
} else {
return 4;
}',
];
yield [
'<?php
if ($foo)
return 1;
if ($bar)
echo \'bar\';
else {
return 3;
}',
'<?php
if ($foo)
return 1;
elseif ($bar)
echo \'bar\';
else {
return 3;
}',
];
yield [
'<?php
if ($foo)
?><?php
elseif ($bar)
?><?php
else {
?><?php
}',
];
yield [
'<?php
if ($foo) {
?><?php
return;
}
if ($bar)
?><?php
else {
?><?php
}',
'<?php
if ($foo) {
?><?php
return;
} elseif ($bar)
?><?php
else {
?><?php
}',
];
yield [
'<?php
while (1) {
if (2) {
if (3) {
if (4) {
die;
}
if (5) {
exit;
} else {#foo
throw new \Exception();
}
'.'
continue;
}
if (6) {
return null;
} else {
return 1;
}
'.'
break;
}
/* bar */if (7)
return 2 + 3;
else {# baz
die(\'foo\');
}
}',
'<?php
while (1) {
if (2) {
if (3) {
if (4) {
die;
} elseif (5) {
exit;
} else {#foo
throw new \Exception();
}
'.'
continue;
} else if (6) {
return null;
} else {
return 1;
}
'.'
break;
} else/* bar */if (7)
return 2 + 3;
else {# baz
die(\'foo\');
}
}',
];
yield [
'<?php
if ($a === false)
{
if ($v) { $ret = "foo"; }
elseif($a)
die;
}
elseif($a)
$ret .= $value;
return $ret;',
];
yield [
'<?php
if ($a)
echo 1;
else if ($b)
die;
else {
echo 2;
}',
];
yield [
'<?php
if ($a) {
echo 1;
} else if ($b)
die;
else {
echo 2;
}',
];
yield [
'<?php
if ($a) {
echo 1;
} else if ($b) {
die;
} else {
echo 2;
}',
];
yield [
'<?php
if ($foo) {
return 1;
}
if ($bar) {
return 2;
}
if ($baz) {
throw new class extends Exception{};
} else {
return 4;
}',
'<?php
if ($foo) {
return 1;
} elseif ($bar) {
return 2;
} else if ($baz) {
throw new class extends Exception{};
} else {
return 4;
}',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected): void
{
$this->doTest($expected);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php
if ($foo) {
$a = $bar ?? throw new \Exception();
} elseif ($bar) {
echo 1;
}
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/EmptyLoopConditionFixerTest.php | tests/Fixer/ControlStructure/EmptyLoopConditionFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\EmptyLoopConditionFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\EmptyLoopConditionFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\EmptyLoopConditionFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class EmptyLoopConditionFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'from `for` to `while`' => [
'<?php
while (true){ if(foo()) {break;}}
for(;$i < 1;){ if(foo()) {break;}}',
'<?php
for(;;){ if(foo()) {break;}}
for(;$i < 1;){ if(foo()) {break;}}',
];
yield 'from `do while` to `while`' => [
'<?php while (true){ if(foo()) {break;}}',
'<?php do{ if(foo()) {break;}}while(true);',
];
yield 'from `while` to `for`' => [
'<?php
for(;;){ if(foo()) {break;}}
while(false){ echo 1; }
while($a()) { echo 2; }
',
'<?php
while(true){ if(foo()) {break;}}
while(false){ echo 1; }
while($a()) { echo 2; }
',
['style' => 'for'],
];
yield 'from `do while` to `for`' => [
'<?php for(;;){ if(foo()) {break;}}',
'<?php do{ if(foo()) {break;}}while(true);',
['style' => 'for'],
];
yield 'multiple `do while` to `while`' => [
'<?php while (true){}while (true){}while (true){}while (true){}while (true){}while (true){}',
'<?php do{}while(true);do{}while(true);do{}while(true);do{}while(true);do{}while(true);do{}while(true);',
];
yield 'multiple nested `do while` to `while`' => [
'<?php while (true){while (true){while (true){while (true){while (true){while (true){while (true){}}}}}}}',
'<?php do{do{do{do{do{do{do{}while(true);}while(true);}while(true);}while(true);}while(true);}while(true);}while(true);',
];
// comment cases
yield 'comment inside empty `for` condition' => [
'<?php while (true)/* 1 *//* 2 */{}',
'<?php for(/* 1 */;;/* 2 */){}',
];
yield 'comment following empty `for` condition' => [
'<?php for(;;)/* 3 */{}',
'<?php while(true/* 3 */){}',
['style' => 'for'],
];
// space cases
yield 'lot of space' => [
'<?php while (true){ foo3(); } ',
'<?php do{ foo3(); } while(true) ; ',
];
yield [
'<?php
while (true) {
foo1();
}
',
'<?php
do {
foo1();
}
while(
true
)
;',
];
// do not fix cases
yield 'not empty `while` condition' => [
'<?php while(true === foo()){ if(foo()) {break;}}',
null,
['style' => 'for'],
];
yield 'not empty `for` condition' => [
'<?php for(;foo();){ if(foo()) {break;}}',
];
yield 'not empty `do while` condition' => [
'<?php do{ if(foo()) {break;}}while(foo());',
];
yield '`while` false' => [
'<?php while(false){ if(foo()) {break;}}',
null,
['style' => 'for'],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/NoAlternativeSyntaxFixerTest.php | tests/Fixer/ControlStructure/NoAlternativeSyntaxFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\NoAlternativeSyntaxFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoAlternativeSyntaxFixer>
*
* @author Eddilbert Macharia <edd.cowan@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\NoAlternativeSyntaxFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoAlternativeSyntaxFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
declare(ticks = 1) {
}
',
'<?php
declare(ticks = 1) :
enddeclare;
',
];
yield [
'<?php
switch ($foo) {
case 1:
}
switch ($foo) {
case 1:
} ?>',
'<?php
switch ($foo):
case 1:
endswitch;
switch ($foo) :
case 1:
endswitch ?>',
];
yield [
'<?php
if ($some1) {
if ($some2) {
if ($some3) {
$test = true;
}
}
}
',
'<?php
if ($some1) :
if ($some2) :
if ($some3) :
$test = true;
endif;
endif;
endif;
',
];
yield [
'<?php if ($some) { $test = true; } else { $test = false; }',
];
yield [
'<?php if ($some) /* foo */ { $test = true; } else { $test = false; }',
'<?php if ($some) /* foo */ : $test = true; else :$test = false; endif;',
];
yield [
'<?php if ($some) { $test = true; } else { $test = false; }',
'<?php if ($some) : $test = true; else :$test = false; endif;',
];
yield [
'<?php if ($some) { if($test){echo $test;}$test = true; } else { $test = false; }',
'<?php if ($some) : if($test){echo $test;}$test = true; else : $test = false; endif;',
];
yield [
'<?php foreach (array("d") as $item) { echo $item;}',
'<?php foreach (array("d") as $item):echo $item;endforeach;',
];
yield [
'<?php foreach (array("d") as $item) { if($item){echo $item;}}',
'<?php foreach (array("d") as $item):if($item){echo $item;}endforeach;',
];
yield [
'<?php while (true) { echo "c";}',
'<?php while (true):echo "c";endwhile;',
];
yield [
'<?php foreach (array("d") as $item) { while ($item) { echo "dd";}}',
'<?php foreach (array("d") as $item):while ($item):echo "dd";endwhile;endforeach;',
];
yield [
'<?php foreach (array("d") as $item) { while ($item) { echo "dd" ; } }',
'<?php foreach (array("d") as $item): while ($item) : echo "dd" ; endwhile; endforeach;',
];
yield [
'<?php if ($some) { $test = true; } elseif ($some !== "test") { $test = false; }',
'<?php if ($some) : $test = true; elseif ($some !== "test") : $test = false; endif;',
];
yield [
'<?php if ($condition) { ?><p>This is visible.</p><?php } ?>',
'<?php if ($condition): ?><p>This is visible.</p><?php endif; ?>',
];
yield [
'<?php if ($condition): ?><p>This is visible.</p><?php endif; ?>',
null,
['fix_non_monolithic_code' => false],
];
yield [
'<?php if (true) { ?>Text display.<?php } ?>',
'<?php if (true): ?>Text display.<?php endif; ?>',
['fix_non_monolithic_code' => true],
];
yield [
'<?php if (true): ?>Text display.<?php endif; ?>',
null,
['fix_non_monolithic_code' => false],
];
yield [
'<?php if ($condition) { ?><?= "xd"; ?><?php } ?>',
'<?php if ($condition): ?><?= "xd"; ?><?php endif; ?>',
['fix_non_monolithic_code' => true],
];
yield [
'<?php if ($condition): ?><?= "xd"; ?><?php endif; ?>',
null,
['fix_non_monolithic_code' => false],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/ControlStructureContinuationPositionFixerTest.php | tests/Fixer/ControlStructure/ControlStructureContinuationPositionFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Preg;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\ControlStructureContinuationPositionFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\ControlStructureContinuationPositionFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\ControlStructureContinuationPositionFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ControlStructureContinuationPositionFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'else (same line, default)' => [
'<?php
if ($foo) {
foo();
} else {
bar();
}',
'<?php
if ($foo) {
foo();
}
else {
bar();
}',
];
yield 'elseif (same line, default)' => [
'<?php
if ($foo) {
foo();
} elseif ($bar) {
bar();
}',
'<?php
if ($foo) {
foo();
}
elseif ($bar) {
bar();
}',
];
yield 'else if (same line, default)' => [
'<?php
if ($foo) {
foo();
} else if ($bar) {
bar();
}',
'<?php
if ($foo) {
foo();
}
else if ($bar) {
bar();
}',
];
yield 'do while (same line, default)' => [
'<?php
do {
foo();
} while ($foo);',
'<?php
do {
foo();
}
while ($foo);',
];
yield 'try catch finally (same line, default)' => [
'<?php
try {
foo();
} catch (Throwable $e) {
bar();
} finally {
baz();
}',
'<?php
try {
foo();
}
catch (Throwable $e) {
bar();
}
finally {
baz();
}',
];
yield 'else (next line)' => [
'<?php
if ($foo) {
foo();
}
else {
bar();
}',
'<?php
if ($foo) {
foo();
} else {
bar();
}',
['position' => 'next_line'],
];
yield 'elseif (next line)' => [
'<?php
if ($foo) {
foo();
}
elseif ($bar) {
bar();
}',
'<?php
if ($foo) {
foo();
} elseif ($bar) {
bar();
}',
['position' => 'next_line'],
];
yield 'else if (next line)' => [
'<?php
if ($foo) {
foo();
}
else if ($bar) {
bar();
}',
'<?php
if ($foo) {
foo();
} else if ($bar) {
bar();
}',
['position' => 'next_line'],
];
yield 'do while (next line)' => [
'<?php
do {
foo();
}
while ($foo);',
'<?php
do {
foo();
} while ($foo);',
['position' => 'next_line'],
];
yield 'try catch finally (next line)' => [
'<?php
try {
foo();
}
catch (Throwable $e) {
bar();
}
finally {
baz();
}',
'<?php
try {
foo();
} catch (Throwable $e) {
bar();
} finally {
baz();
}',
['position' => 'next_line'],
];
yield 'else with comment after closing brace' => [
'<?php
if ($foo) {
foo();
} // comment
else {
bar();
}',
];
yield 'elseif with comment after closing brace' => [
'<?php
if ($foo) {
foo();
} // comment
elseif ($bar) {
bar();
}',
];
yield 'else if with comment after closing brace' => [
'<?php
if ($foo) {
foo();
} // comment
else if ($bar) {
bar();
}',
];
yield 'do while with comment after closing brace' => [
'<?php
do {
foo();
} // comment
while (false);',
];
yield 'try catch finally with comment after closing brace' => [
'<?php
try {
foo();
} // comment
catch (Throwable $e) {
bar();
} // comment
finally {
baz();
}',
];
yield 'while not after do' => [
'<?php
if ($foo) {
foo();
}
while ($bar) {
bar();
}',
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideWithWhitespacesConfigCases
*/
public function testWithWhitespacesConfig(string $expected, ?string $input = null, ?array $configuration = null): void
{
if (null !== $configuration) {
$this->fixer->configure($configuration);
}
$this->fixer->setWhitespacesConfig(
new WhitespacesFixerConfig(' ', "\r\n"),
);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideWithWhitespacesConfigCases(): iterable
{
foreach (self::provideFixCases() as $label => $case) {
yield $label => [
Preg::replace('/\n/', "\r\n", $case[0]),
isset($case[1]) ? Preg::replace('/\n/', "\r\n", $case[1]) : null,
$case[2] ?? null,
];
}
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/NoUnneededControlParenthesesFixerTest.php | tests/Fixer/ControlStructure/NoUnneededControlParenthesesFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\NoUnneededControlParenthesesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoUnneededControlParenthesesFixer>
*
* @author Sullivan Senechal <soullivaneuh@gmail.com>
* @author Gregor Harlan <gharlan@web.de>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\NoUnneededControlParenthesesFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoUnneededControlParenthesesFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
$configuration = [
'statements' => [
'break',
'clone',
'continue',
'echo_print',
'return',
'switch_case',
'yield',
'yield_from',
'others',
],
];
yield [
'<?php while ($x) { while ($y) { break 2; } }',
'<?php while ($x) { while ($y) { break (2); } }',
$configuration,
];
yield [
'<?php while ($x) { while ($y) { break 2; } }',
'<?php while ($x) { while ($y) { break(2); } }',
$configuration,
];
yield [
'<?php while ($x) { while ($y) { continue 2; } }',
'<?php while ($x) { while ($y) { continue (2); } }',
$configuration,
];
yield [
'<?php while ($x) { while ($y) { continue 2; } }',
'<?php while ($x) { while ($y) { continue(2); } }',
$configuration,
];
yield [
'<?php
$var = clone ($obj1 ?: $obj2);
$var = clone ($obj1 ? $obj1->getSubject() : $obj2);
',
null,
$configuration,
];
yield [
'<?php clone $object;',
'<?php clone ($object);',
$configuration,
];
yield [
'<?php clone new Foo();',
'<?php clone (new Foo());',
$configuration,
];
yield [
'<?php
foo(clone $a);
foo(clone $a, 1);
$a = $b ? clone $b : $c;
',
'<?php
foo(clone($a));
foo(clone($a), 1);
$a = $b ? clone($b) : $c;
',
$configuration,
];
yield [
'<?php
echo (1 + 2) . $foo;
print (1 + 2) . $foo;
',
null,
$configuration,
];
yield [
'<?php echo (1 + 2) * 10, "\n";',
null,
$configuration,
];
yield [
'<?php echo (1 + 2) * 10, "\n" ?>',
null,
$configuration,
];
yield [
'<?php echo "foo" ?>',
'<?php echo ("foo") ?>',
$configuration,
];
yield [
'<?php print "foo" ?>',
'<?php print ("foo") ?>',
$configuration,
];
yield [
'<?php echo "foo2"; print "foo";',
'<?php echo ("foo2"); print ("foo");',
$configuration,
];
yield [
'<?php echo "foo"; print "foo1";',
'<?php echo("foo"); print("foo1");',
$configuration,
];
yield [
'<?php echo 2; print 2;',
'<?php echo(2); print(2);',
$configuration,
];
yield [
'<?php
echo $a ? $b : $c;
echo ($a ? $b : $c) ? $d : $e;
echo 10 * (2 + 3);
echo ("foo"), ("bar");
echo my_awesome_function("foo");
echo $this->getOutput(1);
',
'<?php
echo ($a ? $b : $c);
echo ($a ? $b : $c) ? $d : $e;
echo 10 * (2 + 3);
echo ("foo"), ("bar");
echo my_awesome_function("foo");
echo $this->getOutput(1);
',
$configuration,
];
yield [
'<?php return (1 + 2) * 10;',
'<?php return ((1 + 2) * 10);',
$configuration,
];
yield [
'<?php return "prod";',
'<?php return ("prod");',
$configuration,
];
yield [
'<?php return $x;',
'<?php return($x);',
$configuration,
];
yield [
'<?php return 2;',
'<?php return(2);',
$configuration,
];
yield [
'<?php return 2?>',
'<?php return(2)?>',
$configuration,
];
yield [
'<?php
switch ($a) {
case "prod":
break;
}
',
null,
$configuration,
];
yield [
'<?php
switch ($a) {
case "prod":
break;
}
',
'<?php
switch ($a) {
case ("prod"):
break;
}
',
$configuration,
];
yield [
'<?php
switch ($a) {
case $x;
}
',
'<?php
switch ($a) {
case($x);
}
',
$configuration,
];
yield [
'<?php
switch ($a) {
case 2;
}
',
'<?php
switch ($a) {
case(2);
}
',
$configuration,
];
yield [
'<?php
$a = 5.1;
$b = 1.0;
switch($a) {
case (int) $a < 1 : {
echo "leave alone";
break;
}
case $a < 2/* test */: {
echo "fix 1";
break;
}
case 3 : {
echo "fix 2";
break;
}
case /**//**/ // test
4
/**///
/**/: {
echo "fix 3";
break;
}
case ((int)$b) + 4.1: {
echo "fix 4";
break;
}
case ($b + 1) * 2: {
echo "leave alone";
break;
}
}
',
'<?php
$a = 5.1;
$b = 1.0;
switch($a) {
case (int) $a < 1 : {
echo "leave alone";
break;
}
case ($a < 2)/* test */: {
echo "fix 1";
break;
}
case (3) : {
echo "fix 2";
break;
}
case /**/(/**/ // test
4
/**/)//
/**/: {
echo "fix 3";
break;
}
case (((int)$b) + 4.1): {
echo "fix 4";
break;
}
case ($b + 1) * 2: {
echo "leave alone";
break;
}
}
',
$configuration,
];
yield [
'<?php while ($x) { while ($y) { break#
#
2#
#
; } }',
'<?php while ($x) { while ($y) { break#
(#
2#
)#
; } }',
$configuration,
];
yield [
'<?php
function foo() { yield "prod"; }
',
null,
$configuration,
];
yield [
'<?php
function foo() { yield (1 + 2) * 10; }
',
null,
$configuration,
];
yield [
'<?php
function foo() { yield (1 + 2) * 10; }
',
'<?php
function foo() { yield ((1 + 2) * 10); }
',
$configuration,
];
yield [
'<?php
function foo() { yield "prod"; }
',
'<?php
function foo() { yield ("prod"); }
',
$configuration,
];
yield [
'<?php
function foo() { yield 2; }
',
'<?php
function foo() { yield(2); }
',
$configuration,
];
yield [
'<?php
function foo() { $a = (yield $x); }
',
'<?php
function foo() { $a = (yield($x)); }
',
$configuration,
];
yield [
'<?php
$var = clone ($obj1->getSubject() ?? $obj2);
',
null,
$configuration,
];
$configuration = [
'statements' => [
'break',
'clone',
'continue',
'echo_print',
'return',
'switch_case',
'yield',
'yield_from',
'others',
'negative_instanceof',
],
];
yield '===' => [
'<?php $at = $a === $b;',
'<?php $at = ($a) === ($b);',
$configuration,
];
yield 'yield/from fun' => [
'<?php
function foo3() { $a = (yield $x); }
function foo4() { yield from (1 + 2) * 10; }
function foo5() { yield from "prod"; }
function foo6() { $a = (yield $x); }
function foo7() { yield from 2; }
function foo8() { $a = (yield from $x); }
',
'<?php
function foo3() { $a = (yield $x); }
function foo4() { yield from ((1 + 2) * 10); }
function foo5() { yield from ("prod"); }
function foo6() { $a = (yield($x)); }
function foo7() { yield from(2); }
function foo8() { $a = (yield from($x)); }
',
$configuration,
];
yield 'clone stuff' => [
'<?php
clone new $f[0];
$a1 = clone new foo;
$a2 = clone new foo();
$a3 = [clone $f];
$a4 = [1, clone $f];
$a5 = [clone $f, 2];
$a6 = [1, clone $f, 2];
$c1 = fn() => clone $z;
for ( clone new Bar(1+2) ; $i < 100; ++$i) {
$i = foo();
}
clone $object2[0]->foo()[2] /* 1 */ ?>',
'<?php
clone (new $f[0]);
$a1 = clone (new foo);
$a2 = clone (new foo());
$a3 = [clone ($f)];
$a4 = [1, clone ($f)];
$a5 = [clone ($f), 2];
$a6 = [1, clone ($f), 2];
$c1 = fn() => clone ($z);
for ( clone(new Bar(1+2) ); $i < 100; ++$i) {
$i = foo();
}
clone ($object2[0]->foo()[2]) /* 1 */ ?>',
$configuration,
];
yield 'print unary wrapped sequence' => [
'<?php $b7 =[ print !$a ,];',
'<?php $b7 =[ print !($a) ,];',
$configuration,
];
yield 'print - sequence' => [
'<?php
$b7 =[ print $a, 1];
$b7 =[1, print $a];
',
'<?php
$b7 =[ print ($a), 1];
$b7 =[1, print ($a)];
',
$configuration,
];
yield 'print - wrapped block' => [
'<?php $b7 =[ print $a ];',
'<?php $b7 =[ print ($a) ];',
$configuration,
];
yield 'print fun' => [
'<?php
for ( print $b+1; foo(); ++$i ) {}
for( print $b+1; foo(); ++$i ) {}
$b1 = $c[print $e+5];
$b2 = [1, print $e++];
$b3 = [print $e+9, 1];
$b4 = [1, 1 + print $e, 1];
$b51 = fn() => print $b."something";
$b52 = fn() => print $b."else";
$b6 = foo(print $a);
$b7 =[ print $a ,];
$b8 =[ print ($a+1) . "1" ,];
',
'<?php
for ( (print $b+1); foo(); ++$i ) {}
for( print ($b+1); foo(); ++$i ) {}
$b1 = $c[(print $e+5)];
$b2 = [1, (print $e++)];
$b3 = [(print $e+9), 1];
$b4 = [1, (1 + print $e), 1];
$b51 = fn() => (print $b."something");
$b52 = fn() => print ($b."else");
$b6 = foo(print ($a));
$b7 =[ (print ($a)) ,];
$b8 =[ (print ($a+1) . "1") ,];
',
$configuration,
];
yield 'simple' => [
'<?php $aw?><?php $av;',
'<?php ($aw)?><?php ($av);',
$configuration,
];
yield 'simple, echo open tag' => [
'<?= $a;',
'<?= ($a);',
$configuration,
];
yield '+ (X),' => [
'<?php $bw = [1 + !!$a, 2 + $b,];',
'<?php $bw = [1 + !!($a), 2 + ($b),];',
$configuration,
];
yield 'op ns' => [
'<?php $aq = A\B::c . $d;',
'<?php $aq = (A\B::c) . $d;',
$configuration,
];
yield 'wrapped FN' => [
'<?php $fn1 = fn($x) => $x + $y;',
'<?php $fn1 = fn($x) => ($x + $y);',
$configuration,
];
yield 'wrapped FN 2 with pre and reference' => [
'<?php $fn1 = fn & ($x) => !$x;',
'<?php $fn1 = fn & ($x) => !($x);',
$configuration,
];
yield 'wrapped FN with `,`' => [
'<?php
$fn1 = array_map(fn() => 1, $array);
$fn2 = array_map($array, fn() => 2);
$fn3 = array_map($array, fn() => 3, $array);
',
'<?php
$fn1 = array_map(fn() => (1), $array);
$fn2 = array_map($array, fn() => (2));
$fn3 = array_map($array, fn() => (3), $array);
',
$configuration,
];
yield 'wrapped FN with return type' => [
'<?php
$fn8 = fn(): int => 123456;
',
'<?php
$fn8 = fn(): int => (123456);
',
$configuration,
];
yield 'wrapped `for` elements' => [
'<?php
for (!$a; $a < 10; ++$a){
echo $a;
}
',
'<?php
for (!($a); ($a < 10); (++$a)){
echo $a;
}
',
$configuration,
];
$statements = [
'echo',
'print',
'return',
'throw',
'yield from',
'yield',
];
foreach ($statements as $statement) {
yield $statement.', no op, space' => [
'<?php function foo(){ '.$statement.' $e; }',
'<?php function foo(){ '.$statement.' ($e); }',
$configuration,
];
yield $statement.', wrapped op, no space' => [
'<?php function foo(){;'.$statement.' $e.$f; }',
'<?php function foo(){;'.$statement.'($e.$f); }',
$configuration,
];
}
yield 'yield wrapped unary' => [
'<?php $a = function($a) {yield ++$a;};',
'<?php $a = function($a) {yield (++$a);};',
$configuration,
];
$constants = [
'__CLASS__',
'__DIR__',
'__FILE__',
'__FUNCTION__',
'__LINE__',
'__METHOD__',
'__NAMESPACE__',
'__TRAIT__',
];
foreach ($constants as $constant) {
yield $constant.'+ op' => [
\sprintf('<?php $a = %s . $b;', $constant),
\sprintf('<?php $a = (%s) . $b;', $constant),
$configuration,
];
}
yield 'break/continue' => [
'<?php
while(foo() && $f) {
while(bar()) {
if ($a) {
break 2;
} else {
continue 2;
}
}
}
',
'<?php
while((foo() && $f)) {
while(bar()) {
if ($a) {
break (2);
} else {
continue (2);
}
}
}
',
$configuration,
];
yield 'switch case' => [
'<?php switch($a) {
case 1: echo 1;
case !$a; echo 2;
}',
'<?php switch($a) {
case(1): echo 1;
case !($a); echo 2;
}',
$configuration,
];
yield 'switch case II' => [
'<?php
switch ($x) {
case $a + 1 + 3:
break;
case 1 + $a + 4:
break;
}
',
'<?php
switch ($x) {
case ($a) + 1 + 3:
break;
case 1 + ($a) + 4:
break;
}
',
$configuration,
];
yield 'bin pre bin' => [
'<?php $t = 1+ $i +1;',
'<?php $t = 1+($i)+1;',
$configuration,
];
yield 'bin, (X))' => [
'<?php $ap = 1 + $a;',
'<?php $ap = 1 + ($a);',
$configuration,
];
yield 'bin close tag' => [
'<?php $d + 2; ?>',
'<?php ($d) + 2; ?>',
$configuration,
];
yield 'bin after open echo' => [
'<?= $d - 55;',
'<?= ($d) - 55;',
$configuration,
];
yield 'more bin + sequences combinations' => [
'<?php
$b1 = [1 + $d];
$b2 = [1 + $d,];
$b3 = [1,1 + $d + 1];
$b4 = [1,1 + $d,];
',
'<?php
$b1 = [1 + ($d)];
$b2 = [1 + ($d),];
$b3 = [1,1 + ($d) + 1];
$b4 = [1,1 + ($d),];
',
$configuration,
];
yield 'bin doggybag' => [
'<?php
while(foo()[1]){bar();}
$aR . "";
$e = [$a] + $b;
$f = [$a] + $b[1];
$a = $b + !$c + $d;
$b = !$a[1]++;
$c = $c + !!!(bool) -$a;
throw $b . "";
',
'<?php
while((foo())[1]){bar();}
($aR) . "";
$e = [$a] + ($b);
$f = [$a] + ($b)[1];
$a = $b + !($c) + $d;
$b = !($a)[1]++;
$c = $c + !!!(bool) -($a);
throw($b) . "";
',
$configuration,
];
$statements = [
'while(foo()){echo 1;}',
';;;;',
'echo',
'print',
'return',
'throw',
'',
];
foreach ($statements as $statement) {
yield $statement.' (X) bin' => [
'<?php '.$statement.' $e . "foo";',
'<?php '.$statement.' ($e) . "foo";',
$configuration,
];
}
yield 'bin after' => [
'<?php
$d * 5;
; $a - $b;
while(foo() + 1 < 100){} $z - 6 ?>',
'<?php
($d) * 5;
; ($a) - $b;
while((foo()) + 1 < 100){} ($z) - 6 ?>',
$configuration,
];
yield 'bin after throw/return' => [
'<?php
function foo() {
if($b) {
throw $e . "";
}
return ((string)$dd)[1] . "a";
}',
'<?php
function foo() {
if(($b)) {
throw($e) . "";
}
return(((string)$dd)[1] . "a");
}',
$configuration,
];
yield 'multiple fixes' => [
'<?php
$a = [];
$b = [1, []];
foo();
while(foo()){
$a = foo2();
}
',
'<?php
$a = ([]);
$b = ([1,([])]);
(foo());
while(foo()){
$a = (foo2());
}
',
$configuration,
];
yield 'access' => [
'<?php
$ag = $b->A::$foo;
$a1 = $b->$c[1];
$a2 = $b->c[1][2];
$a3 = $b->$c[1]->$a[2]->${"abc"};
$a4 = $b->$c[1][2]->${"abc"}(22);
$a5 = $o->$foo();
$o->$c[] = 6;
$a7 = $o->$c[8] (7);
$a9 = $o->abc($a);
$a10 = $o->abc($a)[1];
$a11 = $o->{$bar};
$a12 = $o->{$c->d}($e)[1](2)[$f]->$c[1]()?>
',
'<?php
$ag = (($b)->A::$foo);
$a1 = (($b)->$c[1]);
$a2 = (($b)->c[1][2]);
$a3 = (($b)->$c[1]->$a[2]->${"abc"});
$a4 = (($b)->$c[1][2]->${"abc"}(22));
$a5 = (($o)->$foo());
($o)->$c[] = 6;
$a7 = (($o)->$c[8] (7));
$a9 = (($o)->abc($a));
$a10 = (($o)->abc($a)[1]);
$a11 = (($o)->{$bar});
$a12 = (($o)->{$c->d}($e)[1](2)[$f]->$c[1]())?>
',
$configuration,
];
yield 'simple unary `!`' => [
'<?php
$a1 = !$foo;
$a2 = +$f;
$a3 = -$f;
$a4 = @bar();
',
'<?php
$a1 = !($foo);
$a2 = +($f);
$a3 = -($f);
$a4 = @(bar());
',
$configuration,
];
yield 'pre op ! function call' => [
'<?php $a9 = !foo($a + 1);',
'<?php $a9 = !(foo($a + 1));',
$configuration,
];
yield 'multiple pre in wrapped array init' => [
'<?php $d7 = [!!!!!!!$a5];',
'<?php $d7 = [!!!!!!!($a5)];',
$configuration,
];
yield 'pre op cast' => [
'<?php $a6 = (bool)$foo;',
'<?php $a6 = (bool)($foo);',
$configuration,
];
yield 'pre op ! wrapped' => [
'<?php if (!$z) {echo 1;}',
'<?php if ((!($z))) {echo 1;}',
$configuration,
];
yield 'crazy unary' => [
'<?php
$b0 = !!!(bool) $a1;
$b1 = [!!!(bool) $a2,1];
!!!(bool) $a3;
!!!(bool) $a4;
$b = 1 + (!!!!!!!$a5);
$a = !$a[1]++;
while(!!!(bool) $a2[1] ){echo 1;}
$b = @$a[1];
$b = ++$a[1];
',
'<?php
$b0 = !!!(bool) ($a1);
$b1 = [!!!(bool) ($a2),1];
(!!!(bool) (($a3)));
(!!!(bool) ($a4));
$b = 1 + (!!!!!!!($a5));
$a = !($a)[1]++;
while(!!!(bool) ($a2)[1] ){echo 1;}
$b = @($a)[1];
$b = ++($a)[1];
',
$configuration,
];
yield 'logic &&' => [
'<?php $arr = $a && $b;',
'<?php $arr = ($a) && $b;',
$configuration,
];
yield 'bin before' => [
'<?php
$ax = $d + $a;
$dx = 1 + $z ?>',
'<?php
$ax = $d + ($a);
$dx = 1 + ($z) ?>',
$configuration,
];
yield 'bin before and after' => [
'<?php
echo 1 + 2 + 3;
echo 1.5 + 2.5 + 3.5;
echo 1 + $obj->value + 3;
echo 1 + Obj::VALUE + 3;
',
'<?php
echo 1 + (2) + 3;
echo 1.5 + (2.5) + 3.5;
echo 1 + ($obj->value) + 3;
echo 1 + (Obj::VALUE) + 3;
',
$configuration,
];
yield 'new class as sequence element' => [
'<?php $a7 = [1, new class($a+$b) {}];',
'<?php $a7 = [1, (new class($a+$b) {})];',
$configuration,
];
yield 'pre @ sequence' => [
'<?php $a8 = [1, @ ($f.$b)() ];',
'<?php $a8 = [1, @( ($f.$b)() )];',
$configuration,
];
yield 'inside `{` and `}`' => [
'<?php
while(foo()) { bar(); }
while(bar()){foo1();};
if($a){foo();}
',
'<?php
while(foo()) { (bar()); }
while(bar()){(foo1());};
if($a){(foo());}
',
$configuration,
];
yield 'block type dynamic var brace' => [
'<?php ${$bar};',
'<?php ${($bar)};',
$configuration,
];
yield 'block type dynamic prop brace' => [
'<?php $foo->{$bar};',
'<?php $foo->{($bar)};',
$configuration,
];
yield 'anonymous class wrapped init' => [
'<?php $a11 = new class(1){};',
'<?php $a11 = new class((1)){};',
$configuration,
];
yield 'return, new long array notation, no space' => [
'<?php return array();',
'<?php return(array());',
$configuration,
];
yield 'block type array square brace' => [
'<?php
echo $a13 = [1];
$bX = [1,];
$b0 = [1, 2, 3,];
$b1 = [$a + 1];
$b2 = [-1 + $a];
$b3 = [2 + $c];
',
'<?php
echo $a13 = [(1)];
$bX = [(1),];
$b0 = [(1),(2),(3),];
$b1 = [($a) + 1];
$b2 = [-1 + ($a)];
$b3 = [2 + ($c)];
',
$configuration,
];
yield 'multiple array construct elements' => [
'<?php echo $a14 = [1, $a(1,$b(3)), 3+4]?>',
'<?php echo $a14 = [(1),($a(1,$b(3))),(3+4)]?>',
$configuration,
];
yield 'double comma and `)`' => [
'<?php echo sprintf("%d%s", $e, $f);',
'<?php echo sprintf("%d%s", ($e), ($f));',
$configuration,
];
yield 'two wrapped function calls' => [
'<?php foo(); $ap = foo();',
'<?php (foo()); $ap = (foo());',
$configuration,
];
yield 'wrapped function call, op + call as arg' => [
'<?php $bk = foo(1 + bar()) ?>',
'<?php $bk = (foo(1 + bar())) ?>',
$configuration,
];
yield 'wrapped function call, short open, semicolon' => [
'<?= foo1z() ?>',
'<?=(foo1z()) ?>',
$configuration,
];
yield 'wrapped function call, short open, close tag' => [
'<?= foo2A();',
'<?=( foo2A());',
$configuration,
];
yield 'wrapped returns' => [
'<?php function A($a) {return 1;}',
'<?php function A($a) {return (1);}',
$configuration,
];
yield 'wrapped returns ops' => [
'<?php function A($a1,$b2) {return ++$a1+$b2;}',
'<?php function A($a1,$b2) {return (++$a1+$b2);}',
$configuration,
];
yield 'throws, no space' => [
'<?php throw $z . 2;',
'<?php throw($z . 2);',
$configuration,
];
yield 'throws + op, wrapped in {}' => [
'<?php if (k()) { throw new $a.$b(1,2); } ?>',
'<?php if (k()) { throw (new $a.$b(1,2)); } ?>',
$configuration,
];
yield 'dynamic class name op' => [
'<?php $xX = ($d+$e)->test();',
'<?php $xX = (($d+$e))->test();',
$configuration,
];
yield 'token type changing edge case' => [
'<?php $y1 = (new Foo())->bar;',
'<?php $y1 = ((new Foo()))->bar;',
$configuration,
];
yield 'brace class instantiation open, double wrapped, no assign' => [
'<?php (new Foo())->bar();',
'<?php (((new Foo())))->bar();',
$configuration,
];
yield 'brace class instantiation open, multiple wrapped' => [
'<?php $y0 = (new Foo())->bar;',
'<?php $y0 = (((((new Foo())))))->bar;',
$configuration,
];
yield 'wrapped instance check' => [
'<?php
; $foo instanceof Foo;
; $foo() instanceof Foo;
$l1 = $foo instanceof $z;
$l2 = $foo instanceof $z[1];
$l3 = [$foo instanceof $z->a[1]];
$l4 = [1, $foo instanceof $a[1]->$f];
$l5 = [$foo instanceof Foo, 1];
$l6 = [1, $foo instanceof Foo, 1];
$fn1 = fn($x) => $fx instanceof Foo;
for ($foo instanceof Foo ; $i < 1; ++$i) { echo $i; }
class foo {
public function bar() {
self instanceof static;
self instanceof self;
$a instanceof static;
self instanceof $a;
$a instanceof self;
}
}
',
'<?php
; ($foo instanceof Foo);
; ($foo() instanceof Foo);
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | true |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/TrailingCommaInMultilineFixerTest.php | tests/Fixer/ControlStructure/TrailingCommaInMultilineFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer>
*
* @author Sebastiaan Stok <s.stok@rollerscapes.net>
* @author Kuba Werłos <werlos@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class TrailingCommaInMultilineFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
// long syntax tests
yield ['<?php $x = array();'];
yield ['<?php $x = array("foo");'];
yield ['<?php $x = array("foo", );'];
yield ["<?php \$x = array(\n'foo',\n);", "<?php \$x = array(\n'foo'\n);"];
yield ["<?php \$x = array('foo',\n);"];
yield ["<?php \$x = array('foo',\n);", "<?php \$x = array('foo'\n);"];
yield ["<?php \$x = array('foo', /* boo */\n);", "<?php \$x = array('foo' /* boo */\n);"];
yield ["<?php \$x = array('foo',\n/* boo */\n);", "<?php \$x = array('foo'\n/* boo */\n);"];
yield ["<?php \$x = array(\narray('foo',\n),\n);", "<?php \$x = array(\narray('foo'\n)\n);"];
yield ["<?php \$x = array(\narray('foo'),\n);", "<?php \$x = array(\narray('foo')\n);"];
yield ["<?php \$x = array(\n /* He */ \n);"];
yield [
"<?php \$x = array('a', 'b', 'c',\n 'd', 'q', 'z');",
];
yield [
"<?php \$x = array('a', 'b', 'c',\n'd', 'q', 'z');",
];
yield [
"<?php \$x = array('a', 'b', 'c',\n'd', 'q', 'z' );",
];
yield [
"<?php \$x = array('a', 'b', 'c',\n'd', 'q', 'z'\t);",
];
yield ["<?php \$x = array(\n<<<EOT\noet\nEOT\n);"];
yield ["<?php \$x = array(\n<<<'EOT'\noet\nEOT\n);"];
yield [
'<?php
$foo = array(
array(
),
);',
];
yield [
'<?php
$a = array(
1 => array(
2 => 3,
),
);',
'<?php
$a = array(
1 => array(
2 => 3
)
);',
];
yield [
"<?php
\$x = array(
'foo',
'bar',
array(
'foo',
'bar',
array(
'foo',
'bar',
array(
'foo',
('bar' ? true : !false),
('bar' ? array(true) : !(false)),
array(
'foo',
'bar',
array(
'foo',
('bar'),
),
),
),
),
),
);",
"<?php
\$x = array(
'foo',
'bar',
array(
'foo',
'bar',
array(
'foo',
'bar',
array(
'foo',
('bar' ? true : !false),
('bar' ? array(true) : !(false)),
array(
'foo',
'bar',
array(
'foo',
('bar'),
)
)
)
)
)
);",
];
yield [
'<?php
$a = array("foo" => function ($b) {
return "bar".$b;
});',
];
yield [
'<?php
return array(
"a" => 1,
"b" => 2,
);',
'<?php
return array(
"a" => 1,
"b" => 2
);',
];
yield [
'<?php
$test = array("foo", <<<TWIG
foo
bar
baz
TWIG
, $twig);',
];
yield [
'<?php
$test = array("foo", <<<\'TWIG\'
foo
bar
baz
TWIG
, $twig);',
];
// short syntax tests
yield ['<?php $x = array([]);'];
yield ['<?php $x = [[]];'];
yield ['<?php $x = ["foo",];'];
yield ['<?php $x = bar(["foo",]);'];
yield ["<?php \$x = bar(['foo',\n]);", "<?php \$x = bar(['foo'\n]);"];
yield ["<?php \$x = ['foo', \n];"];
yield ['<?php $x = array([],);'];
yield ['<?php $x = [[],];'];
yield ['<?php $x = [$y,];'];
yield ["<?php \$x = [\n /* He */ \n];"];
yield [
'<?php
$foo = [
[
],
];',
];
yield [
'<?php
$a = ["foo" => function ($b) {
return "bar".$b;
}];',
];
yield [
'<?php
return [
"a" => 1,
"b" => 2,
];',
'<?php
return [
"a" => 1,
"b" => 2
];',
];
yield [
'<?php
$test = ["foo", <<<TWIG
foo
bar
baz
TWIG
, $twig];',
];
yield [
'<?php
$test = ["foo", <<<\'TWIG\'
foo
bar
baz
TWIG
, $twig];',
];
// no array tests
yield [
"<?php
throw new BadMethodCallException(
sprintf(
'Method \"%s\" not implemented',
__METHOD__
)
);",
];
yield [
"<?php
throw new BadMethodCallException(sprintf(
'Method \"%s\" not implemented',
__METHOD__
));",
];
yield [
"<?php
namespace FOS\\RestBundle\\Controller;
class ExceptionController extends ContainerAware
{
public function showAction(Request \$request, \$exception, DebugLoggerInterface \$logger = null, \$format = 'html')
{
if (!\$exception instanceof DebugFlattenException && !\$exception instanceof HttpFlattenException) {
throw new \\InvalidArgumentException(sprintf(
'ExceptionController::showAction can only accept some exceptions (%s, %s), \"%s\" given',
'Symfony\\Component\\HttpKernel\\Exception\\FlattenException',
'Symfony\\Component\\Debug\\Exception\\FlattenException',
get_class(\$exception)
));
}
}
}",
];
yield [
'<?php
function foo(array $a)
{
bar(
baz(
1
)
);
}',
];
yield [
'<?php
$var = array(
"string",
//comment
);',
'<?php
$var = array(
"string"
//comment
);',
];
yield [
'<?php
$var = array(
"string",
/* foo */);',
'<?php
$var = array(
"string"
/* foo */);',
];
yield [
'<?php
$var = [
"string",
/* foo */];',
'<?php
$var = [
"string"
/* foo */];',
];
yield [
'<?php
function a()
{
yield array(
"a" => 1,
"b" => 2,
);
}',
'<?php
function a()
{
yield array(
"a" => 1,
"b" => 2
);
}',
];
yield [
'<?php
function a()
{
yield [
"a" => 1,
"b" => 2,
];
}',
'<?php
function a()
{
yield [
"a" => 1,
"b" => 2
];
}',
];
yield ['<?php
while(
(
(
$a
)
)
) {}',
];
yield [
"<?php foo('a', 'b', 'c', 'd', 'q', 'z');",
null,
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
"<?php function foo(\$a,\n\$b\n) {};",
null,
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
'<?php foo(1, 2, [
ARRAY_ELEMENT_1,
ARRAY_ELEMENT_2
], 3, 4);',
null,
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
"<?php \$var = array('a', 'b',\n 'c', 'd');",
null,
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
"<?php \$var = list(\$a, \$b,\n \$c, \$d) = [1, 2, 3, 4];",
null,
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
"<?php if (true || \n false) {}",
null,
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
"<?php \$var = foo('a', 'b', 'c',\n 'd', 'q', 'z');",
null, // do not fix if not configured
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARRAYS]],
];
yield [
"<?php \$var = foo('a', 'b', 'c',\n 'd', 'q', 'z');",
null,
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
"<?php \$var = foo('a', 'b', 'c',\n 'd', 'q', 'z',\n);",
"<?php \$var = foo('a', 'b', 'c',\n 'd', 'q', 'z'\n);",
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
"<?php \$var = \$foonction('a', 'b', 'c',\n 'd', 'q', 'z');",
null,
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
"<?php \$var = \$fMap[100]('a', 'b', 'c',\n 'd', 'q', 'z');",
null,
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
"<?php \$var = new Foo('a', 'b', 'c',\n 'd', 'q', 'z',\n);",
"<?php \$var = new Foo('a', 'b', 'c',\n 'd', 'q', 'z'\n);",
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
"<?php \$var = new class('a', 'b', 'c',\n 'd', 'q', 'z',\n) extends Foo {};",
"<?php \$var = new class('a', 'b', 'c',\n 'd', 'q', 'z'\n) extends Foo {};",
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
'<?php
$obj->method(
1,
2,
);
',
'<?php
$obj->method(
1,
2
);
',
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield 'function-like language constructs' => [
'<?php
isset(
$a,
$b,
);
unset(
$a,
$b,
);
list(
$a,
$b,
) = $foo;
',
'<?php
isset(
$a,
$b
);
unset(
$a,
$b
);
list(
$a,
$b
) = $foo;
',
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
'<?php
array(
1,
2,
);
[
3,
4,
];
foo(
5,
6,
);
',
'<?php
array(
1,
2
);
[
3,
4
];
foo(
5,
6
);
',
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARRAYS, TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
'<?php
while(
(
(
$a
)
)
) {}',
null,
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARRAYS, TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
<<<'EXPECTED'
<?php
$a = [
<<<'EOD'
foo
EOD,
];
EXPECTED,
<<<'INPUT'
<?php
$a = [
<<<'EOD'
foo
EOD
];
INPUT,
['after_heredoc' => true],
];
yield [
'<?php $a = new class() {function A() { return new static(
1,
2,
); }};',
'<?php $a = new class() {function A() { return new static(
1,
2
); }};',
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
'<?php
$a = [11,2,3];
[
$c,
$d,
] = $a;
',
'<?php
$a = [11,2,3];
[
$c,
$d
] = $a;
',
['elements' => ['array_destructuring']],
];
}
/**
* @requires PHP <8.0
*/
public function testFixPre80(): void
{
$this->fixer->configure([
'elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARRAYS, TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS],
]);
// ELEMENTS_PARAMETERS got unconfigured, as not valid <8.0
$this->doTest(
"<?php function foo(\$a\n) { return [1,\n]; }",
"<?php function foo(\$a\n) { return [1\n]; }",
);
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php function foo($x, $y) {}',
null,
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS]],
];
yield [
'<?php function foo(
$x,
$y
) {}',
null, // do not fix if not configured
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARRAYS, TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]],
];
yield [
'<?php function foo(
$x,
$y,
) {}',
'<?php function foo(
$x,
$y
) {}',
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS]],
];
yield [
'<?php $x = function(
$x,
$y,
) {};',
'<?php $x = function(
$x,
$y
) {};',
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS]],
];
yield [
'<?php $x = fn(
$x,
$y,
) => $x + $y;',
'<?php $x = fn(
$x,
$y
) => $x + $y;',
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS]],
];
yield 'match' => [
'<?php
$m = match ($a) {
200, 300 => null,
400 => 1,
500 => function() {return 2;},
600 => static function() {return 4;},
default => 3,
};
$z = match ($a) {
1 => 0,
2 => 1,
};
$b = match($c) {19 => 28, default => 333};
',
'<?php
$m = match ($a) {
200, 300 => null,
400 => 1,
500 => function() {return 2;},
600 => static function() {return 4;},
default => 3
};
$z = match ($a) {
1 => 0,
2 => 1
};
$b = match($c) {19 => 28, default => 333};
',
['elements' => ['match']],
];
yield 'match with last comma in the same line as closing brace' => [
'<?php
$x = match ($a) { 1 => 0,
2 => 1 };
',
null,
['elements' => ['match']],
];
yield 'match and parameters' => [
'<?php function foo(
$a,
) {
return [
1,
];
}',
'<?php function foo(
$a
) {
return [
1
];
}',
['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARRAYS, TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS]],
];
yield 'empty match' => [
<<<'PHP'
<?php
$a = match($x) {
default => function () {},
};
$b = match($y) {
// Hello!
};
$c = match($z) {
};
PHP,
<<<'PHP'
<?php
$a = match($x) {
default => function () {}
};
$b = match($y) {
// Hello!
};
$c = match($z) {
};
PHP,
['elements' => ['match']],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/NoUnneededCurlyBracesFixerTest.php | tests/Fixer/ControlStructure/NoUnneededCurlyBracesFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\NoUnneededCurlyBracesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoUnneededCurlyBracesFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\NoUnneededCurlyBracesFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoUnneededCurlyBracesFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'simple sample, last token candidate' => [
'<?php echo 1;',
'<?php { echo 1;}',
];
yield 'minimal sample, first token candidate' => [
'<?php // {}',
'<?php {} // {}',
];
yield [
'<?php
echo 0; //
echo 1;
switch($a) {
case 2: echo 3; break;
}
echo 4; echo 5; //
',
'<?php
{ { echo 0; } } //
{echo 1;}
switch($a) {
case 2: {echo 3; break;}
}
echo 4; { echo 5; }//
',
];
yield 'no fixes' => [
'<?php
foreach($a as $b){}
while($a){}
do {} while($a);
if ($c){}
if ($c){}else{}
if ($c){}elseif($d){}
if ($c) {}elseif($d)/** */{ } else/**/{ }
try {} catch(\Exception $e) {}
function test(){}
$a = function() use ($c){};
class A extends B {}
interface D {}
trait E {}
',
];
yield 'no fixes II' => [
'<?php
declare(ticks=1) {
// entire script here
}
#',
];
yield 'no fix catch/try/finally' => [
'<?php
try {
} catch(\Exception $e) {
} finally {
}
',
];
yield 'no fix namespace block' => [
'<?php
namespace {
}
namespace A {
}
namespace A\B {
}
',
];
yield 'provideNoFix7Cases' => [
'<?php
use some\a\{ClassA, ClassB, ClassC as C};
use function some\a\{fn_a, fn_b, fn_c};
use const some\a\{ConstA, ConstB, ConstC};
use some\x\{ClassD, function CC as C, function D, const E, function A\B};
class Foo
{
public function getBar(): array
{
}
}
',
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield 'no fixes, offset access syntax with curly braces' => [
'<?php
echo ${$a};
echo $a{1};
',
];
}
/**
* @dataProvider provideFixNamespaceCases
*/
public function testFixNamespace(string $expected, ?string $input = null): void
{
$this->fixer->configure(['namespaces' => true]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixNamespaceCases(): iterable
{
yield [
'<?php
namespace Foo;
function Bar(){}
',
'<?php
namespace Foo {
function Bar(){}
}
',
];
yield [
'<?php
namespace A5 {
function AA(){}
}
namespace B6 {
function BB(){}
}',
];
yield [
'<?php
namespace Foo7;
function Bar(){}
',
'<?php
namespace Foo7 {
function Bar(){}
}',
];
yield [
'<?php
namespace Foo8\A;
function Bar(){}
?>',
"<?php
namespace Foo8\\A\t \t {
function Bar(){}
} ?>",
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/SwitchContinueToBreakFixerTest.php | tests/Fixer/ControlStructure/SwitchContinueToBreakFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\SwitchContinueToBreakFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\SwitchContinueToBreakFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SwitchContinueToBreakFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'alternative syntax |' => [
'<?php
switch($foo):
case 3:
continue;
endswitch?>
',
];
yield 'alternative syntax ||' => [
'<?php
foreach ([] as $v) {
continue;
}
if ($foo != 0) {
}
switch ($foo):
endswitch;',
];
yield 'nested switches' => [
'<?php
switch($z) {
case 1:
switch($x) {
case 2:
switch($y) {
case 3:
switch($z) {
case 4:
break; // z.1
}
break; // z
}
break; // y
}
break; // x
}
',
'<?php
switch($z) {
case 1:
switch($x) {
case 2:
switch($y) {
case 3:
switch($z) {
case 4:
continue; // z.1
}
continue; // z
}
continue; // y
}
continue; // x
}
',
];
yield 'nested 2' => [
'<?php
while ($foo) {
switch ($bar) {
case "baz":
while ($xyz) {
switch($zA) {
case 1:
break 3; // fix
}
if ($z) continue;
if ($zz){ continue; }
if ($zzz) continue 3;
if ($zzz){ continue 3; }
if ($b) break 2; // fix
}
switch($zG) {
case 1:
switch($zF) {
case 1:
break 1; // fix
case 2:
break 2; // fix
case 3:
break 3; // fix
case 4:
while($a){
while($a){
while($a){
if ($a) {
break 4; // fix
} elseif($z) {
break 5; // fix
} else {
break 6; // fix
}
continue 7;
}
}
}
continue 4;
}
break 2; // fix
}
}
}
',
'<?php
while ($foo) {
switch ($bar) {
case "baz":
while ($xyz) {
switch($zA) {
case 1:
continue 3; // fix
}
if ($z) continue;
if ($zz){ continue; }
if ($zzz) continue 3;
if ($zzz){ continue 3; }
if ($b) continue 2; // fix
}
switch($zG) {
case 1:
switch($zF) {
case 1:
continue 1; // fix
case 2:
continue 2; // fix
case 3:
continue 3; // fix
case 4:
while($a){
while($a){
while($a){
if ($a) {
continue 4; // fix
} elseif($z) {
continue 5; // fix
} else {
continue 6; // fix
}
continue 7;
}
}
}
continue 4;
}
continue 2; // fix
}
}
}
',
];
yield 'nested do while' => [
'<?php
switch ($a) {
case 1:
do {
switch ($a) {
case 1:
do {
switch ($a) {
case 1:
do {
continue;
} while (false);
break;
}
continue;
} while (false);
break;
}
continue;
} while (false);
break;
}
',
'<?php
switch ($a) {
case 1:
do {
switch ($a) {
case 1:
do {
switch ($a) {
case 1:
do {
continue;
} while (false);
continue;
}
continue;
} while (false);
continue;
}
continue;
} while (false);
continue;
}
',
];
yield 'nested while without {}' => [
'<?php
switch(foo()) {
case 1: while(bar($i))continue;break;
default: echo 7;
}
',
'<?php
switch(foo()) {
case 1: while(bar($i))continue;continue;
default: echo 7;
}
',
];
yield 'nested while with {}' => [
'<?php
switch(foo()) {
case 1: while(bar($i)){ --$i; echo 1; continue;}break;
default: echo 8;
}',
'<?php
switch(foo()) {
case 1: while(bar($i)){ --$i; echo 1; continue;}continue;
default: echo 8;
}',
];
yield 'do not fix cases' => [
'<?php
switch($a) {
case 1:
while (false) {
continue;
}
while (false) break 1;
do {
continue;
} while (false);
for ($a = 0; $a < 1; ++$a) {
continue;
}
foreach ($a as $b) continue;
for (; $i < 1; ++$i) break 1; echo $i;
for (;;) continue;
while(false) continue;
while(false) continue?><?php
// so bad and such a mess, not worth adding a ton of logic to fix this
switch($z) {
case 1:
continue ?> <?php 23;
case 2:
continue 1?> <?php + $a;
}
}
',
];
yield 'not int cases' => [
'<?php
while($b) {
switch($a) {
case 1:
break 01;
case 2:
break 0x1;
case 22:
break 0x01;
case 3:
break 0b1;
case 32:
break 0b0001;
case 4:
break 0b000001;
// do not fix
case 1:
continue 02;
case 2:
continue 0x2;
case 3:
continue 0b10;
}
}',
'<?php
while($b) {
switch($a) {
case 1:
continue 01;
case 2:
continue 0x1;
case 22:
continue 0x01;
case 3:
continue 0b1;
case 32:
continue 0b0001;
case 4:
continue 0b000001;
// do not fix
case 1:
continue 02;
case 2:
continue 0x2;
case 3:
continue 0b10;
}
}',
];
yield 'deep nested case' => [
'<?php
switch ($a) {
case $b:
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
switch ($a) {
case 1:
echo 1;
break 10;
}}}}}}}}}}',
'<?php
switch ($a) {
case $b:
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
switch ($a) {
case 1:
echo 1;
continue 10;
}}}}}}}}}}',
];
yield 'underscore constant' => [
'<?php
switch($a) {
case "a":
echo __FILE__;
break;
}
',
'<?php
switch($a) {
case "a":
echo __FILE__;
continue;
}
',
];
yield 'numeric literal separator' => [
'<?php
switch ($a) {
case $b:
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
switch ($a) {
case 1:
echo 1;
break 1_0;
}}}}}}}}}}',
'<?php
switch ($a) {
case $b:
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
while (false) {
switch ($a) {
case 1:
echo 1;
continue 1_0;
}}}}}}}}}}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/NoUselessElseFixerTest.php | tests/Fixer/ControlStructure/NoUselessElseFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\AbstractNoUselessElseFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\Tokenizer\Tokens;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractNoUselessElseFixer
* @covers \PhpCsFixer\Fixer\ControlStructure\NoUselessElseFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoUselessElseFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoUselessElseFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
if (true) {
$b = $a > 2 ? "" : die
?>
<?php
} else {
echo 798;
}',
];
yield [
'<?php
if (true) {
$b = $a > 2 ? "" : die
?>
<?php ; // useless semicolon case
} else {
echo 798;
}',
];
yield [
'<?php
if (true) {
if($a) die
?>
<?php ; // useless semicolon case
} else {
echo 798;
}',
];
yield [
'<?php
if (true) {
echo 1;
?>
<?php ; // useless semicolon case
} else {
echo 798;
}',
];
yield [
'<?php
if (true) {
echo 777;
if(false) die ?>
<?php
} else {
echo 778;
}',
];
yield [
'<?php
if (true)
echo 3;
else {
?><?php
echo 4;
}
',
];
yield [
'<?php
if (true)
echo 3;
'.'
?><?php
echo 4;
',
'<?php
if (true)
echo 3;
else
?><?php
echo 4;
',
];
yield [
'<?php
if (true)
echo 4;
?><?php echo 5;',
'<?php
if (true)
echo 4;
else?><?php echo 5;',
];
yield from self::generateCases(
'<?php
while(true) {
while(true) {
if ($provideFixIfElseIfElseCases) {
return;
} elseif($a1) {
if ($b) {echo 1; die;} echo 552;
return 1;
} elseif($b) {
%s
} '.'
echo 662;
'.'
}
}
',
'<?php
while(true) {
while(true) {
if ($provideFixIfElseIfElseCases) {
return;
} elseif($a1) {
if ($b) {echo 1; die;} else {echo 552;}
return 1;
} elseif($b) {
%s
} else {
echo 662;
}
}
}
',
);
yield from self::generateCases('<?php
while(true) {
while(true) {
if($a) {
echo 100;
} elseif($b) {
%s
} else {
echo 3;
}
}
}
');
yield from self::generateCases('<?php
while(true) {
while(true) {
if ($a) {
echo 100;
} elseif ($a1) {
echo 99887;
} elseif ($b) {
echo $b+1; //
/* test */
%s
} else {
echo 321;
}
}
}
');
yield [
'<?php
if ($a)
echo 1789;
else if($b)
echo 256;
elseif($c)
echo 3;
if ($a) {
}elseif($d) {
return 1;
}
else
echo 4;
',
];
yield [
'<?php
if ($a)
echo 1789;
else if($b) {
echo 256;
} elseif($c) {
echo 3;
if ($d) {
echo 4;
} elseif($e)
return 1;
} else
echo 4;
',
];
yield from self::generateCases(
'<?php
while(true) {
while(true) {
if ($a) {
%s
} '.'
echo 1;
'.'
}
}
',
'<?php
while(true) {
while(true) {
if ($a) {
%s
} else {
echo 1;
}
}
}
',
);
yield [
'<?php
if ($a) {
GOTO jump;
} '.'
echo 1789;
'.'
jump:
',
'<?php
if ($a) {
GOTO jump;
} else {
echo 1789;
}
jump:
',
];
yield 'nested if' => [
'<?php
if ($x) {
if ($y) {
return 1;
} '.'
return 2;
'.'
} '.'
return 3;
'.'
',
'<?php
if ($x) {
if ($y) {
return 1;
} else {
return 2;
}
} else {
return 3;
}
',
];
yield [
'<?php
if (false)
echo 1;
'.'
',
'<?php
if (false)
echo 1;
else{}
',
];
yield [
'<?php if($a){}',
'<?php if($a){}else{}',
];
yield [
'<?php if($a){ $a = ($b); } ',
'<?php if($a){ $a = ($b); } else {}',
];
yield [
'<?php if ($a) {;} if ($a) {;} /**/ if($a){}',
'<?php if ($a) {;} else {} if ($a) {;} else {/**/} if($a){}else{}',
];
yield [
'<?php
if /**/($a) /**/{ //
/**/
/**/return/**/1/**/;
//
}/**/ /**/
/**/
//
/**/
',
'<?php
if /**/($a) /**/{ //
/**/
/**/return/**/1/**/;
//
}/**/ else /**/{
/**/
//
}/**/
',
];
yield [
'<?php
if ($a) {
if ($b) {
if ($c) {
} elseif ($d) {
return;
} //
//
return;
} //
//
return;
} //
//
',
'<?php
if ($a) {
if ($b) {
if ($c) {
} elseif ($d) {
return;
} else {//
}//
return;
} else {//
}//
return;
} else {//
}//
',
];
yield [
'<?php
if ($a0) {
//
} else {
echo 0;
}
',
];
yield [
'<?php
if (false)
echo "a";
else
echo "a";
',
];
yield [
'<?php if($a2){;} else {echo 27;}',
];
yield [
'<?php if ($a3) {test();} else {echo 3;}',
];
yield [
'<?php if ($a4) {$b = function () {};} else {echo 4;}',
];
yield [
'<?php if ($a5) {$b = function () use ($a){};} else {echo 5;}',
];
yield [
'<?php
if ($a) {
if ($b) return;
} else {
echo 1;
}
',
];
yield [
'<?php
if ($a) {
if ($b) throw new \Exception();
} else {
echo 1;
}
',
];
yield [
'<?php
if ($a) {
if ($b) { throw new \Exception(); }
} else {
echo 1;
}
',
];
yield [
'<?php
$a = true; // 6
if (true === $a)
$b = true === $a ? 1 : die;
else
echo 40;
echo "end";
',
];
yield [
'<?php
$a = true; // 6
if (true === $a)
$b = true === $a ? 1 : exit(1);
else
echo 40;
echo "end";
',
];
yield [
'<?php
$a = true; // 6
if (true === $a)
$b = true === $a ? 1 : exit(1);
else
echo 4;
echo "end";
',
];
yield [
'<?php
if (false)
die;
elseif (true)
if(true)echo 777;else die;
else if (true)
die;
elseif (false)
die;
else
echo 7;
',
];
yield [
'<?php
$tmp = function($b){$b();};
$a =1;
return $tmp(function () use ($a) {
if ($a) {
$a++;
} else {
$a--;
}
});
',
];
yield [
'<?php
$tmp = function($b){$b();};
$a =1;
return $tmp(function () use ($a) {
if ($a) {
$a++;
} elseif($a > 2) {
return 1;
} else {
$a--;
}
});
',
];
yield [
'<?php
return function() {
if (false) {
} elseif (3 > 2) {
} else {
echo 1;
}
};',
];
yield [
'<?php
return function() {
if (false) {
return 1;
} elseif (3 > 2) {
} else {
echo 1;
}
};',
];
$statements = [
'die;',
'throw new Exception($i);',
'while($i < 1) throw/*{}*/new Exception($i);',
'while($i < 1){throw new Exception($i);}',
'do{throw new Exception($i);}while($i < 1);',
'foreach($a as $b)throw new Exception($i);',
'foreach($a as $b){throw new Exception($i);}',
];
foreach ($statements as $statement) {
yield from self::generateConditionsWithoutBracesCase($statement);
}
yield [
'<?php
if ($a === false)
{
if ($v) { $ret = "foo"; if($d){return 1;}echo $a;}
}
else
$ret .= $value;
return $ret;',
'<?php
if ($a === false)
{
if ($v) { $ret = "foo"; if($d){return 1;}else{echo $a;}}
}
else
$ret .= $value;
return $ret;',
];
yield from self::generateConditionsWithoutBracesCase('throw new class extends Exception{};');
yield from self::generateConditionsWithoutBracesCase('throw new class ($a, 9) extends Exception{ public function z($a, $b){ echo 7;} };');
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFix80Cases(): iterable
{
$cases = [
'$bar = $foo1 ?? throw new \Exception($e);',
'$callable = fn() => throw new Exception();',
'$value = $falsableValue ?: throw new InvalidArgumentException();',
'$value = !empty($array)
? reset($array)
: throw new InvalidArgumentException();',
'$a = $condition && throw new Exception();',
'$a = $condition || throw new Exception();',
'$a = $condition and throw new Exception();',
'$a = $condition or throw new Exception();',
];
$template = '<?php
if ($foo) {
%s
} else {
echo 123;
}
';
foreach ($cases as $index => $case) {
yield \sprintf('PHP8 Negative case %d', $index) => [\sprintf($template, $case)];
}
yield from self::generateConditionsWithoutBracesCase('$b = $a ?? throw new Exception($i);');
}
/**
* @param list<int> $expected
*
* @dataProvider provideBlockDetectionCases
*/
public function testBlockDetection(array $expected, string $source, int $index): void
{
Tokens::clearCache();
$tokens = Tokens::fromCode($source);
$result = \Closure::bind(static fn (AbstractNoUselessElseFixer $fixer): array => $fixer->getPreviousBlock($tokens, $index), null, AbstractNoUselessElseFixer::class)($this->fixer);
self::assertSame($expected, $result);
}
/**
* @return iterable<int, array{list<int>, string, int}>
*/
public static function provideBlockDetectionCases(): iterable
{
$source = '<?php
if ($a)
echo 1;
elseif ($a) ///
echo 2;
else if ($b) /**/ echo 3;
else
echo 4;
';
yield [[2, 11], $source, 13];
yield [[13, 24], $source, 26];
yield [[26, 39], $source, 41];
$source = '<?php
if ($a) {
if ($b) {
}
echo 1;
} elseif (true) {
echo 2;
} else if (false) {
echo 3;
} elseif ($c) {
echo 4;
} else
echo 1;
';
yield [[2, 25], $source, 27];
yield [[27, 40], $source, 42];
yield [[59, 72], $source, 74];
}
/**
* @param array<int, bool> $indexes
*
* @dataProvider provideIsInConditionWithoutBracesCases
*/
public function testIsInConditionWithoutBraces(array $indexes, string $input): void
{
$tokens = Tokens::fromCode($input);
foreach ($indexes as $index => $expected) {
self::assertSame(
$expected,
\Closure::bind(static fn (AbstractNoUselessElseFixer $fixer): bool => $fixer->isInConditionWithoutBraces($tokens, $index, 0), null, AbstractNoUselessElseFixer::class)($this->fixer),
\sprintf('Failed in condition without braces check for index %d', $index),
);
}
}
/**
* @return iterable<int, array{array<int, bool>, string}>
*/
public static function provideIsInConditionWithoutBracesCases(): iterable
{
yield [
[
18 => false, // return
25 => false, // return
36 => false, // return
],
'<?php
if ($x) {
if ($y) {
return 1;
}
return 2;
} else {
return 3;
}
',
];
yield [
[
0 => false,
29 => false, // throw
],
'<?php
if ($v) { $ret = "foo"; }
else
if($a){}else{throw new Exception($i);}
',
];
yield [
[
0 => false,
38 => true, // throw
],
'<?php
if ($v) { $ret = "foo"; }
else
for($i =0;$i < 1;++$i) throw new Exception($i);
',
];
yield [
[
0 => false,
26 => true, // throw
28 => true, // new
30 => true, // Exception
],
'<?php
if ($v) { $ret = "foo"; }
else
while(false){throw new Exception($i);}
',
];
yield [
[
0 => false,
30 => true, // throw
32 => true, // new
34 => true, // Exception
],
'<?php
if ($v) { $ret = "foo"; }
else
foreach($a as $b){throw new Exception($i);}
',
];
yield [
[
0 => false,
25 => true, // throw
27 => true, // new
29 => true, // Exception
],
'<?php
if ($v) { $ret = "foo"; }
else
while(false)throw new Exception($i);
',
];
yield [
[
26 => true, // throw
],
'<?php
if ($v) { $ret = "foo"; }
elseif($a)
do{throw new Exception($i);}while(false);
',
];
yield [
[
4 => false, // 1
13 => true, // if (2nd)
21 => true, // true
33 => true, // while
43 => false, // echo
45 => false, // 2
46 => false, // ;
51 => false, // echo (123)
],
'<?php
echo 1;
if ($a) if ($a) while(true)echo 1;
elseif($c) while(true){if($d){echo 2;}};
echo 123;
',
];
yield [
[
2 => false, // echo
13 => true, // echo
15 => true, // 2
20 => true, // die
23 => false, // echo
],
'<?php
echo 1;
if ($a) echo 2;
else die; echo 3;
',
];
yield [
[
8 => true, // die
9 => true, // /**/
15 => true, // die
],
'<?php
if ($a)
die/**/;
else
/**/die/**/;#
',
];
yield [
[
8 => true, // die
9 => true, // /**/
15 => true, // die
],
'<?php
if ($a)
die/**/;
else
/**/die/**/?>
',
];
}
/**
* @return iterable<array{0: non-empty-string, 1?: non-empty-string}>
*/
private static function generateConditionsWithoutBracesCase(string $statement): iterable
{
$ifTemplate = '<?php
if ($a === false)
{
if ($v) %s
}
else
$ret .= $value;
return $ret;';
$ifElseIfTemplate = '<?php
if ($a === false)
{
if ($v) { $ret = "foo"; }
elseif($a)
%s
}
else
$ret .= $value;
return $ret;';
$ifElseTemplate = '<?php
if ($a === false)
{
if ($v) { $ret = "foo"; }
else
%s
}
else
$ret .= $value;
return $ret;';
yield [\sprintf($ifTemplate, $statement)];
yield [\sprintf($ifElseTemplate, $statement)];
yield [\sprintf($ifElseIfTemplate, $statement)];
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
private static function generateCases(string $expected, ?string $input = null): iterable
{
$cases = [];
foreach ([
'exit;',
'exit();',
'exit(1);',
'die;',
'die();',
'die(1);',
'break;',
'break 2;',
'break (2);',
'continue;',
'continue 2;',
'continue (2);',
'return;',
'return 1;',
'return (1);',
'return "a";',
'return 8+2;',
'return null;',
'return sum(1+8*6, 2);',
'throw $e;',
'throw ($e);',
'throw new \Exception;',
'throw new \Exception();',
'throw new \Exception((string)12+1);',
] as $case) {
if (null === $input) {
$cases[] = [\sprintf($expected, $case)];
$cases[] = [\sprintf($expected, strtoupper($case))];
if ($case !== strtolower($case)) {
$cases[] = [\sprintf($expected, strtolower($case))];
}
} else {
$cases[] = [\sprintf($expected, $case), \sprintf($input, $case)];
$cases[] = [\sprintf($expected, strtoupper($case)), \sprintf($input, strtoupper($case))];
if ($case !== strtolower($case)) {
$cases[] = [\sprintf($expected, strtolower($case)), \sprintf($input, strtolower($case))];
}
}
}
return $cases;
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/ControlStructureBracesFixerTest.php | tests/Fixer/ControlStructure/ControlStructureBracesFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\ControlStructureBracesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\ControlStructureBracesFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ControlStructureBracesFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'if' => [
'<?php if ($foo) { foo(); }',
'<?php if ($foo) foo();',
];
yield 'else' => [
'<?php
if ($foo) { foo(); }
else { bar(); }',
'<?php
if ($foo) { foo(); }
else bar();',
];
yield 'elseif' => [
'<?php
if ($foo) { foo(); }
elseif ($bar) { bar(); }',
'<?php
if ($foo) { foo(); }
elseif ($bar) bar();',
];
yield 'else if' => [
'<?php
if ($foo) { foo(); }
else if ($bar) { bar(); }',
'<?php
if ($foo) { foo(); }
else if ($bar) bar();',
];
yield 'for' => [
'<?php for (;;) { foo(); }',
'<?php for (;;) foo();',
];
yield 'foreach' => [
'<?php foreach ($foo as $bar) { foo(); }',
'<?php foreach ($foo as $bar) foo();',
];
yield 'while' => [
'<?php while ($foo) { foo(); }',
'<?php while ($foo) foo();',
];
yield 'do while' => [
'<?php
do { foo(); }
while ($foo);',
'<?php
do foo();
while ($foo);',
];
yield 'empty if' => [
'<?php if ($foo);',
];
yield 'empty else' => [
'<?php
if ($foo) { foo(); }
else;',
];
yield 'empty elseif' => [
'<?php
if ($foo) { foo(); }
elseif ($bar);',
];
yield 'empty else if' => [
'<?php
if ($foo) { foo(); }
else if ($bar);',
];
yield 'empty for' => [
'<?php for (;;);',
];
yield 'empty foreach' => [
'<?php foreach ($foo as $bar);',
];
yield 'empty while' => [
'<?php while ($foo);',
];
yield 'empty do while' => [
'<?php do; while ($foo);',
];
yield 'nested if using alternative syntax' => [
'<?php if ($foo) { if ($bar): ?> foo <?php endif; } ?>',
'<?php if ($foo) if ($bar): ?> foo <?php endif; ?>',
];
yield 'nested for using alternative syntax' => [
'<?php if ($foo) { for (;;): ?> foo <?php endfor; } ?>',
'<?php if ($foo) for (;;): ?> foo <?php endfor; ?>',
];
yield 'nested foreach using alternative syntax' => [
'<?php if ($foo) { foreach ($foo as $bar): ?> foo <?php endforeach; } ?>',
'<?php if ($foo) foreach ($foo as $bar): ?> foo <?php endforeach; ?>',
];
yield 'nested while using alternative syntax' => [
'<?php if ($foo) { while ($foo): ?> foo <?php endwhile; } ?>',
'<?php if ($foo) while ($foo): ?> foo <?php endwhile; ?>',
];
yield 'nested switch using alternative syntax' => [
'<?php if ($foo) { switch ($foo): case 1: ?> foo <?php endswitch; } ?>',
'<?php if ($foo) switch ($foo): case 1: ?> foo <?php endswitch; ?>',
];
yield 'declare followed by closing tag' => [
'<?php declare(strict_types=1) ?>',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/NoTrailingCommaInListCallFixerTest.php | tests/Fixer/ControlStructure/NoTrailingCommaInListCallFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\NoTrailingCommaInListCallFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoTrailingCommaInListCallFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoTrailingCommaInListCallFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
list($a, $b) = foo();
list($a, , $c, $d) = foo();
list($a, , $c) = foo();
list($a) = foo();
list($a , $b) = foo();
list($a, /* $b */, $c) = foo();
',
'<?php
list($a, $b) = foo();
list($a, , $c, $d, ) = foo();
list($a, , $c, , ) = foo();
list($a, , , , , ) = foo();
list($a , $b , ) = foo();
list($a, /* $b */, $c, ) = foo();
',
];
yield [
'<?php
list(
$a#
,#
#
) = $a;',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/NoBreakCommentFixerTest.php | tests/Fixer/ControlStructure/NoBreakCommentFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\NoBreakCommentFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoBreakCommentFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\NoBreakCommentFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoBreakCommentFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = [], ?WhitespacesFixerConfig $whitespacesConfig = null): void
{
$this->fixer->configure($configuration);
$this->fixer->setWhitespacesConfig($whitespacesConfig ?? new WhitespacesFixerConfig());
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration, 3?: WhitespacesFixerConfig}>
*/
public static function provideFixCases(): iterable
{
yield from self::getFixCases();
$differentCommentTextCases = self::getFixCases();
$replaceCommentText = static fn (string $php): string => strtr($php, [
'No break' => 'Fall-through case!',
'no break' => 'fall-through case!',
]);
foreach ($differentCommentTextCases as $case) {
$case[0] = $replaceCommentText($case[0]);
if (isset($case[1])) {
$case[1] = $replaceCommentText($case[1]);
}
yield [
$case[0],
$case[1] ?? null,
['comment_text' => 'fall-through case!'],
];
}
yield [
'<?php
switch ($foo) {
case 1:
foo();
// no break
// fall-through case!
case 2:
bar();
// no break
// fall-through case!
default:
baz();
}',
'<?php
switch ($foo) {
case 1:
foo();
// no break
case 2:
bar();
// no break
default:
baz();
}',
['comment_text' => 'fall-through case!'],
];
foreach (self::getFixCases() as $case) {
$case[0] = str_replace("\n", "\r\n", $case[0]);
if (isset($case[1])) {
$case[1] = str_replace("\n", "\r\n", $case[1]);
}
yield [
$case[0],
$case[1] ?? null,
[],
new WhitespacesFixerConfig(' ', "\r\n"),
];
}
yield 'with comment text with special regexp characters' => [
'<?php
switch ($foo) {
case 1:
foo();
// ~***(//[No break here.]\\\)***~
case 2:
bar();
// ~***(//[No break here.]\\\)***~
default:
baz();
}',
'<?php
switch ($foo) {
case 1:
foo();
// ~***(//[No break here.]\\\)***~
case 2:
bar();
default:
baz();
}',
['comment_text' => '~***(//[No break here.]\\\)***~'],
];
yield 'with comment text with trailing spaces' => [
'<?php
switch ($foo) {
case 1:
foo();
// no break
default:
baz();
}',
'<?php
switch ($foo) {
case 1:
foo();
default:
baz();
}',
['comment_text' => 'no break '],
];
}
/**
* @param array<string, mixed> $configuration
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(array $configuration, string $expectedExceptionMessage): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches($expectedExceptionMessage);
$this->fixer->configure($configuration);
}
/**
* @return iterable<int, array{array<string, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield [
['comment_text' => "No\nbreak"],
'/^\[no_break_comment\] Invalid configuration: The comment text must not contain new lines\.$/',
];
yield [
['comment_text' => "No\r\nbreak"],
'/^\[no_break_comment\] Invalid configuration: The comment text must not contain new lines\.$/',
];
yield [
['comment_text' => "No\rbreak"],
'/^\[no_break_comment\] Invalid configuration: The comment text must not contain new lines\.$/',
];
yield [
['foo' => true],
'/^\[no_break_comment\] Invalid configuration: The option "foo" does not exist\. Defined options are: "comment_text"\.$/',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php
switch ($foo) {
case 1:
foo() ?? throw new \Exception();
// no break
case 2:
$a = $condition and throw new Exception();
// no break
case 3:
$callable = fn() => throw new Exception();
// no break
case 4:
$value = $falsableValue ?: throw new InvalidArgumentException();
// no break
default:
echo "PHP8";
}
',
'<?php
switch ($foo) {
case 1:
foo() ?? throw new \Exception();
case 2:
$a = $condition and throw new Exception();
case 3:
$callable = fn() => throw new Exception();
case 4:
$value = $falsableValue ?: throw new InvalidArgumentException();
default:
echo "PHP8";
}
',
];
yield [
'<?php
match ($foo) {
1 => "a",
default => "b"
};
match ($bar) {
2 => "c",
default => "d"
};
match ($baz) {
3 => "e",
default => "f"
};
',
];
yield 'switch with break and nested match' => [
'<?php switch ($value) {
case 1:
$x = match (true) {
default => 2
};
break;
default:
$x = 3;
}',
];
yield 'switch without break and nested match' => [
'<?php switch ($value) {
case 1:
$x = match (true) {
default => 2
};
// no break
default:
$x = 3;
}',
'<?php switch ($value) {
case 1:
$x = match (true) {
default => 2
};
default:
$x = 3;
}',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'enums' => [
'<?php
enum Suit {
case Hearts;
case Diamonds;
case Clubs;
case Spades;
}
enum UserStatus: string {
case Pending = \'P\';
case Active = \'A\';
case Suspended = \'S\';
case CanceledByUser = \'C\';
}
switch($a) { // pass the `is candidate` check
case 1:
echo 1;
break;
}
',
];
yield 'enum with function having return type and switch in that function' => [
<<<'PHP'
<?php enum Foo: int
{
case C1 = 1;
case C2 = 2;
case C3 = 3;
public static function f(string $s): self
{
switch ($s) {
case 'a':
case 'b':
return self::C1;
case 'c':
return self::C2;
case 'd':
case 'e':
return self::C3;
default:
throw new Exception();
}
}
}
PHP,
];
yield 'enum with exit' => [
<<<'PHP'
<?php
enum MyEnum: string
{
case A = 'a';
public static function forHost(string $host): self
{
switch ($host) {
case 'hub.a':
case 'hub.b':
exit(1);
default:
throw new Exception('Unknown host: ' . $host);
}
}
}
PHP,
];
yield 'enum with continue' => [
<<<'PHP'
<?php
enum MyEnum: string
{
case A = 'a';
public static function forHost(array $hosts): self
{
foreach($hosts as $host) {
switch ($host) {
case 'hub.a':
case 'hub.b':
continue 2;
default:
throw new Exception('Unknown host: ' . $host);
}
}
}
}
PHP,
];
yield 'enum with break' => [
<<<'PHP'
<?php
enum MyEnum: string
{
case A = 'a';
public static function forHost(array $hosts): self
{
foreach($hosts as $host) {
switch ($host) {
case 'hub.a':
case 'hub.b':
break 2;
default:
throw new Exception('Unknown host: ' . $host);
}
}
}
}
PHP,
];
yield 'enum with throw' => [
<<<'PHP'
<?php
enum MyEnum: string
{
case A = 'a';
public static function forHost(array $hosts): self
{
foreach($hosts as $host) {
switch ($host) {
case 'hub.a':
case 'hub.b':
throw new RuntimeException('boo');
default:
throw new Exception('Unknown host: ' . $host);
}
}
}
}
PHP,
];
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
private static function getFixCases(): iterable
{
yield [
'<?php
switch ($foo) {
case 1:
foo();
// no break
case 2:
bar();
// no break
default:
baz();
}',
'<?php
switch ($foo) {
case 1:
foo();
case 2:
bar();
default:
baz();
}',
];
yield [
'<?php
switch ($foo) {
case 1:
foo();
// no break
case 2:
bar();
// no break
default:
baz();
}',
'<?php
switch ($foo) {
case 1:
foo();
case 2:
bar();
default:
baz();
}',
];
yield [
'<?php
switch ($foo) {
case 1:
foo();
// no break
case 2:
bar();
// no break
default:
baz();
}',
'<?php
switch ($foo) {
case 1:
foo(); // no break
case 2:
bar(); // no break
default:
baz();
}',
];
yield [
'<?php
switch ($foo) {
case 1;
foo();
// no break
case 2;
bar();
// no break
default;
baz();
}',
'<?php
switch ($foo) {
case 1;
foo();
case 2;
bar();
default;
baz();
}',
];
yield [
'<?php
switch ($foo) {
case 1:
foo();
// foo
// no break
case 2:
bar();
}',
'<?php
switch ($foo) {
case 1:
foo();
// foo
case 2:
bar();
}',
];
yield [
'<?php
switch ($foo) { case 1: foo();
// no break
case 2: bar(); }',
'<?php
switch ($foo) { case 1: foo(); case 2: bar(); }',
];
yield [
'<?php
switch ($foo) { case 1: foo();
// no break
case 2: bar(); }',
'<?php
switch ($foo) { case 1: foo();case 2: bar(); }',
];
yield [
'<?php
switch ($foo) {
case 1;
foreach ($bar as $baz) {
break;
}
// no break
case 2;
bar();
}',
'<?php
switch ($foo) {
case 1;
foreach ($bar as $baz) {
break;
}
case 2;
bar();
}',
];
yield [
'<?php
switch ($foo) {
case 1;
for ($i = 0; $i < 1; ++$i) {
break;
}
// no break
case 2;
bar();
}',
'<?php
switch ($foo) {
case 1;
for ($i = 0; $i < 1; ++$i) {
break;
}
case 2;
bar();
}',
];
yield [
'<?php
switch ($foo) {
case 1;
if ($foo) {
break;
}
// no break
case 2;
bar();
}',
'<?php
switch ($foo) {
case 1;
if ($foo) {
break;
}
case 2;
bar();
}',
];
yield [
'<?php
switch ($foo) {
case 1;
do {
break;
} while ($bar);
// no break
case 2;
bar();
}',
'<?php
switch ($foo) {
case 1;
do {
break;
} while ($bar);
case 2;
bar();
}',
];
yield [
'<?php
switch ($foo) {
case 1;
$foo = function ($bar) {
foreach ($bar as $baz) {
break;
}
};
// no break
case 2;
bar();
}',
'<?php
switch ($foo) {
case 1;
$foo = function ($bar) {
foreach ($bar as $baz) {
break;
}
};
case 2;
bar();
}',
];
yield [
<<<'PHP'
<?php
switch ($foo) {
case 1;
$foo = function ($bar): void {
foreach ($bar as $baz) {
break;
}
};
// no break
case 2;
bar();
}
PHP,
<<<'PHP'
<?php
switch ($foo) {
case 1;
$foo = function ($bar): void {
foreach ($bar as $baz) {
break;
}
};
case 2;
bar();
}
PHP,
];
yield [
'<?php
switch ($foo) {
case 1:
switch ($bar) {
case 1:
foo();
// no break
case 2:
bar();
}
break;
case 2:
switch ($bar) {
case 1:
bar();
// no break
case 2:
foo();
}
}',
'<?php
switch ($foo) {
case 1:
switch ($bar) {
case 1:
foo();
case 2:
bar();
}
break;
case 2:
switch ($bar) {
case 1:
bar();
case 2:
foo();
}
}',
];
yield [
'<?php
switch ($foo) {
case 1:
switch ($bar):
case 1:
foo();
// no break
case 2:
bar();
endswitch;
break;
case 2:
switch ($bar):
case 1:
bar();
// no break
case 2:
foo();
endswitch;
}',
'<?php
switch ($foo) {
case 1:
switch ($bar):
case 1:
foo();
case 2:
bar();
endswitch;
break;
case 2:
switch ($bar):
case 1:
bar();
case 2:
foo();
endswitch;
}',
];
yield [
'<?php
switch ($foo) {
case 1:
foo();
continue;
case 2:
bar();
continue;
default:
baz();
}',
'<?php
switch ($foo) {
case 1:
foo();
// no break
continue;
case 2:
bar();
// no break
continue;
default:
baz();
}',
];
yield [
'<?php
switch ($foo) {
case 1:
return foo();
case 2:
return bar();
default:
return baz();
}',
'<?php
switch ($foo) {
case 1:
return foo();
// no break
case 2:
return bar();
// no break
default:
return baz();
}',
];
yield [
'<?php
switch ($foo) {
case 1:
return foo();
case 2:
return bar();
default:
return baz();
}',
'<?php
switch ($foo) {
case 1:
// no break
return foo();
case 2:
// no break
return bar();
default:
return baz();
}',
];
yield [
'<?php
switch ($foo) {
case 1:
foo();
break;
case 2:
bar();
break;
default:
baz();
}',
'<?php
switch ($foo) {
case 1:
foo();
// no break
break;
case 2:
bar();
// no break
break;
default:
baz();
}',
];
yield [
'<?php
switch ($foo) {
case 1:
foo();
break;
case 2:
bar();
break;
case 21:
bar();
break;
case 22:
bar();
break;
case 23:
bar();
break;
case 24:
bar();
break;
case 3:
baz();
break;
default:
qux();
}',
'<?php
switch ($foo) {
case 1:
foo();
# no break
break;
case 2:
bar();
/* no break */
break;
case 21:
bar();
/*no break*/
break;
case 22:
bar();
/* no break */
break;
case 23:
bar();
/*no break */
break;
case 24:
bar();
/* no break*/
break;
case 3:
baz();
/** no break */
break;
default:
qux();
}',
];
yield [
'<?php
switch ($foo) {
case 1:
case 2:
bar();
break;
default:
baz();
}',
'<?php
switch ($foo) {
case 1:
// no break
case 2:
bar();
// no break
break;
default:
baz();
}',
];
yield [
'<?php
switch ($foo) {
case 1:
foo();
}',
'<?php
switch ($foo) {
case 1:
foo();
// no break
}',
];
yield [
'<?php
switch ($foo) {
default:
foo();
}',
'<?php
switch ($foo) {
default:
foo();
// no break
}',
];
yield [
'<?php switch ($foo) { case 1: switch ($bar) { case 1: switch ($baz) { case 1: $foo = 1;
// no break
case 2: $foo = 2; }
// no break
case 2: switch ($baz) { case 1: $foo = 3;
// no break
case 2: $foo = 4; } }
// no break
case 2: switch ($bar) { case 1: switch ($baz) { case 1: $foo = 5;
// no break
case 2: $foo = 6; }
// no break
case 2: switch ($baz) { case 1: $foo = 7;
// no break
case 2: $foo = 8; } } }',
'<?php switch ($foo) { case 1: switch ($bar) { case 1: switch ($baz) { case 1: $foo = 1; case 2: $foo = 2; } case 2: switch ($baz) { case 1: $foo = 3; case 2: $foo = 4; } } case 2: switch ($bar) { case 1: switch ($baz) { case 1: $foo = 5; case 2: $foo = 6; } case 2: switch ($baz) { case 1: $foo = 7; case 2: $foo = 8; } } }',
];
yield [
'<?php
switch ($foo):
case 1:
foo();
// no break
case 2:
bar();
endswitch;',
'<?php
switch ($foo):
case 1:
foo();
case 2:
bar();
// no break
endswitch;',
];
yield [
'<?php
switch ($foo) {
case 1:
?>foo<?php
// no break
case 2:
?>bar<?php
break;
}',
'<?php
switch ($foo) {
case 1:
?>foo<?php
case 2:
?>bar<?php
// no break
break;
}',
];
yield [
'<?php
switch ($foo) {
case 1:
?>foo<?php
// no break
case 2:
?>bar<?php
break;
}',
'<?php
switch ($foo) {
case 1:
?>foo<?php
case 2:
?>bar<?php
// no break
break;
}',
];
yield [
'<?php
switch ($foo) {
case 1:
?>foo<?php // foo
// no break
case 2:
?>bar<?php // bar
break;
}',
'<?php
switch ($foo) {
case 1:
?>foo<?php // foo
case 2:
?>bar<?php // bar
// no break
break;
}',
];
yield [
'<?php
switch ($foo) {
case 1:
foo();
// no break
case 2:
bar();
// no break
default:
baz();
}',
'<?php
switch ($foo) {
case 1:
// no break
foo();
case 2:
// no break
bar();
default:
baz();
}',
];
yield [
'<?php
switch ($foo) {
case 1:
die;
case 2:
exit;
default:
die;
}',
'<?php
switch ($foo) {
case 1:
// no break
die;
case 2:
// no break
exit;
default:
die;
}',
];
yield [
'<?php
switch ($foo) {
case 1: {
throw new \Exception();
}
case 2:
?>
<?php
throw new \Exception();
default:
throw new \Exception();
}',
'<?php
switch ($foo) {
case 1: {
// no break
throw new \Exception();
}
case 2:
?>
<?php
// no break
throw new \Exception();
default:
throw new \Exception();
}',
];
yield [
'<?php
switch ($foo) {
case 1:
goto a;
case 2:
goto a;
default:
goto a;
}
a:
echo \'foo\';',
'<?php
switch ($foo) {
case 1:
// no break
goto a;
case 2:
// no break
goto a;
default:
goto a;
}
a:
echo \'foo\';',
];
yield [
'<?php
switch ($foo) {
case "bar":
if (1) {
} else {
}
$aaa = new Bar();
break;
default:
$aaa = new Baz();
}',
];
yield [
'<?php
switch ($foo) {
case 1:
?>
<?php
// no break
default:
?>
<?php
}',
'<?php
switch ($foo) {
case 1:
?>
<?php
default:
?>
<?php
}',
];
yield [
'<?php
switch ($foo) {
case 1:
?>
<?php
// no break
default:
?>
<?php }',
'<?php
switch ($foo) {
case 1:
?>
<?php default:
?>
<?php }',
];
yield [
'<?php
switch ($foo) {
case 1:
foo();
// no break
case 2:
bar();
}',
'<?php
switch ($foo) {
case 1:
foo();
// No break
case 2:
bar();
}',
];
yield [
'<?php
switch ($a) {
case 1:
throw new \Exception("");
case 2;
throw new \Exception("");
case 3:
throw new \Exception("");
case 4;
throw new \Exception("");
case 5:
throw new \Exception("");
case 6;
throw new \Exception("");
}
',
];
yield [
'<?php
switch ($f) {
case 1:
if ($a) {
return "";
}
throw new $f();
case Z:
break;
}',
];
yield [
'<?php
switch ($foo) {
case 1;
$foo = new class {
public function foo($bar)
{
foreach ($bar as $baz) {
break;
}
}
};
// no break
case 2;
bar();
}',
'<?php
switch ($foo) {
case 1;
$foo = new class {
public function foo($bar)
{
foreach ($bar as $baz) {
break;
}
}
};
case 2;
bar();
}',
];
yield [
'<?php
switch ($foo) {
case 1;
$foo = new class(1) {
public function foo($bar)
{
foreach ($bar as $baz) {
break;
}
}
};
// no break
case 2;
bar();
}',
'<?php
switch ($foo) {
case 1;
$foo = new class(1) {
public function foo($bar)
{
foreach ($bar as $baz) {
break;
}
}
};
case 2;
bar();
}',
];
yield [
'<?php
switch($a) {
case 1:
$a = function () { throw new \Exception(""); };
// no break
case 2:
$a = new class(){
public function foo () { throw new \Exception(""); }
};
// no break
case 3:
echo 5;
// no break
default:
echo 1;
}
',
'<?php
switch($a) {
case 1:
$a = function () { throw new \Exception(""); };
case 2:
$a = new class(){
public function foo () { throw new \Exception(""); }
};
case 3:
echo 5;
default:
echo 1;
}
',
];
yield [
'<?php
switch ($foo) {
case 10:
echo 1;
/* no break because of some more details stated here */
case 22:
break;
}',
];
yield [
'<?php
switch ($foo) {
case 10:
echo 1;
# no break because of some more details stated here */
case 22:
break;
}',
];
yield [
'<?php
switch ($foo) {
case 100:
echo 10;
/* no breaking windows please */
// no break
case 220:
break;
}',
'<?php
switch ($foo) {
case 100:
echo 10;
/* no breaking windows please */
case 220:
break;
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/EmptyLoopBodyFixerTest.php | tests/Fixer/ControlStructure/EmptyLoopBodyFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\EmptyLoopBodyFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\EmptyLoopBodyFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\EmptyLoopBodyFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class EmptyLoopBodyFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
if ([] === $configuration) {
$this->doTest($expected, $input);
$this->fixer->configure(['style' => 'braces']);
if (null === $input) {
$this->doTest($expected, $input);
} else {
$this->doTest($input, $expected);
}
} else {
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'simple "while"' => [
'<?php while(foo());',
'<?php while(foo()){}',
];
yield 'simple "for"' => [
'<?php for($i = 0;foo();++$i);',
'<?php for($i = 0;foo();++$i){}',
];
yield 'simple "foreach"' => [
'<?php foreach (Foo() as $f);',
'<?php foreach (Foo() as $f){}',
];
yield '"while" followed by "do-while"' => [
'<?php while(foo(static function(){})); do{ echo 1; }while(bar());',
'<?php while(foo(static function(){})){} do{ echo 1; }while(bar());',
];
yield 'empty "while" after "if"' => [
'<?php
if ($foo) {
echo $bar;
} while(foo());
',
'<?php
if ($foo) {
echo $bar;
} while(foo()){}
',
];
yield 'nested and mixed loops' => [
'<?php
do {
while($foo()) {
while(B()); // fix
for($i = 0;foo();++$i); // fix
for($i = 0;foo();++$i) {
foreach (Foo() as $f); // fix
}
}
} while(foo());
',
'<?php
do {
while($foo()) {
while(B()){} // fix
for($i = 0;foo();++$i){} // fix
for($i = 0;foo();++$i) {
foreach (Foo() as $f){} // fix
}
}
} while(foo());
',
];
yield 'not empty "while"' => [
'<?php while(foo()){ bar(); };',
];
yield 'not empty "for"' => [
'<?php for($i = 0; foo(); ++$i){ bar(); }',
];
yield 'not empty "foreach"' => [
'<?php foreach (foo() as $f){ echo 1; }',
];
yield 'test with lot of space' => [
'<?php while (foo1())
;
echo 1;
',
'<?php while (foo1())
{
}
echo 1;
',
['style' => 'semicolon'],
];
yield 'empty "foreach" with comment' => [
'<?php foreach (Foo() as $f) {
// $this->add($f);
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/SwitchCaseSemicolonToColonFixerTest.php | tests/Fixer/ControlStructure/SwitchCaseSemicolonToColonFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\SwitchCaseSemicolonToColonFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\SwitchCaseSemicolonToColonFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SwitchCaseSemicolonToColonFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{string, string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
switch (1) {
case f(function () { return; }):
break;
}
',
'<?php
switch (1) {
case f(function () { return; });
break;
}
',
];
yield [
'<?php
switch ($a) {
case 42:
break;
}
',
'<?php
switch ($a) {
case 42;
break;
}
',
];
yield [
'<?php
switch ($a) {
case ["foo" => "bar"]:
break;
}
',
'<?php
switch ($a) {
case ["foo" => "bar"];
break;
}
',
];
yield [
'<?php
switch ($a) {
case 42:
break;
case 1:
switch ($a) {
case 42:
break;
default :
echo 1;
}
}',
'<?php
switch ($a) {
case 42;
break;
case 1:
switch ($a) {
case 42;
break;
default ;
echo 1;
}
}',
];
yield [
'<?php
switch ($a) {
case 42:;;// NoEmptyStatementFixer should clean this up (partly)
break;
}
',
'<?php
switch ($a) {
case 42;;;// NoEmptyStatementFixer should clean this up (partly)
break;
}
',
];
yield [
'<?php
switch ($a) {
case $b ? "c" : "d" :
break;
}
',
'<?php
switch ($a) {
case $b ? "c" : "d" ;
break;
}
',
];
yield [
'<?php
switch ($a) {
case $b ? "c" : "d": break;
}
',
'<?php
switch ($a) {
case $b ? "c" : "d"; break;
}
',
];
yield [
'<?php
switch($a) {
case (int) $a < 1: {
echo "leave ; alone";
break;
}
case ($a < 2)/* test */ : {
echo "fix 1";
break;
}
case (3):{
echo "fix 2";
break;
}
case /**/(/**/ // test
4
/**/)//
/**/: {
echo "fix 3";
break;
}
case (((int)$b) + 4.1) : {
echo "fix 4";
break;
}
case ($b + 1) * 2 : {;;
echo "leave alone";
break;
}
}
',
'<?php
switch($a) {
case (int) $a < 1; {
echo "leave ; alone";
break;
}
case ($a < 2)/* test */ ; {
echo "fix 1";
break;
}
case (3);{
echo "fix 2";
break;
}
case /**/(/**/ // test
4
/**/)//
/**/; {
echo "fix 3";
break;
}
case (((int)$b) + 4.1) ; {
echo "fix 4";
break;
}
case ($b + 1) * 2 ; {;;
echo "leave alone";
break;
}
}
',
];
yield 'nested switch in switch case' => [
'<?php
switch (1) {
case new class {public function A(){echo 1;switch(time()){case 1: echo 2;}}}:break;}
',
'<?php
switch (1) {
case new class {public function A(){echo 1;switch(time()){case 1; echo 2;}}};break;}
',
];
yield [
'<?php
switch (1) {
case $b ? f(function () { return; }) : new class {public function A(){echo 1;}} :
break;
}
',
'<?php
switch (1) {
case $b ? f(function () { return; }) : new class {public function A(){echo 1;}} ;
break;
}
',
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield [
'<?php
switch ($a) {
case $b ? "c" : "this" ? "is" : "ugly":
break;
}
',
'<?php
switch ($a) {
case $b ? "c" : "this" ? "is" : "ugly";
break;
}
',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'Simple match' => [
'<?php
echo match ($a) {
default => "foo",
};
',
];
yield 'Match in switch' => [
'<?php
switch ($foo) {
case "bar":
echo match ($a) {
default => "foo",
};
break;
}
',
];
yield 'Match in case value' => [
'<?php
switch ($foo) {
case match ($bar) {
default => "foo",
}: echo "It works!";
}
',
'<?php
switch ($foo) {
case match ($bar) {
default => "foo",
}; echo "It works!";
}
',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'enums' => [
'<?php
enum Suit {
case Hearts; // do not fix
}
enum UserStatus: string {
case Pending = "P"; // do not fix
public function label(): string {
switch (foo()) {
case 42: // do fix
bar();
$a = new class() {
public function bar() {
switch (foo()) {
case 43: // do fix
bar();
}
$expressionResult = match ($condition) {
default => baz(),
};
}
};
$a->bar();
break;
}
return "label";
}
}
$expressionResult = match ($condition) {
default => baz(),
};
',
'<?php
enum Suit {
case Hearts; // do not fix
}
enum UserStatus: string {
case Pending = "P"; // do not fix
public function label(): string {
switch (foo()) {
case 42; // do fix
bar();
$a = new class() {
public function bar() {
switch (foo()) {
case 43; // do fix
bar();
}
$expressionResult = match ($condition) {
default => baz(),
};
}
};
$a->bar();
break;
}
return "label";
}
}
$expressionResult = match ($condition) {
default => baz(),
};
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/SwitchCaseSpaceFixerTest.php | tests/Fixer/ControlStructure/SwitchCaseSpaceFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\SwitchCaseSpaceFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\SwitchCaseSpaceFixer>
*
* @author Sullivan Senechal <soullivaneuh@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SwitchCaseSpaceFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
switch (1) {
case (1 #
)#
:
echo 1;
}
?>
',
];
yield [
'<?php
switch (1) {
case 1 #
: echo 1;
}
?>
',
];
yield [
'<?php
switch ($a) {
case 42:
break;
}
',
];
yield [
'<?php
switch ($a) {
case false:
break;
}
',
];
yield [
'<?php
switch ($a) {
case false:
break;
default:
}
',
];
yield [
'<?php
switch ($a) {
case "prod":
break;
}
',
'<?php
switch ($a) {
case "prod" :
break;
}
',
];
yield [
'<?php
switch ($a) {
case "prod":
break;
}
',
'<?php
switch ($a) {
case "prod" :
break;
}
',
];
yield [
'<?php
switch ($a) {
case 42:
break;
}
',
'<?php
switch ($a) {
case 42 :
break;
}
',
];
yield [
'<?php
switch ($a) {
case false:
break;
}
',
'<?php
switch ($a) {
case false :
break;
}
',
];
yield [
'<?php
switch ($a) {
case false:
break;
default:
}
',
'<?php
switch ($a) {
case false :
break;
default :
}
',
];
yield [
'<?php
switch ($a) {
case 42:
break;
}
',
'<?php
switch ($a) {
case 42 :
break;
}
',
];
yield [
'<?php
switch ($a) {
case $b ? "c" : "d":
break;
}
',
'<?php
switch ($a) {
case $b ? "c" : "d" :
break;
}
',
];
yield [
'<?php
switch ($a) {
case $b ? "c" : "d": break;
}
',
'<?php
switch ($a) {
case $b ? "c" : "d" : break;
}
',
];
yield [
'<?php
switch ($a) {
case $b ?: $c:
break;
}
',
'<?php
switch ($a) {
case $b ?: $c :
break;
}
',
];
yield [
'<?php
$a = 5.1;
$b = 1.0;
switch($a) {
case (int) $a < 1: {
echo "leave alone";
break;
}
case ($a < 2)/* test */ : {
echo "fix 1";
break;
}
case (3): {
echo "fix 2";
break;
}
case /**/(/**/ // test
4
/**/)//
/**/ : {
echo "fix 3";
break;
}
case (((int)$b) + 4.1): {
echo "fix 4";
break;
}
case ($b + 1) * 2: {
echo "leave alone";
break;
}
}
',
'<?php
$a = 5.1;
$b = 1.0;
switch($a) {
case (int) $a < 1 : {
echo "leave alone";
break;
}
case ($a < 2)/* test */ : {
echo "fix 1";
break;
}
case (3) : {
echo "fix 2";
break;
}
case /**/(/**/ // test
4
/**/)//
/**/ : {
echo "fix 3";
break;
}
case (((int)$b) + 4.1) : {
echo "fix 4";
break;
}
case ($b + 1) * 2 : {
echo "leave alone";
break;
}
}
',
];
yield [
'<?php
switch ($a) {
case 42:
break;
case 1:
switch ($a) {
case 42:
break;
default:
echo 1 ;
}
}
',
'<?php
switch ($a) {
case 42 :
break;
case 1 :
switch ($a) {
case 42 :
break;
default :
echo 1 ;
}
}
',
];
yield [
'<?php
switch($foo) {
case 4: ; ;
case 31 + test(";"); ; ; ;;
case 1 + test(";"); // ;
case (1+2/*;*/);
case 1;
case 2;
return 1;
default;
return 2;
}',
'<?php
switch($foo) {
case 4 : ; ;
case 31 + test(";") ; ; ; ;;
case 1 + test(";") ; // ;
case (1+2/*;*/) ;
case 1 ;
case 2 ;
return 1;
default ;
return 2;
}',
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield [
'<?php
switch ($a) {
case $b ? "c" : "this" ? "is" : "ugly":
break;
}
',
'<?php
switch ($a) {
case $b ? "c" : "this" ? "is" : "ugly" :
break;
}
',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php
match ($foo) {
1 => "a",
default => "b"
};
match ($bar) {
2 => "c",
default=> "d"
};
match ($baz) {
3 => "e",
default => "f"
};
',
];
yield [
'<?php
$a = function (): ?string {
return $rank ? match (true) {
$rank <= 1000 => \'bronze\',
default => null,
} : null;
};',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'enums' => [
'<?php
enum Suit {
case Hearts;
case Diamonds ;
case Clubs ;
case Spades ;
}
enum UserStatus: string {
case Pending = \'P\';
case Active = \'A\';
case Suspended = \'S\';
case CanceledByUser = \'C\' ;
}
switch ($a) {
default:
echo 1;
}
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/YodaStyleFixerTest.php | tests/Fixer/ControlStructure/YodaStyleFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\YodaStyleFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class YodaStyleFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
foreach (self::provideEqualityPairs() as $case) {
yield [
$case[0],
$case[1] ?? null,
['equal' => true, 'identical' => true] + ($case[2] ?? []),
];
if (($case[1] ?? null) === null) {
$expected = $case[0];
$input = null;
} else {
$expected = $case[1];
$input = $case[0];
}
yield [
$expected,
$input,
['equal' => false, 'identical' => false] + ($case[2] ?? []),
];
}
foreach (self::provideLessGreaterPairs() as $case) {
yield [
$case[0],
$case[1],
['less_and_greater' => true],
];
yield [
$case[1],
$case[0],
['less_and_greater' => false],
];
}
yield [
'<?php
$a = 1 === $b;
$b = $c != 1;
$c = $c > 3;
',
'<?php
$a = $b === 1;
$b = $c != 1;
$c = $c > 3;
',
[
'equal' => null,
'identical' => true,
'less_and_greater' => false,
],
];
yield [
'<?php
$a = [1, 2, 3];
while (2 !== $b = array_pop($c));
',
null,
[
'identical' => false,
],
];
yield [
'<?php
if ($revision->event == \'created\') {
foreach ($revision->getModified() as $col => $data) {
$model->$col = $data[\'new\'];
}
} else {
foreach ($revision->getModified() as $col => $data) {
$model->$col = $data[\'old\'];
}
}',
null,
[
'equal' => false,
'identical' => false,
],
];
}
/**
* @param array<string, mixed> $config
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(array $config, string $expectedMessage): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches("#^\\[{$this->fixer->getName()}\\] {$expectedMessage}$#");
$this->fixer->configure($config);
}
/**
* @return iterable<int, array{array<string, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield [['equal' => 2], 'Invalid configuration: The option "equal" with value 2 is expected to be of type "bool" or "null", but is of type "(int|integer)"\.'];
yield [['_invalid_' => true], 'Invalid configuration: The option "_invalid_" does not exist\. Defined options are: "always_move_variable", "equal", "identical", "less_and_greater"\.'];
}
/**
* @dataProvider provideFixPre81Cases
*
* @requires PHP <8.0
*/
public function testFixPre81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixPre81Cases(): iterable
{
yield [
'<?php return \A/*5*/\/*6*/B\/*7*/C::MY_CONST === \A/*1*//*1*//*1*//*1*//*1*/\/*2*/B/*3*/\C/*4*/::$myVariable;',
'<?php return \A/*1*//*1*//*1*//*1*//*1*/\/*2*/B/*3*/\C/*4*/::$myVariable === \A/*5*/\/*6*/B\/*7*/C::MY_CONST;',
];
yield [
'<?php return A\/**//**//**/B/*a*//*a*//*a*//*a*/::MY_CONST === B\C::$myVariable;',
'<?php return B\C::$myVariable === A\/**//**//**/B/*a*//*a*//*a*//*a*/::MY_CONST;',
];
yield ['<?php return $foo === $bar[$baz]{1};'];
yield ['<?php return $foo->$a[1] === $bar[$baz]{1}->$a[1][2][3]->$d[$z]{1};'];
yield ['<?php return $m->a{2}+1 == 2;'];
yield ['<?php return $m{2}+1 == 2;'];
yield [
'<?php echo 1 === (unset) $a ? 8 : 7;',
'<?php echo (unset) $a === 1 ? 8 : 7;',
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php
if ($a = true === $obj instanceof (foo())) {
echo 1;
}',
'<?php
if ($a = $obj instanceof (foo()) === true) {
echo 1;
}',
];
yield [
'<?php $i = $this?->getStuff() === $myVariable;',
'<?php $i = $myVariable === $this?->getStuff();',
['equal' => true, 'identical' => true, 'always_move_variable' => true],
];
yield [
'<?php 42 === $a->b[5]?->c;',
'<?php $a->b[5]?->c === 42;',
];
yield [
'<?php return $this->myObject1?->{$index}+$b === "";',
null,
['equal' => true, 'identical' => true],
];
yield [
'<?php new Foo(bar: 1 === $var);',
'<?php new Foo(bar: $var === 1);',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'does not make a lot of sense but is valid syntax, do not break 1' => [
'<?php $b = strlen( ... ) === $a;',
];
yield 'does not make a lot of sense but is valid syntax, do not break 2' => [
'<?php $b = $a === strlen( ... );',
];
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
private static function provideEqualityPairs(): iterable
{
yield [
'<?php $a = 1 + ($b + $c) === true ? 1 : 2;',
null,
['always_move_variable' => true],
];
yield [
'<?php $a = true === ($b + $c) ? 1 : 2;',
'<?php $a = ($b + $c) === true ? 1 : 2;',
['always_move_variable' => true],
];
yield [
'<?php
if ((1 === $a) === 1) {
return;
}',
'<?php
if (($a === 1) === 1) {
return;
}',
['always_move_variable' => false],
];
yield [
'<?php
if (true === (1 !== $foo[0])) {
return;
}',
'<?php
if (($foo[0] !== 1) === true) {
return;
}',
['always_move_variable' => true],
];
yield [
'<?php return 1 !== $a [$b];',
'<?php return $a [$b] !== 1;',
];
yield [
'<?= 1 === $a ? 5 : 7;',
'<?= $a === 1 ? 5 : 7;',
];
yield [
'<?php print 1 === 1343;',
];
yield [
'<?php
echo 3 === $a ? 2 : 4;
',
'<?php
echo $a === 3 ? 2 : 4;
',
];
yield [
'<?php 1 === foo($a) ? 1 : 2;',
'<?php foo($a) === 1 ? 1 : 2;',
];
yield [
'<?php 1 === $a::$a ? 1 : 2;',
'<?php $a::$a === 1 ? 1 : 2;',
];
yield [
'<?php 1 === (bool) $a ? 8 : 7;',
'<?php (bool) $a === 1 ? 8 : 7;',
];
yield [
'<?php 1 === new $a ? 1 : 2;',
'<?php new $a === 1 ? 1 : 2;',
];
yield [
'<?php 1 === "a".$a ? 5 : 6;',
'<?php "a".$a === 1 ? 5 : 6;',
];
yield [
'<?php 1 === __DIR__.$a ? 5 : 6;',
'<?php __DIR__.$a === 1 ? 5 : 6;',
];
yield [
'<?php 1 === $a.$b ? 5 : 6;',
'<?php $a.$b === 1 ? 5 : 6;',
];
yield [
'<?php echo 1 === (object) $a ? 8 : 7;',
'<?php echo (object) $a === 1 ? 8 : 7;',
];
yield [
'<?php echo 1 === (int) $a ? 8 : 7;',
'<?php echo (int) $a === 1 ? 8 : 7;',
];
yield [
'<?php echo 1 === (float) $a ? 8 : 7;',
'<?php echo (float) $a === 1 ? 8 : 7;',
];
yield [
'<?php echo 1 === (string) $a ? 8 : 7;',
'<?php echo (string) $a === 1 ? 8 : 7;',
];
yield [
'<?php echo 1 === (array) $a ? 8 : 7;',
'<?php echo (array) $a === 1 ? 8 : 7;',
];
yield [
'<?php echo 1 === (bool) $a ? 8 : 7;',
'<?php echo (bool) $a === 1 ? 8 : 7;',
];
yield [
'<?php
if ($a = true === $obj instanceof A) {
echo \'A\';
}',
'<?php
if ($a = $obj instanceof A === true) {
echo \'A\';
}',
];
yield [
'<?php echo 1 === !!$a ? 8 : 7;',
'<?php echo !!$a === 1 ? 8 : 7;',
];
yield [
'<?php $a = 1 === new b ? 1 : 2;',
'<?php $a = new b === 1 ? 1 : 2;',
];
yield [
'<?php $a = 1 === empty($a) ? 1 : 2;',
'<?php $a = empty($a) === 1 ? 1 : 2;',
];
yield [
'<?php $b = 1 === clone $a ? 5 : 9;',
'<?php $b = clone $a === 1 ? 5 : 9;',
];
yield [
'<?php while(1 === $a ? 1 : 2){};',
'<?php while($a === 1 ? 1 : 2){};',
];
yield [
'<?php switch(1 === $a){
case true: echo 1;
};',
'<?php switch($a === 1){
case true: echo 1;
};',
];
yield [
'<?php echo 1 === $a ? 1 : 2;',
'<?php echo $a === 1 ? 1 : 2;',
];
// Don't fix cases.
yield ['<?php $a = 1 === 1;'];
yield ['<?php $b = $b === $c;'];
yield ['<?php $c = $$b === $$c;'];
yield ['<?php $d = count($this->array[$var]) === $a;'];
yield ['<?php $e = $a === count($this->array[$var]);'];
yield ['<?php $f = ($a123 & self::MY_BITMASK) === $a;'];
yield ['<?php $g = $a === ($a456 & self::MY_BITMASK);'];
yield ['<?php $h = $this->getStuff() === $myVariable;'];
yield ['<?php $i = $myVariable === $this->getStuff();'];
yield ['<?php $j = 2 * $myVar % 3 === $a;'];
yield ['<?php return $k === 2 * $myVar % 3;'];
yield ['<?php $l = $c > 2;'];
yield ['<?php return $this->myObject1->{$index}+$b === "";'];
yield ['<?php return $m[2]+1 == 2;'];
yield ['<?php return $foo === $bar[$baz][1];'];
yield ['<?php $a = $b[$key]["1"] === $c["2"];'];
yield ['<?php return $foo->$a === $foo->$b->$c;'];
yield ['<?php return $x === 2 - 1;'];
yield ['<?php return $x === 2-1;'];
// https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/693
yield ['<?php return array(2) == $o;'];
yield ['<?php return $p == array(2);'];
yield ['<?php return $p == array("2");'];
yield ['<?php return $p == array(TWO);'];
yield ['<?php return $p == array(array());'];
yield ['<?php return $p == [[]];'];
yield ['<?php return array($q) == $a;'];
yield ['<?php return $r == array($a);'];
yield ['<?php $s = ((array(2))) == $a;'];
yield ['<?php $t = $a == ((array(2)));'];
yield ['<?php list($a) = $c === array(1) ? $b : $d;'];
yield ['<?php $b = 7 === list($a) = [7];'];
yield ['<?php $a = function(){} === array(0);'];
yield ['<?php $z = $n == list($a) = $b;'];
yield ['<?php return $n == list($a) = $b;'];
// Fix cases.
yield 'Array destruct by ternary.' => [
'<?php list($a) = 11 === $c ? $b : $d;',
'<?php list($a) = $c === 11 ? $b : $d;',
];
yield 'Less spacing.' => [
'<?php $z=2==$a;$b=$c>1&&$c<=10;',
'<?php $z=$a==2;$b=$c>1&&$c<=10;',
];
yield 'Comments.' => [
'<?php $z = /**/ /**/2/**/ /**/
# aa
/**/==/**/$a/***/;',
'<?php $z = /**/ /**/$a/**/ /**/
# aa
/**/==/**/2/***/;',
];
yield [
'<?php return 2 == ($a)?>',
];
yield [
'<?php return ($a) == 2?>',
];
yield [
'<?php return 2 == ($a)?>',
'<?php return ($a) == 2?>',
['always_move_variable' => true],
];
yield [
'<?php $a = ($c === ((null === $b)));',
'<?php $a = ($c === (($b === null)));',
];
yield [
'<?php return null == $a[2];',
'<?php return $a[2] == null;',
];
yield [
'<?php return "" === $this->myArray[$index];',
'<?php return $this->myArray[$index] === "";',
];
yield [
'<?php return "" === $this->myArray[$index]->/*1*//*2*//*3*/a;',
'<?php return $this->myArray[$index]->/*1*//*2*//*3*/a === "";',
];
yield [
'<?php return "" === $this->myArray[$index]->a;',
'<?php return $this->myArray[$index]->a === "";',
];
yield [
'<?php return "" === $this->myObject2-> {$index};',
'<?php return $this->myObject2-> {$index} === "";',
];
yield [
'<?php return "" === $this->myObject3->{$index}->a;',
'<?php return $this->myObject3->{$index}->a === "";',
];
yield [
'<?php return "" === $this->myObject4->{$index}->{$index}->a;',
'<?php return $this->myObject4->{$index}->{$index}->a === "";',
];
yield [
'<?php return "" === $this->myObject4->$index->a;',
'<?php return $this->myObject4->$index->a === "";',
];
yield [
'<?php return self::MY_CONST === self::$myVariable;',
'<?php return self::$myVariable === self::MY_CONST;',
];
yield [
'<?php return \A\B\C::MY_CONST === \A\B\C::$myVariable;',
'<?php return \A\B\C::$myVariable === \A\B\C::MY_CONST;',
];
yield [
'<?php $a = 1 == $$a?>',
'<?php $a = $$a == 1?>',
];
yield 'Nested case' => [
'<?php return null === $a[0 === $b ? $c : $d];',
'<?php return $a[$b === 0 ? $c : $d] === null;',
];
yield [
'<?php return null === $this->{null === $a ? "a" : "b"};',
'<?php return $this->{$a === null ? "a" : "b"} === null;',
];
yield 'Complex code sample.' => [
'<?php
if ($a == $b) {
return null === $b ? (null === $a ? 0 : 0 === $a->b) : 0 === $b->a;
} else {
if ($c === (null === $b)) {
return false === $d;
}
}',
'<?php
if ($a == $b) {
return $b === null ? ($a === null ? 0 : $a->b === 0) : $b->a === 0;
} else {
if ($c === ($b === null)) {
return $d === false;
}
}',
];
yield [
'<?php $b = list($a) = 7 === [7];', // makes no sense, but valid PHP syntax
'<?php $b = list($a) = [7] === 7;',
];
yield [
'<?php $a = 1 === function(){};',
'<?php $a = function(){} === 1;',
];
yield [
'<?php
$z#1
#2
=
#3
1#4
#5
===#6
#7
$a#8
#9
;#10',
'<?php
$z#1
#2
=
#3
$a#4
#5
===#6
#7
1#8
#9
;#10',
];
yield [
'<?php $i = 2 === $this/*a*//*b*//*c*//*d*//*e*//*f*/->getStuff();',
'<?php $i = $this/*a*//*b*//*c*//*d*//*e*//*f*/->getStuff() === 2;',
];
yield [
'<?php return "" === $this->myObject5->{$index}->/*1*//*2*/b;',
'<?php return $this->myObject5->{$index}->/*1*//*2*/b === "";',
];
yield [
'<?php
function hello() {}
1 === $a ? b() : c();
',
'<?php
function hello() {}
$a === 1 ? b() : c();
',
];
yield [
'<?php
class A{}
1 === $a ? b() : c();
',
'<?php
class A{}
$a === 1 ? b() : c();
',
];
yield [
'<?php
function foo() {
foreach ($arr as $key => $value) {
false !== uniqid() ? 1 : 2;
}
false !== uniqid() ? 1 : 2;
}',
'<?php
function foo() {
foreach ($arr as $key => $value) {
uniqid() !== false ? 1 : 2;
}
uniqid() !== false ? 1 : 2;
}',
];
yield [
'<?php false === $a = array();',
];
yield [
'<?php $e = count($this->array[$var]) === $a;',
'<?php $e = $a === count($this->array[$var]);',
['always_move_variable' => true],
];
yield [
'<?php $i = $this->getStuff() === $myVariable;',
'<?php $i = $myVariable === $this->getStuff();',
['always_move_variable' => true],
];
yield [
'<?php $g = ($a789 & self::MY_BITMASK) === $a;',
null,
['always_move_variable' => true],
];
yield [
'<?php return $myVar + 2 === $k;',
'<?php return $k === $myVar + 2;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar . $b === $k;',
'<?php return $k === $myVar . $b;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar - 2 === $k;',
'<?php return $k === $myVar - 2;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar * 2 === $k;',
'<?php return $k === $myVar * 2;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar / 2 === $k;',
'<?php return $k === $myVar / 2;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar % 2 === $k;',
'<?php return $k === $myVar % 2;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar ** 2 === $k;',
'<?php return $k === $myVar ** 2;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar < 2 === $k;',
'<?php return $k === $myVar < 2;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar > 2 === $k;',
'<?php return $k === $myVar > 2;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar <= 2 === $k;',
'<?php return $k === $myVar <= 2;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar >= 2 === $k;',
'<?php return $k === $myVar >= 2;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar . 2 === $k;',
'<?php return $k === $myVar . 2;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar << 2 === $k;',
'<?php return $k === $myVar << 2;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar >> 2 === $k;',
'<?php return $k === $myVar >> 2;',
['always_move_variable' => true],
];
yield [
'<?php return !$myVar === $k;',
'<?php return $k === !$myVar;',
['always_move_variable' => true],
];
yield [
'<?php return $myVar instanceof Foo === $k;',
'<?php return $k === $myVar instanceof Foo;',
['always_move_variable' => true],
];
yield [
'<?php return (bool) $myVar === $k;',
'<?php return $k === (bool) $myVar;',
['always_move_variable' => true],
];
yield [
'<?php return (int) $myVar === $k;',
'<?php return $k === (int) $myVar;',
['always_move_variable' => true],
];
yield [
'<?php return (float) $myVar === $k;',
'<?php return $k === (float) $myVar;',
['always_move_variable' => true],
];
yield [
'<?php return (string) $myVar === $k;',
'<?php return $k === (string) $myVar;',
['always_move_variable' => true],
];
yield [
'<?php return (array) $myVar === $k;',
'<?php return $k === (array) $myVar;',
['always_move_variable' => true],
];
yield [
'<?php return (object) $myVar === $k;',
'<?php return $k === (object) $myVar;',
['always_move_variable' => true],
];
yield [
'<?php $a = null === foo();',
'<?php $a = foo() === null;',
];
yield [
'<?php $a = \'foo\' === foo();',
'<?php $a = foo() === \'foo\';',
];
yield [
'<?php $a = "foo" === foo();',
'<?php $a = foo() === "foo";',
];
yield [
'<?php $a = 1 === foo();',
'<?php $a = foo() === 1;',
];
yield [
'<?php $a = 1.2 === foo();',
'<?php $a = foo() === 1.2;',
];
yield [
'<?php $a = true === foo();',
'<?php $a = foo() === true;',
];
yield [
'<?php $a = false === foo();',
'<?php $a = foo() === false;',
];
yield [
'<?php $a = -1 === reset($foo);',
'<?php $a = reset($foo) === -1;',
];
yield [
'<?php $a = - 1 === reset($foo);',
'<?php $a = reset($foo) === - 1;',
];
yield [
'<?php $a = -/* bar */1 === reset($foo);',
'<?php $a = reset($foo) === -/* bar */1;',
];
yield [
'<?php return array() === $array;',
'<?php return $array === array();',
];
yield [
'<?php return [] === $array;',
'<?php return $array === [];',
];
yield [
'<?php return array(/* foo */) === $array;',
'<?php return $array === array(/* foo */);',
];
yield [
'<?php return [
// 1
] === $array;',
'<?php return $array === [
// 1
];',
];
yield [
'<?php $a = $b = null === $c;',
'<?php $a = $b = $c === null;',
];
$template = '<?php $a = ($b + $c) %s 1 === true ? 1 : 2;';
$operators = ['||', '&&'];
foreach ($operators as $operator) {
yield [
\sprintf($template, $operator),
null,
['always_move_variable' => true],
];
}
$assignmentOperators = ['=', '**=', '*=', '|=', '+=', '-=', '^=', '<<=', '>>=', '&=', '.=', '/=', '%=', '??='];
$logicalOperators = ['xor', 'or', 'and', '||', '&&', '??'];
foreach ([...$assignmentOperators, ...$logicalOperators] as $operator) {
yield [
\sprintf('<?php $a %s 4 === $b ? 2 : 3;', $operator),
\sprintf('<?php $a %s $b === 4 ? 2 : 3;', $operator),
];
}
foreach ($assignmentOperators as $operator) {
yield [
\sprintf('<?php 1 === $x %s 2;', $operator),
];
}
yield ['<?php $a = $b + 1 <=> $d;'];
yield [
'<?php $a = new class(10) extends SomeClass implements SomeInterface {} === $a;/**/',
];
yield [
'<?php $a = $b ?? 1 ?? 2 == $d;',
'<?php $a = $b ?? 1 ?? $d == 2;',
];
yield [
'<?php $a = 1 === new class(10) extends SomeClass implements SomeInterface {};/**/',
'<?php $a = new class(10) extends SomeClass implements SomeInterface {} === 1;/**/',
];
yield [
'<?php
function a() {
for ($i = 1; $i <= 3; $i++) {
echo yield 1 === $i ? 1 : 2;
}
}
',
'<?php
function a() {
for ($i = 1; $i <= 3; $i++) {
echo yield $i === 1 ? 1 : 2;
}
}
',
];
yield [
'<?php function test() {return yield 1 !== $a [$b];};',
'<?php function test() {return yield $a [$b] !== 1;};',
];
yield [
'<?php function test() {return yield 1 === $a;};',
'<?php function test() {return yield $a === 1;};',
];
yield [
'<?php
$a = 1;
switch ($a) {
case 1 === $a:
echo 123;
break;
}
',
'<?php
$a = 1;
switch ($a) {
case $a === 1:
echo 123;
break;
}
',
];
yield 'require' => [
'<?php require 1 === $var ? "A.php" : "B.php";',
'<?php require $var === 1 ? "A.php" : "B.php";',
];
yield 'require_once' => [
'<?php require_once 1 === $var ? "A.php" : "B.php";',
'<?php require_once $var === 1 ? "A.php" : "B.php";',
];
yield 'include' => [
'<?php include 1 === $var ? "A.php" : "B.php";',
'<?php include $var === 1 ? "A.php" : "B.php";',
];
yield 'include_once' => [
'<?php include_once 1 === $var ? "A.php" : "B.php";',
'<?php include_once $var === 1 ? "A.php" : "B.php";',
];
yield 'yield from' => [
'<?php function test() {return yield from 1 === $a ? $c : $d;};',
'<?php function test() {return yield from $a === 1 ? $c : $d;};',
];
yield [
'<?php if (1_000 === $b);',
'<?php if ($b === 1_000);',
];
yield [
'<?php fn() => $c === array(1) ? $b : $d;',
null,
[
'less_and_greater' => false,
],
];
yield ['<?php list("a" => $a, "b" => $b, "c" => $c) = $c === array(1) ? $b : $d;'];
yield ['<?php list(list("x" => $x1, "y" => $y1), list("x" => $x2, "y" => $y2)) = $points;'];
yield ['<?php list("first" => list($x1, $y1), "second" => list($x2, $y2)) = $points;'];
yield ['<?php [$a, $b, $c] = [1, 2, 3];'];
yield ['<?php ["a" => $a, "b" => $b, "c" => $c] = $a[0];'];
yield ['<?php $b = 7 === [$a] = [7];']; // makes no sense, but valid PHP syntax
yield ['<?php [$a] = $c === array(1) ? $b : $d;'];
yield ['<?php $z = $n == [$a] = $b;'];
yield ['<?php return $n == [$a] = $b;'];
yield [
'<?php list("a" => $a, "b" => $b, "c" => $c) = 1 === $c ? $b : $d;',
'<?php list("a" => $a, "b" => $b, "c" => $c) = $c === 1 ? $b : $d;',
];
yield [
'<?php list("a" => $a, "b" => $b, "c" => $c) = A::B === $c ? $b : $d;',
'<?php list("a" => $a, "b" => $b, "c" => $c) = $c === A::B ? $b : $d;',
];
yield [
'<?php list( (2 === $c ? "a" : "b") => $b) = ["a" => 7 === $c ? 5 : 1, "b" => 7];',
'<?php list( ($c === 2 ? "a" : "b") => $b) = ["a" => $c === 7 ? 5 : 1, "b" => 7];',
];
yield [
'<?php [ (ABC::A === $c ? "a" : "b") => $b] = ["a" => 7 === $c ? 5 : 1, "b" => 7];',
'<?php [ ($c === ABC::A ? "a" : "b") => $b] = ["a" => $c === 7 ? 5 : 1, "b" => 7];',
];
yield 'Array destruct by ternary.' => [
'<?php [$a] = 11 === $c ? $b : $d;',
'<?php [$a] = $c === 11 ? $b : $d;',
];
yield [
'<?php $b = [$a] = 7 === [7];', // makes no sense, but valid PHP syntax
'<?php $b = [$a] = [7] === 7;',
];
}
/**
* @return iterable<int, array{string, string}>
*/
private static function provideLessGreaterPairs(): iterable
{
yield [
'<?php $a = 3 <= $b;',
'<?php $a = $b >= 3;',
];
yield [
'<?php $a = 3 > $b;',
'<?php $a = $b < 3;',
];
yield [
'<?php $a = (3 > $b) || $d;',
'<?php $a = ($b < 3) || $d;',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/SimplifiedIfReturnFixerTest.php | tests/Fixer/ControlStructure/SimplifiedIfReturnFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\SimplifiedIfReturnFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\SimplifiedIfReturnFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SimplifiedIfReturnFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'simple' => [
'<?php return (bool) ($foo) ;',
'<?php if ($foo) { return true; } return false;',
];
yield 'simple-negative' => [
'<?php return ! ($foo) ;',
'<?php if ($foo) { return false; } return true;',
];
yield 'simple-negative II' => [
'<?php return ! (!$foo && $a()) ;',
'<?php if (!$foo && $a()) { return false; } return true;',
];
yield 'simple-braceless' => [
'<?php return (bool) ($foo) ;',
'<?php if ($foo) return true; return false;',
];
yield 'simple-braceless-negative' => [
'<?php return ! ($foo) ;',
'<?php if ($foo) return false; return true;',
];
yield 'bug-consecutive-ifs' => [
'<?php if ($bar) { return 1; } return (bool) ($foo) ;',
'<?php if ($bar) { return 1; } if ($foo) { return true; } return false;',
];
yield 'bug-consecutive-ifs-negative' => [
'<?php if ($bar) { return 1; } return ! ($foo) ;',
'<?php if ($bar) { return 1; } if ($foo) { return false; } return true;',
];
yield 'bug-consecutive-ifs-braceless' => [
'<?php if ($bar) return 1; return (bool) ($foo) ;',
'<?php if ($bar) return 1; if ($foo) return true; return false;',
];
yield 'bug-consecutive-ifs-braceless-negative' => [
'<?php if ($bar) return 1; return ! ($foo) ;',
'<?php if ($bar) return 1; if ($foo) return false; return true;',
];
yield [
<<<'EOT'
<?php
function f1() { return (bool) ($f1) ; }
function f2() { return true; } return false;
function f3() { return (bool) ($f3) ; }
function f4() { return true; } return false;
function f5() { return (bool) ($f5) ; }
function f6() { return false; } return true;
function f7() { return ! ($f7) ; }
function f8() { return false; } return true;
function f9() { return ! ($f9) ; }
EOT,
<<<'EOT'
<?php
function f1() { if ($f1) { return true; } return false; }
function f2() { return true; } return false;
function f3() { if ($f3) { return true; } return false; }
function f4() { return true; } return false;
function f5() { if ($f5) { return true; } return false; }
function f6() { return false; } return true;
function f7() { if ($f7) { return false; } return true; }
function f8() { return false; } return true;
function f9() { if ($f9) { return false; } return true; }
EOT,
];
yield 'preserve-comments' => [
<<<'EOT'
<?php
// C1
return (bool)
# C2
(
/* C3 */
$foo
/** C4 */
)
// C5
# C6
// C7
# C8
/* C9 */
/** C10 */
// C11
# C12
;
/* C13 */
EOT,
<<<'EOT'
<?php
// C1
if
# C2
(
/* C3 */
$foo
/** C4 */
)
// C5
{
# C6
return
// C7
true
# C8
;
/* C9 */
}
/** C10 */
return
// C11
false
# C12
;
/* C13 */
EOT,
];
yield 'preserve-comments-braceless' => [
<<<'EOT'
<?php
// C1
return (bool)
# C2
(
/* C3 */
$foo
/** C4 */
)
// C5
# C6
// C7
# C8
/* C9 */
/** C10 */
// C11
# C12
;
/* C13 */
EOT,
<<<'EOT'
<?php
// C1
if
# C2
(
/* C3 */
$foo
/** C4 */
)
// C5
# C6
return
// C7
true
# C8
;
/* C9 */
/** C10 */
return
// C11
false
# C12
;
/* C13 */
EOT,
];
yield 'else-if' => [
'<?php if ($bar) { return $bar; } else return (bool) ($foo) ;',
'<?php if ($bar) { return $bar; } else if ($foo) { return true; } return false;',
];
yield 'else-if-negative' => [
'<?php if ($bar) { return $bar; } else return ! ($foo) ;',
'<?php if ($bar) { return $bar; } else if ($foo) { return false; } return true;',
];
yield 'else-if-braceless' => [
'<?php if ($bar) return $bar; else return (bool) ($foo) ;',
'<?php if ($bar) return $bar; else if ($foo) return true; return false;',
];
yield 'else-if-braceless-negative' => [
'<?php if ($bar) return $bar; else return ! ($foo) ;',
'<?php if ($bar) return $bar; else if ($foo) return false; return true;',
];
yield 'elseif' => [
'<?php if ($bar) { return $bar; } return (bool) ($foo) ;',
'<?php if ($bar) { return $bar; } elseif ($foo) { return true; } return false;',
];
yield 'elseif-negative' => [
'<?php if ($bar) { return $bar; } return ! ($foo) ;',
'<?php if ($bar) { return $bar; } elseif ($foo) { return false; } return true;',
];
yield 'elseif-braceless' => [
'<?php if ($bar) return $bar; return (bool) ($foo) ;',
'<?php if ($bar) return $bar; elseif ($foo) return true; return false;',
];
yield 'elseif-braceless-negative' => [
'<?php if ($bar) return $bar; return ! ($foo) ;',
'<?php if ($bar) return $bar; elseif ($foo) return false; return true;',
];
yield 'no braces loops' => [
'<?php
function foo1(string $str, array $letters): bool
{
foreach ($letters as $letter)
if ($str === $letter)
return true;
return false;
}
function foo2(int $z): bool
{
for ($i = 0; $i < 3; ++$i)
if ($i === $z)
return true;
return false;
}
function foo3($y): bool
{
while ($x = bar())
if ($x === $z)
return true;
return false;
}
',
];
yield 'alternative syntax not supported' => [
'<?php
if ($foo):
return true;
else:
return false;
endif;
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/IncludeFixerTest.php | tests/Fixer/ControlStructure/IncludeFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\IncludeFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\IncludeFixer>
*
* @author Саша Стаменковић <umpirsky@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class IncludeFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php include # A
# B
# C
"a"# D
# E
# F
;# G
# H',
'<?php include# A
(# B
# C
"a"# D
# E
)# F
;# G
# H',
];
yield [
'<?php include $a;',
'<?php include ( $a ) ;',
];
yield [
'<?php
require_once "test1.php";
include_once "test2.php";
require "test3.php";
include "test4.php";',
'<?php
require_once("test1.php");
include_once("test2.php");
require("test3.php");
include("test4.php");',
];
yield [
'<?php
require_once #1
#2
#3
"test1.php"#4
#5
#6
;',
'<?php
require_once #1
(#2
#3
"test1.php"#4
)#5
#6
;',
];
yield [
'<?php foo(require "foo.php");',
'<?php foo(require("foo.php"));',
];
yield [
'<?php $foo[include "foo.php"];',
'<?php $foo[include("foo.php")];',
];
$template = '<?php %s';
foreach (['require', 'require_once', 'include', 'include_once'] as $statement) {
yield [
\sprintf($template.' "foo.php"?>', $statement),
\sprintf($template.' ("foo.php") ?>', $statement),
];
yield [
\sprintf($template.' /**/"foo.php"// test
?>', $statement),
\sprintf($template.'/**/ ("foo.php") // test
?>', $statement),
];
yield [
\sprintf($template.' $a;', $statement),
\sprintf($template.'$a;', $statement),
];
yield [
\sprintf($template.' $a;', $statement),
\sprintf($template.' $a;', $statement),
];
yield [
\sprintf($template.' $a; ', $statement),
\sprintf($template.' $a ; ', $statement),
];
yield [
\sprintf($template." /**/'foo.php';", $statement),
\sprintf($template."/**/'foo.php';", $statement),
];
yield [
\sprintf($template." 'foo.php';", $statement),
\sprintf($template."'foo.php';", $statement),
];
yield [
\sprintf($template." 'foo.php';", $statement),
\sprintf($template." 'foo.php';", $statement),
];
yield [
\sprintf($template." 'foo.php';", $statement),
\sprintf($template."('foo.php');", $statement),
];
yield [
\sprintf($template." 'foo.php';", $statement),
\sprintf($template."( 'foo.php');", $statement),
];
yield [
\sprintf($template." 'foo.php';", $statement),
\sprintf($template." ( 'foo.php' );", $statement),
];
yield [
\sprintf($template." '\".__DIR__.\"/../bootstrap.php';", $statement),
];
yield [
\sprintf('<?php // %s foo', $statement),
];
yield [
\sprintf('<?php /* %s foo */', $statement),
];
yield [
\sprintf('<?php /** %s foo */', $statement),
];
yield [
\sprintf($template.'($a ? $b : $c) . $d;', $statement),
];
yield [
\sprintf($template.' ($a ? $b : $c) . $d;', $statement),
];
yield [
\sprintf('<?php exit("POST must %s \"file\"");', $statement),
];
yield [
\sprintf('<?php ClassCollectionLoader::load(%s($this->getCacheDir().\'classes.map\'), $this->getCacheDir(), $name, $this->debug, false, $extension);', $statement),
];
yield [
\sprintf('<?php $foo = (false === %s($zfLibraryPath."/Zend/Loader/StandardAutoloader.php"));', $statement),
];
yield [
\sprintf($template.' "Buzz/foo-Bar.php";', $statement),
\sprintf($template.' ( "Buzz/foo-Bar.php" );', $statement),
];
yield [
\sprintf($template.' "$buzz/foo-Bar.php";', $statement),
\sprintf($template.' ( "$buzz/foo-Bar.php" );', $statement),
];
yield [
\sprintf($template.' "{$buzz}/foo-Bar.php";', $statement),
\sprintf($template.' ( "{$buzz}/foo-Bar.php" );', $statement),
];
yield [
\sprintf($template.' $foo ? "foo.php" : "bar.php";', $statement),
\sprintf($template.'($foo ? "foo.php" : "bar.php");', $statement),
];
yield [
\sprintf($template.' $foo ? "foo.php" : "bar.php";', $statement),
\sprintf($template.'($foo ? "foo.php" : "bar.php");', $statement),
];
yield [
\sprintf("<?php return %s __DIR__.'foo.php';", $statement),
\sprintf("<?php return %s __DIR__.'foo.php';", $statement),
];
yield [
\sprintf("<?php \$foo = %s __DIR__.('foo.php');", $statement),
\sprintf("<?php \$foo = %s __DIR__.('foo.php');", $statement),
];
yield [
\sprintf("<?php %s __DIR__.('foo.php');", $statement),
\sprintf("<?php %s (__DIR__.('foo.php'));", $statement),
];
yield [
\sprintf("<?php %s __DIR__ . ('foo.php');", $statement),
\sprintf("<?php %s (__DIR__ . ('foo.php'));", $statement),
];
yield [
\sprintf("<?php %s dirname(__FILE__).'foo.php';", $statement),
\sprintf("<?php %s (dirname(__FILE__).'foo.php');", $statement),
];
yield [
\sprintf('<?php %s "foo/".CONSTANT."/bar.php";', $statement),
\sprintf('<?php %s("foo/".CONSTANT."/bar.php");', $statement),
];
yield [
\sprintf('<?php %s "foo/".CONSTANT."/bar.php"; %s "foo/".CONSTANT."/bar.php";', $statement, $statement),
\sprintf('<?php %s("foo/".CONSTANT."/bar.php"); %s("foo/".CONSTANT."/bar.php");', $statement, $statement),
];
yield [
\sprintf('<?php %s "foo/".CONSTANT."/bar.php"; $foo = "bar";', $statement),
\sprintf('<?php %s("foo/".CONSTANT."/bar.php"); $foo = "bar";', $statement),
];
yield [
\sprintf('<?php %s "foo/".CONSTANT."/bar.php"; foo();', $statement),
\sprintf('<?php %s("foo/".CONSTANT."/bar.php"); foo();', $statement),
];
yield [
\sprintf('<?php %s "foo/" . CONSTANT . "/bar.php";', $statement),
\sprintf('<?php %s("foo/" . CONSTANT . "/bar.php");', $statement),
];
yield [
\sprintf('<?php %s SOME_CONST . "file.php"; %s Foo::Bar($baz);', $statement, $statement),
\sprintf('<?php %s( SOME_CONST . "file.php" ); %s Foo::Bar($baz);', $statement, $statement),
];
yield [
\sprintf('<?php %s SOME_CONST . "file1.php"; %s Foo::Bar($baz);', $statement, $statement),
\sprintf('<?php %s SOME_CONST . "file1.php"; %s Foo::Bar($baz);', $statement, $statement),
];
yield $statement.': binary string lower case' => [
\sprintf($template." b'foo.php';", $statement),
\sprintf($template."(b'foo.php');", $statement),
];
yield $statement.': binary string upper case' => [
\sprintf($template." B'foo.php';", $statement),
\sprintf($template."(B'foo.php');", $statement),
];
}
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/NoUnneededBracesFixerTest.php | tests/Fixer/ControlStructure/NoUnneededBracesFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\NoUnneededBracesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoUnneededBracesFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\NoUnneededBracesFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoUnneededBracesFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'simple sample, last token candidate' => [
'<?php echo 1;',
'<?php { echo 1;}',
];
yield 'minimal sample, first token candidate' => [
'<?php // {}',
'<?php {} // {}',
];
yield [
'<?php
echo 0; //
echo 1;
switch($a) {
case 2: echo 3; break;
}
echo 4; echo 5; //
',
'<?php
{ { echo 0; } } //
{echo 1;}
switch($a) {
case 2: {echo 3; break;}
}
echo 4; { echo 5; }//
',
];
yield 'no fixes' => [
'<?php
foreach($a as $b){}
while($a){}
do {} while($a);
if ($c){}
if ($c){}else{}
if ($c){}elseif($d){}
if ($c) {}elseif($d)/** */{ } else/**/{ }
try {} catch(\Exception $e) {}
function test(){}
$a = function() use ($c){};
class A extends B {}
interface D {}
trait E {}
',
];
yield 'no fixes II' => [
'<?php
declare(ticks=1) {
// entire script here
}
#',
];
yield 'no fix catch/try/finally' => [
'<?php
try {
} catch(\Exception $e) {
} finally {
}
',
];
yield 'no fix namespace block' => [
'<?php
namespace {
}
namespace A {
}
namespace A\B {
}
',
];
yield 'provideNoFix7Cases' => [
'<?php
use some\a\{ClassA, ClassB, ClassC as C};
use function some\a\{fn_a, fn_b, fn_c};
use const some\a\{ConstA, ConstB, ConstC};
use some\x\{ClassD, function CC as C, function D, const E, function A\B};
class Foo
{
public function getBar(): array
{
}
}
',
];
yield [
'<?php
namespace Foo;
function Bar(){}
',
'<?php
namespace Foo {
function Bar(){}
}
',
['namespaces' => true],
];
yield [
'<?php
namespace A5 {
function AA(){}
}
namespace B6 {
function BB(){}
}',
null,
['namespaces' => true],
];
yield [
'<?php
namespace Foo7;
function Bar(){}
',
'<?php
namespace Foo7 {
function Bar(){}
}',
['namespaces' => true],
];
yield [
'<?php
namespace Foo8\A;
function Bar(){}
?>',
"<?php
namespace Foo8\\A\t \t {
function Bar(){}
} ?>",
['namespaces' => true],
];
yield [
'<?php
namespace A;
class X {}
',
'<?php
namespace A {
class X {}
}',
['namespaces' => true],
];
yield [
'<?php
namespace {
class X {}
}',
null,
['namespaces' => true],
];
yield 'imports with single class in braces' => [
<<<'PHP'
<?php
use Foo1\Bar1;
use Foo2\Bar2\Baz2;
use NotFoo3\{Bar3, Baz3};
use NotFoo4\{Bar4\Baz4, Qux4};
use NotFoo5\{
Baz5
};
PHP,
<<<'PHP'
<?php
use Foo1\{Bar1};
use Foo2\{Bar2\Baz2};
use NotFoo3\{Bar3, Baz3};
use NotFoo4\{Bar4\Baz4, Qux4};
use NotFoo5\{
Baz5
};
PHP,
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield 'no fixes, offset access syntax with curly braces' => [
'<?php
echo ${$a};
echo $a{1};
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/ControlStructure/ElseifFixerTest.php | tests/Fixer/ControlStructure/ElseifFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\ControlStructure;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\ControlStructure\ElseifFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\ElseifFixer>
*
* @author Leszek Prabucki <leszek.prabucki@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ElseifFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php if ($some) { $test = true; } else { $test = false; }'];
yield [
'<?php if ($some) { $test = true; } elseif ($some !== "test") { $test = false; }',
'<?php if ($some) { $test = true; } else if ($some !== "test") { $test = false; }',
];
yield [
'<?php if ($some) { $test = true; } elseif ($some !== "test") { $test = false; }',
'<?php if ($some) { $test = true; } else if ($some !== "test") { $test = false; }',
];
yield [
'<?php $js = \'if (foo.a) { foo.a = "OK"; } else if (foo.b) { foo.b = "OK"; }\';',
];
yield [
'<?php
if ($a) {
$x = 1;
} elseif ($b) {
$x = 2;
}',
'<?php
if ($a) {
$x = 1;
} else
if ($b) {
$x = 2;
}',
];
yield [
'<?php
if ($a) {
} elseif/**/ ($b) {
}
',
'<?php
if ($a) {
} else /**/ if ($b) {
}
',
];
yield [
'<?php
if ($a) {
} elseif//
($b) {
}
',
'<?php
if ($a) {
} else //
if ($b) {
}
',
];
yield [
'<?php if ($a) {} /**/elseif ($b){}',
'<?php if ($a) {} /**/else if ($b){}',
];
yield ['<?php if ($x) { foo(); } else if ($y): bar(); endif;'];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Basic/OctalNotationFixerTest.php | tests/Fixer/Basic/OctalNotationFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Basic;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Basic\OctalNotationFixer
*
* @requires PHP 8.1
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\OctalNotationFixer>
*
* @author SpacePossum
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class OctalNotationFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
$a = 0;
$b = \'0\';
$foo = 0b01;
$foo = 0x01;
$foo = 0B01;
$foo = 0X01;
$foo = 0b0;
$foo = 0x0;
$foo = 0B0;
$foo = 0X0;
$foo = 1;
$foo = 10;
',
];
yield [
'<?php $foo = 0o0000;',
'<?php $foo = 00000;',
];
yield [
'<?php
$foo = 0o123;
$foo = 0o1;
',
'<?php
$foo = 0123;
$foo = 01;
',
];
yield [
'<?php $foo = 0o1_234;',
'<?php $foo = 01_234;',
];
yield [
'<?php $foo = +0o1_234;',
'<?php $foo = +01_234;',
];
yield [
'<?php $foo = -0o123_456;',
'<?php $foo = -0_123_456;',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Basic/CurlyBracesPositionFixerTest.php | tests/Fixer/Basic/CurlyBracesPositionFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Basic;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Basic\CurlyBracesPositionFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\CurlyBracesPositionFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Basic\CurlyBracesPositionFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class CurlyBracesPositionFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'if (default)' => [
'<?php
if ($foo) {
foo();
}',
'<?php
if ($foo)
{
foo();
}',
];
yield 'if (next line)' => [
'<?php
if ($foo)
{
foo();
}',
'<?php
if ($foo) {
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'else (default)' => [
'<?php
if ($foo) {
foo();
}
else {
bar();
}',
'<?php
if ($foo)
{
foo();
}
else
{
bar();
}',
];
yield 'else (next line)' => [
'<?php
if ($foo)
{
foo();
}
else
{
bar();
}',
'<?php
if ($foo) {
foo();
}
else {
bar();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'elseif (default)' => [
'<?php
if ($foo) {
foo();
}
elseif ($bar) {
bar();
}',
'<?php
if ($foo)
{
foo();
}
elseif ($bar)
{
bar();
}',
];
yield 'elseif (next line)' => [
'<?php
if ($foo)
{
foo();
}
elseif ($bar)
{
bar();
}',
'<?php
if ($foo) {
foo();
}
elseif ($bar) {
bar();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'else if (default)' => [
'<?php
if ($foo) {
foo();
}
else if ($bar) {
bar();
}',
'<?php
if ($foo)
{
foo();
}
else if ($bar)
{
bar();
}',
];
yield 'else if (next line)' => [
'<?php
if ($foo)
{
foo();
}
else if ($bar)
{
bar();
}',
'<?php
if ($foo) {
foo();
}
else if ($bar) {
bar();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'for (default)' => [
'<?php
for (;;) {
foo();
}',
'<?php
for (;;)
{
foo();
}',
];
yield 'for (next line)' => [
'<?php
for (;;)
{
foo();
}',
'<?php
for (;;) {
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'foreach (default)' => [
'<?php
foreach ($foo as $bar) {
foo();
}',
'<?php
foreach ($foo as $bar)
{
foo();
}',
];
yield 'foreach (next line)' => [
'<?php
foreach ($foo as $bar)
{
foo();
}',
'<?php
foreach ($foo as $bar) {
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'while (default)' => [
'<?php
while ($foo) {
foo();
}',
'<?php
while ($foo)
{
foo();
}',
];
yield 'while (next line)' => [
'<?php
while ($foo)
{
foo();
}',
'<?php
while ($foo) {
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'do while (default)' => [
'<?php
do {
foo();
} while ($foo);',
'<?php
do
{
foo();
} while ($foo);',
];
yield 'do while (next line)' => [
'<?php
do
{
foo();
} while ($foo);',
'<?php
do {
foo();
} while ($foo);',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'switch (default)' => [
'<?php
switch ($foo) {
case 1:
foo();
}',
'<?php
switch ($foo)
{
case 1:
foo();
}',
];
yield 'switch (next line)' => [
'<?php
switch ($foo)
{
case 1:
foo();
}',
'<?php
switch ($foo) {
case 1:
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'try catch finally (open brace on the same line)' => [
'<?php
try {
foo();
}
catch (Exception $e) {
bar();
}
finally {
baz();
}',
'<?php
try
{
foo();
}
catch (Exception $e)
{
bar();
}
finally
{
baz();
}',
];
yield 'try catch finally (open brace on the next line)' => [
'<?php
try
{
foo();
}
catch (Exception $e)
{
bar();
}
finally
{
baz();
}',
'<?php
try {
foo();
}
catch (Exception $e) {
bar();
}
finally {
baz();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'class (default)' => [
'<?php
class Foo
{
}',
'<?php
class Foo {
}',
];
yield 'class (same line)' => [
'<?php
class Foo {
}',
'<?php
class Foo
{
}',
['classes_opening_brace' => 'same_line'],
];
yield 'function (default)' => [
'<?php
function foo()
{
}',
'<?php
function foo() {
}',
];
yield 'function (same line)' => [
'<?php
function foo() {
}',
'<?php
function foo()
{
}',
['functions_opening_brace' => 'same_line'],
];
yield 'anonymous function (default)' => [
'<?php
$foo = function () {
};',
'<?php
$foo = function ()
{
};',
];
yield 'anonymous function (next line)' => [
'<?php
$foo = function ()
{
};',
'<?php
$foo = function () {
};',
['anonymous_functions_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'with blank lines inside braces' => [
'<?php
class Foo
{
public function foo()
{
if (true) {
echo "foo";
}
}
}',
'<?php
class Foo {
public function foo() {
if (true)
{
echo "foo";
}
}
}',
];
yield 'with comment after opening brace (default)' => [
'<?php
function foo() /* foo */ // foo
{
}',
'<?php
function foo() /* foo */ { // foo
}',
];
yield 'with comment after opening brace (same line)' => [
'<?php
function foo() { // foo
/* foo */
}',
'<?php
function foo() // foo
{ /* foo */
}',
['functions_opening_brace' => 'same_line'],
];
yield 'next line with multiline signature' => [
'<?php
if (
$foo
) {
foo();
}',
'<?php
if (
$foo
)
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with newline before closing parenthesis' => [
'<?php
if ($foo
) {
foo();
}',
'<?php
if ($foo
)
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with newline in signature but not before closing parenthesis' => [
'<?php
if (
$foo)
{
foo();
}',
'<?php
if (
$foo) {
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'anonymous class (same line)' => [
'<?php
$foo = new class() {
};',
'<?php
$foo = new class()
{
};',
];
yield 'anonymous class (next line)' => [
'<?php
$foo = new class()
{
};',
'<?php
$foo = new class() {
};',
['anonymous_classes_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with multiline signature and return type' => [
'<?php
function foo(
$foo
): int {
foo();
}',
'<?php
function foo(
$foo
): int
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with multiline signature and return type (nullable)' => [
'<?php
function foo(
$foo
): ?int {
foo();
}',
'<?php
function foo(
$foo
): ?int
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with multiline signature and return type (array)' => [
'<?php
function foo(
$foo
): array {
foo();
}',
'<?php
function foo(
$foo
): array
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with multiline signature and return type (class name)' => [
'<?php
function foo(
$foo
): \Foo\Bar {
foo();
}',
'<?php
function foo(
$foo
): \Foo\Bar
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with newline before closing parenthesis and return type' => [
'<?php
function foo($foo
): int {
foo();
}',
'<?php
function foo($foo
): int
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with newline before closing parenthesis and callable type' => [
'<?php
function foo($foo
): callable {
return function (): void {};
}',
'<?php
function foo($foo
): callable
{
return function (): void {};
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with newline in signature but not before closing parenthesis and return type' => [
'<?php
function foo(
$foo): int
{
foo();
}',
'<?php
function foo(
$foo): int {
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'multiple elseifs' => [
'<?php if ($foo) {
} elseif ($foo) {
} elseif ($foo) {
} elseif ($foo) {
} elseif ($foo) {
} elseif ($foo) {
} elseif ($foo) {
} elseif ($foo) {
} elseif ($foo) {
}',
'<?php if ($foo){
} elseif ($foo){
} elseif ($foo){
} elseif ($foo){
} elseif ($foo){
} elseif ($foo){
} elseif ($foo){
} elseif ($foo){
} elseif ($foo){
}',
];
yield 'open brace preceded by comment and whitespace' => [
'<?php
if (true) { /* foo */
foo();
}',
'<?php
if (true) /* foo */ {
foo();
}',
];
yield 'open brace surrounded by comment and whitespace' => [
'<?php
if (true) { /* foo */ /* bar */
foo();
}',
'<?php
if (true) /* foo */ { /* bar */
foo();
}',
];
yield 'open brace not preceded by space and followed by a comment' => [
'<?php class test
{
public function example()// example
{
}
}
',
'<?php class test
{
public function example(){// example
}
}
',
];
yield 'open brace not preceded by space and followed by a space and comment' => [
'<?php class test
{
public function example() // example
{
}
}
',
'<?php class test
{
public function example(){ // example
}
}
',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'function (multiline + union return)' => [
'<?php
function sum(
int|float $first,
int|float $second,
): int|float {
}',
'<?php
function sum(
int|float $first,
int|float $second,
): int|float
{
}',
];
yield 'function (multiline + union return with whitespace)' => [
'<?php
function sum(
int|float $first,
int|float $second,
): int | float {
}',
'<?php
function sum(
int|float $first,
int|float $second,
): int | float
{
}',
];
yield 'method with static return type' => [
'<?php
class Foo
{
function sum(
$foo
): static {
}
}',
'<?php
class Foo
{
function sum(
$foo
): static
{
}
}',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'function (multiline + intersection return)' => [
'<?php
function foo(
mixed $bar,
mixed $baz,
): Foo&Bar {
}',
];
}
/**
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix82Cases(): iterable
{
yield 'function (multiline + DNF return)' => [
'<?php
function foo(
mixed $bar,
mixed $baz,
): (Foo&Bar)|int|null {
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Basic/NonPrintableCharacterFixerTest.php | tests/Fixer/Basic/NonPrintableCharacterFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Basic;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Basic\NonPrintableCharacterFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\NonPrintableCharacterFixer>
*
* @author Ivan Boprzenkov <ivan.borzenkov@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Basic\NonPrintableCharacterFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NonPrintableCharacterFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, null|string, _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php echo "Hello World !";',
'<?php echo "'.pack('H*', 'e2808b').'Hello'.pack('H*', 'e28087').'World'.pack('H*', 'c2a0').'!";',
['use_escape_sequences_in_strings' => false],
];
yield [
'<?php echo "Hello World !";',
'<?php echo "'
.pack('H*', 'e2808b')
.pack('H*', 'e2808b')
.pack('H*', 'e2808b')
.pack('H*', 'e2808b')
.pack('H*', 'e2808b')
.pack('H*', 'e2808b')
.'Hello World !";',
['use_escape_sequences_in_strings' => false],
];
yield [
'<?php
// echo
echo "Hello World !";',
'<?php
// ec'.pack('H*', 'e2808b').'ho
echo "Hello'.pack('H*', 'e280af').'World'.pack('H*', 'c2a0').'!";',
['use_escape_sequences_in_strings' => false],
];
yield [
'<?php
/**
* @param string $p Param
*/
function f(string $p)
{
echo $p;
}',
'<?php
/**
* @param '.pack('H*', 'e2808b').'string $p Param
*/
function f(string $p)
{
echo $p;
}',
['use_escape_sequences_in_strings' => false],
];
yield [
'<?php echo "$a[0] ${a}";',
'<?php echo "$a'.pack('H*', 'e2808b').'[0]'.pack('H*', 'e2808b').' ${a'.pack('H*', 'e2808b').'}";',
['use_escape_sequences_in_strings' => false],
];
yield [
'<?php echo \'12345\';?>abc<?php ?>',
'<?php echo \'123'.pack('H*', 'e2808b').'45\';?>a'.pack('H*', 'e2808b').'bc<?php ?>',
['use_escape_sequences_in_strings' => false],
];
yield [
'<?php echo "${foo'.pack('H*', 'c2a0').'bar} is great!";',
null,
['use_escape_sequences_in_strings' => false],
];
yield [
'<?php echo $foo'.pack('H*', 'c2a0').'bar;',
null,
['use_escape_sequences_in_strings' => false],
];
yield [
'<?php /* foo *'.pack('H*', 'e2808b').'/ bar */',
null,
['use_escape_sequences_in_strings' => false],
];
yield [
'<?php /** foo *'.pack('H*', 'e2808b').'/ bar */',
null,
['use_escape_sequences_in_strings' => false],
];
yield [
'<?php echo b"Hello World !";',
'<?php echo b"'.pack('H*', 'e2808b').'Hello'.pack('H*', 'e28087').'World'.pack('H*', 'c2a0').'!";',
['use_escape_sequences_in_strings' => false],
];
yield [
'<?php
/**
* @param string $p Param
*/
function f(string $p)
{
echo $p;
}',
'<?php
/**
* @param '.pack('H*', 'e2808b').'string $p Param
*/
function f(string $p)
{
echo $p;
}',
['use_escape_sequences_in_strings' => true],
];
yield [
'<?php echo \'FooBar\\\\\';',
null,
['use_escape_sequences_in_strings' => true],
];
yield [
'<?php echo "Foo\u{200b}Bar";',
'<?php echo "Foo'.pack('H*', 'e2808b').'Bar";',
['use_escape_sequences_in_strings' => true],
];
yield [
'<?php echo "Foo\u{200b}Bar";',
'<?php echo \'Foo'.pack('H*', 'e2808b').'Bar\';',
['use_escape_sequences_in_strings' => true],
];
yield [
'<?php echo "Foo\u{200b} Bar \\\n \\\ \$variableToEscape";',
'<?php echo \'Foo'.pack('H*', 'e2808b').' Bar \n \ $variableToEscape\';',
['use_escape_sequences_in_strings' => true],
];
yield [
'<?php echo <<<\'TXT\'
FooBar\
TXT;
',
null,
['use_escape_sequences_in_strings' => true],
];
yield [
'<?php echo <<<TXT
Foo\u{200b}Bar
TXT;
',
'<?php echo <<<TXT
Foo'.pack('H*', 'e2808b').'Bar
TXT;
',
['use_escape_sequences_in_strings' => true],
];
yield [
'<?php echo <<<TXT
Foo\u{200b}Bar
TXT;
',
'<?php echo <<<\'TXT\'
Foo'.pack('H*', 'e2808b').'Bar
TXT;
',
['use_escape_sequences_in_strings' => true],
];
yield [
'<?php echo <<<TXT
Foo\u{200b} Bar \\\n \\\ \$variableToEscape
TXT;
',
'<?php echo <<<\'TXT\'
Foo'.pack('H*', 'e2808b').' Bar \n \ $variableToEscape
TXT;
',
['use_escape_sequences_in_strings' => true],
];
yield [
'<?php echo \'。\';',
null,
['use_escape_sequences_in_strings' => true],
];
yield [
<<<'EXPECTED'
<?php echo "Double \" quote \u{200b} inside";
EXPECTED,
\sprintf(
<<<'INPUT'
<?php echo 'Double " quote %s inside';
INPUT,
pack('H*', 'e2808b'),
),
['use_escape_sequences_in_strings' => true],
];
yield [
<<<'EXPECTED'
<?php echo "Single ' quote \u{200b} inside";
EXPECTED,
\sprintf(
<<<'INPUT'
<?php echo 'Single \' quote %s inside';
INPUT,
pack('H*', 'e2808b'),
),
['use_escape_sequences_in_strings' => true],
];
yield [
<<<'EXPECTED'
<?php echo <<<STRING
Quotes ' and " to be handled \u{200b} properly \\' and \\"
STRING
;
EXPECTED,
\sprintf(
<<<'INPUT'
<?php echo <<<'STRING'
Quotes ' and " to be handled %s properly \' and \"
STRING
;
INPUT,
pack('H*', 'e2808b'),
),
['use_escape_sequences_in_strings' => true],
];
yield [
<<<'EXPECTED'
<?php echo "\\\u{200b}\"";
EXPECTED,
\sprintf(
<<<'INPUT'
<?php echo '\\%s"';
INPUT,
pack('H*', 'e2808b'),
),
['use_escape_sequences_in_strings' => true],
];
yield [
<<<'EXPECTED'
<?php echo "\\\u{200b}'";
EXPECTED,
\sprintf(
<<<'INPUT'
<?php echo '\\%s\'';
INPUT,
pack('H*', 'e2808b'),
),
['use_escape_sequences_in_strings' => true],
];
yield [
<<<'EXPECTED'
<?php echo "Backslash 1 \\ \u{200b}";
EXPECTED,
\sprintf(
<<<'INPUT'
<?php echo 'Backslash 1 \ %s';
INPUT,
pack('H*', 'e2808b'),
),
['use_escape_sequences_in_strings' => true],
];
yield [
<<<'EXPECTED'
<?php echo "Backslash 2 \\ \u{200b}";
EXPECTED,
\sprintf(
<<<'INPUT'
<?php echo 'Backslash 2 \\ %s';
INPUT,
pack('H*', 'e2808b'),
),
['use_escape_sequences_in_strings' => true],
];
yield [
<<<'EXPECTED'
<?php echo "Backslash 3 \\\\ \u{200b}";
EXPECTED,
\sprintf(
<<<'INPUT'
<?php echo 'Backslash 3 \\\ %s';
INPUT,
pack('H*', 'e2808b'),
),
['use_escape_sequences_in_strings' => true],
];
yield [
<<<'EXPECTED'
<?php echo "Backslash 4 \\\\ \u{200b}";
EXPECTED,
\sprintf(
<<<'INPUT'
<?php echo 'Backslash 4 \\\\ %s';
INPUT,
pack('H*', 'e2808b'),
),
['use_escape_sequences_in_strings' => true],
];
yield [
"<?php \"String in single quotes, having non-breaking space: \\u{a0}, linebreak: \n, and single quote inside: ' is a dangerous mix.\";",
"<?php 'String in single quotes, having non-breaking space: ".pack('H*', 'c2a0').", linebreak: \n, and single quote inside: \\' is a dangerous mix.';",
['use_escape_sequences_in_strings' => true],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Basic/BracesPositionFixerTest.php | tests/Fixer/Basic/BracesPositionFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Basic;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Basic\BracesPositionFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\BracesPositionFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Basic\BracesPositionFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class BracesPositionFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'if (default)' => [
'<?php
if ($foo) {
foo();
}',
'<?php
if ($foo)
{
foo();
}',
];
yield 'if (next line)' => [
'<?php
if ($foo)
{
foo();
}',
'<?php
if ($foo) {
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'else (default)' => [
'<?php
if ($foo) {
foo();
}
else {
bar();
}',
'<?php
if ($foo)
{
foo();
}
else
{
bar();
}',
];
yield 'else (next line)' => [
'<?php
if ($foo)
{
foo();
}
else
{
bar();
}',
'<?php
if ($foo) {
foo();
}
else {
bar();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'elseif (default)' => [
'<?php
if ($foo) {
foo();
}
elseif ($bar) {
bar();
}',
'<?php
if ($foo)
{
foo();
}
elseif ($bar)
{
bar();
}',
];
yield 'elseif (next line)' => [
'<?php
if ($foo)
{
foo();
}
elseif ($bar)
{
bar();
}',
'<?php
if ($foo) {
foo();
}
elseif ($bar) {
bar();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'else if (default)' => [
'<?php
if ($foo) {
foo();
}
else if ($bar) {
bar();
}',
'<?php
if ($foo)
{
foo();
}
else if ($bar)
{
bar();
}',
];
yield 'else if (next line)' => [
'<?php
if ($foo)
{
foo();
}
else if ($bar)
{
bar();
}',
'<?php
if ($foo) {
foo();
}
else if ($bar) {
bar();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'for (default)' => [
'<?php
for (;;) {
foo();
}',
'<?php
for (;;)
{
foo();
}',
];
yield 'for (next line)' => [
'<?php
for (;;)
{
foo();
}',
'<?php
for (;;) {
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'foreach (default)' => [
'<?php
foreach ($foo as $bar) {
foo();
}',
'<?php
foreach ($foo as $bar)
{
foo();
}',
];
yield 'foreach (next line)' => [
'<?php
foreach ($foo as $bar)
{
foo();
}',
'<?php
foreach ($foo as $bar) {
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'while (default)' => [
'<?php
while ($foo) {
foo();
}',
'<?php
while ($foo)
{
foo();
}',
];
yield 'while (next line)' => [
'<?php
while ($foo)
{
foo();
}',
'<?php
while ($foo) {
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'do while (default)' => [
'<?php
do {
foo();
} while ($foo);',
'<?php
do
{
foo();
} while ($foo);',
];
yield 'do while (next line)' => [
'<?php
do
{
foo();
} while ($foo);',
'<?php
do {
foo();
} while ($foo);',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'switch (default)' => [
'<?php
switch ($foo) {
case 1:
foo();
}',
'<?php
switch ($foo)
{
case 1:
foo();
}',
];
yield 'switch (next line)' => [
'<?php
switch ($foo)
{
case 1:
foo();
}',
'<?php
switch ($foo) {
case 1:
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'try catch finally (open brace on the same line)' => [
'<?php
try {
foo();
}
catch (Exception $e) {
bar();
}
finally {
baz();
}',
'<?php
try
{
foo();
}
catch (Exception $e)
{
bar();
}
finally
{
baz();
}',
];
yield 'try catch finally (open brace on the next line)' => [
'<?php
try
{
foo();
}
catch (Exception $e)
{
bar();
}
finally
{
baz();
}',
'<?php
try {
foo();
}
catch (Exception $e) {
bar();
}
finally {
baz();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'class (default)' => [
'<?php
class Foo
{
}',
'<?php
class Foo {
}',
];
yield 'class (same line)' => [
'<?php
class Foo {
}',
'<?php
class Foo
{
}',
['classes_opening_brace' => 'same_line'],
];
yield 'function (default)' => [
'<?php
function foo()
{
}',
'<?php
function foo() {
}',
];
yield 'function (same line)' => [
'<?php
function foo() {
}',
'<?php
function foo()
{
}',
['functions_opening_brace' => 'same_line'],
];
yield 'anonymous function (default)' => [
'<?php
$foo = function () {
};',
'<?php
$foo = function ()
{
};',
];
yield 'anonymous function (next line)' => [
'<?php
$foo = function ()
{
};',
'<?php
$foo = function () {
};',
['anonymous_functions_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'with blank lines inside braces' => [
'<?php
class Foo
{
public function foo()
{
if (true) {
echo "foo";
}
}
}',
'<?php
class Foo {
public function foo() {
if (true)
{
echo "foo";
}
}
}',
];
yield 'with comment after opening brace (default)' => [
'<?php
function foo() /* foo */ // foo
{
}',
'<?php
function foo() /* foo */ { // foo
}',
];
yield 'with comment after opening brace (same line)' => [
'<?php
function foo() { // foo
/* foo */
}',
'<?php
function foo() // foo
{ /* foo */
}',
['functions_opening_brace' => 'same_line'],
];
yield 'next line with multiline signature' => [
'<?php
if (
$foo
) {
foo();
}',
'<?php
if (
$foo
)
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with newline before closing parenthesis' => [
'<?php
if ($foo
) {
foo();
}',
'<?php
if ($foo
)
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with newline in signature but not before closing parenthesis' => [
'<?php
if (
$foo)
{
foo();
}',
'<?php
if (
$foo) {
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'anonymous class (same line)' => [
'<?php
$foo = new class() {
};',
'<?php
$foo = new class()
{
};',
];
yield 'anonymous class (next line)' => [
'<?php
$foo = new class()
{
};',
'<?php
$foo = new class() {
};',
['anonymous_classes_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with multiline signature and return type' => [
'<?php
function foo(
$foo
): int {
foo();
}',
'<?php
function foo(
$foo
): int
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with multiline signature and return type (nullable)' => [
'<?php
function foo(
$foo
): ?int {
foo();
}',
'<?php
function foo(
$foo
): ?int
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with multiline signature and return type (array)' => [
'<?php
function foo(
$foo
): array {
foo();
}',
'<?php
function foo(
$foo
): array
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with multiline signature and return type (class name)' => [
'<?php
function foo(
$foo
): \Foo\Bar {
foo();
}',
'<?php
function foo(
$foo
): \Foo\Bar
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with newline before closing parenthesis and return type' => [
'<?php
function foo($foo
): int {
foo();
}',
'<?php
function foo($foo
): int
{
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with newline before closing parenthesis and callable type' => [
'<?php
function foo($foo
): callable {
return function (): void {};
}',
'<?php
function foo($foo
): callable
{
return function (): void {};
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'next line with newline in signature but not before closing parenthesis and return type' => [
'<?php
function foo(
$foo): int
{
foo();
}',
'<?php
function foo(
$foo): int {
foo();
}',
['control_structures_opening_brace' => 'next_line_unless_newline_at_signature_end'],
];
yield 'multiple elseifs' => [
'<?php if ($foo) {
} elseif ($foo) {
} elseif ($foo) {
} elseif ($foo) {
} elseif ($foo) {
} elseif ($foo) {
} elseif ($foo) {
} elseif ($foo) {
} elseif ($foo) {
}',
'<?php if ($foo){
} elseif ($foo){
} elseif ($foo){
} elseif ($foo){
} elseif ($foo){
} elseif ($foo){
} elseif ($foo){
} elseif ($foo){
} elseif ($foo){
}',
];
yield 'open brace preceded by comment and whitespace' => [
'<?php
if (true) { /* foo */
foo();
}',
'<?php
if (true) /* foo */ {
foo();
}',
];
yield 'open brace surrounded by comment and whitespace' => [
'<?php
if (true) { /* foo */ /* bar */
foo();
}',
'<?php
if (true) /* foo */ { /* bar */
foo();
}',
];
yield 'open brace not preceded by space and followed by a comment' => [
'<?php class test
{
public function example()// example
{
}
}
',
'<?php class test
{
public function example(){// example
}
}
',
];
yield 'open brace not preceded by space and followed by a space and comment' => [
'<?php class test
{
public function example() // example
{
}
}
',
'<?php class test
{
public function example(){ // example
}
}
',
];
yield [
<<<'PHP'
<?php
if ($x) {//
f();
}
PHP,
<<<'PHP'
<?php
if ($x)//
{
f();
}
PHP,
];
yield 'variable terminated by close tag' => [
<<<'PHP'
<?php
$pager = new class {
public function bar()
{
return 'baz';
}
};
echo $pager->bar()
?>
PHP,
<<<'PHP'
<?php
$pager = new class { public function bar() { return 'baz'; } };
echo $pager->bar()
?>
PHP,
];
yield 'confirm {$foo} does not cause harm' => [
<<<'PHP'
<?php
$foo = "foo";
if($foo) {
echo "
/* some sql */ {$foo} /* some sql */;
";
}
PHP,
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'function (multiline + union return)' => [
'<?php
function sum(
int|float $first,
int|float $second,
): int|float {
}',
'<?php
function sum(
int|float $first,
int|float $second,
): int|float
{
}',
];
yield 'function (multiline + union return with whitespace)' => [
'<?php
function sum(
int|float $first,
int|float $second,
): int | float {
}',
'<?php
function sum(
int|float $first,
int|float $second,
): int | float
{
}',
];
yield 'method with static return type' => [
'<?php
class Foo
{
function sum(
$foo
): static {
}
}',
'<?php
class Foo
{
function sum(
$foo
): static
{
}
}',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'function (multiline + intersection return)' => [
'<?php
function foo(
mixed $bar,
mixed $baz,
): Foo&Bar {
}',
];
}
/**
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix82Cases(): iterable
{
yield 'function (multiline + DNF return)' => [
'<?php
function foo(
mixed $bar,
mixed $baz,
): (Foo&Bar)|int|null {
}',
];
}
/**
* @dataProvider provideFix84Cases
*
* @requires PHP 8.4
*/
public function testFix84(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix84Cases(): iterable
{
yield 'property hook one-liners' => [
<<<'PHP'
<?php abstract class Foo
{
abstract public bool $b { get; set; }
abstract public int $i { set(int $i) { $this->i = $i + 10; } get; }
}
PHP,
];
yield 'property hook' => [
<<<'PHP'
<?php class C
{
private int $i {
get => 2;
}
}
PHP,
<<<'PHP'
<?php class C
{
private int $i {
get => 2;
}
}
PHP,
];
yield 'property hook with default' => [
<<<'PHP'
<?php class C
{
private mixed $wat = [-7, "a" . 'b', null, array(), 1/3, self::FOO, ] {
set(mixed $wat) {
$this->wat = $wat;
}
}
private mixed $wat2 = array(-7, "a" . 'b', null, array(), 1/3, self::FOO, ) {
set(mixed $wat2) {
$this->wat2 = $wat2;
}
}
}
PHP,
<<<'PHP'
<?php class C
{
private mixed $wat = [-7, "a" . 'b', null, array(), 1/3, self::FOO, ] {
set(mixed $wat) {
$this->wat = $wat;
}
}
private mixed $wat2 = array(-7, "a" . 'b', null, array(), 1/3, self::FOO, ) {
set(mixed $wat2) {
$this->wat2 = $wat2;
}
}
}
PHP,
];
yield 'property hook in promoted property' => [
<<<'PHP'
<?php class CarPark
{
public function __construct(
public Car $car {
set(Car $car) {
$this->car = $car;
$this->car->parked();
}
},
) {}
}
PHP,
<<<'PHP'
<?php class CarPark
{
public function __construct(
public Car $car {
set(Car $car)
{
$this->car = $car;
$this->car->parked();
}
},
) {}
}
PHP,
];
yield 'property hook in promoted property with default' => [
<<<'PHP'
<?php class IntVal
{
public function __construct(
public int $int = 5 {
set(int $int) {
$this->int = $int;
}
},
) {}
}
PHP,
<<<'PHP'
<?php class IntVal
{
public function __construct(
public int $int = 5 {
set(int $int)
{
$this->int = $int;
}
},
) {}
}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Basic/EncodingFixerTest.php | tests/Fixer/Basic/EncodingFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Basic;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Basic\EncodingFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\EncodingFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class EncodingFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, ?\SplFileInfo $file = null): void
{
$this->doTest($expected, $input, $file);
}
/**
* @return iterable<int, array{0: string, 1?: string, 2?: \SplFileInfo}>
*/
public static function provideFixCases(): iterable
{
yield self::prepareTestCase('test-utf8.case1.php', 'test-utf8.case1-bom.php');
yield self::prepareTestCase('test-utf8.case2.php', 'test-utf8.case2-bom.php');
yield ['<?php '];
}
/**
* @return array{string, null|string, \SplFileInfo}
*/
private static function prepareTestCase(string $expectedFilename, ?string $inputFilename = null): array
{
$expectedFile = new \SplFileInfo(__DIR__.'/../../Fixtures/FixerTest/encoding/'.$expectedFilename);
$inputFile = null !== $inputFilename ? new \SplFileInfo(__DIR__.'/../../Fixtures/FixerTest/encoding/'.$inputFilename) : null;
return [
(string) file_get_contents($expectedFile->getRealPath()),
null !== $inputFile ? (string) file_get_contents($inputFile->getRealPath()) : null,
$inputFile ?? $expectedFile,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Basic/NumericLiteralSeparatorFixerTest.php | tests/Fixer/Basic/NumericLiteralSeparatorFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Basic;
use PhpCsFixer\Fixer\Basic\NumericLiteralSeparatorFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Basic\NumericLiteralSeparatorFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\NumericLiteralSeparatorFixer>
*
* @author Marvin Heilemann <marvin.heilemann+github@googlemail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Basic\NumericLiteralSeparatorFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NumericLiteralSeparatorFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, ?array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'do not override existing separator' => [
<<<'PHP'
<?php
echo 0B01010100_01101000;
echo 70_10_00;
PHP,
null,
[
'override_existing' => false,
'strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR,
],
];
yield 'override existing separator' => [
<<<'PHP'
<?php
echo 1_234.5;
echo 701_000;
PHP,
<<<'PHP'
<?php
echo 123_4.5;
echo 70_10_00;
PHP,
[
'override_existing' => true,
'strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR,
],
];
yield from self::yieldCases([
'decimal' => [
'1234' => '1_234',
'-1234' => '-1_234',
'12345' => '12_345',
'123456' => '123_456',
],
'binary' => [
'0b0101010001101000' => '0b01010100_01101000',
'0b01010100011010000110010101101111' => '0b01010100_01101000_01100101_01101111',
'0b110001000' => '0b1_10001000',
],
'float' => [
'.001' => null,
'.1001' => '.100_1',
'0.0001' => '0.000_1',
'0.001' => null,
'1234.5' => '1_234.5',
'1.2345' => '1.234_5',
'1234e5' => '1_234e5',
'1234E5' => '1_234E5',
'1e2345' => '1e2_345',
'1234.5678e1234' => '1_234.567_8e1_234',
'.5678e1234' => '.567_8e1_234',
'1.1e-1234' => '1.1e-1_234',
'1.1e-12345' => '1.1e-12_345',
'1.1e-123456' => '1.1e-123_456',
'.1e-12345' => '.1e-12_345',
],
'hexadecimal' => [
'0x42726F776E' => '0x42_72_6F_77_6E',
'0X42726F776E' => '0X42_72_6F_77_6E',
'0x2726F776E' => '0x2_72_6F_77_6E',
'0x1234567890abcdef' => '0x12_34_56_78_90_ab_cd_ef',
'0X1234567890ABCDEF' => '0X12_34_56_78_90_AB_CD_EF',
'0x1234e5' => '0x12_34_e5',
],
'octal' => [
'012345' => '012_345',
'0123456' => '0123_456',
'01234567' => '01_234_567',
],
]);
yield 'do not change float to int when there is nothing after the dot' => ['<?php $x = 100.;'];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @requires PHP 8.1
*
* @dataProvider provideFix81Cases
*/
public function testFix81(string $expected, ?string $input = null, ?array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix81Cases(): iterable
{
yield 'do not override existing separator' => [
'<?php echo 0o123_45;',
null,
[
'override_existing' => false,
'strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR,
],
];
yield 'override existing separator' => [
'<?php echo 1_234.5;',
'<?php echo 123_4.5;',
[
'override_existing' => true,
'strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR,
],
];
yield from self::yieldCases([
'octal' => [
'0o12345' => '0o12_345',
'0o123456' => '0o123_456',
],
]);
}
/**
* @param array<string, array<mixed, mixed>> $cases
*
* @return iterable<string, array{0: string, 1?: null|string, 2?: array<string, mixed>}>
*/
private static function yieldCases(array $cases): iterable
{
foreach ($cases as $pairsType => $pairs) {
foreach ($pairs as $withoutSeparator => $withSeparator) {
if (null === $withSeparator) {
yield "do not modify valid {$pairsType} {$withoutSeparator}" => [
\sprintf('<?php echo %s;', $withoutSeparator),
null,
['strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR],
];
} else {
yield "add separator to {$pairsType} {$withoutSeparator}" => [
\sprintf('<?php echo %s;', $withSeparator),
\sprintf('<?php echo %s;', $withoutSeparator),
['strategy' => NumericLiteralSeparatorFixer::STRATEGY_USE_SEPARATOR],
];
}
}
foreach ($pairs as $withoutSeparator => $withSeparator) {
if (null === $withSeparator) {
continue;
}
yield "remove separator from {$pairsType} {$withoutSeparator}" => [
\sprintf('<?php echo %s;', $withoutSeparator),
\sprintf('<?php echo %s;', $withSeparator),
['strategy' => NumericLiteralSeparatorFixer::STRATEGY_NO_SEPARATOR],
];
}
}
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Basic/NoTrailingCommaInSinglelineFixerTest.php | tests/Fixer/Basic/NoTrailingCommaInSinglelineFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Basic;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Basic\NoTrailingCommaInSinglelineFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoTrailingCommaInSinglelineFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{string, null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php [$x, $y] = $a;',
'<?php [$x, $y,] = $a;',
];
yield 'group_import' => [
'<?php use a\{ClassA, ClassB};',
'<?php use a\{ClassA, ClassB,};',
];
yield 'lots of nested' => [
'<?php $a = [1,[1,[1,[1,[1,[1,[1,[1]],[1,[1,[1,[1,[1,[1,[1,[1]]]]]]]]]]]]]];',
'<?php $a = [1,[1,[1,[1,[1,[1,[1,[1,],],[1,[1,[1,[1,[1,[1,[1,[1,],],],],],],],],],],],],],];',
];
yield 'simple var' => [
'<?php $a(1);',
'<?php $a(1,);',
['elements' => ['arguments']],
];
yield '&' => [
'<?php $a = &foo($a);',
'<?php $a = &foo($a,);',
['elements' => ['arguments']],
];
yield 'open' => [
'<?php foo($a);',
'<?php foo($a,);',
['elements' => ['arguments']],
];
yield '=' => [
'<?php $b = foo($a);',
'<?php $b = foo($a,);',
['elements' => ['arguments']],
];
yield '.' => [
'<?php $c = $b . foo($a);',
'<?php $c = $b . foo($a,);',
['elements' => ['arguments']],
];
yield '(' => [
'<?php (foo($a/* 1X */ /* 2 */ ));',
'<?php (foo($a /* 1X */ , /* 2 */ ));',
['elements' => ['arguments']],
];
yield '\\' => [
'<?php \foo($a);',
'<?php \foo($a,);',
['elements' => ['arguments']],
];
yield 'A\\' => [
'<?php A\foo($a);',
'<?php A\foo($a,);',
['elements' => ['arguments']],
];
yield '\A\\' => [
'<?php \A\foo($a);',
'<?php \A\foo($a,);',
['elements' => ['arguments']],
];
yield ';' => [
'<?php ; foo($a);',
'<?php ; foo($a,);',
['elements' => ['arguments']],
];
yield '}' => [
'<?php if ($a) { echo 1;} foo($a);',
'<?php if ($a) { echo 1;} foo($a,);',
['elements' => ['arguments']],
];
yield 'test method call' => [
'<?php $o->abc($a);',
'<?php $o->abc($a,);',
['elements' => ['arguments']],
];
yield 'nested call' => [
'<?php $o->abc($a,foo(1));',
'<?php $o->abc($a,foo(1,));',
['elements' => ['arguments']],
];
yield 'wrapped' => [
'<?php echo (new Process())->getOutput(1);',
'<?php echo (new Process())->getOutput(1,);',
['elements' => ['arguments']],
];
yield 'dynamic function and method calls' => [
'<?php $b->$a(1); $c("");',
'<?php $b->$a(1,); $c("",);',
['elements' => ['arguments']],
];
yield 'static function call' => [
'<?php
unset($foo->bar);
$b = isset($foo->bar);
',
'<?php
unset($foo->bar,);
$b = isset($foo->bar,);
',
['elements' => ['arguments']],
];
yield 'unset' => [
'<?php A::foo(1);',
'<?php A::foo(1,);',
['elements' => ['arguments']],
];
yield 'anonymous_class construction' => [
'<?php new class(1, 2) {};',
'<?php new class(1, 2,) {};',
['elements' => ['arguments']],
];
yield 'array/property access call' => [
'<?php
$a = [
"e" => static function(int $a): void{ echo $a;},
"d" => [
[2 => static function(int $a): void{ echo $a;}]
]
];
$a["e"](1);
$a["d"][0][2](1);
$z = new class { public static function b(int $a): void {echo $a; }};
$z::b(1);
${$e}(1);
$$e(2);
$f(0)(1);
$g["e"](1); // foo',
'<?php
$a = [
"e" => static function(int $a): void{ echo $a;},
"d" => [
[2 => static function(int $a): void{ echo $a;}]
]
];
$a["e"](1,);
$a["d"][0][2](1,);
$z = new class { public static function b(int $a): void {echo $a; }};
$z::b(1,);
${$e}(1,);
$$e(2,);
$f(0,)(1,);
$g["e"](1,); // foo',
['elements' => ['arguments']],
];
yield 'do not fix' => [
'<?php
function someFunction ($p1){}
function & foo($a,$b): array { return []; }
foo (
1,
2,
);
$a = new class (
$a,
) {};
isset($a, $b);
unset($a,$b);
list($a,$b) = $a;
$a = [1,2,3,];
$a = array(1,2,3,);
function foo1(string $param = null ): void
{
}
;',
null,
['elements' => ['arguments']],
];
yield [
'<?php $x = array();',
null,
['elements' => ['array']],
];
yield [
'<?php $x = array("foo");',
null,
['elements' => ['array']],
];
yield [
'<?php $x = array("foo");',
'<?php $x = array("foo", );',
['elements' => ['array']],
];
yield [
"<?php \$x = array(\n'foo', \n);",
null,
['elements' => ['array']],
];
yield [
"<?php \$x = array('foo', \n);",
null,
['elements' => ['array']],
];
yield [
"<?php \$x = array(array('foo'), \n);",
"<?php \$x = array(array('foo',), \n);",
['elements' => ['array']],
];
yield [
"<?php \$x = array(array('foo',\n), \n);",
null,
['elements' => ['array']],
];
yield [
'<?php
$test = array("foo", <<<TWIG
foo
TWIG
, $twig, );',
null,
['elements' => ['array']],
];
yield [
'<?php
$test = array(
"foo", <<<TWIG
foo
TWIG
, $twig, );',
null,
['elements' => ['array']],
];
yield [
'<?php
$test = array("foo", <<<\'TWIG\'
foo
TWIG
, $twig, );',
null,
['elements' => ['array']],
];
yield [
'<?php
$test = array(
"foo", <<<\'TWIG\'
foo
TWIG
, $twig, );',
null,
['elements' => ['array']],
];
// Short syntax
yield [
'<?php $x = array([]);',
null,
['elements' => ['array']],
];
yield [
'<?php $x = [[]];',
null,
['elements' => ['array']],
];
yield [
'<?php $x = ["foo"];',
'<?php $x = ["foo",];',
['elements' => ['array']],
];
yield [
'<?php $x = bar(["foo"]);',
'<?php $x = bar(["foo",]);',
['elements' => ['array']],
];
yield [
"<?php \$x = bar([['foo'],\n]);",
null,
['elements' => ['array']],
];
yield [
"<?php \$x = ['foo', \n];",
null,
['elements' => ['array']],
];
yield [
'<?php $x = array([]);',
'<?php $x = array([],);',
['elements' => ['array']],
];
yield [
'<?php $x = [[]];',
'<?php $x = [[],];',
['elements' => ['array']],
];
yield [
'<?php $x = [$y[""]];',
'<?php $x = [$y[""],];',
['elements' => ['array']],
];
yield [
'<?php
$test = ["foo", <<<TWIG
foo
TWIG
, $twig, ];',
null,
['elements' => ['array']],
];
yield [
'<?php
$test = [
"foo", <<<TWIG
foo
TWIG
, $twig, ];',
null,
['elements' => ['array']],
];
yield [
'<?php
$test = ["foo", <<<\'TWIG\'
foo
TWIG
, $twig, ];',
null,
['elements' => ['array']],
];
yield [
'<?php
$test = [
"foo", <<<\'TWIG\'
foo
TWIG
, $twig, ];',
null,
['elements' => ['array']],
];
yield [
'<?php $x = array(...$foo);',
'<?php $x = array(...$foo, );',
['elements' => ['array']],
];
yield [
'<?php $x = [...$foo];',
'<?php $x = [...$foo, ];',
['elements' => ['array']],
];
yield [
'<?php
list($a1, $b) = foo();
list($a2, , $c, $d) = foo();
list($a3, , $c) = foo();
list($a4) = foo();
list($a5 , $b) = foo();
list($a6, /* $b */, $c) = foo();
',
'<?php
list($a1, $b) = foo();
list($a2, , $c, $d, ) = foo();
list($a3, , $c, , ) = foo();
list($a4, , , , , ) = foo();
list($a5 , $b , ) = foo();
list($a6, /* $b */, $c, ) = foo();
',
['elements' => ['array_destructuring']],
];
yield [
'<?php
list(
$a#
,#
#
) = $a;',
null,
['elements' => ['array_destructuring']],
];
yield [
'<?php
[$a7, $b] = foo();
[$a8, , $c, $d] = foo();
[$a9, , $c] = foo();
[$a10] = foo();
[$a11 , $b] = foo();
[$a12, /* $b */, $c] = foo();
',
'<?php
[$a7, $b] = foo();
[$a8, , $c, $d, ] = foo();
[$a9, , $c, , ] = foo();
[$a10, , , , , ] = foo();
[$a11 , $b , ] = foo();
[$a12, /* $b */, $c, ] = foo();
',
['elements' => ['array_destructuring']],
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php function foo(
#[MyAttr(1, 2,)] Type $myParam,
) {}
$foo1b = function() use ($bar, ) {};
',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php $object?->method(1); strlen(...);',
'<?php $object?->method(1,); strlen(...);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Basic/NoMultipleStatementsPerLineFixerTest.php | tests/Fixer/Basic/NoMultipleStatementsPerLineFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Basic;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Basic\NoMultipleStatementsPerLineFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\NoMultipleStatementsPerLineFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoMultipleStatementsPerLineFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'simple' => [
'<?php
foo();
bar();',
'<?php
foo(); bar();',
];
yield 'for loop' => [
'<?php
for ($i = 0; $i < 10; ++$i) {
foo();
}',
];
yield 'mixed `;` and close tag' => [
'<?php ++$a;
++$b ?>',
'<?php ++$a; ++$b ?>',
];
yield 'followed by closing brace' => [
'<?php if ($foo) { foo(); }',
];
yield 'followed by closing tag' => [
'<?php foo(); ?>',
];
yield 'if alternative syntax' => [
'<?php if ($foo): foo(); endif;',
];
yield 'for alternative syntax' => [
'<?php for (;;): foo(); endfor;',
];
yield 'foreach alternative syntax' => [
'<?php foreach ($foo as $bar): foo(); endforeach;',
];
yield 'while alternative syntax' => [
'<?php while ($foo): foo(); endwhile;',
];
yield 'switch alternative syntax' => [
'<?php switch ($foo): case true: foo(); endswitch;',
];
}
/**
* @dataProvider provideFix84Cases
*
* @requires PHP 8.4
*/
public function testFix84(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: string}>
*/
public static function provideFix84Cases(): iterable
{
yield "don't touch property hooks" => [
'<?php interface I {
public string $readable { get; }
public string $writeable { set; }
public string $both { get; set; }
public string $differentCasing { GET; Set; }
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Basic/PsrAutoloadingFixerTest.php | tests/Fixer/Basic/PsrAutoloadingFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Basic;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
* @author Kuba Werłos <werlos@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Basic\PsrAutoloadingFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PsrAutoloadingFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = [], ?\SplFileInfo $file = null): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input, $file ?? new \SplFileInfo(__FILE__));
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration, 3?: \SplFileInfo}>
*/
public static function provideFixCases(): iterable
{
foreach (['class', 'interface', 'trait'] as $element) {
yield \sprintf('%s with originally short name', $element) => [
\sprintf('<?php %s PsrAutoloadingFixerTest {}', $element),
\sprintf('<?php %s Foo {}', $element),
];
}
yield 'abstract class' => [
'<?php abstract class PsrAutoloadingFixerTest {}',
'<?php abstract class WrongName {}',
];
yield 'final class' => [
'<?php final class PsrAutoloadingFixerTest {}',
'<?php final class WrongName {}',
];
yield 'class with originally long name' => [
'<?php class PsrAutoloadingFixerTest {}',
'<?php class FooFooFooFooFooFooFooFooFooFooFooFooFoo {}',
];
yield 'class with wrong casing' => [
'<?php class PsrAutoloadingFixerTest {}',
'<?php class psrautoloadingfixertest {}',
];
yield 'namespaced class with wrong casing' => [
'<?php namespace Foo; class PsrAutoloadingFixerTest {}',
'<?php namespace Foo; class psrautoloadingfixertest {}',
];
yield 'class with wrong casing (1 level namespace)' => [
'<?php class Basic_PsrAutoloadingFixerTest {}',
'<?php class BASIC_PSRAUTOLOADINGFIXERTEST {}',
];
yield 'class with wrong casing (2 levels namespace)' => [
'<?php class Fixer_Basic_PsrAutoloadingFixerTest {}',
'<?php class FIXER_BASIC_PSRAUTOLOADINGFIXERTEST {}',
];
yield 'class with name not matching directory structure' => [
'<?php class PsrAutoloadingFixerTest {}',
'<?php class Aaaaa_Bbbbb_PsrAutoloadingFixerTest {}',
];
yield 'configured directory (1 subdirectory)' => [
'<?php class Basic_PsrAutoloadingFixerTest {}',
'<?php class PsrAutoloadingFixerTest {}',
['dir' => __DIR__.'/..'],
];
yield 'configured directory (2 subdirectories)' => [
'<?php class Fixer_Basic_PsrAutoloadingFixerTest {}',
'<?php class PsrAutoloadingFixerTest {}',
['dir' => __DIR__.'/../..'],
];
yield 'configured directory (other directory)' => [
'<?php namespace Basic; class Foobar {}',
null,
['dir' => __DIR__.'/../../Test'],
];
yield 'multiple classy elements in file' => [
'<?php interface Foo {} class Bar {}',
];
yield 'namespace with wrong casing' => [
'<?php namespace Fixer\Basic; class PsrAutoloadingFixerTest {}',
'<?php namespace Fixer\BASIC; class PsrAutoloadingFixerTest {}',
['dir' => __DIR__.'/../..'],
];
yield 'multiple namespaces in file' => [
'<?php namespace Foo\Helpers; function helper() {}; namespace Foo\Domain; class Feature {}',
];
yield 'namespace and class with comments' => [
'<?php namespace /* namespace here */ PhpCsFixer\Tests\Fixer\Basic; class /* hi there */ PsrAutoloadingFixerTest /* hello */ {} /* class end */',
'<?php namespace /* namespace here */ PhpCsFixer\Tests\Fixer\Basic; class /* hi there */ Foo /* hello */ {} /* class end */',
];
yield 'namespace partially matching directory structure' => [
'<?php namespace Foo\Bar\Baz\FIXER\Basic; class PsrAutoloadingFixerTest {}',
];
yield 'namespace partially matching directory structure with comment' => [
'<?php namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic; class /* hi there */ PsrAutoloadingFixerTest {}',
];
yield 'namespace partially matching directory structure with configured directory' => [
'<?php namespace Foo\Bar\Baz\Fixer\Basic; class PsrAutoloadingFixerTest {}',
'<?php namespace Foo\Bar\Baz\FIXER\Basic; class PsrAutoloadingFixerTest {}',
['dir' => __DIR__.'/../..'],
];
yield 'namespace partially matching directory structure with comment and configured directory' => [
'<?php namespace /* hi there */ Foo\Bar\Baz\Fixer\Basic; class /* hi there */ PsrAutoloadingFixerTest {}',
'<?php namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic; class /* hi there */ PsrAutoloadingFixerTest {}',
['dir' => __DIR__.'/../..'],
];
yield 'namespace not matching directory structure' => [
'<?php namespace Foo\Bar\Baz; class PsrAutoloadingFixerTest {}',
];
yield 'namespace not matching directory structure with configured directory' => [
'<?php namespace Foo\Bar\Baz; class PsrAutoloadingFixerTest {}',
null,
['dir' => __DIR__],
];
yield [ // namespace with wrong casing
'<?php
namespace PhpCsFixer\Fixer;
class FixerInterface {}
',
'<?php
namespace PhpCsFixer\fixer;
class FixerInterface {}
',
['dir' => __DIR__.'/../../../src'],
new \SplFileInfo(__DIR__.'/../../../src/Fixer/FixerInterface.php'),
];
yield [ // class with wrong casing (2 levels namespace)
'<?php
class Fixer_Basic_PsrAutoloadingFixer {}
',
'<?php
class Fixer_bASIc_PsrAutoloadingFixer {}
',
['dir' => __DIR__.'/../../../src'],
new \SplFileInfo(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
];
yield [ // namespaced class with wrong casing
'<?php
namespace PhpCsFixer\Fixer;
class FixerInterface {}
',
'<?php
namespace PhpCsFixer\Fixer;
class fixerinterface {}
',
['dir' => __DIR__.'/../../../src'],
new \SplFileInfo(__DIR__.'/../../../src/Fixer/FixerInterface.php'),
];
yield [ // multiple classy elements in file
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
interface SomeInterfaceToBeUsedInTests {}
class blah {}
/* class foo */',
];
yield [ // multiple namespaces in file
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
interface SomeInterfaceToBeUsedInTests {}
namespace AnotherNamespace;
class blah {}
/* class foo */',
];
yield [ // fix class
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
class PsrAutoloadingFixerTest {}
/* class foo */
',
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
class blah {}
/* class foo */
',
];
yield [ // abstract class
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
abstract class PsrAutoloadingFixerTest {}
/* class foo */
',
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
abstract class blah {}
/* class foo */
',
];
yield [ // final class
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
final class PsrAutoloadingFixerTest {}
/* class foo */
',
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
final class blah {}
/* class foo */
',
];
yield [ // namespace and class with comments
'<?php
namespace /* namespace here */ PhpCsFixer\Fixer\Psr;
class /* hi there */ PsrAutoloadingFixerTest /* why hello */ {}
/* class foo */
',
'<?php
namespace /* namespace here */ PhpCsFixer\Fixer\Psr;
class /* hi there */ blah /* why hello */ {}
/* class foo */
',
];
yield [ // namespace partially matching directory structure
'<?php
namespace Foo\Bar\Baz\FIXER\Basic;
class PsrAutoloadingFixer {}
',
null,
[],
new \SplFileInfo(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
];
yield [ // namespace partially matching directory structure with comment
'<?php
namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic;
class /* hi there */ PsrAutoloadingFixer {}
',
null,
[],
new \SplFileInfo(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
];
yield [ // namespace not matching directory structure
'<?php
namespace Foo\Bar\Baz;
class PsrAutoloadingFixer {}
',
null,
[],
new \SplFileInfo(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
];
yield [ // namespace partially matching directory structure with configured directory
'<?php
namespace Foo\Bar\Baz\Fixer\Basic;
class PsrAutoloadingFixer {}
',
'<?php
namespace Foo\Bar\Baz\FIXER\Basic;
class PsrAutoloadingFixer {}
',
['dir' => __DIR__.'/../../../src'],
new \SplFileInfo(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
];
yield [ // namespace partially matching directory structure with comment and configured directory
'<?php
namespace /* hi there */ Foo\Bar\Baz\Fixer\Basic;
class /* hi there */ PsrAutoloadingFixer {}
',
'<?php
namespace /* hi there */ Foo\Bar\Baz\FIXER\Basic;
class /* hi there */ PsrAutoloadingFixer {}
',
['dir' => __DIR__.'/../../../src'],
new \SplFileInfo(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
];
yield [ // namespace not matching directory structure with configured directory
'<?php
namespace Foo\Bar\Baz;
class PsrAutoloadingFixer {}
',
null,
['dir' => __DIR__.'/../../../src/Fixer/Basic'],
new \SplFileInfo(__DIR__.'/../../../src/Fixer/Basic/PsrAutoloadingFixer.php'),
];
yield [ // class with originally long name
'<?php class PsrAutoloadingFixerTest {}',
'<?php class PsrAutoloadingFixerTestFoo {}',
];
$cases = ['.php', 'Foo.class.php', '4Foo.php', '$#.php'];
foreach (['__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'finally', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor'] as $keyword) {
$cases[] = $keyword.'.php';
$cases[] = strtoupper($keyword).'.php';
}
foreach (['__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__'] as $magicConstant) {
$cases[] = $magicConstant.'.php';
$cases[] = strtolower($magicConstant).'.php';
}
yield from array_map(static fn ($case): array => [
'<?php
namespace Aaa;
class Bar {}',
null,
[],
new \SplFileInfo($case),
], $cases);
yield 'class with anonymous class' => [
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
class PsrAutoloadingFixerTest {
public function foo() {
return new class() implements FooInterface {};
}
}
',
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
class stdClass {
public function foo() {
return new class() implements FooInterface {};
}
}
',
];
yield 'ignore anonymous class implementing interface' => [
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
new class implements Countable {};
',
];
yield 'ignore anonymous class extending other class' => [
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
new class extends stdClass {};
',
];
yield 'ignore multiple classy in file with anonymous class between them' => [
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
class ClassOne {};
new class extends stdClass {};
class ClassTwo {};
',
];
}
/**
* @requires PHP 8.0
*
* @dataProvider provideFix80Cases
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'anonymous + annotation' => [
'<?php
namespace PhpCsFixer\Tests\Fixer\Basic;
new
#[Foo]
class extends stdClass {};
',
];
}
/**
* @requires PHP 8.1
*
* @dataProvider provideFix81Cases
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input, new \SplFileInfo(__FILE__));
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'enum with wrong casing' => [
'<?php enum PsrAutoloadingFixerTest {}',
'<?php enum psrautoloadingfixertest {}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Basic/SingleLineEmptyBodyFixerTest.php | tests/Fixer/Basic/SingleLineEmptyBodyFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Basic;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Basic\SingleLineEmptyBodyFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\SingleLineEmptyBodyFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SingleLineEmptyBodyFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'non-empty class' => [
'<?php class Foo
{
public function bar () {}
}
',
];
yield 'non-empty function body' => [
'<?php
function f1()
{ /* foo */ }
function f2()
{ /** foo */ }
function f3()
{ // foo
}
function f4()
{
return true;
}
',
];
yield 'classes' => [
'<?php
class Foo {}
class Bar extends BarParent {}
class Baz implements BazInterface {}
abstract class A {}
final class F {}
',
'<?php
class Foo
{
}
class Bar extends BarParent
{}
class Baz implements BazInterface {}
abstract class A
{}
final class F
{
}
',
];
yield 'multiple functions' => [
'<?php
function notThis1() { return 1; }
function f1() {}
function f2() {}
function f3() {}
function notThis2(){ return 1; }
',
'<?php
function notThis1() { return 1; }
function f1()
{}
function f2() {
}
function f3()
{
}
function notThis2(){ return 1; }
',
];
yield 'remove spaces' => [
'<?php
function f1() {}
function f2() {}
function f3() {}
',
'<?php
function f1() { }
function f2() { }
function f3() { }
',
];
yield 'add spaces' => [
'<?php
function f1() {}
function f2() {}
function f3() {}
',
'<?php
function f1(){}
function f2(){}
function f3(){}
',
];
yield 'with return types' => [
'<?php
function f1(): void {}
function f2(): \Foo\Bar {}
function f3(): ?string {}
',
'<?php
function f1(): void
{}
function f2(): \Foo\Bar { }
function f3(): ?string {
}
',
];
yield 'abstract functions' => [
'<?php abstract class C {
abstract function f1();
function f2() {}
abstract function f3();
}
if (true) { }
',
'<?php abstract class C {
abstract function f1();
function f2() { }
abstract function f3();
}
if (true) { }
',
];
yield 'every token in separate line' => [
'<?php
function
foo
(
)
:
void {}
',
'<?php
function
foo
(
)
:
void
{
}
',
];
yield 'comments before body' => [
'<?php
function f1()
// foo
{}
function f2()
/* foo */
{}
function f3()
/** foo */
{}
function f4()
/** foo */
/** bar */
{}
',
'<?php
function f1()
// foo
{
}
function f2()
/* foo */
{
}
function f3()
/** foo */
{
}
function f4()
/** foo */
/** bar */
{ }
',
];
yield 'anonymous class' => [
'<?php
$o = new class() {};
',
'<?php
$o = new class() {
};
',
];
yield 'anonymous function' => [
'<?php
$x = function () {};
',
'<?php
$x = function () {
};
',
];
yield 'interface' => [
'<?php interface Foo {}
',
'<?php interface Foo
{
}
',
];
yield 'trait' => [
'<?php trait Foo {}
',
'<?php trait Foo
{
}
',
];
}
/**
* @requires PHP 8.0
*
* @dataProvider provideFix80Cases
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'single-line promoted properties' => [
'<?php class Foo
{
public function __construct(private int $x, private int $y) {}
}
',
'<?php class Foo
{
public function __construct(private int $x, private int $y)
{
}
}
',
];
yield 'multi-line promoted properties' => [
'<?php class Foo
{
public function __construct(
private int $x,
private int $y,
) {}
}
',
'<?php class Foo
{
public function __construct(
private int $x,
private int $y,
) {
}
}
',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'enum' => [
'<?php enum Foo {}
',
'<?php enum Foo
{
}
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Basic/BracesFixerTest.php | tests/Fixer/Basic/BracesFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Basic;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Fixer\Basic\BracesFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Basic\BracesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Basic\BracesFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Basic\BracesFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class BracesFixerTest extends AbstractFixerTestCase
{
private const CONFIGURATION_OOP_POSITION_SAME_LINE = ['position_after_functions_and_oop_constructs' => BracesFixer::LINE_SAME];
private const CONFIGURATION_CTRL_STRUCT_POSITION_NEXT_LINE = ['position_after_control_structures' => BracesFixer::LINE_NEXT];
private const CONFIGURATION_ANONYMOUS_POSITION_NEXT_LINE = ['position_after_anonymous_constructs' => BracesFixer::LINE_NEXT];
public function testInvalidConfigurationClassyConstructs(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches('#^\[braces\] Invalid configuration: The option "position_after_functions_and_oop_constructs" with value "neither" is invalid\. Accepted values are: "next", "same"\.$#');
$this->fixer->configure(['position_after_functions_and_oop_constructs' => 'neither']); // @phpstan-ignore-line
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixControlContinuationBracesCases
*/
public function testFixControlContinuationBraces(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixControlContinuationBracesCases(): iterable
{
yield [
'<?php
$a = function() {
$a = 1;
while (false);
};',
];
yield [
'<?php
$a = function() {
$a = 1;
for ($i=0;$i<5;++$i);
};',
];
yield [
'<?php
class Foo
{
public function A()
{
?>
Test<?php echo $foobar; ?>Test
<?php
$a = 1;
}
}',
];
yield [
'<?php
if (true) {
$a = 1;
} else {
$b = 2;
}',
'<?php
if (true) {
$a = 1;
}
else {
$b = 2;
}',
];
yield [
'<?php
try {
throw new \Exception();
} catch (\LogicException $e) {
// do nothing
} catch (\Exception $e) {
// do nothing
}',
'<?php
try {
throw new \Exception();
}catch (\LogicException $e) {
// do nothing
}
catch (\Exception $e) {
// do nothing
}',
];
yield [
'<?php
if (true) {
echo 1;
} elseif (true) {
echo 2;
}',
'<?php
if (true) {
echo 1;
} elseif (true)
{
echo 2;
}',
];
yield [
'<?php
try {
echo 1;
} catch (Exception $e) {
echo 2;
}',
'<?php
try
{
echo 1;
}
catch (Exception $e)
{
echo 2;
}',
];
yield [
'<?php
class Foo
{
public function bar(
FooInterface $foo,
BarInterface $bar,
array $data = []
) {
}
}',
'<?php
class Foo
{
public function bar(
FooInterface $foo,
BarInterface $bar,
array $data = []
){
}
}',
];
yield [
'<?php
if (1) {
self::${$key} = $val;
self::${$type}[$rule] = $pattern;
self::${$type}[$rule] = array_merge($pattern, self::${$type}[$rule]);
self::${$type}[$rule] = $pattern + self::${$type}["rules"];
}
',
];
yield [
'<?php
if (1) {
do {
$a = 1;
} while (true);
}',
];
yield [
'<?php
if /* 1 */ (2) {
}',
'<?php
if /* 1 */ (2) {}',
];
yield [
'<?php
if (1) {
echo $items[0]->foo;
echo $collection->items[1]->property;
}
',
];
yield [
'<?php
$a = function() {
$a = 1;
while (false);
};',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
$a = function()
{
$a = 1;
while (false);
};',
'<?php
$a = function() {
$a = 1;
while (false);
};',
self::CONFIGURATION_ANONYMOUS_POSITION_NEXT_LINE,
];
yield [
'<?php
$a = function() {
$a = 1;
for ($i=0;$i<5;++$i);
};',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
$a = function()
{
$a = 1;
for ($i=0;$i<5;++$i);
};',
'<?php
$a = function() {
$a = 1;
for ($i=0;$i<5;++$i);
};',
self::CONFIGURATION_ANONYMOUS_POSITION_NEXT_LINE,
];
yield [
'<?php
class Foo {
public function A() {
?>
Test<?php echo $foobar; ?>Test
<?php
$a = 1;
}
}',
'<?php
class Foo
{
public function A()
{
?>
Test<?php echo $foobar; ?>Test
<?php
$a = 1;
}
}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
$a = 1;
} else {
$b = 2;
}',
'<?php
if (true) {
$a = 1;
}
else {
$b = 2;
}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true)
{
$a = 1;
}
else
{
$b = 2;
}',
'<?php
if (true) {
$a = 1;
}
else {
$b = 2;
}',
self::CONFIGURATION_CTRL_STRUCT_POSITION_NEXT_LINE,
];
yield [
'<?php
try {
throw new \Exception();
} catch (\LogicException $e) {
// do nothing
} catch (\Exception $e) {
// do nothing
}',
'<?php
try {
throw new \Exception();
}catch (\LogicException $e) {
// do nothing
}
catch (\Exception $e) {
// do nothing
}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
try
{
throw new \Exception();
}
catch (\LogicException $e)
{
// do nothing
}
catch (\Exception $e)
{
// do nothing
}',
'<?php
try {
throw new \Exception();
}catch (\LogicException $e) {
// do nothing
}
catch (\Exception $e) {
// do nothing
}',
self::CONFIGURATION_CTRL_STRUCT_POSITION_NEXT_LINE,
];
yield [
'<?php
if (true) {
echo 1;
} elseif (true) {
echo 2;
}',
'<?php
if (true) {
echo 1;
} elseif (true)
{
echo 2;
}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
try {
echo 1;
} catch (Exception $e) {
echo 2;
}',
'<?php
try
{
echo 1;
}
catch (Exception $e)
{
echo 2;
}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
class Foo {
public function bar(
FooInterface $foo,
BarInterface $bar,
array $data = []
) {
}
}',
'<?php
class Foo
{
public function bar(
FooInterface $foo,
BarInterface $bar,
array $data = []
){
}
}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (1) {
self::${$key} = $val;
self::${$type}[$rule] = $pattern;
self::${$type}[$rule] = array_merge($pattern, self::${$type}[$rule]);
self::${$type}[$rule] = $pattern + self::${$type}["rules"];
}
',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (1) {
do {
$a = 1;
} while (true);
}',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if /* 1 */ (2) {
}',
'<?php
if /* 1 */ (2) {}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (1) {
echo $items[0]->foo;
echo $collection->items[1]->property;
}
',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php class A {
/** */
}',
'<?php class A
/** */
{
}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
class Foo
{
public function foo()
{
foo();
// baz
bar();
}
}',
'<?php
class Foo
{
public function foo(){
foo();
// baz
bar();
}
}',
];
yield [
'<?php
class Foo
{
public function foo($foo)
{
return $foo // foo
? \'foo\'
: \'bar\'
;
}
}',
];
yield [
'<?php
class Foo
{
/**
* Foo.
*/
public $foo;
/**
* Bar.
*/
public $bar;
}',
'<?php
class Foo {
/**
* Foo.
*/
public $foo;
/**
* Bar.
*/
public $bar;
}',
];
yield [
'<?php
class Foo
{
/*
* Foo.
*/
public $foo;
/*
* Bar.
*/
public $bar;
}',
'<?php
class Foo {
/*
* Foo.
*/
public $foo;
/*
* Bar.
*/
public $bar;
}',
];
yield [
'<?php
if (1==1) {
$a = 1;
// test
$b = 2;
}',
'<?php
if (1==1) {
$a = 1;
// test
$b = 2;
}',
];
yield [
'<?php
if (1==1) {
$a = 1;
# test
$b = 2;
}',
'<?php
if (1==1) {
$a = 1;
# test
$b = 2;
}',
];
yield [
'<?php
if (1==1) {
$a = 1;
/** @var int $b */
$b = a();
}',
'<?php
if (1==1) {
$a = 1;
/** @var int $b */
$b = a();
}',
];
yield [
'<?php
if ($b) {
if (1==1) {
$a = 1;
// test
$b = 2;
}
}
',
'<?php
if ($b) {
if (1==1) {
$a = 1;
// test
$b = 2;
}
}
',
];
yield [
'<?php
if ($b) {
if (1==1) {
$a = 1;
/* test */
$b = 2;
echo 123;//
}
}
',
'<?php
if ($b) {
if (1==1) {
$a = 1;
/* test */
$b = 2;
echo 123;//
}
}
',
];
yield [
'<?php
class A
{
public function B()
{/*
*/
$a = 1;
}
}',
'<?php
class A {
public function B()
{/*
*/
$a = 1;
}
}',
];
yield [
'<?php
class B
{
public function B()
{
/*
*//**/
$a = 1;
}
}',
'<?php
class B {
public function B()
{
/*
*//**/
$a = 1;
}
}',
];
yield [
'<?php
class C
{
public function C()
{
/* */#
$a = 1;
}
}',
'<?php
class C {
public function C()
{
/* */#
$a = 1;
}
}',
];
yield [
'<?php
if ($a) { /*
*/
echo 1;
}',
'<?php
if ($a){ /*
*/
echo 1;
}',
];
yield [
'<?php
if ($a) { /**/ /*
*/
echo 1;
echo 2;
}',
'<?php
if ($a){ /**/ /*
*/
echo 1;
echo 2;
}',
];
yield [
'<?php
foreach ($foo as $bar) {
if (true) {
}
// comment
elseif (false) {
}
}',
];
yield [
'<?php
function foo()
{
$bar = 1; // multiline ...
// ... comment
$baz = 2; // next comment
}',
];
yield [
'<?php
function foo()
{
$foo = 1;
// multiline...
// ... comment
return $foo;
}',
'<?php
function foo()
{
$foo = 1;
// multiline...
// ... comment
return $foo;
}',
];
yield [
'<?php
function foo()
{
$bar = 1; /* bar */ // multiline ...
// ... comment
$baz = 2; /* baz */ // next comment
}',
];
yield [
'<?php
class Foo
{
public function bar()
{
foreach (new Bar() as $file) {
foo();
}
}
}',
'<?php
class Foo {
public function bar() {
foreach (new Bar() as $file)
{
foo();
}
}
}',
];
yield [
'<?php if ($condition) { ?>
echo 1;
<?php } else { ?>
echo 2;
<?php } ?>
',
];
yield [
'<?php $arr = [true, false]; ?>
<?php foreach ($arr as $index => $item) {
if ($item): ?>
<?php echo $index; ?>
<?php endif;
} ?>',
];
yield [
'<?php
do {
foo();
} // comment
while (false);
',
];
yield [
'<?php
if (true) {
?>
<hr />
<?php
if (true) {
echo \'x\';
}
?>
<hr />
<?php
}',
];
yield [
'<?php
function foo()
{
}',
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixMissingBracesAndIndentCases
*/
public function testFixMissingBracesAndIndent(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixMissingBracesAndIndentCases(): iterable
{
yield [
'<?php
if (true):
$foo = 0;
endif;',
];
yield [
'<?php
if (true) :
$foo = 0;
endif;',
];
yield [
'<?php
if (true) : $foo = 1; endif;',
];
yield [
'<?php
if (true) {
$foo = 1;
}',
'<?php
if (true)$foo = 1;',
];
yield [
'<?php
if (true) {
$foo = 2;
}',
'<?php
if (true) $foo = 2;',
];
yield [
'<?php
if (true) {
$foo = 3;
}',
'<?php
if (true){$foo = 3;}',
];
yield [
'<?php
if (true) {
echo 1;
} else {
echo 2;
}',
'<?php
if(true) { echo 1; } else echo 2;',
];
yield [
'<?php
if (true) {
echo 3;
} else {
echo 4;
}',
'<?php
if(true) echo 3; else { echo 4; }',
];
yield [
'<?php
if (true) {
echo 5;
} else {
echo 6;
}',
'<?php
if (true) echo 5; else echo 6;',
];
yield [
'<?php
if (true) {
while (true) {
$foo = 1;
$bar = 2;
}
}',
'<?php
if (true) while (true) { $foo = 1; $bar = 2;}',
];
yield [
'<?php
if (true) {
if (true) {
echo 1;
} else {
echo 2;
}
} else {
echo 3;
}',
'<?php
if (true) if (true) echo 1; else echo 2; else echo 3;',
];
yield [
'<?php
if (true) {
// sth here...
if ($a && ($b || $c)) {
$d = 1;
}
}',
'<?php
if (true) {
// sth here...
if ($a && ($b || $c)) $d = 1;
}',
];
yield [
'<?php
for ($i = 1; $i < 10; ++$i) {
echo $i;
}
for ($i = 1; $i < 10; ++$i) {
echo $i;
}',
'<?php
for ($i = 1; $i < 10; ++$i) echo $i;
for ($i = 1; $i < 10; ++$i) { echo $i; }',
];
yield [
'<?php
for ($i = 1; $i < 5; ++$i) {
for ($i = 1; $i < 10; ++$i) {
echo $i;
}
}',
'<?php
for ($i = 1; $i < 5; ++$i) for ($i = 1; $i < 10; ++$i) { echo $i; }',
];
yield [
'<?php
do {
echo 1;
} while (false);',
'<?php
do { echo 1; } while (false);',
];
yield [
'<?php
while ($foo->next());',
];
yield [
'<?php
foreach ($foo as $bar) {
echo $bar;
}',
'<?php
foreach ($foo as $bar) echo $bar;',
];
yield [
'<?php
if (true) {
$a = 1;
}',
'<?php
if (true) {$a = 1;}',
];
yield [
'<?php
if (true) {
$a = 1;
}',
'<?php
if (true) {
$a = 1;
}',
];
yield [
'<?php
if (true) {
$a = 1;
$b = 2;
while (true) {
$c = 3;
}
$d = 4;
}',
'<?php
if (true) {
$a = 1;
$b = 2;
while (true) {
$c = 3;
}
$d = 4;
}',
];
yield [
'<?php
if (true) {
$a = 1;
$b = 2;
}',
];
yield [
'<?php
if (1) {
$a = 1;
// comment at end
}',
];
yield [
'<?php
if (1) {
if (2) {
$a = "a";
} elseif (3) {
$b = "b";
// comment
} else {
$c = "c";
}
$d = "d";
}',
];
yield [
'<?php
foreach ($numbers as $num) {
for ($i = 0; $i < $num; ++$i) {
$a = "a";
}
$b = "b";
}',
];
yield [
'<?php
if (1) {
if (2) {
$foo = 2;
if (3) {
$foo = 3;
}
}
}',
];
yield [
'<?php
declare(ticks = 1) {
$ticks = 1;
}',
'<?php
declare (
ticks = 1 ) {
$ticks = 1;
}',
];
yield [
'<?php
if (true) {
foo();
} elseif (true) {
bar();
}',
'<?php
if (true)
{
foo();
} elseif (true)
{
bar();
}',
];
yield [
'<?php
while (true) {
foo();
}',
'<?php
while (true)
{
foo();
}',
];
yield [
'<?php
do {
echo $test;
} while ($test = $this->getTest());',
'<?php
do
{
echo $test;
}
while ($test = $this->getTest());',
];
yield [
'<?php
do {
echo $test;
} while ($test = $this->getTest());',
'<?php
do
{
echo $test;
}while ($test = $this->getTest());',
];
yield [
'<?php
class ClassName
{
/**
* comment
*/
public $foo = null;
}',
'<?php
class ClassName
{
/**
* comment
*/
public $foo = null;
}',
];
yield [
'<?php
while ($true) {
try {
throw new \Exception();
} catch (\Exception $e) {
// do nothing
}
}',
];
yield [
'<?php
interface Foo
{
public function setConfig(ConfigInterface $config);
}',
];
yield [
'<?php
function bar()
{
$a = 1; //comment
}',
];
yield [
'<?php
function & lambda()
{
return function () {
};
}',
];
yield [
'<?php
function nested()
{
$a = "a{$b->c()}d";
}',
];
yield [
'<?php
function foo()
{
$a = $b->{$c->d}($e);
$f->{$g} = $h;
$i->{$j}[$k] = $l;
$m = $n->{$o};
$p = array($q->{$r}, $s->{$t});
$u->{$v}->w = 1;
}',
];
yield [
'<?php
function mixed()
{
$a = $b->{"a{$c}d"}();
}',
];
yield [
'<?php
function mixedComplex()
{
$a = $b->{"a{$c->{\'foo-bar\'}()}d"}();
}',
];
yield [
'<?php
function mixedComplex()
{
$a = ${"b{$foo}"}->{"a{$c->{\'foo-bar\'}()}d"}();
}',
];
yield [
'<?php
if (true):
echo 1;
else:
echo 2;
endif;
',
];
yield [
'<?php
if ($test) { //foo
echo 1;
}',
];
yield [
'<?php
if (true) {
// foo
// bar
if (true) {
print("foo");
print("bar");
}
}',
'<?php
if (true)
// foo
// bar
{
if (true)
{
print("foo");
print("bar");
}
}',
];
yield [
'<?php
if (true) {
// foo
/* bar */
if (true) {
print("foo");
print("bar");
}
}',
'<?php
if (true)
// foo
/* bar */{
if (true)
{
print("foo");
print("bar");
}
}',
];
yield [
'<?php if (true) {
echo "s";
} ?>x',
'<?php if (true) echo "s" ?>x',
];
yield [
'<?php
class Foo
{
public function getFaxNumbers()
{
if (1) {
return $this->phoneNumbers->filter(function ($phone) {
$a = 1;
$b = 1;
$c = 1;
return ($phone->getType() === 1) ? true : false;
});
}
}
}',
'<?php
class Foo
{
public function getFaxNumbers()
{
if (1)
return $this->phoneNumbers->filter(function ($phone) {
$a = 1;
$b = 1;
$c = 1;
return ($phone->getType() === 1) ? true : false;
});
}
}',
];
yield [
'<?php
if (true) {
if (true) {
echo 1;
} elseif (true) {
echo 2;
} else {
echo 3;
}
}
',
'<?php
if(true)
if(true)
echo 1;
elseif(true)
echo 2;
else
echo 3;
',
];
yield [
'<?php
if (true) {
if (true) {
echo 1;
} elseif (true) {
echo 2;
} else {
echo 3;
}
}
echo 4;
',
'<?php
if(true)
if(true)
echo 1;
elseif(true)
echo 2;
else
echo 3;
echo 4;
',
];
yield [
'<?php
if (true) {
if (true) {
echo 1;
} elseif (true) {
echo 2;
} else {
echo 3;
}
}',
'<?php
if(true) if(true) echo 1; elseif(true) echo 2; else echo 3;',
];
yield [
'<?php
if (true) {
if (true) {
echo 1;
} else {
echo 2;
}
} else {
echo 3;
}',
'<?php
if(true) if(true) echo 1; else echo 2; else echo 3;',
];
yield [
'<?php
foreach ($data as $val) {
// test val
if ($val === "errors") {
echo "!";
}
}',
'<?php
foreach ($data as $val)
// test val
if ($val === "errors") {
echo "!";
}',
];
yield [
'<?php
if (1) {
foreach ($data as $val) {
// test val
if ($val === "errors") {
echo "!";
}
}
}',
'<?php
if (1)
foreach ($data as $val)
// test val
if ($val === "errors") {
echo "!";
}',
];
yield [
'<?php
class Foo
{
public function main()
{
echo "Hello";
}
}',
'<?php
class Foo
{
public function main()
{
echo "Hello";
}
}',
];
yield [
'<?php
class Foo
{
public function main()
{
echo "Hello";
}
}',
'<?php
class Foo
{
public function main()
{
echo "Hello";
}
}',
];
yield [
'<?php
class Foo
{
public $bar;
public $baz;
}',
'<?php
class Foo
{
public $bar;
public $baz;
}',
];
yield [
'<?php
function myFunction($foo, $bar)
{
return \Foo::{$foo}($bar);
}',
];
yield [
'<?php
class C
{
public function __construct(
) {
//comment
}
}',
'<?php
class C {
public function __construct(
)
//comment
{}
}',
];
yield [
'<?php
if (true):
$foo = 0;
endif;',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) :
$foo = 0;
endif;',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) : $foo = 1; endif;',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
$foo = 1;
}',
'<?php
if (true)$foo = 1;',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
$foo = 2;
}',
'<?php
if (true) $foo = 2;',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
$foo = 3;
}',
'<?php
if (true){$foo = 3;}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
echo 1;
} else {
echo 2;
}',
'<?php
if(true) { echo 1; } else echo 2;',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
echo 3;
} else {
echo 4;
}',
'<?php
if(true) echo 3; else { echo 4; }',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
echo 5;
} else {
echo 6;
}',
'<?php
if (true) echo 5; else echo 6;',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
while (true) {
$foo = 1;
$bar = 2;
}
}',
'<?php
if (true) while (true) { $foo = 1; $bar = 2;}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
if (true) {
echo 1;
} else {
echo 2;
}
} else {
echo 3;
}',
'<?php
if (true) if (true) echo 1; else echo 2; else echo 3;',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
// sth here...
if ($a && ($b || $c)) {
$d = 1;
}
}',
'<?php
if (true) {
// sth here...
if ($a && ($b || $c)) $d = 1;
}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
for ($i = 1; $i < 10; ++$i) {
echo $i;
}
for ($i = 1; $i < 10; ++$i) {
echo $i;
}',
'<?php
for ($i = 1; $i < 10; ++$i) echo $i;
for ($i = 1; $i < 10; ++$i) { echo $i; }',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
for ($i = 1; $i < 5; ++$i) {
for ($i = 1; $i < 10; ++$i) {
echo $i;
}
}',
'<?php
for ($i = 1; $i < 5; ++$i) for ($i = 1; $i < 10; ++$i) { echo $i; }',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
do {
echo 1;
} while (false);',
'<?php
do { echo 1; } while (false);',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
while ($foo->next());',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
foreach ($foo as $bar) {
echo $bar;
}',
'<?php
foreach ($foo as $bar) echo $bar;',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
$a = 1;
}',
'<?php
if (true) {$a = 1;}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
$a = 1;
}',
'<?php
if (true) {
$a = 1;
}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
$a = 1;
$b = 2;
while (true) {
$c = 3;
}
$d = 4;
}',
'<?php
if (true) {
$a = 1;
$b = 2;
while (true) {
$c = 3;
}
$d = 4;
}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
$a = 1;
$b = 2;
}',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (1) {
$a = 1;
// comment at end
}',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (1) {
if (2) {
$a = "a";
} elseif (3) {
$b = "b";
// comment
} else {
$c = "c";
}
$d = "d";
}',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (1) {
if (2) {
$a = "a";
} elseif (3) {
$b = "b";
// comment line 1
// comment line 2
// comment line 3
// comment line 4
} else {
$c = "c";
}
$d = "d";
}',
'<?php
if (1) {
if (2) {
$a = "a";
} elseif (3) {
$b = "b";
// comment line 1
// comment line 2
// comment line 3
// comment line 4
} else {
$c = "c";
}
$d = "d";
}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
foreach ($numbers as $num) {
for ($i = 0; $i < $num; ++$i) {
$a = "a";
}
$b = "b";
}',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (1) {
if (2) {
$foo = 2;
if (3) {
$foo = 3;
}
}
}',
null,
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
declare(ticks = 1) {
$ticks = 1;
}',
'<?php
declare (
ticks = 1 ) {
$ticks = 1;
}',
self::CONFIGURATION_OOP_POSITION_SAME_LINE,
];
yield [
'<?php
if (true) {
foo();
} elseif (true) {
bar();
}',
'<?php
if (true)
{
foo();
} elseif (true)
{
bar();
}',
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | true |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/FopenFlagsFixerTest.php | tests/Fixer/FunctionNotation/FopenFlagsFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractFopenFlagFixer
* @covers \PhpCsFixer\Fixer\FunctionNotation\FopenFlagsFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\FopenFlagsFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\FopenFlagsFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class FopenFlagsFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'missing "b"' => [
'<?php
$a = fopen($foo, \'rw+b\');
',
'<?php
$a = fopen($foo, \'rw+\');
',
];
yield 'has "t" and "b"' => [
'<?php
$a = \fopen($foo, "rw+b");
',
'<?php
$a = \fopen($foo, "rw+bt");
',
];
yield 'has "t" and no "b" and binary string mod' => [
'<?php
$a = fopen($foo, b\'rw+b\');
',
'<?php
$a = fopen($foo, b\'trw+\');
',
];
// configure remove b
yield 'missing "b" but not configured' => [
'<?php
$a = fopen($foo, \'rw+\');
',
'<?php
$a = fopen($foo, \'rw+t\');
',
['b_mode' => false],
];
yield '"t" and superfluous "b"' => [
'<?php
$a = fopen($foo, \'r+\');
$a = fopen($foo, \'w+r\');
$a = fopen($foo, \'r+\');
$a = fopen($foo, \'w+r\');
',
'<?php
$a = fopen($foo, \'r+bt\');
$a = fopen($foo, \'btw+r\');
$a = fopen($foo, \'r+tb\');
$a = fopen($foo, \'tbw+r\');
',
['b_mode' => false],
];
yield 'superfluous "b"' => [
'<?php
$a = fopen($foo, \'r+\');
$a = fopen($foo, \'w+r\');
',
'<?php
$a = fopen($foo, \'r+b\');
$a = fopen($foo, \'bw+r\');
',
['b_mode' => false],
];
foreach (self::provideDoNotFixCodeSamples() as $name => $code) {
yield $name.' with b_mode' => [$code];
yield $name.' without b_mode' => [$code, null, ['b_mode' => false]];
}
}
/**
* @return iterable<string, string>
*/
private static function provideDoNotFixCodeSamples(): iterable
{
yield 'not simple flags' => '<?php $a = fopen($foo, "t".$a);';
yield 'wrong # of arguments' => '<?php
$b = fopen("br+");
$c = fopen($foo, "w+", 1, 2 , 3);
';
yield '"flags" is too long (must be overridden)' => '<?php $d = fopen($foo, "r+w+a+x+c+etXY");';
yield '"flags" is too short (must be overridden)' => '<?php $d = fopen($foo, "");';
yield 'static method call' => '<?php $e = A::fopen($foo, "w+");';
yield 'method call' => '<?php $f = $b->fopen($foo, "r+");';
yield 'comments, PHPDoc and literal' => '<?php
// fopen($foo, "rw");
/* fopen($foo, "rw"); */
echo("fopen($foo, \"rw\")");
';
yield 'invalid flag values' => '<?php
$a = fopen($foo, \'\');
$a = fopen($foo, \'k\');
$a = fopen($foo, \'kz\');
$a = fopen($foo, \'k+\');
$a = fopen($foo, \'+k\');
$a = fopen($foo, \'xct++\');
$a = fopen($foo, \'w+r+r+\');
$a = fopen($foo, \'+btrw+\');
$a = fopen($foo, \'b+rw\');
$a = fopen($foo, \'bbrw+\');
$a = fopen($foo, \'brw++\');
$a = fopen($foo, \'++brw\');
$a = fopen($foo, \'ybrw+\');
$a = fopen($foo, \'rr\');
$a = fopen($foo, \'ロ\');
$a = fopen($foo, \'ロ+\');
$a = fopen($foo, \'rロ\');
$a = \fopen($foo, \'w+ロ\');
';
yield 'second argument not string' => '<?php
echo "abc"; // to pass the candidate check
$a = fopen($foo, 1);
$a = fopen($foo, $a);
$a = fopen($foo, null);
';
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/SingleLineThrowFixerTest.php | tests/Fixer/FunctionNotation/SingleLineThrowFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\SingleLineThrowFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\SingleLineThrowFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class SingleLineThrowFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php throw new Exception; foo(
"Foo"
);'];
yield ['<?php throw new $exceptionName; foo(
"Foo"
);'];
yield ['<?php throw $exception; foo(
"Foo"
);'];
yield ['<?php throw new Exception("Foo.", 0);'];
yield [
'<?php throw new Exception("Foo.", 0);',
'<?php throw new Exception(
"Foo.",
0
);',
];
yield [
'<?php throw new Exception("Foo." . "Bar");',
'<?php throw new Exception(
"Foo."
.
"Bar"
);',
];
yield [
'<?php throw new Exception(new ExceptionReport("Foo"), 0);',
'<?php throw new Exception(
new
ExceptionReport("Foo"),
0
);',
];
yield [
'<?php throw new Exception(sprintf(\'Error with number "%s".\', 42));',
'<?php throw new Exception(sprintf(
\'Error with number "%s".\',
42
));',
];
yield [
'<?php throw new SomeVendor\Exception("Foo.");',
'<?php throw new SomeVendor\Exception(
"Foo."
);',
];
yield [
'<?php throw new \SomeVendor\Exception("Foo.");',
'<?php throw new \SomeVendor\Exception(
"Foo."
);',
];
yield [
'<?php throw $this->exceptionFactory->createAnException("Foo");',
'<?php throw $this
->exceptionFactory
->createAnException(
"Foo"
);',
];
yield [
'<?php throw $this->getExceptionFactory()->createAnException("Foo");',
'<?php throw $this
->getExceptionFactory()
->createAnException(
"Foo"
);',
];
yield [
'<?php throw $this->getExceptionFactory()->createAnException(function ($x, $y) { return $x === $y + 2; });',
'<?php throw $this
->getExceptionFactory()
->createAnException(
function
(
$x,
$y
)
{
return $x === $y + 2
;
}
);',
];
yield [
'<?php throw ExceptionFactory::createAnException("Foo");',
'<?php throw ExceptionFactory
::
createAnException(
"Foo"
);',
];
yield [
'<?php throw new Exception("Foo.", 0);',
'<?php throw
new
Exception
(
"Foo."
,
0
);',
];
yield [
'<?php throw new $exceptionName("Foo.");',
'<?php throw new $exceptionName(
"Foo."
);',
];
yield [
'<?php throw new $exceptions[4];',
'<?php throw new $exceptions[
4
];',
];
yield [
'<?php throw clone $exceptionName("Foo.");',
'<?php throw clone $exceptionName(
"Foo."
);',
];
yield [
'<?php throw new WeirdException("Foo.", -20, "An elephant", 1, 2, 3, 4, 5, 6, 7, 8);',
'<?php throw new WeirdException("Foo.", -20, "An elephant",
1,
2,
3, 4, 5, 6, 7, 8
);',
];
yield [
'<?php
if ($foo) {
throw new Exception("It is foo.", 1);
} else {
throw new \Exception("It is not foo.", 0);
}
',
'<?php
if ($foo) {
throw new Exception(
"It is foo.",
1
);
} else {
throw new \Exception(
"It is not foo.", 0
);
}
',
];
yield [
'<?php throw new Exception( /* A */"Foo", /* 1 */0 /* 2 */); //3',
'<?php throw new Exception( // A
"Foo", // 1
0 // 2
); //3',
];
yield [
'<?php throw new Exception( /* 0123 */ "Foo", /* 1 */0 /* 2 */); //3',
'<?php throw new Exception( /* 0123 */
"Foo", // 1
0 // 2
); //3',
];
yield [
'<?php throw new Exception( /* X */ "Foo", /* 1 */0 /* 2 */); //3',
'<?php throw new Exception( /* X
*/
"Foo", // 1
0 // 2
); //3',
];
yield [
'<?php throw new Exception( 0, /* 1 2 3 */ /*4*/ "Foo", /*5*/ /*6*/0 /*7*/);',
'<?php throw new Exception( 0, /*
1
2
3
*/
/*4*/ "Foo", /*5*/
/*6*/0 /*7*/);',
];
yield [
'<?php throw new Exception( /* 0 */${"Foo" /* a */}, /*b */fnx/*c */(/*d */0/*e */)/*f */);',
'<?php throw new Exception( // 0
${"Foo"
# a
}, #b
fnx#c
(#d
0#e
)#f
);',
];
yield [
"<?php throw new Exception('Message.'. 1);",
"<?php throw new Exception('Message.'.
1
);",
];
yield [
'<?php throw new class() extends Exception
{
protected $message = "Custom message";
}
;',
'<?php throw
new class()
extends Exception
{
protected $message = "Custom message";
}
;',
];
yield [
'<?php throw new class extends Exception
{
protected $message = "Custom message";
}
;',
'<?php throw
new class
extends Exception
{
protected $message = "Custom message";
}
;',
];
yield [
'<?php throw new Exception("Foo.", 0)?>',
'<?php throw new Exception(
"Foo.",
0
)?>',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php throw $this?->getExceptionFactory()?->createAnException("Foo");',
'<?php throw $this
?->getExceptionFactory()
?->createAnException(
"Foo"
);',
];
yield [
'<?php
match ($number) {
1 => $function->one(),
2 => $function->two(),
default => throw new \NotOneOrTwo()
};
',
];
yield [
'<?php
match ($number) {
1 => $function->one(),
2 => throw new Exception("Number 2 is not allowed."),
1 => $function->three(),
default => throw new \NotOneOrTwo()
};
',
];
yield [
'<?php throw new Exception(match ($a) { 1 => "a", 3 => "b" });',
'<?php throw new Exception(match ($a) {
1 => "a",
3 => "b"
});',
];
yield [
'<?php
$var = [
$something[1] ?? throw new Exception(123)
];
',
'<?php
$var = [
$something[1] ?? throw new Exception(
123
)
];
',
];
yield [
'<?php
$var = [
$something[1] ?? throw new Exception()
];
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/NoUselessSprintfFixerTest.php | tests/Fixer/FunctionNotation/NoUselessSprintfFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\NoUselessSprintfFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\NoUselessSprintfFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoUselessSprintfFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'simple' => [
'<?php echo "bar";',
'<?php echo sprintf("bar");',
];
yield 'simple II' => [
"<?php echo 'bar' ?>",
"<?php echo sprintf('bar') ?>",
];
yield 'simple III' => [
'<?php echo $bar;',
'<?php echo sprintf($bar);',
];
yield 'simple IV' => [
'<?php echo 1;',
'<?php echo sprintf(1);',
];
yield 'minimal' => [
'<?php $foo;',
'<?php sprintf($foo);',
];
yield 'case insensitive' => [
'<?php echo "bar";',
'<?php echo SPRINTF("bar");',
];
yield 'nested' => [
'<?php echo /* test */"bar";',
'<?php echo sprintf(sprintf(sprintf(/* test */sprintf(sprintf(sprintf("bar"))))));',
];
yield [
'<?php namespace Foo {
function sprintf($foo) {
echo $foo;
}
}',
];
yield [
'<?php namespace Foo;
function sprintf($foo) {
echo $foo;
}
',
];
yield [
'<?php
echo \Foo\sprintf("abc");
echo $bar->sprintf($foo);
echo Bar::sprintf($foo);
echo sprint(...$foo);
echo sprint("%d", 1);
echo sprint("%d%d%d", 1, 2, 3);
echo sprint();
',
];
yield [
'<?php echo sprint[2]("foo");',
];
yield 'trailing comma' => [
'<?php echo "bar";',
'<?php echo sprintf("bar",);',
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield [
'<?php echo "bar";',
'<?php echo \ sprintf("bar");',
];
yield [
'<?php echo A /* 1 */ \ B \ sprintf("bar");',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixerTest.php | tests/Fixer/FunctionNotation/NullableTypeDeclarationForDefaultNullValueFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\NullableTypeDeclarationForDefaultNullValueFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\NullableTypeDeclarationForDefaultNullValueFixer>
*
* @author HypeMC <hypemc@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\NullableTypeDeclarationForDefaultNullValueFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NullableTypeDeclarationForDefaultNullValueFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php function foo($param = null) {}'];
yield ['<?php function foo($param1 = null, $param2 = null) {}'];
yield ['<?php function foo(&$param = null) {}'];
yield ['<?php function foo(& $param = null) {}'];
yield ['<?php function foo(/**int*/ $param = null) {}'];
yield ['<?php function foo(/**int*/ &$param = null) {}'];
yield ['<?php $foo = function ($param = null) {};'];
yield ['<?php $foo = function (&$param = null) {};'];
yield ['<?php function foo(?string $param = null) {}'];
yield ['<?php function foo(?string $param= null) {}'];
yield ['<?php function foo(?string $param =null) {}'];
yield ['<?php function foo(?string $param=null) {}'];
yield ['<?php function foo(?string $param1 = null, ?string $param2 = null) {}'];
yield ['<?php function foo(?string &$param = null) {}'];
yield ['<?php function foo(?string & $param = null) {}'];
yield ['<?php function foo(?string /*comment*/$param = null) {}'];
yield ['<?php function foo(?string /*comment*/&$param = null) {}'];
yield ['<?php function foo(? string $param = null) {}'];
yield ['<?php function foo(?/*comment*/string $param = null) {}'];
yield ['<?php function foo(? /*comment*/ string $param = null) {}'];
yield ['<?php $foo = function (?string $param = null) {};'];
yield ['<?php $foo = function (?string &$param = null) {};'];
yield ['<?php function foo(?Baz $param = null) {}'];
yield ['<?php function foo(?\Baz $param = null) {}'];
yield ['<?php function foo(?Bar\Baz $param = null) {}'];
yield ['<?php function foo(?\Bar\Baz $param = null) {}'];
yield ['<?php function foo(?Baz &$param = null) {}'];
yield ['<?php function foo(?\Baz &$param = null) {}'];
yield ['<?php function foo(?Bar\Baz &$param = null) {}'];
yield ['<?php function foo(?\Bar\Baz &$param = null) {}'];
yield ['<?php function foo(?Baz & $param = null) {}'];
yield ['<?php function foo(?\Baz & $param = null) {}'];
yield ['<?php function foo(?Bar\Baz & $param = null) {}'];
yield ['<?php function foo(?\Bar\Baz & $param = null) {}'];
yield ['<?php function foo(?array &$param = null) {}'];
yield ['<?php function foo(?array & $param = null) {}'];
yield ['<?php function foo(?callable &$param = null) {}'];
yield ['<?php function foo(?callable & $param = null) {}'];
yield ['<?php $foo = function (?Baz $param = null) {};'];
yield ['<?php $foo = function (?Baz &$param = null) {};'];
yield ['<?php $foo = function (?Baz & $param = null) {};'];
yield ['<?php class Test { public function foo(?Bar\Baz $param = null) {} }'];
yield ['<?php class Test { public function foo(?self $param = null) {} }'];
yield ['<?php function foo(...$param) {}'];
yield ['<?php function foo(array ...$param) {}'];
yield ['<?php function foo(?array ...$param) {}'];
yield ['<?php function foo(mixed $param = null) {}'];
yield from self::createBothWaysCases(self::provideBothWaysCases());
yield [
'<?php function foo( ?string $param = null) {}',
'<?php function foo( string $param = null) {}',
];
yield [
'<?php function foo(/*comment*/?string $param = null) {}',
'<?php function foo(/*comment*/string $param = null) {}',
];
yield [
'<?php function foo( /*comment*/ ?string $param = null) {}',
'<?php function foo( /*comment*/ string $param = null) {}',
];
yield [
'<?php function foo(string $param = null) {}',
'<?php function foo(? string $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(/*comment*/string $param = null) {}',
'<?php function foo(?/*comment*/string $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(/*comment*/ string $param = null) {}',
'<?php function foo(? /*comment*/ string $param = null) {}',
['use_nullable_type_declaration' => false],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix80Cases(): iterable
{
yield from self::createBothWaysCases(self::provideBothWays80Cases());
yield [
'<?php function foo(string $param = null) {}',
'<?php function foo(string|null $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(string $param= null) {}',
'<?php function foo(string | null $param= null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(string $param =null) {}',
'<?php function foo(string| null $param =null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(string $param=null) {}',
'<?php function foo(string |null $param=null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(string $param1 = null, string $param2 = null) {}',
'<?php function foo(null|string $param1 = null, null | string $param2 = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(string &$param = null) {}',
'<?php function foo(null| string &$param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(string & $param = null) {}',
'<?php function foo(null |string & $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(string|int /*comment*/$param = null) {}',
'<?php function foo(string|null|int /*comment*/$param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(string | int /*comment*/&$param = null) {}',
'<?php function foo(string | null | int /*comment*/&$param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php $foo = function (string $param = null) {};',
'<?php $foo = function (NULL | string $param = null) {};',
['use_nullable_type_declaration' => false],
];
yield [
'<?php $foo = function (string|int &$param = null) {};',
'<?php $foo = function (string|NULL|int &$param = null) {};',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(Bar\Baz $param = null) {}',
'<?php function foo(Bar\Baz|null $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(\Bar\Baz $param = null) {}',
'<?php function foo(null | \Bar\Baz $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(Bar\Baz &$param = null) {}',
'<?php function foo(Bar\Baz | NULL &$param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(\Bar\Baz &$param = null) {}',
'<?php function foo(NULL|\Bar\Baz &$param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php $foo = function(array $a = null,
array $b = null, array $c = null, array
$d = null) {};',
'<?php $foo = function(array|null $a = null,
array | null $b = null, array | NULL $c = null, NULL|array
$d = null) {};',
['use_nullable_type_declaration' => false],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @requires PHP <8.0
*
* @dataProvider provideFixPre81Cases
*/
public function testFixPre81(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixPre81Cases(): iterable
{
yield 'do not fix pre PHP 8.1' => [
'<?php
function foo1(&/*comment*/$param = null) {}
function foo2(?string &/*comment*/$param2 = null) {}
',
];
yield [
'<?php function foo(?string &/* comment */$param = null) {}',
'<?php function foo(string &/* comment */$param = null) {}',
];
yield [
'<?php function foo(string &/* comment */$param = null) {}',
'<?php function foo(?string &/* comment */$param = null) {}',
['use_nullable_type_declaration' => false],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected);
}
/**
* @return iterable<int, array{string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php
class Foo
{
public function __construct(
protected readonly ?bool $nullable = null,
) {}
}
',
];
yield [
'<?php
class Foo {
public function __construct(
public readonly ?string $readonlyString = null,
readonly public ?int $readonlyInt = null,
) {}
}',
null,
['use_nullable_type_declaration' => false],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix82Cases(): iterable
{
yield from self::createBothWaysCases(self::provideBothWays82Cases());
yield [
'<?php function foo(\Bar\Baz&\Bar\Qux $param = null) {}',
'<?php function foo((\Bar\Baz&\Bar\Qux)|NULL $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(\Bar\Baz&\Bar\Qux $param = null) {}',
'<?php function foo(null|(\Bar\Baz&\Bar\Qux) $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo((\Bar\Baz&\Bar\Qux)|(\Bar\Quux&\Bar\Corge) $param = null) {}',
'<?php function foo((\Bar\Baz&\Bar\Qux)|null|(\Bar\Quux&\Bar\Corge) $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo((\Bar\Baz&\Bar\Qux)|\Bar\Quux $param = null) {}',
'<?php function foo(null|(\Bar\Baz&\Bar\Qux)|\Bar\Quux $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo( \Bar\Baz&\Bar\Qux $param = null) {}',
'<?php function foo( ( \Bar\Baz&\Bar\Qux ) | null $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(\Bar\Baz&\Bar\Qux $param = null) {}',
'<?php function foo(null | ( \Bar\Baz&\Bar\Qux ) $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(( \Bar\Baz&\Bar\Qux )|\Bar\Quux $param = null) {}',
'<?php function foo(null | ( \Bar\Baz&\Bar\Qux )|\Bar\Quux $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo(( \Bar\Baz & \Bar\Qux )|\Bar\Quux & $param = null) {}',
'<?php function foo(null | ( \Bar\Baz & \Bar\Qux )|\Bar\Quux & $param = null) {}',
['use_nullable_type_declaration' => false],
];
yield [
'<?php function foo((\Bar\Baz&\Bar\Qux) | (\Bar\Quux&\Bar\Corge) $param = null) {}',
'<?php function foo((\Bar\Baz&\Bar\Qux) | null | (\Bar\Quux&\Bar\Corge) $param = null) {}',
['use_nullable_type_declaration' => false],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix84Cases
*
* @requires PHP 8.4
*/
public function testFix84(string $expected, ?string $input = null, array $configuration = []): void
{
$this->testFix($expected, $input, $configuration);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix84Cases(): iterable
{
yield 'asymmetric visibility - use_nullable_type_declaration: true' => [
<<<'PHP'
<?php
class Foo {
public function __construct(
public protected(set) ?int $a = null,
protected protected(set) ?int $b = null,
protected private(set) ?int $c = null,
public(set) ?int $d = null,
protected(set) ?int $e = null,
private(set) ?int $f = null,
) {}
}
PHP,
null,
['use_nullable_type_declaration' => true],
];
yield 'asymmetric visibility - use_nullable_type_declaration: false' => [
<<<'PHP'
<?php
class Foo {
public function __construct(
public protected(set) ?int $a = null,
protected protected(set) ?int $b = null,
protected private(set) ?int $c = null,
public(set) ?int $d = null,
protected(set) ?int $e = null,
private(set) ?int $f = null,
) {}
}
PHP,
null,
['use_nullable_type_declaration' => false],
];
}
/**
* @return iterable<int, array{string, string}>
*/
private static function provideBothWaysCases(): iterable
{
yield [
'<?php function foo(?string $param = null) {}',
'<?php function foo(string $param = null) {}',
];
yield [
'<?php function foo(?string $param= null) {}',
'<?php function foo(string $param= null) {}',
];
yield [
'<?php function foo(?string $param =null) {}',
'<?php function foo(string $param =null) {}',
];
yield [
'<?php function foo(?string $param=null) {}',
'<?php function foo(string $param=null) {}',
];
yield [
'<?php function foo(?string $param1 = null, ?string $param2 = null) {}',
'<?php function foo(string $param1 = null, string $param2 = null) {}',
];
yield [
'<?php function foo(?string &$param = null) {}',
'<?php function foo(string &$param = null) {}',
];
yield [
'<?php function foo(?string & $param = null) {}',
'<?php function foo(string & $param = null) {}',
];
yield [
'<?php function foo(?string /*comment*/$param = null) {}',
'<?php function foo(string /*comment*/$param = null) {}',
];
yield [
'<?php function foo(?string /*comment*/&$param = null) {}',
'<?php function foo(string /*comment*/&$param = null) {}',
];
yield [
'<?php $foo = function (?string $param = null) {};',
'<?php $foo = function (string $param = null) {};',
];
yield [
'<?php $foo = function (?string &$param = null) {};',
'<?php $foo = function (string &$param = null) {};',
];
yield [
'<?php function foo(?Baz $param = null) {}',
'<?php function foo(Baz $param = null) {}',
];
yield [
'<?php function foo(?\Baz $param = null) {}',
'<?php function foo(\Baz $param = null) {}',
];
yield [
'<?php function foo(?Bar\Baz $param = null) {}',
'<?php function foo(Bar\Baz $param = null) {}',
];
yield [
'<?php function foo(?\Bar\Baz $param = null) {}',
'<?php function foo(\Bar\Baz $param = null) {}',
];
yield [
'<?php function foo(?Baz &$param = null) {}',
'<?php function foo(Baz &$param = null) {}',
];
yield [
'<?php function foo(?\Baz &$param = null) {}',
'<?php function foo(\Baz &$param = null) {}',
];
yield [
'<?php function foo(?Bar\Baz &$param = null) {}',
'<?php function foo(Bar\Baz &$param = null) {}',
];
yield [
'<?php function foo(?\Bar\Baz &$param = null) {}',
'<?php function foo(\Bar\Baz &$param = null) {}',
];
yield [
'<?php function foo(?Baz & $param = null) {}',
'<?php function foo(Baz & $param = null) {}',
];
yield [
'<?php function foo(?\Baz & $param = null) {}',
'<?php function foo(\Baz & $param = null) {}',
];
yield [
'<?php function foo(?Bar\Baz & $param = null) {}',
'<?php function foo(Bar\Baz & $param = null) {}',
];
yield [
'<?php function foo(?\Bar\Baz & $param = null) {}',
'<?php function foo(\Bar\Baz & $param = null) {}',
];
yield [
'<?php function foo(?array &$param = null) {}',
'<?php function foo(array &$param = null) {}',
];
yield [
'<?php function foo(?array & $param = null) {}',
'<?php function foo(array & $param = null) {}',
];
yield [
'<?php function foo(?callable $param = null) {}',
'<?php function foo(callable $param = null) {}',
];
yield [
'<?php $foo = function (?Baz $param = null) {};',
'<?php $foo = function (Baz $param = null) {};',
];
yield [
'<?php $foo = function (?Baz &$param = null) {};',
'<?php $foo = function (Baz &$param = null) {};',
];
yield [
'<?php $foo = function (?Baz & $param = null) {};',
'<?php $foo = function (Baz & $param = null) {};',
];
yield [
'<?php class Test { public function foo(?Bar\Baz $param = null) {} }',
'<?php class Test { public function foo(Bar\Baz $param = null) {} }',
];
yield [
'<?php class Test { public function foo(?self $param = null) {} }',
'<?php class Test { public function foo(self $param = null) {} }',
];
yield [
'<?php function foo(?iterable $param = null) {}',
'<?php function foo(iterable $param = null) {}',
];
yield [
'<?php $foo = function(?array $a = null,
?array $b = null, ?array $c = null, ?array
$d = null) {};',
'<?php $foo = function(array $a = null,
array $b = null, array $c = null, array
$d = null) {};',
];
yield [
'<?php function foo(?string $param = NULL) {}',
'<?php function foo(string $param = NULL) {}',
];
yield [
'<?php $foo = fn (?string $param = null) => null;',
'<?php $foo = fn (string $param = null) => null;',
];
yield [
'<?php $foo = fn (?string &$param = null) => null;',
'<?php $foo = fn (string &$param = null) => null;',
];
yield [
'<?php $foo = fn (?Baz $param = null) => null;',
'<?php $foo = fn (Baz $param = null) => null;',
];
yield [
'<?php $foo = fn (?Baz &$param = null) => null;',
'<?php $foo = fn (Baz &$param = null) => null;',
];
yield [
'<?php $foo = fn (?Baz & $param = null) => null;',
'<?php $foo = fn (Baz & $param = null) => null;',
];
yield [
'<?php $foo = fn(?array $a = null,
?array $b = null, ?array $c = null, ?array
$d = null) => null;',
'<?php $foo = fn(array $a = null,
array $b = null, array $c = null, array
$d = null) => null;',
];
}
/**
* @return iterable<array{string, string}>
*/
private static function provideBothWays80Cases(): iterable
{
yield [
'<?php function foo(string|int|null $param = null) {}',
'<?php function foo(string|int $param = null) {}',
];
yield [
'<?php function foo(string|int|null $param = NULL) {}',
'<?php function foo(string|int $param = NULL) {}',
];
yield [
'<?php function foo(string|int|null /*comment*/$param = null) {}',
'<?php function foo(string|int /*comment*/$param = null) {}',
];
yield [
'<?php function foo(string | int|null &$param = null) {}',
'<?php function foo(string | int &$param = null) {}',
];
yield [
'<?php function foo(string | int|null & $param = null) {}',
'<?php function foo(string | int & $param = null) {}',
];
yield [
'<?php function foo(string | int|null /*comment*/&$param = null) {}',
'<?php function foo(string | int /*comment*/&$param = null) {}',
];
yield [
'<?php function foo(string|int|null $param1 = null, string | int|null /*comment*/&$param2 = null) {}',
'<?php function foo(string|int $param1 = null, string | int /*comment*/&$param2 = null) {}',
];
yield 'trailing comma' => [
'<?php function foo(?string $param = null,) {}',
'<?php function foo(string $param = null,) {}',
];
yield 'property promotion' => [
'<?php class Foo {
public function __construct(
public ?string $paramA = null,
protected ?string $paramB = null,
private ?string $paramC = null,
?string $paramD = null,
$a = []
) {}
}',
'<?php class Foo {
public function __construct(
public ?string $paramA = null,
protected ?string $paramB = null,
private ?string $paramC = null,
string $paramD = null,
$a = []
) {}
}',
];
yield 'attribute' => [
'<?php function foo(#[AnAttribute] ?string $param = null) {}',
'<?php function foo(#[AnAttribute] string $param = null) {}',
];
yield 'attributes' => [
'<?php function foo(
#[AnAttribute] ?string $a = null,
#[AnAttribute] ?string $b = null,
#[AnAttribute] ?string $c = null
) {}',
'<?php function foo(
#[AnAttribute] string $a = null,
#[AnAttribute] string $b = null,
#[AnAttribute] string $c = null
) {}',
];
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
private static function provideBothWays82Cases(): iterable
{
yield 'Skip standalone null types' => [
'<?php function foo(null $param = null) {}',
];
yield 'Skip standalone NULL types' => [
'<?php function foo(NULL $param = null) {}',
];
yield [
'<?php function foo((\Bar\Baz&\Bar\Qux)|null $param = null) {}',
'<?php function foo(\Bar\Baz&\Bar\Qux $param = null) {}',
];
yield [
'<?php function foo((\Bar\Baz&\Bar\Qux)|\Bar\Quux|null $param = null) {}',
'<?php function foo((\Bar\Baz&\Bar\Qux)|\Bar\Quux $param = null) {}',
];
yield [
'<?php function foo((\Bar\Baz&\Bar\Qux)|(\Bar\Quux&\Bar\Corge)|null $param = null) {}',
'<?php function foo((\Bar\Baz&\Bar\Qux)|(\Bar\Quux&\Bar\Corge) $param = null) {}',
];
yield [
'<?php function foo( (\Bar\Baz&\Bar\Qux)|null $param = null) {}',
'<?php function foo( \Bar\Baz&\Bar\Qux $param = null) {}',
];
yield [
'<?php function foo((\Bar\Baz&\Bar\Qux)|\Bar\Quux|null &$param = null) {}',
'<?php function foo((\Bar\Baz&\Bar\Qux)|\Bar\Quux &$param = null) {}',
];
yield [
'<?php function foo( (\Bar\Baz&\Bar\Qux)|null & $param = null) {}',
'<?php function foo( \Bar\Baz&\Bar\Qux & $param = null) {}',
];
yield [
'<?php function foo( (\Bar\Baz&\Bar\Qux)|null/*comment*/&$param = null) {}',
'<?php function foo( \Bar\Baz&\Bar\Qux/*comment*/&$param = null) {}',
];
}
/**
* @param iterable<array{string, 1?: string}> $cases
*
* @return iterable<array{string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
private static function createBothWaysCases(iterable $cases): iterable
{
foreach ($cases as $key => $case) {
yield $key => $case;
if (\count($case) > 2) {
throw new \BadMethodCallException(\sprintf('Method "%s" does not support handling "configuration" input yet, please implement it.', __METHOD__));
}
$reversed = array_reverse($case);
yield \sprintf('Inversed %s', $key) => [
$reversed[0],
$reversed[1] ?? null,
['use_nullable_type_declaration' => false],
];
}
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php | tests/Fixer/FunctionNotation/MethodArgumentSpaceFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Preg;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer>
*
* @author Kuanhung Chen <ericj.tw@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class MethodArgumentSpaceFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = [], ?WhitespacesFixerConfig $whitespacesConfig = null): void
{
$this->fixer->configure($configuration);
$this->fixer->setWhitespacesConfig($whitespacesConfig ?? new WhitespacesFixerConfig());
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration, 3?: WhitespacesFixerConfig}>
*/
public static function provideFixCases(): iterable
{
foreach (self::getFixCases() as $case) {
yield [
$case[0],
$case[1] ?? null,
$case[2] ?? [],
new WhitespacesFixerConfig(
str_contains($case[0], "\t") ? "\t" : (Preg::match('/\n \S/', $case[0]) ? ' ' : ' '),
),
];
}
foreach (self::getFixCases() as $case) {
yield [
str_replace("\n", "\r\n", $case[0]),
isset($case[1]) ? str_replace("\n", "\r\n", $case[1]) : null,
$case[2] ?? [],
new WhitespacesFixerConfig(
str_contains($case[0], "\t") ? "\t" : (Preg::match('/\n \S/', $case[0]) ? ' ' : ' '),
"\r\n",
),
];
}
yield [
'<?php function A($c, ...$a){}',
'<?php function A($c ,...$a){}',
];
yield [
<<<'EXPECTED'
<?php
foo(
<<<'EOD'
bar
EOD,
'baz'
);
EXPECTED,
<<<'INPUT'
<?php
foo(
<<<'EOD'
bar
EOD
,
'baz'
);
INPUT,
['after_heredoc' => true],
];
yield [
<<<'EXPECTED'
<?php
foo(
$bar,
$baz,
);
EXPECTED,
null,
['on_multiline' => 'ensure_fully_multiline'],
];
yield [
'<?php
functionCall(
1,
2,
3,
);',
'<?php
functionCall(
1, 2,
3,
);',
[
'on_multiline' => 'ensure_fully_multiline',
],
];
yield [
'<?php foo(1, 2, 3, );',
'<?php foo(1,2,3,);',
];
yield [
'<?php
$fn = fn(
$test1,
$test2
) => null;',
'<?php
$fn = fn(
$test1, $test2
) => null;',
[
'on_multiline' => 'ensure_fully_multiline',
],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix80Cases(): iterable
{
yield 'multiple attributes' => [
'<?php
class MyClass
{
public function __construct(
private string $id,
#[Foo]
#[Bar]
private ?string $name = null,
) {}
}',
'<?php
class MyClass
{
public function __construct(
private string $id,
#[Foo] #[Bar] private ?string $name = null,
) {}
}',
];
yield 'keep attributes as-is' => [
'<?php
class MyClass
{
public function __construct(
private string $id,
#[Foo] #[Bar] private ?string $name = null,
) {}
}',
null,
[
'attribute_placement' => 'ignore',
],
];
yield 'multiple attributes on the same line as argument' => [
'<?php
class MyClass
{
public function __construct(
private string $id,
#[Foo] #[Bar] private ?string $name = null,
) {}
}',
'<?php
class MyClass
{
public function __construct(
private string $id,
#[Foo]
#[Bar]
private ?string $name = null,
) {}
}',
[
'attribute_placement' => 'same_line',
],
];
yield 'single attribute markup with comma separated list' => [
'<?php
class MyClass
{
public function __construct(
private string $id,
#[Foo, Bar]
private ?string $name = null,
) {}
}',
'<?php
class MyClass
{
public function __construct(
private string $id,
#[Foo, Bar] private ?string $name = null,
) {}
}',
];
yield 'attributes with arguments' => [
'<?php
class MyClass
{
public function __construct(
private string $id,
#[Foo(value: 1234, otherValue: [1, 2, 3])]
#[Bar(Bar::BAZ, array(\'[\',\']\'))]
private ?string $name = null,
) {}
}',
'<?php
class MyClass
{
public function __construct(
private string $id,
#[Foo(value: 1234, otherValue: [1, 2, 3])] #[Bar(Bar::BAZ, array(\'[\',\']\'))] private ?string $name = null,
) {}
}',
];
yield 'fully qualified attributes' => [
'<?php
function foo(
#[\Foo\Bar]
$bar,
#[\Foo\Baz]
$baz,
#[\Foo\Buzz]
$buzz
) {}',
'<?php
function foo(
#[\Foo\Bar] $bar, #[\Foo\Baz] $baz, #[\Foo\Buzz] $buzz
) {}',
];
yield 'multiline attributes' => [
'<?php
function foo(
$foo,
#[
Foo\Bar,
Foo\Baz,
Foo\Buzz(a: \'astral\', b: 1234),
]
$bar
) {}',
'<?php
function foo($foo, #[
Foo\Bar,
Foo\Baz,
Foo\Buzz(a: \'astral\', b: 1234),
] $bar) {}',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php
[Foo::class, \'method\'](
...
) ?>',
'<?php
[Foo::class, \'method\']( ...
) ?>',
];
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
private static function getFixCases(): iterable
{
yield [
'<?php
// space '.'
$var1 = $a->some_method(
$var2
);
// space '.'
$var2 = some_function(
$var2
);
// space '.'
$var2a = $z[1](
$var2a
);
'.'
$var3 = function( $a, $b ) { };
',
'<?php
// space '.'
$var1 = $a->some_method(
$var2);
// space '.'
$var2 = some_function(
$var2);
// space '.'
$var2a = $z[1](
$var2a
);
'.'
$var3 = function( $a , $b ) { };
',
[
'on_multiline' => 'ensure_fully_multiline',
],
];
yield 'ensure_fully_multiline for nested calls - one level' => [
'<?php
foo(
$x,
bar(
$foo,
$bar
)
);
',
'<?php
foo($x,
bar($foo,
$bar));
',
[
'on_multiline' => 'ensure_fully_multiline',
],
];
yield 'ensure_fully_multiline for nested calls - two levels' => [
'<?php
foo(
$x,
bar(
$foo,
baz(
$bar,
$baz
)
)
);
',
'<?php
foo($x,
bar($foo,
baz($bar,
$baz)));
',
[
'on_multiline' => 'ensure_fully_multiline',
],
];
yield 'default' => [
'<?php xyz("", "", "", "");',
'<?php xyz("","","","");',
];
yield 'test method arguments' => [
'<?php function xyz($a=10, $b=20, $c=30) {}',
'<?php function xyz($a=10,$b=20,$c=30) {}',
];
yield 'test method arguments with multiple spaces' => [
'<?php function xyz($a=10, $b=20, $c=30) {}',
'<?php function xyz($a=10, $b=20 , $c=30) {}',
];
yield 'test method arguments with multiple spaces (kmsac)' => [
'<?php function xyz($a=10, $b=20, $c=30) {}',
'<?php function xyz($a=10, $b=20 , $c=30) {}',
['keep_multiple_spaces_after_comma' => true],
];
yield 'test method call (I)' => [
'<?php xyz($a=10, $b=20, $c=30);',
'<?php xyz($a=10 ,$b=20,$c=30);',
];
yield 'test method call (II)' => [
'<?php xyz($a=10, $b=20, $this->foo(), $c=30);',
'<?php xyz($a=10,$b=20 ,$this->foo() ,$c=30);',
];
yield 'test method call with multiple spaces (I)' => [
'<?php xyz($a=10, $b=20, $c=30);',
'<?php xyz($a=10 , $b=20 , $c=30);',
];
yield 'test method call with multiple spaces (I) (kmsac)' => [
'<?php xyz($a=10, $b=20, $c=30);',
'<?php xyz($a=10 , $b=20 , $c=30);',
['keep_multiple_spaces_after_comma' => true],
];
yield 'test method call with tab' => [
'<?php xyz($a=10, $b=20, $c=30);',
"<?php xyz(\$a=10 , \$b=20 ,\t \$c=30);",
];
yield 'test method call with tab (kmsac)' => [
"<?php xyz(\$a=10, \$b=20,\t \$c=30);",
"<?php xyz(\$a=10 , \$b=20 ,\t \$c=30);",
['keep_multiple_spaces_after_comma' => true],
];
yield 'test method call with multiple spaces (II)' => [
'<?php xyz($a=10, $b=20, $this->foo(), $c=30);',
'<?php xyz($a=10,$b=20 , $this->foo() ,$c=30);',
];
yield 'test method call with multiple spaces (II) (kmsac)' => [
'<?php xyz($a=10, $b=20, $this->foo(), $c=30);',
'<?php xyz($a=10,$b=20 , $this->foo() ,$c=30);',
['keep_multiple_spaces_after_comma' => true],
];
yield 'test named class constructor call' => [
'<?php new Foo($a=10, $b=20, $this->foo(), $c=30);',
'<?php new Foo($a=10,$b=20 ,$this->foo() ,$c=30);',
];
yield 'test named class constructor call with multiple spaces' => [
'<?php new Foo($a=10, $b=20, $c=30);',
'<?php new Foo($a=10 , $b=20 , $c=30);',
];
yield 'test named class constructor call with multiple spaces (kmsac)' => [
'<?php new Foo($a=10, $b=20, $c=30);',
'<?php new Foo($a=10 , $b=20 , $c=30);',
['keep_multiple_spaces_after_comma' => true],
];
yield 'test anonymous class constructor call' => [
'<?php new class ($a=10, $b=20, $this->foo(), $c=30) {};',
'<?php new class ($a=10,$b=20 ,$this->foo() ,$c=30) {};',
];
yield 'test anonymous class constructor call with multiple spaces' => [
'<?php new class ($a=10, $b=20, $c=30) extends Foo {};',
'<?php new class ($a=10 , $b=20 , $c=30) extends Foo {};',
];
yield 'test anonymous class constructor call with multiple spaces (kmsac)' => [
'<?php new class ($a=10, $b=20, $c=30) {};',
'<?php new class ($a=10 , $b=20 , $c=30) {};',
['keep_multiple_spaces_after_comma' => true],
];
yield 'test receiving data in list context with omitted values' => [
'<?php list($a, $b, , , $c) = foo();',
'<?php list($a, $b,, ,$c) = foo();',
];
yield 'test receiving data in list context with omitted values and multiple spaces' => [
'<?php list($a, $b, , , $c) = foo();',
'<?php list($a, $b,, ,$c) = foo();',
];
yield 'test receiving data in list context with omitted values and multiple spaces (kmsac)' => [
'<?php list($a, $b, , , $c) = foo();',
'<?php list($a, $b,, ,$c) = foo();',
['keep_multiple_spaces_after_comma' => true],
];
yield 'skip array' => [
'<?php array(10 , 20 ,30); $foo = [ 10,50 , 60 ] ?>',
];
yield 'list call with trailing comma' => [
'<?php list($path, $mode, ) = foo();',
'<?php list($path, $mode,) = foo();',
];
yield 'list call with trailing comma multi line' => [
'<?php
list(
$a,
$b,
) = foo();
',
'<?php
list(
$a ,
$b ,
) = foo();
',
];
yield 'inline comments with spaces' => [
'<?php xyz($a=10, /*comment1*/ $b=2000, /*comment2*/ $c=30);',
'<?php xyz($a=10, /*comment1*/ $b=2000,/*comment2*/ $c=30);',
];
yield 'inline comments with spaces (kmsac)' => [
'<?php xyz($a=10, /*comment1*/ $b=2000, /*comment2*/ $c=30);',
'<?php xyz($a=10, /*comment1*/ $b=2000,/*comment2*/ $c=30);',
['keep_multiple_spaces_after_comma' => true],
];
yield 'multi line testing method call' => [
'<?php if (1) {
xyz(
$a=10,
$b=20,
$c=30
);
}',
'<?php if (1) {
xyz(
$a=10 ,
$b=20,
$c=30
);
}',
];
yield 'multi line anonymous class constructor call' => [
'<?php if (1) {
new class (
$a=10,
$b=20,
$c=30
) {};
}',
'<?php if (1) {
new class (
$a=10 ,
$b=20,$c=30) {};
}',
];
yield 'skip arrays but replace arg methods' => [
'<?php fnc(1, array(2, func2(6, 7) ,4), 5);',
'<?php fnc(1,array(2, func2(6, 7) ,4), 5);',
];
yield 'skip arrays but replace arg methods (kmsac)' => [
'<?php fnc(1, array(2, func2(6, 7) ,4), 5);',
'<?php fnc(1,array(2, func2(6, 7) ,4), 5);',
['keep_multiple_spaces_after_comma' => true],
];
yield 'ignore commas inside call argument' => [
'<?php fnc(1, array(2, 3 ,4), 5);',
];
yield 'skip multi line array' => [
'<?php
array(
10 ,
20,
30
);',
];
yield 'skip short array' => [
'<?php
$foo = ["a"=>"apple", "b"=>"bed" ,"c"=>"car"];
$bar = ["a" ,"b" ,"c"];
',
];
yield 'don\'t change HEREDOC and NOWDOC' => [
"<?php if (1) {
\$this->foo(
<<<EOTXTa
heredoc
EOTXTa
,
<<<'EOTXTb'
nowdoc
EOTXTb
,
'foo'
);
}",
];
yield 'with_random_comments on_multiline:ignore' => [
'<?php xyz#
(#
""#
,#
$a#
);',
null,
['on_multiline' => 'ignore'],
];
yield 'with_random_comments on_multiline:ensure_single_line' => [
'<?php xyz#
(#
""#
,#
$a#
);',
null,
['on_multiline' => 'ensure_single_line'],
];
yield 'with_random_comments on_multiline:ensure_fully_multiline' => [
'<?php xyz#
(#
""#
,#
$a#
);',
'<?php xyz#
(#
""#
,#
$a#
);',
['on_multiline' => 'ensure_fully_multiline'],
];
yield 'test half-multiline function becomes fully-multiline' => [
<<<'EXPECTED'
<?php
functionCall(
'a',
'b',
'c'
);
EXPECTED,
<<<'INPUT'
<?php
functionCall(
'a', 'b',
'c'
);
INPUT,
];
yield 'test wrongly formatted half-multiline function becomes fully-multiline' => [
'<?php
f(
1,
2,
3
);',
'<?php
f(1,2,
3);',
];
yield 'function calls with here doc cannot be anything but multiline' => [
<<<'EXPECTED'
<?php
str_replace(
"\n",
PHP_EOL,
<<<'TEXT'
1) someFile.php
TEXT
);
EXPECTED,
<<<'INPUT'
<?php
str_replace("\n", PHP_EOL, <<<'TEXT'
1) someFile.php
TEXT
);
INPUT,
];
yield 'test barely multiline function with blank lines becomes fully-multiline' => [
<<<'EXPECTED'
<?php
functionCall(
'a',
'b',
'c'
);
EXPECTED,
<<<'INPUT'
<?php
functionCall('a', 'b',
'c');
INPUT,
];
yield 'test indentation is preserved' => [
<<<'EXPECTED'
<?php
if (true) {
functionCall(
'a',
'b',
'c'
);
}
EXPECTED,
<<<'INPUT'
<?php
if (true) {
functionCall(
'a', 'b',
'c'
);
}
INPUT,
];
yield 'test multiline array arguments do not trigger multiline' => [
<<<'EXPECTED'
<?php
defraculate(1, array(
'a',
'b',
'c',
), 42);
EXPECTED,
];
yield 'test multiline function arguments do not trigger multiline' => [
<<<'EXPECTED'
<?php
defraculate(1, function () {
$a = 42;
}, 42);
EXPECTED,
];
yield 'test violation after opening parenthesis' => [
<<<'EXPECTED'
<?php
defraculate(
1,
2,
3
);
EXPECTED,
<<<'INPUT'
<?php
defraculate(
1, 2, 3);
INPUT,
];
yield 'test violation after opening parenthesis, indented with two spaces' => [
<<<'EXPECTED'
<?php
defraculate(
1,
2,
3
);
EXPECTED,
<<<'INPUT'
<?php
defraculate(
1, 2, 3);
INPUT,
];
yield 'test violation after opening parenthesis, indented with tabs' => [
<<<'EXPECTED'
<?php
defraculate(
1,
2,
3
);
EXPECTED,
<<<'INPUT'
<?php
defraculate(
1, 2, 3);
INPUT,
];
yield 'test violation before closing parenthesis' => [
<<<'EXPECTED'
<?php
defraculate(
1,
2,
3
);
EXPECTED,
<<<'INPUT'
<?php
defraculate(1, 2, 3
);
INPUT,
];
yield 'test violation before closing parenthesis in nested call' => [
<<<'EXPECTED'
<?php
getSchwifty('rick', defraculate(
1,
2,
3
), 'morty');
EXPECTED,
<<<'INPUT'
<?php
getSchwifty('rick', defraculate(1, 2, 3
), 'morty');
INPUT,
];
yield 'test with comment between arguments' => [
<<<'EXPECTED'
<?php
functionCall(
'a', /* comment */
'b',
'c'
);
EXPECTED,
<<<'INPUT'
<?php
functionCall(
'a',/* comment */'b',
'c'
);
INPUT,
];
yield 'test with deeply nested arguments' => [
<<<'EXPECTED'
<?php
foo(
'a',
'b',
[
'c',
'd', bar('e', 'f'),
baz(
'g',
['h',
'i',
]
),
]
);
EXPECTED,
<<<'INPUT'
<?php
foo('a',
'b',
[
'c',
'd', bar('e', 'f'),
baz('g',
['h',
'i',
]),
]);
INPUT,
];
yield 'multiline string argument' => [
<<<'UNAFFECTED'
<?php
$this->with('<?php
%s
class FooClass
{
}', $comment, false);
UNAFFECTED,
];
yield 'arrays with whitespace inside' => [
<<<'UNAFFECTED'
<?php
$a = array/**/( 1);
$a = array/**/( 12,
7);
$a = array/***/(123, 7);
$a = array ( 1,
2);
UNAFFECTED,
];
yield 'test code that should not be affected (because not a function nor a method)' => [
<<<'UNAFFECTED'
<?php
if (true &&
true
) {
// do whatever
}
UNAFFECTED,
];
yield 'test ungodly code' => [
<<<'EXPECTED'
<?php
$a = function#
(#
#
$a#
#
,#
#
$b,
$c#
#
)#
use (
$b1,
$c1,
$d1
) {
};
EXPECTED,
<<<'INPUT'
<?php
$a = function#
(#
#
$a#
#
,#
#
$b,$c#
#
)#
use ($b1,
$c1,$d1) {
};
INPUT,
];
yield 'test list' => [
<<<'UNAFFECTED'
<?php
// no fix
list($a,
$b, $c) = $a;
isset($a,
$b, $c);
unset($a,
$b, $c);
array(1,
2,3
);
UNAFFECTED,
];
yield 'test function argument with multiline echo in it' => [
<<<'UNAFFECTED'
<?php
call_user_func(function ($arguments) {
echo 'a',
'b';
}, $argv);
UNAFFECTED,
];
yield 'test function argument with oneline echo in it' => [
<<<'EXPECTED'
<?php
call_user_func(
function ($arguments) {
echo 'a', 'b';
},
$argv
);
EXPECTED,
<<<'INPUT'
<?php
call_user_func(function ($arguments) {
echo 'a', 'b';
},
$argv);
INPUT,
];
yield 'ensure_single_line' => [
<<<'EXPECTED'
<?php
function foo($a, $b) {
// foo
}
foo($a, $b);
EXPECTED,
<<<'INPUT'
<?php
function foo(
$a,
$b
) {
// foo
}
foo(
$a,
$b
);
INPUT,
['on_multiline' => 'ensure_single_line'],
];
yield 'ensure_single_line_with_random_comments' => [
<<<'EXPECTED'
<?php
function foo(/* foo */// bar
$a, /* foo */// bar
$b#foo
) {
// foo
}
foo(/* foo */// bar
$a, /* foo */// bar
$b#foo
);
EXPECTED,
null,
['on_multiline' => 'ensure_single_line'],
];
yield 'ensure_single_line_with_consecutive_newlines' => [
<<<'EXPECTED'
<?php
function foo($a, $b) {
// foo
}
foo($a, $b);
EXPECTED,
<<<'INPUT'
<?php
function foo(
$a,
$b
) {
// foo
}
foo(
$a,
$b
);
INPUT,
['on_multiline' => 'ensure_single_line'],
];
yield 'ensure_single_line_methods' => [
<<<'EXPECTED'
<?php
class Foo {
public static function foo1($a, $b, $c) {}
private function foo2($a, $b, $c) {}
}
EXPECTED,
<<<'INPUT'
<?php
class Foo {
public static function foo1(
$a,
$b,
$c
) {}
private function foo2(
$a,
$b,
$c
) {}
}
INPUT,
['on_multiline' => 'ensure_single_line'],
];
yield 'ensure_single_line_methods_in_anonymous_class' => [
<<<'EXPECTED'
<?php
new class {
public static function foo1($a, $b, $c) {}
private function foo2($a, $b, $c) {}
};
EXPECTED,
<<<'INPUT'
<?php
new class {
public static function foo1(
$a,
$b,
$c
) {}
private function foo2(
$a,
$b,
$c
) {}
};
INPUT,
['on_multiline' => 'ensure_single_line'],
];
yield 'ensure_single_line_keep_spaces_after_comma' => [
<<<'EXPECTED'
<?php
function foo($a, $b) {
// foo
}
foo($a, $b);
EXPECTED,
<<<'INPUT'
<?php
function foo(
$a,
$b
) {
// foo
}
foo(
$a,
$b
);
INPUT,
[
'on_multiline' => 'ensure_single_line',
'keep_multiple_spaces_after_comma' => true,
],
];
yield 'fix closing parenthesis (without trailing comma)' => [
'<?php
if (true) {
execute(
$foo,
$bar
);
}',
'<?php
if (true) {
execute(
$foo,
$bar
);
}',
[
'on_multiline' => 'ensure_fully_multiline',
],
];
yield 'test anonymous functions' => [
'<?php
$example = function () use ($message1, $message2) {
};',
'<?php
$example = function () use ($message1,$message2) {
};',
];
yield 'test first element in same line, space before comma and inconsistent indent' => [
'<?php foo(
"aaa
bbb",
$c,
$d,
$e,
$f
);
',
'<?php foo("aaa
bbb",
$c, $d ,
$e,
$f);
',
];
yield 'test first element in same line, space before comma and inconsistent indent with comments' => [
'<?php foo(
"aaa
bbb", // comment1
$c, /** comment2 */
$d,
$e/* comment3 */,
$f
);# comment4
',
'<?php foo("aaa
bbb", // comment1
$c, /** comment2 */$d ,
$e/* comment3 */,
$f);# comment4
',
];
yield [
'<?php
foo(
/* bar */
"baz"
);
',
'<?php
foo(
/* bar */ "baz"
);
',
];
yield [<<<'PHP'
<?php
function f()
{
echo <<<TEXT
some text
{$object->method(
42
)}
some text
TEXT;
}
PHP];
yield [<<<'PHP'
<?php
function f()
{
echo <<<'TEXT'
some text
{$object->method(
42
)}
some text
TEXT;
}
PHP];
yield [<<<'PHP'
<?php
function f()
{
echo <<<TEXT
some text
some value: {$object->method(
42
)}
some text
TEXT;
}
PHP];
yield [<<<'PHP'
<div>
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | true |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/StaticLambdaFixerTest.php | tests/Fixer/FunctionNotation/StaticLambdaFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\StaticLambdaFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\StaticLambdaFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class StaticLambdaFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'sample' => [
"<?php\n\$a = static function () use (\$b)\n{ echo \$b;\n};",
"<?php\n\$a = function () use (\$b)\n{ echo \$b;\n};",
];
yield 'minimal double fix case' => [
'<?php $a=static function(){};$b=static function(){};',
'<?php $a=function(){};$b=function(){};',
];
yield [
'<?php $a /**/ = /**/ static function(){};',
'<?php $a /**/ = /**/ function(){};',
];
yield [
'<?php $a /**/ = /**/ static function(){};',
'<?php $a /**/ = /**/ function(){};',
];
yield [
'<?php $a /**/ = /**/static function(){};',
'<?php $a /**/ = /**/function(){};',
];
yield [
'<?php $a=static fn() => null;$b=static fn() => null;',
'<?php $a=fn() => null;$b=fn() => null;',
];
yield [
'<?php $a /**/ = /**/ static fn() => null;',
'<?php $a /**/ = /**/ fn() => null;',
];
yield [
'<?php $a /**/ = /**/ static fn() => null;',
'<?php $a /**/ = /**/ fn() => null;',
];
yield [
'<?php $a /**/ = /**/static fn() => null; echo $this->foo();',
'<?php $a /**/ = /**/fn() => null; echo $this->foo();',
];
yield [
'<?php $a /**/ = /**/ static fn() => null ?> <?php echo $this->foo();',
'<?php $a /**/ = /**/ fn() => null ?> <?php echo $this->foo();',
];
yield [
'<?php
class B
{
public function C()
{
$a = fn () => var_dump($this);
$a();
}
}
',
];
yield [
'<?php static fn($a = ["foo" => "bar"]) => [];',
'<?php fn($a = ["foo" => "bar"]) => [];',
];
yield [
'<?php class Foo {
public function getNames()
{
return \array_map(
static fn ($item) => $item->getName(),
$this->getItems()
);
}
}',
'<?php class Foo {
public function getNames()
{
return \array_map(
fn ($item) => $item->getName(),
$this->getItems()
);
}
}',
];
yield [
'<?php class Foo {
public function getNames()
{
return \array_map(
fn ($item) => $item->getName(1, $this->foo()),
$this->getItems()
);
}
}',
];
yield [
'<?php
class A
{
public function B()
{
$a = function () {
$b = \'this\';
var_dump($$b);
};
$a();
}
}
',
];
yield [
'<?php
class B
{
public function C()
{
$a = function () {
var_dump($THIS);
};
$a();
}
}
',
];
yield [
'<?php
class D
{
public function E()
{
$a = function () {
$c = include __DIR__.\'/return_this.php\';
var_dump($c);
};
$a();
}
}
',
];
yield [
'<?php
class F
{
public function G()
{
$a = function () {
$d = include_once __DIR__.\'/return_this.php\';
var_dump($d);
};
$a();
}
}
',
];
yield [
'<?php
class H
{
public function I()
{
$a = function () {
$e = require_once __DIR__.\'/return_this.php\';
var_dump($e);
};
$a();
}
}
',
];
yield [
'<?php
class J
{
public function K()
{
$a = function () {
$f = require __DIR__.\'/return_this.php\';
var_dump($f);
};
$a();
}
}
',
];
yield [
'<?php
class L
{
public function M()
{
$a = function () {
$g = \'this\';
$h = ${$g};
var_dump($h);
};
$a();
}
}
',
];
yield [
'<?php
class N
{
public function O()
{
$a = function () {
$a = [0 => \'this\'];
var_dump(${$a[0]});
};
}
}
',
];
yield [
'<?php
class N
{
public function O()
{
$a = function () {
return new class($this) {};
};
}
}
',
];
yield [
'<?php function test(){} test();',
];
yield [
'<?php abstract class P
{
function test0(){}
public function test1(){}
protected function test2(){}
protected abstract function test3();
private function test4(){}
}
',
];
yield [
'<?php
class Q
{
public function abc()
{
$b = function () {
$c = eval(\'return $this;\');
var_dump($c);
};
$b();
}
}
$b = new Q();
$b->abc();
',
];
yield [
'<?php
class A {}
class B extends A {
public function foo()
{
$c = function () {
return parent::foo();
};
}
}',
];
yield [
'<?php
class B
{
public function C()
{
return array_map(
fn () => $this,
[]
);
}
}
',
];
yield 'anonymous function returning defining class that uses `$this`' => [
<<<'PHP'
<?php
$f = static function () {
class C extends P {
public function f() { return $this->f2(); }
}
};
PHP,
<<<'PHP'
<?php
$f = function () {
class C extends P {
public function f() { return $this->f2(); }
}
};
PHP,
];
yield 'anonymous function defining trait that uses `$this`' => [
<<<'PHP'
<?php
$f = static function () {
trait T {
public function f() { return $this->f2(); }
}
};
PHP,
<<<'PHP'
<?php
$f = function () {
trait T {
public function f() { return $this->f2(); }
}
};
PHP,
];
yield 'anonymous function using anonymous class that uses `$this`' => [
<<<'PHP'
<?php
$f = static function () {
$o = new class { function f() { return $this->x; } };
return $o->f();
};
PHP,
<<<'PHP'
<?php
$f = function () {
$o = new class { function f() { return $this->x; } };
return $o->f();
};
PHP,
];
yield 'arrow function returning anonymous class that uses `$this`' => [
'<?php return static fn () => new class { function f() { return $this->x; } };',
'<?php return fn () => new class { function f() { return $this->x; } };',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/CombineNestedDirnameFixerTest.php | tests/Fixer/FunctionNotation/CombineNestedDirnameFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\CombineNestedDirnameFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\CombineNestedDirnameFixer>
*
* @author Gregor Harlan
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class CombineNestedDirnameFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php dirname();',
];
yield [
'<?php dirname($path);',
];
yield [
'<?php dirname($path, 3);',
];
yield [
'<?php dirname($path, 2);',
'<?php dirname(dirname($path));',
];
yield [
'<?php dirname /* a */ ( /* b */ /* c */ $path /* d */, 2);',
'<?php dirname /* a */ ( /* b */ dirname( /* c */ $path) /* d */);',
];
yield [
'<?php dirname($path, 3);',
'<?php dirname(\dirname(dirname($path)));',
];
yield [
'<?php dirname($path, 4);',
'<?php dirname(dirname($path, 3));',
];
yield [
'<?php dirname($path, 4);',
'<?php dirname(dirname($path), 3);',
];
yield [
'<?php dirname($path, 5);',
'<?php dirname(dirname($path, 2), 3);',
];
yield [
'<?php dirname($path, 5);',
'<?php dirname(dirname(dirname($path), 3));',
];
yield [
'<?php dirname(dirname($path, $level));',
];
yield [
'<?php dirname("foo/".dirname($path));',
];
yield [
'<?php dirname(dirname($path).$foo);',
];
yield [
'<?php foo\dirname(dirname($path));',
];
yield [
'<?php dirname(foo(dirname($path, 2)), 2);',
'<?php dirname(dirname(foo(dirname(dirname($path)))));',
];
yield [
'<?php new dirname(dirname($path, 2));',
'<?php new dirname(dirname(dirname($path)));',
];
yield [
'<?php dirname($path, 3);',
'<?php dirname(dirname(dirname($path, ), ));',
];
yield [
'<?php dirname($path, 3);',
'<?php dirname(dirname(dirname($path, ), ), );',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield ['<?php $a = dirname(dirname(...));'];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/UseArrowFunctionsFixerTest.php | tests/Fixer/FunctionNotation/UseArrowFunctionsFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\UseArrowFunctionsFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\UseArrowFunctionsFixer>
*
* @author Gregor Harlan
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class UseArrowFunctionsFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php foo(function () use ($a, &$b) { return 1; });',
];
yield [
'<?php foo(function () { bar(); return 1; });',
];
yield [
'<?php foo(fn()=> 1);',
'<?php foo(function(){return 1;});',
];
yield [
'<?php foo(fn()=>$a);',
'<?php foo(function()use($a){return$a;});',
];
yield [
'<?php foo( fn () => 1 );',
'<?php foo( function () { return 1; } );',
];
yield [
'<?php $func = static fn &(array &$a, string ...$b): ?int => 1;',
'<?php $func = static function &(array &$a, string ...$b): ?int { return 1; };',
];
yield [
<<<'EXPECTED'
<?php
foo(1, fn (int $a, Foo $b) => bar($a, $c), 2);
EXPECTED,
<<<'INPUT'
<?php
foo(1, function (int $a, Foo $b) use ($c, $d) {
return bar($a, $c);
}, 2);
INPUT,
];
yield [
<<<'EXPECTED'
<?php
foo(fn () => 1);
EXPECTED,
<<<'INPUT'
<?php
foo(function () {
return 1;
});
INPUT,
];
yield [
<<<'EXPECTED'
<?php
foo(fn ($a) => fn () => $a + 1);
EXPECTED,
<<<'INPUT'
<?php
foo(function ($a) {
return function () use ($a) {
return $a + 1;
};
});
INPUT,
];
yield [
<<<'EXPECTED'
<?php
foo(function () {// comment
return 1;
});
EXPECTED,
];
yield [
<<<'EXPECTED'
<?php
foo(function () {
// comment
return 1;
});
EXPECTED,
];
yield [
<<<'EXPECTED'
<?php
foo(function () {
return 1; // comment
});
EXPECTED,
];
yield [
<<<'EXPECTED'
<?php
foo(function () {
return 1;
// comment
});
EXPECTED,
];
yield [
<<<'PHP'
<?php
foo(fn () =>
1);
PHP,
<<<'PHP'
<?php
foo(function () {
return
1;
});
PHP,
];
yield [
<<<'PHP'
<?php
$func = fn (
$a,
$b
) => 1;
PHP,
<<<'PHP'
<?php
$func = function (
$a,
$b
) {
return 1;
};
PHP,
];
yield [
<<<'EXPECTED'
<?php
$func = function () {
return function () {
foo();
};
};
EXPECTED,
];
yield [
'<?php $testDummy = fn () => null;',
'<?php $testDummy = function () { return; };',
];
yield [
'<?php $testDummy = fn () => null ;',
'<?php $testDummy = function () { return ; };',
];
yield [
'<?php $testDummy = fn () => null/* foo */;',
'<?php $testDummy = function () { return/* foo */; };',
];
yield [
<<<'PHP'
<?php return fn () => [
CONST_A,
CONST_B,
];
PHP,
<<<'PHP'
<?php return function () {
return [
CONST_A,
CONST_B,
];
};
PHP,
];
yield [
'<?php
foo(
fn () => 42
'.'
);',
'<?php
foo(
function () {
return 42
;
}
);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php | tests/Fixer/FunctionNotation/PhpdocToParamTypeFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @group phpdoc
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\PhpdocToParamTypeFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\PhpdocToParamTypeFixer>
*
* @author Jan Gantzert <jan@familie-gantzert.de>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\PhpdocToParamTypeFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocToParamTypeFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'type declaration already defined' => [
'<?php /** @param int $foo */ function foo(int $foo) {}',
];
yield 'type declaration already defined with wrong phpdoc type' => [
'<?php /** @param string $foo */ function foo(int $foo) {}',
];
yield 'no phpdoc param' => [
'<?php function my_foo() {}',
];
yield 'invalid - phpdoc param without variable' => [
'<?php /** @param */ function my_foo($bar) {}',
];
yield 'invalid - phpdoc param with non existing class' => [
'<?php /** @param \9 */ function my_foo($bar) {}',
];
yield 'invalid - phpdoc param with false class hint' => [
'<?php /** @param $foo \Foo\\\Bar */ function my_foo($foo) {}',
];
yield 'invalid - phpdoc param with false param order' => [
'<?php /** @param $foo string */ function my_foo($foo) {}',
];
yield 'invalid - phpdoc param with hint for next method' => [
'<?php
/**
* @param string $bar
*/
function my_foo() {}
function my_foo2($bar) {}
',
];
yield 'invalid - phpdoc param with keyword' => [
'<?php
/** @param Break $foo */ function foo_break($foo) {}
/** @param __CLASS__ $foo */ function foo_class($foo) {}
/** @param I\Want\To\Break\\\Free $foo */ function foo_queen($foo) {}
',
];
yield 'non-root class with single int param' => [
'<?php /** @param int $bar */ function my_foo(int $bar) {}',
'<?php /** @param int $bar */ function my_foo($bar) {}',
];
yield 'non-root class with single float param' => [
'<?php /** @param float $bar */ function my_foo(float $bar) {}',
'<?php /** @param float $bar */ function my_foo($bar) {}',
];
yield 'non-root class with multiple string params' => [
'<?php
/**
* @param string $bar
* @param string $baz
*/
function my_foo(string $bar, string $baz) {}',
'<?php
/**
* @param string $bar
* @param string $baz
*/
function my_foo($bar, $baz) {}',
];
yield 'non-root class with not sorted multiple string params' => [
'<?php
/**
* @param int $foo
* @param string $bar
*/
function my_foo(string $bar, int $foo) {}',
'<?php
/**
* @param int $foo
* @param string $bar
*/
function my_foo($bar, $foo) {}',
];
yield 'non-root class with not sorted multiple params and different types' => [
'<?php
/**
* @param int $foo
* @param string $bar
* @param Baz $hey
* @param float $tab
* @param bool $baz
*/
function my_foo(string $bar, int $foo, bool $baz, float $tab, Baz $hey) {}',
'<?php
/**
* @param int $foo
* @param string $bar
* @param Baz $hey
* @param float $tab
* @param bool $baz
*/
function my_foo($bar, $foo, $baz, $tab, $hey) {}',
];
yield 'non-root class with massive string params' => [
'<?php
/**
* @param string $bar
* @param string $baz
* @param string $tab
* @param string $foo
*/
function my_foo(string $bar, string $baz, string $tab, string $foo) {}',
'<?php
/**
* @param string $bar
* @param string $baz
* @param string $tab
* @param string $foo
*/
function my_foo($bar, $baz, $tab, $foo) {}',
];
yield 'non-root class with different types of params' => [
'<?php
/**
* @param string $bar
* @param int $baz
* @param float $tab
*/
function my_foo(string $bar, int $baz, float $tab) {}',
'<?php
/**
* @param string $bar
* @param int $baz
* @param float $tab
*/
function my_foo($bar, $baz, $tab) {}',
];
yield 'non-root namespaced class' => [
'<?php /** @param My\Bar $foo */ function my_foo(My\Bar $foo) {}',
'<?php /** @param My\Bar $foo */ function my_foo($foo) {}',
];
yield 'root class' => [
'<?php /** @param \My\Bar $foo */ function my_foo(\My\Bar $foo) {}',
'<?php /** @param \My\Bar $foo */ function my_foo($foo) {}',
];
yield 'interface' => [
'<?php interface Foo { /** @param Bar $bar */ function my_foo(Bar $bar); }',
'<?php interface Foo { /** @param Bar $bar */ function my_foo($bar); }',
];
yield 'iterable return on ^7.1' => [
'<?php /** @param iterable $counter */ function my_foo(iterable $counter) {}',
'<?php /** @param iterable $counter */ function my_foo($counter) {}',
];
yield 'array native type' => [
'<?php /** @param array $foo */ function my_foo(array $foo) {}',
'<?php /** @param array $foo */ function my_foo($foo) {}',
];
yield 'callable type' => [
'<?php /** @param callable $foo */ function my_foo(callable $foo) {}',
'<?php /** @param callable $foo */ function my_foo($foo) {}',
];
yield 'self accessor' => [
'<?php
class Foo {
/** @param self $foo */ function my_foo(self $foo) {}
}
',
'<?php
class Foo {
/** @param self $foo */ function my_foo($foo) {}
}
',
];
yield 'report static as self' => [
'<?php
class Foo {
/** @param static $foo */ function my_foo(self $foo) {}
}
',
'<?php
class Foo {
/** @param static $foo */ function my_foo($foo) {}
}
',
];
yield 'skip resource special type' => [
'<?php /** @param $bar resource */ function my_foo($bar) {}',
];
yield 'skip mixed special type' => [
'<?php /** @param $bar mixed */ function my_foo($bar) {}',
];
yield 'null alone cannot be a param type' => [
'<?php /** @param $bar null */ function my_foo($bar) {}',
];
yield 'array of types' => [
'<?php /** @param Foo[] $foo */ function my_foo(array $foo) {}',
'<?php /** @param Foo[] $foo */ function my_foo($foo) {}',
];
yield 'nullable array of types' => [
'<?php /** @param null|Foo[] $foo */ function my_foo(?array $foo) {}',
'<?php /** @param null|Foo[] $foo */ function my_foo($foo) {}',
];
yield 'nullable and mixed types of arrays' => [
'<?php /** @param null|Foo[]|Bar[] $foo */ function my_foo(?array $foo) {}',
'<?php /** @param null|Foo[]|Bar[] $foo */ function my_foo($foo) {}',
];
yield 'nullable and array and array of types' => [
'<?php /** @param null|Foo[]|array $foo */ function my_foo(?array $foo) {}',
'<?php /** @param null|Foo[]|array $foo */ function my_foo($foo) {}',
];
yield 'nullable array of array of types' => [
'<?php /** @param null|Foo[][] $foo */ function my_foo(?array $foo) {}',
'<?php /** @param null|Foo[][] $foo */ function my_foo($foo) {}',
];
yield 'nullable and string param' => [
'<?php /** @param null|string $foo */ function my_foo(?string $foo) {}',
'<?php /** @param null|string $foo */ function my_foo($foo) {}',
];
yield 'nullable and int param' => [
'<?php /** @param null|int $foo */ function my_foo(?int $foo) {}',
'<?php /** @param null|int $foo */ function my_foo($foo) {}',
];
yield 'nullable and float param' => [
'<?php /** @param null|float $foo */ function my_foo(?float $foo) {}',
'<?php /** @param null|float $foo */ function my_foo($foo) {}',
];
yield 'nullable and bool param' => [
'<?php /** @param null|bool $foo */ function my_foo(?bool $foo) {}',
'<?php /** @param null|bool $foo */ function my_foo($foo) {}',
];
yield 'nullable and callable param' => [
'<?php /** @param null|callable $foo */ function my_foo(?callable $foo) {}',
'<?php /** @param null|callable $foo */ function my_foo($foo) {}',
];
yield 'nullable and iterable param' => [
'<?php /** @param null|iterable $foo */ function my_foo(?iterable $foo) {}',
'<?php /** @param null|iterable $foo */ function my_foo($foo) {}',
];
yield 'nullable and class name param' => [
'<?php /** @param null|Foo $foo */ function my_foo(?Foo $foo) {}',
'<?php /** @param null|Foo $foo */ function my_foo($foo) {}',
];
yield 'nullable with ? notation in phpDoc' => [
'<?php /** @param ?Foo $foo */ function my_foo(?Foo $foo) {}',
'<?php /** @param ?Foo $foo */ function my_foo($foo) {}',
];
yield 'array and iterable param' => [
'<?php /** @param Foo[]|iterable $foo */ function my_foo(iterable $foo) {}',
'<?php /** @param Foo[]|iterable $foo */ function my_foo($foo) {}',
];
yield 'object param' => [
'<?php /** @param object $foo */ function my_foo(object $foo) {}',
'<?php /** @param object $foo */ function my_foo($foo) {}',
];
yield 'nullable and object param' => [
'<?php /** @param null|object $foo */ function my_foo(?object $foo) {}',
'<?php /** @param null|object $foo */ function my_foo($foo) {}',
];
yield 'generics with single type' => [
'<?php /** @param array<foo> $foo */ function my_foo(array $foo) {}',
'<?php /** @param array<foo> $foo */ function my_foo($foo) {}',
];
yield 'generics with multiple types' => [
'<?php /** @param array<int, string> $foo */ function my_foo(array $foo) {}',
'<?php /** @param array<int, string> $foo */ function my_foo($foo) {}',
];
yield 'stop searching last token' => [
'<?php class Foo { /** @param Bar $bar */ public function foo($tab) { } }',
];
yield 'param by reference' => [
'<?php /** @param array $data */ function foo(array &$data) {}',
'<?php /** @param array $data */ function foo(&$data) {}',
];
yield 'param by reference in PHPDoc too' => [
<<<'PHP'
<?php class C {
/** @param array&$data */ function foo1(array &$data) {}
/** @param array& $data */ function foo2(array &$data) {}
/** @param array& $data */ function foo3(array &$data) {}
/** @param array &$data */ function foo4(array &$data) {}
/** @param array & $data */ function foo5(array &$data) {}
}
PHP,
<<<'PHP'
<?php class C {
/** @param array&$data */ function foo1(&$data) {}
/** @param array& $data */ function foo2(&$data) {}
/** @param array& $data */ function foo3(&$data) {}
/** @param array &$data */ function foo4(&$data) {}
/** @param array & $data */ function foo5(&$data) {}
}
PHP,
];
yield 'optional param by reference' => [
'<?php /** @param null|string[] $matches */ function matchAll(?array &$matches) {}',
'<?php /** @param null|string[] $matches */ function matchAll(&$matches) {}',
];
yield 'void as type in phpdoc' => [
'<?php /** @param void $bar */ function foo($bar) {}',
];
yield 'array and traversable' => [
'<?php /** @param array|Traversable $foo */ function my_foo(iterable $foo) {}',
'<?php /** @param array|Traversable $foo */ function my_foo($foo) {}',
];
yield 'array and traversable with leading slash' => [
'<?php /** @param array|\Traversable $foo */ function my_foo(iterable $foo) {}',
'<?php /** @param array|\Traversable $foo */ function my_foo($foo) {}',
];
yield 'string array and iterable' => [
'<?php /** @param string[]|iterable $foo */ function my_foo(iterable $foo) {}',
'<?php /** @param string[]|iterable $foo */ function my_foo($foo) {}',
];
yield 'array and traversable in a namespace' => [
'<?php
namespace App;
/** @param array|Traversable $foo */
function my_foo($foo) {}
',
];
yield 'array and traversable with leading slash in a namespace' => [
'<?php
namespace App;
/** @param array|\Traversable $foo */
function my_foo(iterable $foo) {}
',
'<?php
namespace App;
/** @param array|\Traversable $foo */
function my_foo($foo) {}
',
];
yield 'array and imported traversable in a namespace' => [
'<?php
namespace App;
use Traversable;
/** @param array|Traversable $foo */
function my_foo(iterable $foo) {}
',
'<?php
namespace App;
use Traversable;
/** @param array|Traversable $foo */
function my_foo($foo) {}
',
];
yield 'array and objects aliased as traversable in a namespace' => [
'<?php
namespace App;
use Foo as Traversable;
/** @param array|Traversable $foo */
function my_foo($foo) {}
',
];
yield 'array of objects and traversable' => [
'<?php /** @param Foo[]|Traversable $foo */ function my_foo(iterable $foo) {}',
'<?php /** @param Foo[]|Traversable $foo */ function my_foo($foo) {}',
];
yield 'array of objects and iterable' => [
'<?php /** @param object[]|iterable $foo */ function my_foo(iterable $foo) {}',
'<?php /** @param object[]|iterable $foo */ function my_foo($foo) {}',
];
yield 'array of strings and array of ints' => [
'<?php /** @param string[]|int[] $foo */ function my_foo(array $foo) {}',
'<?php /** @param string[]|int[] $foo */ function my_foo($foo) {}',
];
yield 'do not fix scalar types when configured as such' => [
'<?php /** @param int $foo */ function my_foo($foo) {}',
null,
['scalar_types' => false],
];
yield 'do not fix union types when configured as such' => [
'<?php /** @param int|string $foo */ function my_foo($foo) {}',
null,
['union_types' => false],
];
yield 'do not fix function call' => [
'<?php
/** @param string $foo */
function bar($notFoo) {
return baz($foo);
}
',
];
yield 'do not fix function call when no parameter' => [
'<?php
/** @param string $foo */
function bar() {
return baz($foo);
}
',
];
yield 'intersection types' => [
'<?php
/** @param Bar&Baz $x */
function bar($x) {}
',
];
yield 'very long class name before ampersand' => [
'<?php
/** @param Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar&Baz $x */
function bar($x) {}
',
];
yield 'very long class name after ampersand' => [
'<?php
/** @param Bar&Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz $x */
function bar($x) {}
',
];
yield 'support for arrow function' => [
'<?php
Utils::stableSort(
$elements,
/**
* @param int $a
*
* @return array
*/
static fn(int $a) => [$a],
fn($a, $b) => 1,
);',
'<?php
Utils::stableSort(
$elements,
/**
* @param int $a
*
* @return array
*/
static fn($a) => [$a],
fn($a, $b) => 1,
);',
];
yield 'types defined with "phpstan-type"' => [
<<<'PHP'
<?php
/**
* @phpstan-type _Pair array{int, int}
*/
class Foo {
/**
* @param _Pair $x
*/
public function f($x): void {}
}
/**
* @phpstan-type _Trio array{int, int, int}
*/
class Bar {
/**
* @param _Trio $x
*/
public function f($x): void {}
}
PHP,
];
yield 'types defined with "phpstan-import-type"' => [
<<<'PHP'
<?php
/**
* @phpstan-import-type _Pair from FooFoo
*/
class Foo {
/**
* @param _Pair $x
*/
public function f($x): void {}
}
/**
* @phpstan-import-type _Trio from BarBar
*/
class Bar {
/**
* @param _Trio $x
*/
public function f($x): void {}
}
PHP,
];
yield 'types defined with "phpstan-import-type" with alias' => [
<<<'PHP'
<?php
/**
* @phpstan-import-type ConflictingType from FooFoo as Not_ConflictingType
*/
class Foo {
/**
* @param Not_ConflictingType $x
*/
public function f1($x): void {}
/**
* @param ConflictingType $x
*/
public function f2(ConflictingType $x): void {}
}
PHP,
<<<'PHP'
<?php
/**
* @phpstan-import-type ConflictingType from FooFoo as Not_ConflictingType
*/
class Foo {
/**
* @param Not_ConflictingType $x
*/
public function f1($x): void {}
/**
* @param ConflictingType $x
*/
public function f2($x): void {}
}
PHP,
];
yield 'types defined with "psalm-type"' => [
<<<'PHP'
<?php
/**
* @psalm-type _Pair = array{int, int}
*/
class Foo {
/**
* @param _Pair $x
*/
public function f($x): void {}
}
/**
* @psalm-type _Trio array{int, int, int}
*/
class Bar {
/**
* @param _Trio $x
*/
public function f($x): void {}
}
PHP,
];
yield 'types defined with "psalm-import-type"' => [
<<<'PHP'
<?php
/**
* @psalm-import-type _Pair from FooFoo
*/
class Foo {
/**
* @param _Pair $x
*/
public function f($x): void {}
}
/**
* @psalm-import-type _Trio from BarBar
*/
class Bar {
/**
* @param _Trio $x
*/
public function f($x): void {}
}
PHP,
];
yield 'types defined with "psalm-import-type" with alias' => [
<<<'PHP'
<?php
/**
* @psalm-import-type Bar from FooFoo as BarAliased
*/
class Foo {
/** @param Bar $x */
public function f1(Bar $x): void {}
/** @param BarAliased $x */
public function f2($x): void {}
/** @param BarAliased $x */
public function f3($x): void {}
/** @param Bar $x */
public function f4(Bar $x): void {}
}
PHP,
<<<'PHP'
<?php
/**
* @psalm-import-type Bar from FooFoo as BarAliased
*/
class Foo {
/** @param Bar $x */
public function f1($x): void {}
/** @param BarAliased $x */
public function f2($x): void {}
/** @param BarAliased $x */
public function f3($x): void {}
/** @param Bar $x */
public function f4($x): void {}
}
PHP,
];
yield 'types aliased in config:types_map' => [
<<<'PHP'
<?php
class Token {
/**
* @param _PhpTokenPrototype $token token prototype
*/
public function __construct($token) {}
/**
* @param _PhpTokenPrototypePartial|Token $other token or it's prototype
* @param bool $caseSensitive perform a case sensitive comparison
*/
public function equals($other, bool $caseSensitive = true): bool {}
}
PHP,
null,
[
'types_map' => [
'_PhpTokenPrototype' => '_PhpTokenArray|string',
'_PhpTokenPrototypePartial' => '_PhpTokenArrayPartial|string',
],
],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixPre80Cases(): iterable
{
yield 'skip mixed type of param' => [
'<?php
/**
* @param mixed $bar
*/
function my_foo($bar) {}',
];
yield 'skip T aliased to mixed type of param, while mixed is not yet available <8.0' => [
'<?php
/**
* @param T $bar
*/
function my_foo($bar) {}',
null,
['types_map' => ['T' => 'mixed']],
];
yield 'skip union types' => [
'<?php /** @param Foo|Bar $bar */ function my_foo($bar) {}',
];
yield 'skip union types including nullable' => [
'<?php /** @param string|?int $bar */ function my_foo($bar) {}',
];
yield 'skip union types including array' => [
'<?php /** @param array|Foo $expected */ function testResolveIntersectionOfPaths($expected) {}',
];
yield 'skip primitive or array union types' => [
'<?php /** @param string|string[] $expected */ function testResolveIntersectionOfPaths($expected) {}',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'non-root class with mixed type of param for php >= 8' => [
'<?php
/**
* @param mixed $bar
*/
function my_foo(mixed $bar) {}',
'<?php
/**
* @param mixed $bar
*/
function my_foo($bar) {}',
];
yield 'union types' => [
'<?php /** @param Foo|Bar $bar */ function my_foo(Foo|Bar $bar) {}',
'<?php /** @param Foo|Bar $bar */ function my_foo($bar) {}',
];
yield 'union types including nullable' => [
'<?php /** @param string|?int $bar */ function my_foo(string|int|null $bar) {}',
'<?php /** @param string|?int $bar */ function my_foo($bar) {}',
];
yield 'union types including generics' => [
'<?php /** @param array<string, int>|string $bar */ function my_foo(array|string $bar) {}',
'<?php /** @param array<string, int>|string $bar */ function my_foo($bar) {}',
];
yield 'fix union types including generics' => [
'<?php /** @param string|array<string, int> $bar */ function my_foo(string|array $bar) {}',
'<?php /** @param string|array<string, int> $bar */ function my_foo($bar) {}',
];
yield 'union types including array' => [
'<?php /** @param array|Foo $expected */ function testResolveIntersectionOfPaths(array|Foo $expected) {}',
'<?php /** @param array|Foo $expected */ function testResolveIntersectionOfPaths($expected) {}',
];
yield 'primitive or array union types' => [
'<?php /** @param string|string[] $expected */ function testResolveIntersectionOfPaths(string|array $expected) {}',
'<?php /** @param string|string[] $expected */ function testResolveIntersectionOfPaths($expected) {}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/NoSpacesAfterFunctionNameFixerTest.php | tests/Fixer/FunctionNotation/NoSpacesAfterFunctionNameFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\NoSpacesAfterFunctionNameFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\NoSpacesAfterFunctionNameFixer>
*
* @author Varga Bence <vbence@czentral.org>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoSpacesAfterFunctionNameFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'test function call' => [
'<?php abc($a);',
'<?php abc ($a);',
];
yield 'test method call' => [
'<?php $o->abc($a);',
'<?php $o->abc ($a);',
];
yield 'test function-like constructs' => [
'<?php
include("something.php");
include_once("something.php");
require("something.php");
require_once("something.php");
print("hello");
unset($hello);
isset($hello);
empty($hello);
die($hello);
echo("hello");
array("hello");
list($a, $b) = $c;
eval("a");
foo();
$foo = &ref();
',
'<?php
include ("something.php");
include_once ("something.php");
require ("something.php");
require_once ("something.php");
print ("hello");
unset ($hello);
isset ($hello);
empty ($hello);
die ($hello);
echo ("hello");
array ("hello");
list ($a, $b) = $c;
eval ("a");
foo ();
$foo = &ref ();
',
];
yield [
'<?php echo foo(1) ? "y" : "n";',
'<?php echo foo (1) ? "y" : "n";',
];
yield [
'<?php echo isset($name) ? "y" : "n";',
'<?php echo isset ($name) ? "y" : "n";',
];
yield [
'<?php include (isHtml())? "1.html": "1.php";',
'<?php include (isHtml ())? "1.html": "1.php";',
];
// skip other language constructs
yield [
'<?php $a = 2 * (1 + 1);',
];
yield [
'<?php echo ($a == $b) ? "foo" : "bar";',
];
yield [
'<?php echo ($a == test($b)) ? "foo" : "bar";',
];
yield [
'<?php include ($html)? "custom.html": "custom.php";',
];
yield 'don\'t touch function declarations' => [
'<?php
function TisMy ($p1)
{
print $p1;
}
',
];
yield [
'<?php class A {
function TisMy ($p1)
{
print $p1;
}
}',
];
yield 'test dynamic by array' => [
'<?php $a["e"](1); $a[2](1);',
'<?php $a["e"] (1); $a[2] (1);',
];
yield 'test variable variable' => [
'<?php
${$e}(1);
$$e(2);
',
"<?php
\${\$e}\t(1);
\$\$e (2);
",
];
yield 'test dynamic function and method calls' => [
'<?php $b->$a(); $c();',
'<?php $b->$a (); $c ();',
];
yield 'test function call comment' => [
'<?php abc#
($a);',
];
yield [
'<?php echo (new Process())->getOutput();',
'<?php echo (new Process())->getOutput ();',
];
yield [
'<?php $a()(1);',
'<?php $a () (1);',
];
yield [
'<?php
echo (function () {})();
echo ($propertyValue ? "TRUE" : "FALSE") . EOL;
echo(FUNCTION_1);
echo (EXPRESSION + CONST_1), CONST_2;
',
'<?php
echo (function () {})();
echo ($propertyValue ? "TRUE" : "FALSE") . EOL;
echo (FUNCTION_1);
echo (EXPRESSION + CONST_1), CONST_2;
',
];
yield [
'<?php
include(SOME_PATH_1);
include_once(SOME_PATH_2);
require(SOME_PATH_3);
require_once(SOME_PATH_4);
print(SOME_VALUE);
print(FIRST_HALF_OF_STRING_1 . SECOND_HALF_OF_STRING_1);
print((FIRST_HALF_OF_STRING_2) . (SECOND_HALF_OF_STRING_2));
',
'<?php
include (SOME_PATH_1);
include_once (SOME_PATH_2);
require (SOME_PATH_3);
require_once (SOME_PATH_4);
print (SOME_VALUE);
print (FIRST_HALF_OF_STRING_1 . SECOND_HALF_OF_STRING_1);
print ((FIRST_HALF_OF_STRING_2) . (SECOND_HALF_OF_STRING_2));
',
];
yield [
'<?php
include (DIR) . FILENAME_1;
include_once (DIR) . (FILENAME_2);
require (DIR) . FILENAME_3;
require_once (DIR) . (FILENAME_4);
print (FIRST_HALF_OF_STRING_1) . SECOND_HALF_OF_STRING_1;
print (FIRST_HALF_OF_STRING_2) . ((((SECOND_HALF_OF_STRING_2))));
print ((FIRST_HALF_OF_STRING_3)) . ((SECOND_HALF_OF_STRING_3));
print ((((FIRST_HALF_OF_STRING_4)))) . ((((SECOND_HALF_OF_STRING_4))));
',
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield 'test dynamic by array, curly mix' => [
'<?php $a["e"](1); $a{2}(1);',
'<?php $a["e"] (1); $a{2} (1);',
];
yield 'test dynamic by array, curly only' => [
'<?php $a{"e"}(1); $a{2}(1);',
'<?php $a{"e"} (1); $a{2} (1);',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php strlen(...);',
'<?php strlen (...);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/RegularCallableCallFixerTest.php | tests/Fixer/FunctionNotation/RegularCallableCallFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\RegularCallableCallFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\RegularCallableCallFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class RegularCallableCallFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'call by name - list' => [
'<?php
dont_touch_me(1, 2);
foo();
foo();
call_user_func("foo" . "bar"); // not (yet?) supported by Fixer, possible since PHP 7+
var_dump(1, 2);
Bar\Baz::d(1, 2);
\Bar\Baz::d(1, 2);',
'<?php
dont_touch_me(1, 2);
call_user_func(\'foo\');
call_user_func("foo");
call_user_func("foo" . "bar"); // not (yet?) supported by Fixer, possible since PHP 7+
call_user_func("var_dump", 1, 2);
call_user_func("Bar\Baz::d", 1, 2);
call_user_func("\Bar\Baz::d", 1, 2);',
];
yield 'call by name - array' => [
'<?php Bar\Baz::d(...[1, 2]);',
'<?php call_user_func_array("Bar\Baz::d", [1, 2]);',
];
yield 'call by array-as-name, not supported' => [
'<?php
call_user_func(array("Bar\baz", "myCallbackMethod"), 1, 2);
call_user_func(["Bar\baz", "myCallbackMethod"], 1, 2);
call_user_func([$obj, "myCallbackMethod"], 1, 2);
call_user_func([$obj, $cb."Method"], 1, 2);
call_user_func(array(__NAMESPACE__ ."Foo", "test"), 1, 2);
call_user_func(array("Foo", "parent::method"), 1, 2); // no way to convert `parent::`
',
];
yield 'call by variable' => [
'<?php
$c(1, 2);
$a["b"]["c"](1, 2);
',
'<?php
call_user_func($c, 1, 2);
call_user_func($a["b"]["c"], 1, 2);
',
];
yield 'call with comments' => [
'<?php
dont_touch_me(/* a */1, 2/** b */);
foo();
foo(/* a */1, 2/** b */);
foo(/* a *//** b *//** c */1/** d */,/** e */ 2);
call_user_func("foo" . "bar"); // not (yet?) supported by Fixer, possible since PHP 7+
var_dump(1, /*
aaa
*/ 2);
var_dump(3 /*
aaa
*/, 4);
Bar\Baz::d(1, 2);
\Bar\Baz::d(1, 2);',
'<?php
dont_touch_me(/* a */1, 2/** b */);
call_user_func(\'foo\');
call_user_func("foo", /* a */1, 2/** b */);
call_user_func("foo"/* a *//** b */, /** c */1/** d */,/** e */ 2);
call_user_func("foo" . "bar"); // not (yet?) supported by Fixer, possible since PHP 7+
call_user_func("var_dump", 1, /*
aaa
*/ 2);
call_user_func("var_dump", 3 /*
aaa
*/, 4);
call_user_func("Bar\Baz::d", 1, 2);
call_user_func("\Bar\Baz::d", 1, 2);',
];
yield 'single var' => [
'<?php $foo() ?>',
'<?php \call_user_func($foo) ?>',
];
yield 'unsafe repeated variable' => [
'<?php call_user_func($foo, $foo = "bar");',
];
yield 'call by property' => [
'<?php
($f->c)(1, 2);
($f->{c})(1, 2);
($x["y"]->c)(1, 2);
($x["y"]->{"c"})(1, 2);
',
'<?php
call_user_func($f->c, 1, 2);
call_user_func($f->{c}, 1, 2);
call_user_func($x["y"]->c, 1, 2);
call_user_func($x["y"]->{"c"}, 1, 2);
',
];
yield 'call by anon-function' => [
'<?php
(function ($a, $b) { var_dump($a, $b); })(1, 2);
(static function ($a, $b) { var_dump($a, $b); })(1, 2);
',
'<?php
call_user_func(function ($a, $b) { var_dump($a, $b); }, 1, 2);
call_user_func(static function ($a, $b) { var_dump($a, $b); }, 1, 2);
',
];
yield 'complex cases' => [
'<?php
call_user_func(\'a\'.$a.$b, 1, 2);
($a/**/.$b)(1, 2);
(function (){})();
($a["b"]["c"]->a)(1, 2, 3, 4);
($a::$b)(1, 2);
($a[1]::$b[2][3])([&$c], array(&$d));
',
'<?php
call_user_func(\'a\'.$a.$b, 1, 2);
call_user_func($a/**/.$b, 1, 2);
\call_user_func(function (){});
call_user_func($a["b"]["c"]->a, 1, 2, 3, 4);
call_user_func($a::$b, 1, 2);
call_user_func($a[1]::$b[2][3], [&$c], array(&$d));
',
];
yield [
'<?php ($a(1, 2))([&$x], array(&$z));',
'<?php call_user_func($a(1, 2), [&$x], array(&$z));',
];
yield 'redeclare/override' => [
'<?php
if (!function_exists("call_user_func")) {
function call_user_func($foo){}
}
',
];
yield 'function name with escaped slash' => [
'<?php \pack(...$args);',
'<?php call_user_func_array("\\\pack", $args);',
];
yield 'function call_user_func_array with leading slash' => [
'<?php \pack(...$args);',
'<?php \call_user_func_array("\\\pack", $args);',
];
yield 'function call_user_func_array caps' => [
'<?php \pack(...$args);',
'<?php \CALL_USER_FUNC_ARRAY("\\\pack", $args);',
];
yield [
'<?php foo(1,);',
'<?php call_user_func("foo", 1,);',
];
yield 'empty string double quote' => [
'<?php call_user_func("", 1,);',
];
yield 'empty string single quote' => [
'<?php call_user_func(\' \', 1,);',
];
yield 'string with padding' => [
'<?php call_user_func(" padded ", 1,);',
];
yield 'binary string lower double quote' => [
'<?php call_user_func(b"foo", 1,);',
];
yield 'binary string upper single quote' => [
'<?php call_user_func(B"foo", 1,);',
];
yield 'static property as first argument' => [
'<?php
class Foo {
public static $factory;
public static function createFromFactory(...$args) {
return call_user_func_array(static::$factory, $args);
}
}',
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield 'call by variable' => [
'<?php
$a{"b"}{"c"}(1, 2);
',
'<?php
call_user_func($a{"b"}{"c"}, 1, 2);
',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php \call_user_func(...) ?>',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/LambdaNotUsedImportFixerTest.php | tests/Fixer/FunctionNotation/LambdaNotUsedImportFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\LambdaNotUsedImportFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\LambdaNotUsedImportFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class LambdaNotUsedImportFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'simple' => [
'<?php $foo = function() {};',
'<?php $foo = function() use ($bar) {};',
];
yield 'simple, one of two' => [
'<?php $foo = function & () use ( $foo) { echo $foo; };',
'<?php $foo = function & () use ($bar, $foo) { echo $foo; };',
];
yield 'simple, one used, one reference, two not used' => [
'<?php $foo = function() use ($bar, &$foo ) { echo $bar; };',
'<?php $foo = function() use ($bar, &$foo, $not1, $not2) { echo $bar; };',
];
yield 'simple, but witch comments' => [
'<?php $foo = function()
# 1
#2
# 3
#4
# 5
#6
{};',
'<?php $foo = function()
use
# 1
( #2
# 3
$bar #4
# 5
) #6
{};',
];
yield 'nested lambda I' => [
'<?php
$f = function() {
return function ($d) use ($c) {
$b = 1; echo $c;
};
};
',
'<?php
$f = function() use ($b) {
return function ($d) use ($c) {
$b = 1; echo $c;
};
};
',
];
yield 'nested lambda II' => [
'<?php
// do not fix
$f = function() use ($a) { return function() use ($a) { return function() use ($a) { return function() use ($a) { echo $a; }; }; }; };
$f = function() use ($b) { return function($b) { return function($b) { return function($b) { echo $b; }; }; }; };
// do fix
$f = function() { return function() { return function() { return function() { }; }; }; };
',
'<?php
// do not fix
$f = function() use ($a) { return function() use ($a) { return function() use ($a) { return function() use ($a) { echo $a; }; }; }; };
$f = function() use ($b) { return function($b) { return function($b) { return function($b) { echo $b; }; }; }; };
// do fix
$f = function() use ($a) { return function() use ($a) { return function() use ($a) { return function() use ($a) { }; }; }; };
',
];
yield 'anonymous class' => [
'<?php
$a = function() use ($b) { new class($b){}; }; // do not fix
$a = function() { new class(){ public function foo($b){echo $b;}}; }; // do fix
',
'<?php
$a = function() use ($b) { new class($b){}; }; // do not fix
$a = function() use ($b) { new class(){ public function foo($b){echo $b;}}; }; // do fix
',
];
yield 'anonymous class with a string argument' => [
'<?php $function = function () {
new class("bar") {};
};',
'<?php $function = function () use ($foo) {
new class("bar") {};
};',
];
yield 'reference' => [
'<?php $fn = function() use(&$b) {} ?>',
];
yield 'compact 1' => [
'<?php $foo = function() use ($b) { return compact(\'b\'); };',
];
yield 'compact 2' => [
'<?php $foo = function() use ($b) { return \compact(\'b\'); };',
];
yield 'eval' => [
'<?php $foo = function($c) use ($b) { eval($c); };',
];
yield 'include' => [
'<?php $foo = function($c) use ($b) { include __DIR__."/test3.php"; };',
];
yield 'include_once' => [
'<?php $foo = function($c) use ($b) { include_once __DIR__."/test3.php"; };',
];
yield 'require' => [
'<?php $foo = function($c) use ($b) { require __DIR__."/test3.php"; };',
];
yield 'require_once' => [
'<?php $foo = function($c) use ($b) { require_once __DIR__."/test3.php"; };',
];
yield '${X}' => [
'<?php $foo = function($g) use ($b) { $h = ${$g}; };',
];
yield '$$c' => [
'<?php $foo = function($g) use ($b) { $h = $$g; };',
];
yield 'interpolation 1' => [
'<?php $foo = function() use ($world) { echo "hello $world"; };',
];
yield 'interpolation 2' => [
'<?php $foo = function() use ($world) { echo "hello {$world}"; };',
];
yield 'interpolation 3' => [
'<?php $foo = function() use ($world) { echo "hello ${world}"; };',
];
yield 'heredoc' => [
'<?php
$b = 123;
$foo = function() use ($b) {
echo
<<<"TEST"
Foo $b
TEST;
};
$foo();
',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, string $input): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'simple' => [
'<?php $foo = function() {};',
'<?php $foo = function() use ($bar,) {};',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/VoidReturnFixerTest.php | tests/Fixer/FunctionNotation/VoidReturnFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\VoidReturnFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\VoidReturnFixer>
*
* @author Mark Nielsen
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class VoidReturnFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{string, 1?: ?string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php class Test { public function __construct() {} }'];
yield ['<?php class Test { public function __destruct() {} }'];
yield ['<?php class Test { public function __clone() {} }'];
yield ['<?php function foo($param) { return $param; }'];
yield ['<?php function foo($param) { return null; }'];
yield ['<?php function foo($param) { yield; }'];
yield ['<?php function foo($param) { yield $param; }'];
yield ['<?php function foo($param) { yield from test(); }'];
yield ['<?php function foo($param): Void {}'];
yield ['<?php interface Test { public function foo($param); }'];
yield ['<?php function foo($param) { return function($a) use ($param): string {}; }'];
yield ['<?php abstract class Test { abstract public function foo($param); }'];
yield ['<?php use Foo\ { function Bar }; function test() { return Bar(); }'];
yield ['<?php
/**
* @return array
*/
function foo($param) {}
'];
yield ['<?php
interface Test {
/**
* @return array
*/
public function foo($param);
}
'];
yield [
'<?php function foo($param): void { return; }',
'<?php function foo($param) { return; }',
];
yield [
'<?php function foo($param): void {}',
'<?php function foo($param) {}',
];
yield [
'<?php class Test { public function foo($param): void { return; } }',
'<?php class Test { public function foo($param) { return; } }',
];
yield [
'<?php class Test { public function foo($param): void {} }',
'<?php class Test { public function foo($param) {} }',
];
yield [
'<?php trait Test { public function foo($param): void { return; } }',
'<?php trait Test { public function foo($param) { return; } }',
];
yield [
'<?php trait Test { public function foo($param): void {} }',
'<?php trait Test { public function foo($param) {} }',
];
yield [
'<?php $arr = []; usort($arr, function ($a, $b): void {});',
'<?php $arr = []; usort($arr, function ($a, $b) {});',
];
yield [
'<?php $arr = []; $param = 1; usort($arr, function ($a, $b) use ($param): void {});',
'<?php $arr = []; $param = 1; usort($arr, function ($a, $b) use ($param) {});',
];
yield [
'<?php function foo($param) { return function($a) use ($param): void {}; }',
'<?php function foo($param) { return function($a) use ($param) {}; }',
];
yield [
'<?php function foo($param): void { $arr = []; usort($arr, function ($a, $b) use ($param): void {}); }',
'<?php function foo($param) { $arr = []; usort($arr, function ($a, $b) use ($param) {}); }',
];
yield [
'<?php function foo() { $arr = []; return usort($arr, new class { public function __invoke($a, $b): void {} }); }',
'<?php function foo() { $arr = []; return usort($arr, new class { public function __invoke($a, $b) {} }); }',
];
yield [
'<?php function foo(): void { $arr = []; usort($arr, new class { public function __invoke($a, $b): void {} }); }',
'<?php function foo() { $arr = []; usort($arr, new class { public function __invoke($a, $b) {} }); }',
];
yield [
'<?php
function foo(): void {
$a = function (): void {};
}',
'<?php
function foo() {
$a = function () {};
}',
];
yield [
'<?php
function foo(): void {
(function (): void {
return;
})();
}',
'<?php
function foo() {
(function () {
return;
})();
}',
];
yield [
'<?php
function foo(): void {
(function () {
return 1;
})();
}',
'<?php
function foo() {
(function () {
return 1;
})();
}',
];
yield [
'<?php
function foo(): void {
$b = new class {
public function b1(): void {}
public function b2() { return 2; }
};
}',
'<?php
function foo() {
$b = new class {
public function b1() {}
public function b2() { return 2; }
};
}',
];
yield [
'<?php
/**
* @return void
*/
function foo($param): void {}',
'<?php
/**
* @return void
*/
function foo($param) {}',
];
yield [
'<?php
interface Test {
/**
* @return void
*/
public function foo($param): void;
}',
'<?php
interface Test {
/**
* @return void
*/
public function foo($param);
}',
];
yield [
'<?php
abstract class Test {
/**
* @return void
*/
abstract protected function foo($param): void;
}',
'<?php
abstract class Test {
/**
* @return void
*/
abstract protected function foo($param);
}',
];
yield [
'<?php fn($a) => null;',
];
yield [
'<?php fn($a) => 1;',
];
yield [
'<?php fn($a) => var_dump($a);',
];
$excluded = ['__clone', '__construct', '__debugInfo', '__destruct', '__isset', '__serialize', '__set_state', '__sleep', '__toString'];
foreach (self::provideMagicMethodsDefinitions() as $magicMethodsDefinition) {
$name = $magicMethodsDefinition[0];
$arguments = $magicMethodsDefinition[1] ?? 0;
$isStatic = $magicMethodsDefinition[2] ?? false;
$code = \sprintf(
'<?php class Test { public%s function %s(%s)%%s {} }',
$isStatic ? ' static' : '',
$name,
implode(',', array_map(
static fn (int $n): string => \sprintf('$x%d', $n),
array_keys(array_fill(0, $arguments, true)),
)),
);
$input = \sprintf($code, '');
$expected = \sprintf($code, \in_array($name, $excluded, true) ? '' : ': void');
yield \sprintf('Test if magic method %s is handled without causing syntax error', $name) => [
$expected,
$expected === $input ? null : $input,
];
}
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, 1?: ?string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php
class Foo
{
/**
* @return int|false
*/
#[\ReturnTypeWillChange]
public function test() {}
}
',
];
yield [
'<?php
/**
* @return void
*/
#[\Deprecated]
function test(): void {};
',
'<?php
/**
* @return void
*/
#[\Deprecated]
function test() {};
',
];
}
/**
* @return iterable<array{string, 1?: int, 2?: bool}>
*/
private static function provideMagicMethodsDefinitions(): iterable
{
// List: https://www.php.net/manual/en/language.oop5.magic.php
yield ['__construct'];
yield ['__destruct'];
yield ['__call', 2];
yield ['__callStatic', 2, true];
yield ['__get', 1];
yield ['__set', 2];
yield ['__isset', 1];
yield ['__unset', 1];
yield ['__sleep'];
yield ['__wakeup'];
yield ['__serialize'];
yield ['__unserialize', 1];
yield ['__toString'];
yield ['__invoke'];
yield ['__set_state', 1, true];
yield ['__clone'];
yield ['__debugInfo'];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/NoUnreachableDefaultArgumentValueFixerTest.php | tests/Fixer/FunctionNotation/NoUnreachableDefaultArgumentValueFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\NoUnreachableDefaultArgumentValueFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\NoUnreachableDefaultArgumentValueFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoUnreachableDefaultArgumentValueFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php function bFunction($foo, $bar) {}',
'<?php function bFunction($foo = null, $bar) {}',
];
yield [
'<?php function bFunction($foo, $bar) {}',
'<?php function bFunction($foo = "two words", $bar) {}',
];
yield [
'<?php function cFunction($foo, $bar, $baz) {}',
'<?php function cFunction($foo = false, $bar = "bar", $baz) {}',
];
yield [
'<?php function dFunction($foo, $bar, $baz) {}',
'<?php function dFunction($foo = false, $bar, $baz) {}',
];
yield [
'<?php function foo (Foo $bar = null, $baz) {}',
];
yield [
'<?php function eFunction($foo, $bar, \SplFileInfo $baz, $x = "default") {}',
'<?php function eFunction($foo, $bar = "removedDefault", \SplFileInfo $baz, $x = "default") {}',
];
yield [
<<<'EOT'
<?php
function eFunction($foo, $bar, \SplFileInfo $baz, $x = 'default') {};
function fFunction($foo, $bar, \SplFileInfo $baz, $x = 'default') {};
EOT,
<<<'EOT'
<?php
function eFunction($foo, $bar, \SplFileInfo $baz, $x = 'default') {};
function fFunction($foo, $bar = 'removedValue', \SplFileInfo $baz, $x = 'default') {};
EOT,
];
yield [
'<?php function foo ($bar /* a */ /* b */ , $c) {}',
'<?php function foo ($bar /* a */ = /* b */ 1, $c) {}',
];
yield [
'<?php function hFunction($foo,$bar,\SplFileInfo $baz,$x = 5) {};',
'<?php function hFunction($foo,$bar="removedValue",\SplFileInfo $baz,$x = 5) {};',
];
yield [
'<?php function eFunction($foo, $bar, \SplFileInfo $baz = null, $x) {}',
'<?php function eFunction($foo = PHP_EOL, $bar, \SplFileInfo $baz = null, $x) {}',
];
yield [
'<?php function eFunction($foo, $bar) {}',
'<?php function eFunction($foo = null, $bar) {}',
];
yield [
<<<'EOT'
<?php
function foo(
$a, // test
$b, /* test */
$c, // abc
$d
) {}
EOT,
<<<'EOT'
<?php
function foo(
$a = 1, // test
$b = 2, /* test */
$c = null, // abc
$d
) {}
EOT,
];
yield [
'<?php function foo($foo, $bar) {}',
'<?php function foo($foo = array(array(1)), $bar) {}',
];
yield [
'<?php function a($a, $b) {}',
'<?php function a($a = array("a" => "b", "c" => "d"), $b) {}',
];
yield [
'<?php function a($a, $b) {}',
'<?php function a($a = ["a" => "b", "c" => "d"], $b) {}',
];
yield [
'<?php function a($a, $b) {}',
'<?php function a($a = NULL, $b) {}',
];
yield [
'<?php function a(\SplFileInfo $a = Null, $b) {}',
];
yield [
'<?php function a(array $a = null, $b) {}',
];
yield [
'<?php function a(callable $a = null, $b) {}',
];
yield [
'<?php function a(\SplFileInfo &$a = Null, $b) {}',
];
yield [
'<?php function a(&$a, $b) {}',
'<?php function a(&$a = null, $b) {}',
];
yield [
'<?php $fnc = function ($a, $b = 1) use ($c) {};',
];
yield [
'<?php $fnc = function ($a, $b) use ($c) {};',
'<?php $fnc = function ($a = 1, $b) use ($c) {};',
];
yield [
'<?php function bFunction($foo#
#
#
,#
$bar) {}',
'<?php function bFunction($foo#
=#
null#
,#
$bar) {}',
];
yield [
'<?php function a($a = 1, ...$b) {}',
];
yield [
'<?php function a($a = 1, \SplFileInfo ...$b) {}',
];
yield [
'<?php function foo (?Foo $bar, $baz) {}',
'<?php function foo (?Foo $bar = null, $baz) {}',
];
yield [
'<?php function foo (?Foo $bar = null, ?Baz $baz = null) {}',
];
yield [
'<?php $fn = fn ($a, $b) => null;',
'<?php $fn = fn ($a = 1, $b) => null;',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'handle trailing comma' => [
'<?php function foo($x, $y = 42, $z = 42 ) {}',
];
yield 'handle attributes without arguments' => [
'<?php function foo(
#[Attribute1] $x,
#[Attribute2] $y,
#[Attribute3] $z
) {}',
'<?php function foo(
#[Attribute1] $x,
#[Attribute2] $y = 42,
#[Attribute3] $z
) {}',
];
yield 'handle attributes with arguments' => [
'<?php function foo(
#[Attribute1(1, 2)] $x,
#[Attribute2(3, 4)] $y,
#[Attribute3(5, 6)] $z
) {}',
'<?php function foo(
#[Attribute1(1, 2)] $x,
#[Attribute2(3, 4)] $y = 42,
#[Attribute3(5, 6)] $z
) {}',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'do not crash' => [
'<?php strlen( ... );',
];
}
/**
* @dataProvider provideFix84Cases
*
* @requires PHP 8.4
*/
public function testFix84(string $expected, ?string $input = null): void
{
$this->testFix($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix84Cases(): iterable
{
yield 'do not crash' => [<<<'PHP'
<?php class Foo
{
public function __construct(
public string $myVar {
set(string $value) {
$this->myVar = $value;
}
},
) {}
}
PHP
];
yield 'do not crash 2' => [<<<'PHP'
<?php class Foo
{
public function __construct(
public string $key {
set(string $key) => $this->key = mb_strtolower($key);
},
public int $value,
) {}
}
PHP
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/ReturnTypeDeclarationFixerTest.php | tests/Fixer/FunctionNotation/ReturnTypeDeclarationFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\ReturnTypeDeclarationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\ReturnTypeDeclarationFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\ReturnTypeDeclarationFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ReturnTypeDeclarationFixerTest extends AbstractFixerTestCase
{
public function testInvalidConfiguration(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches(
'#^\[return_type_declaration\] Invalid configuration: The option "s" does not exist\. (Known|Defined) options are: "space_before"\.$#',
);
$this->fixer->configure(['s' => 9_000]);
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php function foo1(int $a) {}',
];
yield [
'<?php function foo2(int $a): string {}',
'<?php function foo2(int $a):string {}',
];
yield [
'<?php function foo3(int $c)/**/ : /**/ string {}',
];
yield [
'<?php function foo4(int $a): string {}',
'<?php function foo4(int $a) : string {}',
];
yield [
'<?php function foo5(int $e)#
: #
#
string {}',
'<?php function foo5(int $e)#
:#
#
string {}',
];
yield [
'<?php
function foo1(int $a): string {}
function foo2(int $a): string {}
function foo3(int $a): string {}
function foo4(int $a): string {}
function foo5(int $a): string {}
function foo6(int $a): string {}
function foo7(int $a): string {}
function foo8(int $a): string {}
function foo9(int $a): string {}
',
'<?php
function foo1(int $a):string {}
function foo2(int $a):string {}
function foo3(int $a):string {}
function foo4(int $a):string {}
function foo5(int $a):string {}
function foo6(int $a):string {}
function foo7(int $a):string {}
function foo8(int $a):string {}
function foo9(int $a):string {}
',
];
yield [
'<?php fn(): int => 1;',
'<?php fn():int => 1;',
];
yield [
'<?php function fooA(int $a) {}',
null,
['space_before' => 'one'],
];
yield [
'<?php function fooB(int $a) : string {}',
'<?php function fooB(int $a):string {}',
['space_before' => 'one'],
];
yield [
'<?php function fooC(int $a)/**/ : /**/string {}',
'<?php function fooC(int $a)/**/:/**/string {}',
['space_before' => 'one'],
];
yield [
'<?php function fooD(int $a) : string {}',
'<?php function fooD(int $a) : string {}',
['space_before' => 'one'],
];
yield [
'<?php function fooE(int $a) /**/ : /**/ string {}',
null,
['space_before' => 'one'],
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, string $input): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php function foo(): mixed{}',
'<?php function foo() : mixed{}',
];
yield [
'<?php class A { public function foo(): static{}}',
'<?php class A { public function foo() :static{}}',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, string $input): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php enum Foo: int {}',
'<?php enum Foo : int {}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/FunctionDeclarationFixerTest.php | tests/Fixer/FunctionNotation/FunctionDeclarationFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer>
*
* @author Denis Sokolov <denis@sokolov.cc>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class FunctionDeclarationFixerTest extends AbstractFixerTestCase
{
/**
* @var _AutogeneratedInputConfiguration
*/
private static array $configurationClosureSpacingNone = ['closure_function_spacing' => FunctionDeclarationFixer::SPACING_NONE];
/**
* @var _AutogeneratedInputConfiguration
*/
private static array $configurationArrowSpacingNone = ['closure_fn_spacing' => FunctionDeclarationFixer::SPACING_NONE];
/**
* @param array<string, mixed> $configuration
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(array $configuration, string $exceptionMessage): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches($exceptionMessage);
$this->fixer->configure($configuration);
}
/**
* @return iterable<int, array{array<string, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield [
['closure_function_spacing' => 'neither'],
'#^\[function_declaration\] Invalid configuration: The option "closure_function_spacing" with value "neither" is invalid\. Accepted values are: "none", "one"\.$#',
];
yield [
['closure_fn_spacing' => 'neither'],
'#^\[function_declaration\] Invalid configuration: The option "closure_fn_spacing" with value "neither" is invalid\. Accepted values are: "none", "one"\.$#',
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
// non-PHP test
'function foo () {}',
];
yield [
'<?php function foo() {}',
'<?php function foo() {}',
];
yield [
'<?php function foo() {}',
'<?php function foo () {}',
];
yield [
'<?php function foo() {}',
'<?php function foo () {}',
];
yield [
'<?php function foo() {}',
'<?php function
foo () {}',
];
yield [
'<?php function ($i) {};',
'<?php function($i) {};',
];
yield [
'<?php function _function() {}',
'<?php function _function () {}',
];
yield [
'<?php function foo($a, $b = true) {}',
'<?php function foo($a, $b = true){}',
];
yield [
'<?php function foo($a, $b = true) {}',
'<?php function foo($a, $b = true) {}',
];
yield [
'<?php function foo($a)
{}',
];
yield [
'<?php function ($a) use ($b) {};',
'<?php function ($a) use ($b) {};',
];
yield [
'<?php $foo = function ($foo) use ($bar, $baz) {};',
'<?php $foo = function ($foo) use($bar, $baz) {};',
];
yield [
'<?php $foo = function ($foo) use ($bar, $baz) {};',
'<?php $foo = function ($foo)use ($bar, $baz) {};',
];
yield [
'<?php $foo = function ($foo) use ($bar, $baz) {};',
'<?php $foo = function ($foo)use($bar, $baz) {};',
];
yield [
'<?php function &foo($a) {}',
'<?php function &foo( $a ) {}',
];
yield [
'<?php function foo($a)
{}',
'<?php function foo( $a)
{}',
];
yield [
'<?php
function foo(
$a,
$b,
$c
) {}',
];
yield [
'<?php $function = function () {};',
'<?php $function = function(){};',
];
yield [
'<?php $function("");',
];
yield [
'<?php function ($a) use ($b) {};',
'<?php function($a)use($b) {};',
];
yield [
'<?php function ($a) use ($b) {};',
'<?php function($a) use ($b) {};',
];
yield [
'<?php function ($a) use ($b) {};',
'<?php function ($a) use ( $b ) {};',
];
yield [
'<?php function &($a) use ($b) {};',
'<?php function &( $a ) use ( $b ) {};',
];
yield [
'<?php
interface Foo
{
public function setConfig(ConfigInterface $config);
}',
];
// do not remove multiline space before { when end of previous line is a comment
yield [
'<?php
function foo() // bar
{ // baz
}',
];
yield [
'<?php
function foo() /* bar */
{ /* baz */
}',
];
yield [
// non-PHP test
'function foo () {}',
null,
self::$configurationClosureSpacingNone,
];
yield [
'<?php function foo() {}',
'<?php function foo() {}',
self::$configurationClosureSpacingNone,
];
yield [
'<?php function foo() {}',
'<?php function foo () {}',
self::$configurationClosureSpacingNone,
];
yield [
'<?php function foo() {}',
'<?php function foo () {}',
self::$configurationClosureSpacingNone,
];
yield [
'<?php function foo() {}',
'<?php function
foo () {}',
self::$configurationClosureSpacingNone,
];
yield [
'<?php function($i) {};',
null,
self::$configurationClosureSpacingNone,
];
yield [
'<?php function _function() {}',
'<?php function _function () {}',
self::$configurationClosureSpacingNone,
];
yield [
'<?php function foo($a, $b = true) {}',
'<?php function foo($a, $b = true){}',
self::$configurationClosureSpacingNone,
];
yield [
'<?php function foo($a, $b = true) {}',
'<?php function foo($a, $b = true) {}',
self::$configurationClosureSpacingNone,
];
yield [
'<?php function foo($a)
{}',
null,
self::$configurationClosureSpacingNone,
];
yield [
'<?php function($a) use ($b) {};',
'<?php function ($a) use ($b) {};',
self::$configurationClosureSpacingNone,
];
yield [
'<?php $foo = function($foo) use ($bar, $baz) {};',
'<?php $foo = function ($foo) use($bar, $baz) {};',
self::$configurationClosureSpacingNone,
];
yield [
'<?php $foo = function($foo) use ($bar, $baz) {};',
'<?php $foo = function ($foo)use ($bar, $baz) {};',
self::$configurationClosureSpacingNone,
];
yield [
'<?php $foo = function($foo) use ($bar, $baz) {};',
'<?php $foo = function ($foo)use($bar, $baz) {};',
self::$configurationClosureSpacingNone,
];
yield [
'<?php function &foo($a) {}',
'<?php function &foo( $a ) {}',
self::$configurationClosureSpacingNone,
];
yield [
'<?php function foo($a)
{}',
'<?php function foo( $a)
{}',
self::$configurationClosureSpacingNone,
];
yield [
'<?php
function foo(
$a,
$b,
$c
) {}',
null,
self::$configurationClosureSpacingNone,
];
yield [
'<?php $function = function() {};',
'<?php $function = function (){};',
self::$configurationClosureSpacingNone,
];
yield [
'<?php $function("");',
null,
self::$configurationClosureSpacingNone,
];
yield [
'<?php function($a) use ($b) {};',
'<?php function ($a)use($b) {};',
self::$configurationClosureSpacingNone,
];
yield [
'<?php function($a) use ($b) {};',
'<?php function ($a) use ($b) {};',
self::$configurationClosureSpacingNone,
];
yield [
'<?php function($a) use ($b) {};',
'<?php function ($a) use ( $b ) {};',
self::$configurationClosureSpacingNone,
];
yield [
'<?php function&($a) use ($b) {};',
'<?php function &( $a ) use ( $b ) {};',
self::$configurationClosureSpacingNone,
];
yield [
'<?php
interface Foo
{
public function setConfig(ConfigInterface $config);
}',
null,
self::$configurationClosureSpacingNone,
];
// do not remove multiline space before { when end of previous line is a comment
yield [
'<?php
function foo() // bar
{ // baz
}',
null,
self::$configurationClosureSpacingNone,
];
yield [
'<?php
function foo() /* bar */
{ /* baz */
}',
null,
self::$configurationClosureSpacingNone,
];
yield [
'<?php function #
foo#
(#
) #
{#
}#',
];
yield [
'<?php
$b = static function ($a) {
echo $a;
};
',
'<?php
$b = static function( $a ) {
echo $a;
};
',
];
yield [
'<?php
$b = static function($a) {
echo $a;
};
',
'<?php
$b = static function ( $a ) {
echo $a;
};
',
self::$configurationClosureSpacingNone,
];
yield ['<?php use function Foo\bar; bar ( 1 );'];
yield ['<?php use function some\test\{fn_a, fn_b, fn_c};'];
yield ['<?php use function some\test\{fn_a, fn_b, fn_c} ?>'];
yield ['<?php use function Foo\bar; bar ( 1 );', null, self::$configurationClosureSpacingNone];
yield ['<?php use function some\test\{fn_a, fn_b, fn_c};', null, self::$configurationClosureSpacingNone];
yield ['<?php use function some\test\{fn_a, fn_b, fn_c} ?>', null, self::$configurationClosureSpacingNone];
yield [
'<?php fn ($i) => null;',
'<?php fn($i) => null;',
];
yield [
'<?php fn ($a) => null;',
'<?php fn ($a) => null;',
];
yield [
'<?php $fn = fn () => null;',
'<?php $fn = fn()=> null;',
];
yield [
'<?php fn &($a) => null;',
'<?php fn &( $a ) => null;',
];
yield [
'<?php fn($i) => null;',
null,
self::$configurationArrowSpacingNone,
];
yield [
'<?php fn($a) => null;',
'<?php fn ($a) => null;',
self::$configurationArrowSpacingNone,
];
yield [
'<?php $fn = fn() => null;',
'<?php $fn = fn ()=> null;',
self::$configurationArrowSpacingNone,
];
yield [
'<?php $fn("");',
null,
self::$configurationArrowSpacingNone,
];
yield [
'<?php fn&($a) => null;',
'<?php fn &( $a ) => null;',
self::$configurationArrowSpacingNone,
];
yield [
'<?php fn&($a,$b) => null;',
'<?php fn &( $a,$b ) => null;',
self::$configurationArrowSpacingNone,
];
yield [
'<?php $b = static fn ($a) => $a;',
'<?php $b = static fn( $a ) => $a;',
];
yield [
'<?php $b = static fn($a) => $a;',
'<?php $b = static fn ( $a ) => $a;',
self::$configurationArrowSpacingNone,
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php function ($i) {};',
'<?php function( $i, ) {};',
];
yield [
'<?php function (
$a,
$b,
$c,
) {};',
'<?php function(
$a,
$b,
$c,
) {};',
];
yield [
'<?php function foo(
$a,
$b,
$c,
) {}',
'<?php function foo (
$a,
$b,
$c,
){}',
];
yield [
'<?php
$b = static function ($a,$b) {
echo $a;
};
',
'<?php
$b = static function( $a,$b, ) {
echo $a;
};
',
];
yield [
'<?php fn&($a,$b) => null;',
'<?php fn &( $a,$b, ) => null;',
self::$configurationArrowSpacingNone,
];
yield [
'<?php
function ($a) use ($b) {};
function ($y) use (
$b,
$c,
) {};
',
'<?php
function ($a) use ($b , ) {};
function ($y) use (
$b,
$c,
) {};
',
];
yield [
'<?php function ($i,) {};',
'<?php function( $i, ) {};',
['trailing_comma_single_line' => true],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php | tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @group phpdoc
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\PhpdocToReturnTypeFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\PhpdocToReturnTypeFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\PhpdocToReturnTypeFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocToReturnTypeFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'no phpdoc return' => [
'<?php function my_foo() {}',
];
yield 'invalid return' => [
'<?php /** @return */ function my_foo() {}',
];
yield 'invalid class 1' => [
'<?php /** @return \9 */ function my_foo() {}',
];
yield 'invalid class 2' => [
'<?php /** @return \Foo\\\Bar */ function my_foo() {}',
];
yield 'invalid class 3' => [
'<?php /** @return Break */ function my_foo() {}',
];
yield 'invalid class 4' => [
'<?php /** @return __CLASS__ */ function my_foo() {}',
];
yield 'invalid class 5' => [
'<?php /** @return I\Want\To\Bre\\\ak\Free */ function queen() {}',
];
yield 'excluded class methods' => [
'<?php
class Foo
{
/** @return Bar */
function __construct() {}
/** @return Bar */
function __destruct() {}
/** @return Bar */
function __clone() {}
}
',
];
yield 'multiple returns' => [
'<?php
/**
* @return Bar
* @return Baz
*/
function xyz() {}
',
];
yield 'non-root class' => [
'<?php /** @return Bar */ function my_foo(): Bar {}',
'<?php /** @return Bar */ function my_foo() {}',
];
yield 'non-root namespaced class' => [
'<?php /** @return My\Bar */ function my_foo(): My\Bar {}',
'<?php /** @return My\Bar */ function my_foo() {}',
];
yield 'root class' => [
'<?php /** @return \My\Bar */ function my_foo(): \My\Bar {}',
'<?php /** @return \My\Bar */ function my_foo() {}',
];
yield 'interface' => [
'<?php interface Foo { /** @return Bar */ function my_foo(): Bar; }',
'<?php interface Foo { /** @return Bar */ function my_foo(); }',
];
yield 'void return on ^7.1' => [
'<?php /** @return void */ function my_foo(): void {}',
'<?php /** @return void */ function my_foo() {}',
];
yield 'iterable return on ^7.1' => [
'<?php /** @return iterable */ function my_foo(): iterable {}',
'<?php /** @return iterable */ function my_foo() {}',
];
yield 'object return on ^7.2' => [
'<?php /** @return object */ function my_foo(): object {}',
'<?php /** @return object */ function my_foo() {}',
];
yield 'fix scalar types by default, int' => [
'<?php /** @return int */ function my_foo(): int {}',
'<?php /** @return int */ function my_foo() {}',
];
yield 'fix scalar types by default, float' => [
'<?php /** @return float */ function my_foo(): float {}',
'<?php /** @return float */ function my_foo() {}',
];
yield 'fix scalar types by default, string' => [
'<?php /** @return string */ function my_foo(): string {}',
'<?php /** @return string */ function my_foo() {}',
];
yield 'fix scalar types by default, bool' => [
'<?php /** @return bool */ function my_foo(): bool {}',
'<?php /** @return bool */ function my_foo() {}',
];
yield 'fix scalar types by default, bool, make unqualified' => [
'<?php /** @return \bool */ function my_foo(): bool {}',
'<?php /** @return \bool */ function my_foo() {}',
];
yield 'fix scalar types by default, false' => [
'<?php /** @return false */ function my_foo(): bool {}',
'<?php /** @return false */ function my_foo() {}',
];
yield 'fix scalar types by default, true' => [
'<?php /** @return true */ function my_foo(): bool {}',
'<?php /** @return true */ function my_foo() {}',
];
yield 'do not fix scalar types when configured as such' => [
'<?php /** @return int */ function my_foo() {}',
null,
['scalar_types' => false],
];
yield 'do not fix union types when configured as such' => [
'<?php /** @return int|string */ function my_foo() {}',
null,
['union_types' => false],
];
yield 'array native type' => [
'<?php /** @return array */ function my_foo(): array {}',
'<?php /** @return array */ function my_foo() {}',
];
yield 'callable type' => [
'<?php /** @return callable */ function my_foo(): callable {}',
'<?php /** @return callable */ function my_foo() {}',
];
yield 'self accessor' => [
'<?php
class Foo {
/** @return self */ function my_foo(): self {}
}
',
'<?php
class Foo {
/** @return self */ function my_foo() {}
}
',
];
yield 'nullable self accessor' => [
'<?php
class Foo {
/** @return self|null */ function my_foo(): ?self {}
}
',
'<?php
class Foo {
/** @return self|null */ function my_foo() {}
}
',
];
yield 'skip resource special type' => [
'<?php /** @return resource */ function my_foo() {}',
];
yield 'null alone cannot be a return type' => [
'<?php /** @return null */ function my_foo() {}',
];
yield 'nullable type' => [
'<?php /** @return null|Bar */ function my_foo(): ?Bar {}',
'<?php /** @return null|Bar */ function my_foo() {}',
];
yield 'nullable type with ? notation in phpDoc' => [
'<?php /** @return ?Bar */ function my_foo(): ?Bar {}',
'<?php /** @return ?Bar */ function my_foo() {}',
];
yield 'nullable type reverse order' => [
'<?php /** @return Bar|null */ function my_foo(): ?Bar {}',
'<?php /** @return Bar|null */ function my_foo() {}',
];
yield 'nullable native type' => [
'<?php /** @return null|array */ function my_foo(): ?array {}',
'<?php /** @return null|array */ function my_foo() {}',
];
yield 'generics' => [
'<?php /** @return array<int, bool> */ function my_foo(): array {}',
'<?php /** @return array<int, bool> */ function my_foo() {}',
];
yield 'array of types' => [
'<?php /** @return Foo[] */ function my_foo(): array {}',
'<?php /** @return Foo[] */ function my_foo() {}',
];
yield 'array of array of types' => [
'<?php /** @return Foo[][] */ function my_foo(): array {}',
'<?php /** @return Foo[][] */ function my_foo() {}',
];
yield 'nullable array of types' => [
'<?php /** @return null|Foo[] */ function my_foo(): ?array {}',
'<?php /** @return null|Foo[] */ function my_foo() {}',
];
yield 'comments' => [
'<?php
class A
{
// comment 0
/** @return Foo */ # comment 1
final/**/public/**/static/**/function/**/bar/**/(/**/$var/**/=/**/1/**/): Foo/**/{# comment 2
} // comment 3
}
',
'<?php
class A
{
// comment 0
/** @return Foo */ # comment 1
final/**/public/**/static/**/function/**/bar/**/(/**/$var/**/=/**/1/**/)/**/{# comment 2
} // comment 3
}
',
];
yield 'array and traversable' => [
'<?php /** @return array|Traversable */ function my_foo(): iterable {}',
'<?php /** @return array|Traversable */ function my_foo() {}',
];
yield 'array and traversable with leading slash' => [
'<?php /** @return array|\Traversable */ function my_foo(): iterable {}',
'<?php /** @return array|\Traversable */ function my_foo() {}',
];
yield 'array and traversable in a namespace' => [
'<?php
namespace App;
/** @return array|Traversable */
function my_foo() {}
',
];
yield 'array and traversable with leading slash in a namespace' => [
'<?php
namespace App;
/** @return array|\Traversable */
function my_foo(): iterable {}
',
'<?php
namespace App;
/** @return array|\Traversable */
function my_foo() {}
',
];
yield 'array and imported traversable in a namespace' => [
'<?php
namespace App;
use Traversable;
/** @return array|Traversable */
function my_foo(): iterable {}
',
'<?php
namespace App;
use Traversable;
/** @return array|Traversable */
function my_foo() {}
',
];
yield 'array and object aliased as traversable in a namespace' => [
'<?php
namespace App;
use Foo as Traversable;
/** @return array|Traversable */
function my_foo() {}
',
];
yield 'array of object and traversable' => [
'<?php /** @return Foo[]|Traversable */ function my_foo(): iterable {}',
'<?php /** @return Foo[]|Traversable */ function my_foo() {}',
];
yield 'array of object and iterable' => [
'<?php /** @return Foo[]|iterable */ function my_foo(): iterable {}',
'<?php /** @return Foo[]|iterable */ function my_foo() {}',
];
yield 'array of string and array of int' => [
'<?php /** @return string[]|int[] */ function my_foo(): array {}',
'<?php /** @return string[]|int[] */ function my_foo() {}',
];
yield 'intersection types' => [
'<?php
/** @return Bar&Baz */
function bar() {}
',
];
yield 'very long class name before ampersand' => [
'<?php
/** @return Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar&Baz */
function bar() {}
',
];
yield 'very long class name after ampersand' => [
'<?php
/** @return Bar&Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz */
function bar() {}
',
];
yield 'arrow function' => [
'<?php /** @return int */ fn(): int => 1;',
'<?php /** @return int */ fn() => 1;',
];
yield 'arrow function (static)' => [
'<?php /** @return int */ static fn(): int => 1;',
'<?php /** @return int */ static fn() => 1;',
];
yield 'arrow function (as argument ended with ,)' => [
'<?php
Utils::stableSort(
$elements,
/**
* @return array
*/
static fn($a): array => [$a],
fn($a, $b) => 1,
);',
'<?php
Utils::stableSort(
$elements,
/**
* @return array
*/
static fn($a) => [$a],
fn($a, $b) => 1,
);',
];
yield 'types defined with "phpstan-type"' => [
<<<'PHP'
<?php
/**
* @phpstan-type _Pair array{int, int}
*/
class Foo {
/**
* @return _Pair
*/
public function f() {}
}
/**
* @phpstan-type _Trio array{int, int, int}
*/
class Bar {
/**
* @return _Trio
*/
public function f() {}
}
PHP,
];
yield 'types defined with "phpstan-import-type"' => [
<<<'PHP'
<?php
/**
* @phpstan-import-type _Pair from FooFoo
*/
class Foo {
/**
* @return _Pair
*/
public function f() {}
}
/**
* @phpstan-import-type _Trio from BarBar
*/
class Bar {
/**
* @return _Trio
*/
public function f() {}
}
PHP,
];
yield 'types defined with "phpstan-import-type" with alias' => [
<<<'PHP'
<?php
/**
* @phpstan-import-type ConflictingType from FooFoo as Not_ConflictingType
*/
class Foo {
/**
* @return Not_ConflictingType
*/
public function f1() {}
/**
* @return ConflictingType
*/
public function f2(): ConflictingType {}
}
PHP,
<<<'PHP'
<?php
/**
* @phpstan-import-type ConflictingType from FooFoo as Not_ConflictingType
*/
class Foo {
/**
* @return Not_ConflictingType
*/
public function f1() {}
/**
* @return ConflictingType
*/
public function f2() {}
}
PHP,
];
yield 'types defined with "psalm-type"' => [
<<<'PHP'
<?php
/**
* @psalm-type _Pair = array{int, int}
*/
class Foo {
/**
* @return _Pair
*/
public function f() {}
}
/**
* @psalm-type _Trio array{int, int, int}
*/
class Bar {
/**
* @return _Trio
*/
public function f() {}
}
PHP,
];
yield 'types defined with "psalm-import-type"' => [
<<<'PHP'
<?php
/**
* @psalm-import-type _Pair from FooFoo
*/
class Foo {
/**
* @return _Pair
*/
public function f() {}
}
/**
* @psalm-import-type _Trio from BarBar
*/
class Bar {
/**
* @return _Trio
*/
public function f() {}
}
PHP,
];
yield 'types defined with "psalm-import-type" with alias' => [
<<<'PHP'
<?php
/**
* @psalm-import-type Bar from FooFoo as BarAliased
*/
class Foo {
/** @return Bar */
public function f1(): Bar {}
/** @return BarAliased */
public function f2() {}
/** @return BarAliased */
public function f3() {}
/** @return Bar */
public function f4(): Bar {}
}
PHP,
<<<'PHP'
<?php
/**
* @psalm-import-type Bar from FooFoo as BarAliased
*/
class Foo {
/** @return Bar */
public function f1() {}
/** @return BarAliased */
public function f2() {}
/** @return BarAliased */
public function f3() {}
/** @return Bar */
public function f4() {}
}
PHP,
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield 'report static as self' => [
'<?php
class Foo {
/** @return static */ function my_foo(): self {}
}
',
'<?php
class Foo {
/** @return static */ function my_foo() {}
}
',
];
yield 'skip mixed special type' => [
'<?php /** @return mixed */ function my_foo() {}',
];
yield 'invalid void return on ^7.1' => [
'<?php /** @return null|void */ function my_foo() {}',
];
yield 'skip union types' => [
'<?php /** @return Foo|Bar */ function my_foo() {}',
];
yield 'skip primitive or array types' => [
'<?php /** @return string|string[] */ function my_foo() {}',
];
yield 'skip nullable union types' => [
'<?php /** @return null|Foo|Bar */ function my_foo() {}',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'static' => [
'<?php
final class Foo {
/** @return static */
public function something(): static {}
}
',
'<?php
final class Foo {
/** @return static */
public function something() {}
}
',
];
yield 'nullable static' => [
'<?php
final class Foo {
/** @return null|static */
public function something(): ?static {}
}
',
'<?php
final class Foo {
/** @return null|static */
public function something() {}
}
',
];
yield 'mixed' => [
'<?php
final class Foo {
/** @return mixed */
public function something(): mixed {}
}
',
'<?php
final class Foo {
/** @return mixed */
public function something() {}
}
',
];
yield 'union types' => [
'<?php /** @return Foo|Bar */ function my_foo(): Foo|Bar {}',
'<?php /** @return Foo|Bar */ function my_foo() {}',
];
yield 'union types including generics' => [
'<?php /** @return string|array<int, string> */ function my_foo(): string|array {}',
'<?php /** @return string|array<int, string> */ function my_foo() {}',
];
yield 'union types including nullable' => [
'<?php /** @return null|Foo|Bar */ function my_foo(): Foo|Bar|null {}',
'<?php /** @return null|Foo|Bar */ function my_foo() {}',
];
yield 'primitive or array types' => [
'<?php /** @return string|string[] */ function my_foo(): string|array {}',
'<?php /** @return string|string[] */ function my_foo() {}',
];
}
/**
* @dataProvider provideFixPre81Cases
*
* @requires PHP <8.1
*/
public function testFixPre81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFixPre81Cases(): iterable
{
yield 'skip never type' => [
'<?php /** @return never */ function bar() {}',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'never type' => [
'<?php /** @return never */ function bar(): never {}',
'<?php /** @return never */ function bar() {}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/PhpdocToPropertyTypeFixerTest.php | tests/Fixer/FunctionNotation/PhpdocToPropertyTypeFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @group phpdoc
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\PhpdocToPropertyTypeFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\PhpdocToPropertyTypeFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\PhpdocToPropertyTypeFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocToPropertyTypeFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'no phpdoc return' => [
'<?php class Foo { private $foo; }',
];
yield 'invalid return' => [
'<?php class Foo { /** @var */ private $foo; }',
];
yield 'invalid class 1' => [
'<?php class Foo { /** @var \9 */ private $foo; }',
];
yield 'invalid class 2' => [
'<?php class Foo { /** @var \Foo\\\Bar */ private $foo; }',
];
yield 'multiple returns' => [
'<?php
class Foo {
/**
* @var Bar
* @var Baz
*/
private $foo;
}
',
];
yield 'non-root class' => [
'<?php class Foo { /** @var Bar */ private Bar $foo; }',
'<?php class Foo { /** @var Bar */ private $foo; }',
];
yield 'non-root namespaced class' => [
'<?php class Foo { /** @var My\Bar */ private My\Bar $foo; }',
'<?php class Foo { /** @var My\Bar */ private $foo; }',
];
yield 'root class' => [
'<?php class Foo { /** @var \My\Bar */ private \My\Bar $foo; }',
'<?php class Foo { /** @var \My\Bar */ private $foo; }',
];
yield 'void' => [
'<?php class Foo { /** @var void */ private $foo; }',
];
yield 'never' => [
'<?php class Foo { /** @var never */ private $foo; }',
];
yield 'iterable' => [
'<?php class Foo { /** @var iterable */ private iterable $foo; }',
'<?php class Foo { /** @var iterable */ private $foo; }',
];
yield 'object' => [
'<?php class Foo { /** @var object */ private object $foo; }',
'<?php class Foo { /** @var object */ private $foo; }',
];
yield 'fix scalar types by default, int' => [
'<?php class Foo { /** @var int */ private int $foo; }',
'<?php class Foo { /** @var int */ private $foo; }',
];
yield 'fix scalar types by default, float' => [
'<?php class Foo { /** @var float */ private float $foo; }',
'<?php class Foo { /** @var float */ private $foo; }',
];
yield 'fix scalar types by default, string' => [
'<?php class Foo { /** @var string */ private string $foo; }',
'<?php class Foo { /** @var string */ private $foo; }',
];
yield 'fix scalar types by default, bool' => [
'<?php class Foo { /** @var bool */ private bool $foo; }',
'<?php class Foo { /** @var bool */ private $foo; }',
];
yield 'fix scalar types by default, false' => [
'<?php class Foo { /** @var false */ private bool $foo; }',
'<?php class Foo { /** @var false */ private $foo; }',
];
yield 'fix scalar types by default, true' => [
'<?php class Foo { /** @var true */ private bool $foo; }',
'<?php class Foo { /** @var true */ private $foo; }',
];
yield 'do not fix scalar types when configured as such' => [
'<?php class Foo { /** @var int */ private $foo; }',
null,
['scalar_types' => false],
];
yield 'do not fix union types when configured as such' => [
'<?php class Foo { /** @var int|string */ private $foo; }',
null,
['union_types' => false],
];
yield 'array native type' => [
'<?php class Foo { /** @var array */ private array $foo; }',
'<?php class Foo { /** @var array */ private $foo; }',
];
yield 'callable type' => [
'<?php class Foo { /** @var callable */ private $foo; }',
];
yield 'self accessor' => [
'<?php class Foo { /** @var self */ private self $foo; }',
'<?php class Foo { /** @var self */ private $foo; }',
];
yield 'report static as self' => [
'<?php class Foo { /** @var static */ private self $foo; }',
'<?php class Foo { /** @var static */ private $foo; }',
];
yield 'skip resource special type' => [
'<?php class Foo { /** @var resource */ private $foo; }',
];
yield 'null alone cannot be a property type' => [
'<?php class Foo { /** @var null */ private $foo; }',
];
yield 'nullable type' => [
'<?php class Foo { /** @var null|Bar */ private ?Bar $foo; }',
'<?php class Foo { /** @var null|Bar */ private $foo; }',
];
yield 'nullable type with ? notation in phpDoc' => [
'<?php class Foo { /** @var ?Bar */ private ?Bar $foo; }',
'<?php class Foo { /** @var ?Bar */ private $foo; }',
];
yield 'nullable type reverse order' => [
'<?php class Foo { /** @var Bar|null */ private ?Bar $foo; }',
'<?php class Foo { /** @var Bar|null */ private $foo; }',
];
yield 'nullable native type' => [
'<?php class Foo { /** @var null|array */ private ?array $foo; }',
'<?php class Foo { /** @var null|array */ private $foo; }',
];
yield 'nullable type with short notation' => [
'<?php class Foo { /** @var ?array */ private ?array $foo; }',
'<?php class Foo { /** @var ?array */ private $foo; }',
];
yield 'nullable type with existing default value' => [
'<?php class Foo { /** @var ?array */ private ?array $foo = []; }',
'<?php class Foo { /** @var ?array */ private $foo = []; }',
];
yield 'generics' => [
'<?php class Foo { /** @var array<int, bool> */ private array $foo; }',
'<?php class Foo { /** @var array<int, bool> */ private $foo; }',
];
yield 'array of types' => [
'<?php class Foo { /** @var Foo[] */ private array $foo; }',
'<?php class Foo { /** @var Foo[] */ private $foo; }',
];
yield 'array of array of types' => [
'<?php class Foo { /** @var Foo[][] */ private array $foo; }',
'<?php class Foo { /** @var Foo[][] */ private $foo; }',
];
yield 'nullable array of types' => [
'<?php class Foo { /** @var null|Foo[] */ private ?array $foo; }',
'<?php class Foo { /** @var null|Foo[] */ private $foo; }',
];
yield 'comments' => [
'<?php
class Foo
{
// comment 0
/** @var Foo */ # comment 1
public/**/Foo $foo/**/;# comment 2
}
',
'<?php
class Foo
{
// comment 0
/** @var Foo */ # comment 1
public/**/$foo/**/;# comment 2
}
',
];
yield 'array and traversable' => [
'<?php class Foo { /** @var array|Traversable */ private iterable $foo; }',
'<?php class Foo { /** @var array|Traversable */ private $foo; }',
];
yield 'array and traversable with leading slash' => [
'<?php class Foo { /** @var array|\Traversable */ private iterable $foo; }',
'<?php class Foo { /** @var array|\Traversable */ private $foo; }',
];
yield 'array and traversable in a namespace' => [
'<?php
namespace App;
class Foo {
/** @var array|Traversable */
private $foo;
}
',
];
yield 'array and traversable with leading slash in a namespace' => [
'<?php
namespace App;
class Foo {
/** @var array|\Traversable */
private iterable $foo;
}
',
'<?php
namespace App;
class Foo {
/** @var array|\Traversable */
private $foo;
}
',
];
yield 'array and imported traversable in a namespace' => [
'<?php
namespace App;
use Traversable;
class Foo {
/** @var array|Traversable */
private iterable $foo;
}
',
'<?php
namespace App;
use Traversable;
class Foo {
/** @var array|Traversable */
private $foo;
}
',
];
yield 'array and object aliased as traversable in a namespace' => [
'<?php
namespace App;
use Bar as Traversable;
class Foo {
/** @var array|Traversable */
private $foo;
}
',
null,
];
yield 'array of object and traversable' => [
'<?php class Foo { /** @var Foo[]|Traversable */ private iterable $foo; }',
'<?php class Foo { /** @var Foo[]|Traversable */ private $foo; }',
];
yield 'array of object and iterable' => [
'<?php class Foo { /** @var Foo[]|iterable */ private iterable $foo; }',
'<?php class Foo { /** @var Foo[]|iterable */ private $foo; }',
];
yield 'array of string and array of int' => [
'<?php class Foo { /** @var string[]|int[] */ private array $foo; }',
'<?php class Foo { /** @var string[]|int[] */ private $foo; }',
];
yield 'trait' => [
'<?php trait Foo { /** @var int */ private int $foo; }',
'<?php trait Foo { /** @var int */ private $foo; }',
];
yield 'static property' => [
'<?php class Foo { /** @var int */ private static int $foo; }',
'<?php class Foo { /** @var int */ private static $foo; }',
];
yield 'static property reverse order' => [
'<?php class Foo { /** @var int */ static private int $foo; }',
'<?php class Foo { /** @var int */ static private $foo; }',
];
yield 'var' => [
'<?php class Foo { /** @var int */ var int $foo; }',
'<?php class Foo { /** @var int */ var $foo; }',
];
yield 'with default value' => [
'<?php class Foo { /** @var int */ public int $foo = 1; }',
'<?php class Foo { /** @var int */ public $foo = 1; }',
];
yield 'multiple properties of the same type' => [
'<?php class Foo {
/**
* @var int $foo
* @var int $bar
*/
public int $foo, $bar;
}',
'<?php class Foo {
/**
* @var int $foo
* @var int $bar
*/
public $foo, $bar;
}',
];
yield 'multiple properties of different types' => [
'<?php class Foo {
/**
* @var int $foo
* @var string $bar
*/
public $foo, $bar;
}',
];
yield 'single property with different annotations' => [
'<?php class Foo {
/**
* @var int $foo
* @var string $foo
*/
public $foo;
}',
];
yield 'multiple properties with missing annotation' => [
'<?php class Foo {
/**
* @var int $foo
*/
public $foo, $bar;
}',
];
yield 'multiple properties with annotation without name' => [
'<?php class Foo {
/**
* @var int
* @var int $bar
*/
public $foo, $bar;
}',
];
yield 'multiple properties with annotation without name reverse order' => [
'<?php class Foo {
/**
* @var int $foo
* @var int
*/
public $foo, $bar;
}',
];
yield 'multiple properties with extra annotations' => [
'<?php class Foo {
/**
* @var string
* @var int $foo
* @var int $bar
* @var int
*/
public int $foo, $bar;
}',
'<?php class Foo {
/**
* @var string
* @var int $foo
* @var int $bar
* @var int
*/
public $foo, $bar;
}',
];
yield 'abstract method' => [
'<?php abstract class Foo {
/** @var Bar */ private Bar $foo;
public abstract function getFoo();
}',
'<?php abstract class Foo {
/** @var Bar */ private $foo;
public abstract function getFoo();
}',
];
yield 'great number of properties' => [
'<?php class Foo {
/** @var string */
private string $foo1;
/** @var string */
private string $foo2;
/** @var int */
private int $foo3;
/** @var string */
private string $foo4;
/** @var string */
private string $foo5;
/** @var string */
private string $foo6;
/** @var string */
private string $foo7;
/** @var int */
private int $foo8;
/** @var string */
private string $foo9;
/** @var int|null */
private ?int $foo10;
}',
'<?php class Foo {
/** @var string */
private $foo1;
/** @var string */
private $foo2;
/** @var int */
private $foo3;
/** @var string */
private $foo4;
/** @var string */
private $foo5;
/** @var string */
private $foo6;
/** @var string */
private $foo7;
/** @var int */
private $foo8;
/** @var string */
private $foo9;
/** @var int|null */
private $foo10;
}',
];
yield 'anonymous class' => [
'<?php new class { /** @var int */ private int $foo; };',
'<?php new class { /** @var int */ private $foo; };',
];
yield 'intersection types' => [
'<?php class Foo { /** @var Bar&Baz */ private $x; }',
];
yield 'very long class name before ampersand' => [
'<?php class Foo { /** @var Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaar&Baz */ private $x; }',
];
yield 'very long class name after ampersand' => [
'<?php class Foo { /** @var Bar&Baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaz */ private $x; }',
];
yield 'types defined with "phpstan-type"' => [
<<<'PHP'
<?php
/**
* @phpstan-type _Pair array{int, int}
*/
class Foo {
/** @var _Pair */
public $x;
}
/**
* @phpstan-type _Trio array{int, int, int}
*/
class Bar {
/** @var _Trio */
private $x;
}
PHP,
];
yield 'types defined with "phpstan-import-type"' => [
<<<'PHP'
<?php
/**
* @phpstan-import-type _Pair from FooFoo
*/
class Foo {
/** @var _Pair */
private $x;
}
/**
* @phpstan-import-type _Trio from BarBar
*/
class Bar {
/** @var _Trio */
private $x;
}
PHP,
];
yield 'types defined with "phpstan-import-type" with alias' => [
<<<'PHP'
<?php
/**
* @phpstan-import-type ConflictingType from FooFoo as Not_ConflictingType
*/
class Foo {
/** @var Not_ConflictingType */
private $x;
/** @var ConflictingType */
private ConflictingType $y;
}
PHP,
<<<'PHP'
<?php
/**
* @phpstan-import-type ConflictingType from FooFoo as Not_ConflictingType
*/
class Foo {
/** @var Not_ConflictingType */
private $x;
/** @var ConflictingType */
private $y;
}
PHP,
];
yield 'types defined with "psalm-type"' => [
<<<'PHP'
<?php
/**
* @psalm-type _Pair = array{int, int}
*/
class Foo {
/** @var _Pair */
private $x;
}
/**
* @psalm-type _Trio array{int, int, int}
*/
class Bar {
/** @var _Trio */
private $x;
}
PHP,
];
yield 'types defined with "psalm-import-type"' => [
<<<'PHP'
<?php
/**
* @psalm-import-type _Pair from FooFoo
*/
class Foo {
/** @var _Pair */
private $x;
}
/**
* @psalm-import-type _Trio from BarBar
*/
class Bar {
/** @var _Trio */
private $x;
}
PHP,
];
yield 'types defined with "psalm-import-type" with alias' => [
<<<'PHP'
<?php
/**
* @psalm-import-type Bar from FooFoo as BarAliased
*/
class Foo {
/** @var Bar */
private Bar $a;
/** @var BarAliased */
private $b;
/** @var BarAliased */
private $c;
/** @var Bar */
private Bar $d;
}
PHP,
<<<'PHP'
<?php
/**
* @psalm-import-type Bar from FooFoo as BarAliased
*/
class Foo {
/** @var Bar */
private $a;
/** @var BarAliased */
private $b;
/** @var BarAliased */
private $c;
/** @var Bar */
private $d;
}
PHP,
];
}
/**
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFixPre80Cases(): iterable
{
yield 'skip mixed type' => [
'<?php class Foo { /** @var mixed */ private $foo; }',
];
yield 'skip union types' => [
'<?php class Foo { /** @var Foo|Bar */ private $foo; }',
];
yield 'skip union nullable types' => [
'<?php class Foo { /** @var null|Foo|Bar */ private $foo; }',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield 'fix mixed type' => [
'<?php class Foo { /** @var mixed */ private mixed $foo; }',
'<?php class Foo { /** @var mixed */ private $foo; }',
];
yield 'union types' => [
'<?php class Foo { /** @var Foo|Bar */ private Foo|Bar $foo; }',
'<?php class Foo { /** @var Foo|Bar */ private $foo; }',
];
yield 'union types including nullable' => [
'<?php class Foo { /** @var null|Foo|Bar */ private Foo|Bar|null $foo; }',
'<?php class Foo { /** @var null|Foo|Bar */ private $foo; }',
];
yield 'union types including generics' => [
'<?php class Foo { /** @var string|array<int, bool> */ private string|array $foo; }',
'<?php class Foo { /** @var string|array<int, bool> */ private $foo; }',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected): void
{
$this->doTest($expected);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'readonly properties are always typed, make sure the fixer does not crash' => [
'<?php class Foo { /** @var int */ private readonly string $foo; }',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/NativeFunctionInvocationFixerTest.php | tests/Fixer/FunctionNotation/NativeFunctionInvocationFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer>
*
* @author Andreas Möller <am@localheinz.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\NativeFunctionInvocationFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NativeFunctionInvocationFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideInvalidConfigurationCases
*
* @param array<string, mixed> $configuration
*/
public function testInvalidConfiguration(array $configuration, string $expectedExceptionMessage): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessage($expectedExceptionMessage);
$this->fixer->configure($configuration);
}
/**
* @return iterable<string, array{array<string, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield 'unknown key' => [
['foo' => 'bar'],
'[native_function_invocation] Invalid configuration: The option "foo" does not exist.',
];
yield 'null' => [
['exclude' => [null]],
'[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "null".',
];
yield 'false' => [
['exclude' => [false]],
'[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "bool".',
];
yield 'true' => [
['exclude' => [true]],
'[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "bool".',
];
yield 'int' => [
['exclude' => [1]],
'[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "int".',
];
yield 'array' => [
['exclude' => [[]]],
'[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "array".',
];
yield 'float' => [
['exclude' => [0.1]],
'[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "float".',
];
yield 'object' => [
['exclude' => [new \stdClass()]],
'[native_function_invocation] Invalid configuration: The option "exclude" with value array is expected to be of type "string[]", but one of the elements is of type "stdClass".',
];
yield 'not-trimmed' => [
['exclude' => [' is_string ']],
'[native_function_invocation] Invalid configuration: Each element must be a non-empty, trimmed string, got "string" instead.',
];
yield 'unknown set' => [
['include' => ['@xxx']],
'[native_function_invocation] Invalid configuration: Unknown set "@xxx", known sets are "@all", "@internal" and "@compiler_optimized".',
];
yield 'non-trimmed set' => [
['include' => [' x ']],
'[native_function_invocation] Invalid configuration: Each element must be a non-empty, trimmed string, got "string" instead.',
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
\is_string($foo);
',
];
yield [
'<?php
\is_string($foo);
',
'<?php
is_string($foo);
',
];
yield [
'<?php
class Foo
{
public function bar($foo)
{
return \is_string($foo);
}
}
',
];
yield [
'<?php
json_encode($foo);
\strlen($foo);
',
'<?php
json_encode($foo);
strlen($foo);
',
];
yield [
'<?php
class Foo
{
public function bar($foo)
{
return \IS_STRING($foo);
}
}
',
'<?php
class Foo
{
public function bar($foo)
{
return IS_STRING($foo);
}
}
',
];
yield 'fix multiple calls in single code' => [
'<?php
json_encode($foo);
\strlen($foo);
\strlen($foo);
',
'<?php
json_encode($foo);
strlen($foo);
strlen($foo);
',
];
yield [
'<?php $name = \get_class($foo, );',
'<?php $name = get_class($foo, );',
];
yield [
'<?php
is_string($foo);
',
null,
['exclude' => ['is_string']],
];
yield [
'<?php
class Foo
{
public function bar($foo)
{
return is_string($foo);
}
}
',
null,
['exclude' => ['is_string']],
];
yield [
'<?php echo count([1]);',
null,
['scope' => 'namespaced'],
];
yield [
'<?php
namespace space1 { ?>
<?php echo \count([2]) ?>
<?php }namespace {echo count([1]);}
',
'<?php
namespace space1 { ?>
<?php echo count([2]) ?>
<?php }namespace {echo count([1]);}
',
['scope' => 'namespaced'],
];
yield [
'<?php
namespace Bar {
echo \strLEN("in 1");
}
namespace {
echo strlen("out 1");
}
namespace {
echo strlen("out 2");
}
namespace Bar{
echo \strlen("in 2");
}
namespace {
echo strlen("out 3");
}
',
'<?php
namespace Bar {
echo strLEN("in 1");
}
namespace {
echo strlen("out 1");
}
namespace {
echo strlen("out 2");
}
namespace Bar{
echo strlen("in 2");
}
namespace {
echo strlen("out 3");
}
',
['scope' => 'namespaced'],
];
yield [
'<?php
namespace space11 ?>
<?php
echo \strlen(__NAMESPACE__);
namespace space2;
echo \strlen(__NAMESPACE__);
',
'<?php
namespace space11 ?>
<?php
echo strlen(__NAMESPACE__);
namespace space2;
echo strlen(__NAMESPACE__);
',
['scope' => 'namespaced'],
];
yield [
'<?php namespace PhpCsFixer\Tests\Fixer\Casing;\count([1]);',
'<?php namespace PhpCsFixer\Tests\Fixer\Casing;count([1]);',
['scope' => 'namespaced'],
];
yield [
'<?php
namespace Space12;
echo \count([1]);
namespace Space2;
echo \count([1]);
?>
',
'<?php
namespace Space12;
echo count([1]);
namespace Space2;
echo count([1]);
?>
',
['scope' => 'namespaced'],
];
yield [
'<?php namespace {echo strlen("out 2");}',
null,
['scope' => 'namespaced'],
];
yield [
'<?php
namespace space13 {
echo \strlen("in 1");
}
namespace space2 {
echo \strlen("in 2");
}
namespace { // global
echo strlen("global 1");
}
',
'<?php
namespace space13 {
echo strlen("in 1");
}
namespace space2 {
echo strlen("in 2");
}
namespace { // global
echo strlen("global 1");
}
',
['scope' => 'namespaced'],
];
yield [
'<?php
namespace space1 {
echo \count([1]);
}
namespace {
echo \count([1]);
}
',
'<?php
namespace space1 {
echo count([1]);
}
namespace {
echo count([1]);
}
',
['scope' => 'all'],
];
yield 'include set + 1, exclude 1' => [
'<?php
echo \count([1]);
\some_other($a, 3);
echo strlen($a);
not_me();
',
'<?php
echo count([1]);
some_other($a, 3);
echo strlen($a);
not_me();
',
[
'include' => [NativeFunctionInvocationFixer::SET_INTERNAL, 'some_other'],
'exclude' => ['strlen'],
],
];
yield 'include @all' => [
'<?php
echo \count([1]);
\some_other($a, 3);
echo \strlen($a);
\me_as_well();
',
'<?php
echo count([1]);
some_other($a, 3);
echo strlen($a);
me_as_well();
',
[
'include' => [NativeFunctionInvocationFixer::SET_ALL],
],
];
yield 'include @compiler_optimized' => [
'<?php
// do not fix
$a = strrev($a);
$a .= str_repeat($a, 4);
$b = already_prefixed_function();
// fix
$c = \get_class($d);
$e = \intval($f);
',
'<?php
// do not fix
$a = strrev($a);
$a .= str_repeat($a, 4);
$b = \already_prefixed_function();
// fix
$c = get_class($d);
$e = intval($f);
',
[
'include' => [NativeFunctionInvocationFixer::SET_COMPILER_OPTIMIZED],
],
];
yield [
'<?php class Foo {
public function & strlen($name) {
}
}
',
];
yield 'scope namespaced and strict enabled' => [
'<?php
$a = not_compiler_optimized_function();
$b = intval($c);
',
'<?php
$a = \not_compiler_optimized_function();
$b = \intval($c);
',
[
'scope' => 'namespaced',
'strict' => true,
],
];
yield [
'<?php
use function foo\json_decode;
json_decode($base);
',
null,
[
'include' => [NativeFunctionInvocationFixer::SET_ALL],
],
];
yield [
'<?php return [\set() => 42];',
'<?php return [set() => 42];',
[
'include' => ['@all'],
],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixPre80Cases
*
* @requires PHP <8.0
*/
public function testFixPre80(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{string, 1?: string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixPre80Cases(): iterable
{
yield 'include @compiler_optimized with strict enabled' => [
'<?php
$a = not_compiler_optimized_function();
$b = not_compiler_optimized_function();
$c = \intval($d);
',
'<?php
$a = \not_compiler_optimized_function();
$b = \ not_compiler_optimized_function();
$c = intval($d);
',
[
'include' => [NativeFunctionInvocationFixer::SET_COMPILER_OPTIMIZED],
'strict' => true,
],
];
yield [
'<?php
echo \/**/strlen($a);
echo \ strlen($a);
echo \#
#
strlen($a);
echo \strlen($a);
',
'<?php
echo \/**/strlen($a);
echo \ strlen($a);
echo \#
#
strlen($a);
echo strlen($a);
',
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix80Cases(): iterable
{
yield 'attribute and strict' => [
'<?php
#[\Attribute(\Attribute::TARGET_CLASS)]
class Foo {}
',
null,
['strict' => true],
];
yield 'null safe operator' => ['<?php $x?->count();'];
yield 'multiple function-calls-like in attribute' => [
'<?php
#[Foo(), Bar(), Baz()]
class Foo {}
',
null,
['include' => ['@all']],
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix84Cases
*
* @requires PHP 8.4
*/
public function testFix84(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix84Cases(): iterable
{
yield 'property hooks' => [
<<<'PHP'
<?php
class Foo
{
public string $bar = '' {
get => $this->bar;
set(string $bar) => \strtolower($bar);
}
public string $baz = '' {
get => $this->baz;
set(string $baz) { $this->baz = \strtoupper($baz); }
}
}
PHP,
<<<'PHP'
<?php
class Foo
{
public string $bar = '' {
get => $this->bar;
set(string $bar) => strtolower($bar);
}
public string $baz = '' {
get => $this->baz;
set(string $baz) { $this->baz = strtoupper($baz); }
}
}
PHP,
['include' => ['@all']],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/MultilinePromotedPropertiesFixerTest.php | tests/Fixer/FunctionNotation/MultilinePromotedPropertiesFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\MultilinePromotedPropertiesFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\MultilinePromotedPropertiesFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\MultilinePromotedPropertiesFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class MultilinePromotedPropertiesFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @requires PHP 8.0
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'single parameter' => [
'<?php class Foo {
public function __construct(
public int $x
) {}
}',
'<?php class Foo {
public function __construct(public int $x) {}
}',
];
yield 'single parameter with trailing comma' => [
'<?php class Foo {
public function __construct(
protected int $x,
) {}
}',
'<?php class Foo {
public function __construct(protected int $x,) {}
}',
];
yield 'multiple parameters' => [
'<?php class Foo {
public function __construct(
private int $x,
private int $y,
private int $z
) {}
}',
'<?php class Foo {
public function __construct(private int $x, private int $y, private int $z) {}
}',
];
yield 'multiple parameters and only one promoted' => [
'<?php class Foo {
public function __construct(
int $x,
private int $y,
int $z
) {}
}',
'<?php class Foo {
public function __construct(int $x, private int $y, int $z) {}
}',
];
yield 'parameters with default values' => [
'<?php class Foo {
public function __construct(
private array $a = [1, 2, 3, 4],
private bool $b = self::DEFAULT_B,
) {}
}',
'<?php class Foo {
public function __construct(private array $a = [1, 2, 3, 4], private bool $b = self::DEFAULT_B,) {}
}',
];
yield 'parameters with attributes' => [
'<?php class Foo {
public function __construct(
private array $a = [1, 2, 3, 4],
#[Bar(1, 2, 3)] private bool $b = self::DEFAULT_B,
) {}
}',
'<?php class Foo {
public function __construct(private array $a = [1, 2, 3, 4], #[Bar(1, 2, 3)] private bool $b = self::DEFAULT_B,) {}
}',
];
yield 'multiple classes' => [
'<?php
class ClassWithSinglePromotedProperty {
public function __construct(
private int $foo
) {}
}
class ClassWithoutConstructor {}
class ClassWithoutPromotedProperties {
public function __construct(string $a, string $b) {}
}
class ClassWithMultiplePromotedProperties {
public function __construct(
private int $x,
private int $y,
private int $z
) {}
}',
'<?php
class ClassWithSinglePromotedProperty {
public function __construct(private int $foo) {}
}
class ClassWithoutConstructor {}
class ClassWithoutPromotedProperties {
public function __construct(string $a, string $b) {}
}
class ClassWithMultiplePromotedProperties {
public function __construct(private int $x, private int $y, private int $z) {}
}',
];
yield '0 parameters with 0 configured' => [
'<?php class Foo {
public function __construct() {}
}',
null,
['minimum_number_of_parameters' => 0],
];
foreach ([0, 1, 2] as $numberOfParameters) {
yield \sprintf('2 parameters with %d configured', $numberOfParameters) => [
'<?php class Foo {
public function __construct(
private int $x,
private int $y
) {}
}',
'<?php class Foo {
public function __construct(private int $x, private int $y) {}
}',
['minimum_number_of_parameters' => $numberOfParameters],
];
yield \sprintf('2 parameters and only one promoted with %d configured', $numberOfParameters) => [
'<?php class Foo {
public function __construct(
int $x,
private int $y
) {}
}',
'<?php class Foo {
public function __construct(int $x, private int $y) {}
}',
['minimum_number_of_parameters' => $numberOfParameters],
];
}
foreach ([3, 4] as $numberOfParameters) {
yield \sprintf('2 parameters with %d configured', $numberOfParameters) => [
'<?php class Foo {
public function __construct(private int $x, private int $y) {}
}',
null,
['minimum_number_of_parameters' => $numberOfParameters],
];
yield \sprintf('2 parameters and only one promoted with %d configured', $numberOfParameters) => [
'<?php class Foo {
public function __construct(int $x, private int $y) {}
}',
null,
['minimum_number_of_parameters' => $numberOfParameters],
];
}
foreach ([1, 2, 3, 4] as $numberOfParameters) {
yield \sprintf('2 parameters and none promoted with %d configured', $numberOfParameters) => [
'<?php class Foo {
public function __construct(int $x, int $y) {}
}',
null,
['minimum_number_of_parameters' => $numberOfParameters],
];
}
yield 'blank lines removed' => [
'<?php class Foo {
public function __construct(
private int $x,
private int $y,
private int $z
) {}
}',
'<?php class Foo {
public function __construct(
private int $x,
private int $y,
private int $z
) {}
}',
];
yield 'no blank lines' => [
'<?php class Foo {
public function __construct(
private int $x,
private int $y,
private int $z
) {}
}',
'<?php class Foo {
public function __construct(
private int $x, private int $y,
private int $z
) {}
}',
];
yield 'blank lines kept' => [
'<?php class Foo {
public function __construct(
private int $x,
private int $y,
private int $z
) {}
}',
'<?php class Foo {
public function __construct(
private int $x, private int $y,
private int $z
) {}
}',
['keep_blank_lines' => true],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/ImplodeCallFixerTest.php | tests/Fixer/FunctionNotation/ImplodeCallFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\ImplodeCallFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\ImplodeCallFixer>
*
* @author Kuba Werłos <werlos@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class ImplodeCallFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ["<?php implode('', [1,2,3]);"];
yield ['<?php implode("", $foo);'];
yield ['<?php implode($foo, $bar);'];
yield ['<?php $arrayHelper->implode($foo);'];
yield ['<?php ArrayHelper::implode($foo);'];
yield ['<?php ArrayHelper\implode($foo);'];
yield ['<?php define("implode", "foo"); implode; bar($baz);'];
yield [
'<?php implode("", $foo);',
'<?php implode($foo, "");',
];
yield [
'<?php \implode("", $foo);',
'<?php \implode($foo, "");',
];
yield [
'<?php implode("Lorem ipsum dolor sit amet", $foo);',
'<?php implode($foo, "Lorem ipsum dolor sit amet");',
];
yield [
'<?php implode(\'\', $foo);',
'<?php implode($foo);',
];
yield [
'<?php IMPlode("", $foo);',
'<?php IMPlode($foo, "");',
];
yield [
'<?php implode("",$foo);',
'<?php implode($foo,"");',
];
yield [
'<?php implode("", $weirdStuff[mt_rand($min, getMax()) + 200]);',
'<?php implode($weirdStuff[mt_rand($min, getMax()) + 200], "");',
];
yield [
'<?php
implode(
"",
$foo
);',
'<?php
implode(
$foo,
""
);',
];
yield [
'<?php
implode(
\'\', $foo
);',
'<?php
implode(
$foo
);',
];
yield [
'<?php
implode(# 1
""/* 2.1 */,# 2.2
$foo# 3
);',
'<?php
implode(# 1
$foo/* 2.1 */,# 2.2
""# 3
);',
];
yield [
'<?php
implode(# 1
# 2
\'\', $foo# 3
# 4
)# 5
;',
'<?php
implode(# 1
# 2
$foo# 3
# 4
)# 5
;',
];
yield [
'<?php
implode(\'\', $a);implode(\'\', $a);implode(\'\', $a);implode(\'\', $a);implode(\'\', $a);implode(\'\', $a);
// comment for testing
implode(\'\', $a);implode(\'\', $a);implode(\'\', $a);implode(\'\', $a);implode(\'\', $a);implode(\'\', $a);
',
'<?php
implode($a);implode($a);implode($a);implode($a);implode($a);implode($a);
// comment for testing
implode($a);implode($a);implode($a);implode($a);implode($a);implode($a);
',
];
yield [
'<?php implode("", $foo, );',
'<?php implode($foo, "", );',
];
yield [
'<?php implode(\'\', $foo, );',
'<?php implode($foo, );',
];
yield [
'<?php
implode(
"",
$foo,
);',
'<?php
implode(
$foo,
"",
);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/NoTrailingCommaInSinglelineFunctionCallFixerTest.php | tests/Fixer/FunctionNotation/NoTrailingCommaInSinglelineFunctionCallFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\NoTrailingCommaInSinglelineFunctionCallFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\NoTrailingCommaInSinglelineFunctionCallFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoTrailingCommaInSinglelineFunctionCallFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'simple var' => [
'<?php $a(1);',
'<?php $a(1,);',
];
yield '&' => [
'<?php $a = &foo($a);',
'<?php $a = &foo($a,);',
];
yield 'open' => [
'<?php foo($a);',
'<?php foo($a,);',
];
yield '=' => [
'<?php $b = foo($a);',
'<?php $b = foo($a,);',
];
yield '.' => [
'<?php $c = $b . foo($a);',
'<?php $c = $b . foo($a,);',
];
yield '(' => [
'<?php (foo($a/* 1 */ /* 2 */ ));',
'<?php (foo($a /* 1 */ , /* 2 */ ));',
];
yield '\\' => [
'<?php \foo($a);',
'<?php \foo($a,);',
];
yield 'A\\' => [
'<?php A\foo($a);',
'<?php A\foo($a,);',
];
yield '\A\\' => [
'<?php \A\foo($a);',
'<?php \A\foo($a,);',
];
yield ';' => [
'<?php ; foo($a);',
'<?php ; foo($a,);',
];
yield '}' => [
'<?php if ($a) { echo 1;} foo($a);',
'<?php if ($a) { echo 1;} foo($a,);',
];
yield 'test method call' => [
'<?php $o->abc($a);',
'<?php $o->abc($a,);',
];
yield 'nested call' => [
'<?php $o->abc($a,foo(1));',
'<?php $o->abc($a,foo(1,));',
];
yield 'wrapped' => [
'<?php echo (new Process())->getOutput(1);',
'<?php echo (new Process())->getOutput(1,);',
];
yield 'dynamic function and method calls' => [
'<?php $b->$a(1); $c("");',
'<?php $b->$a(1,); $c("",);',
];
yield 'static function call' => [
'<?php
unset($foo->bar);
$b = isset($foo->bar);
list($a,$b) = $a;
',
'<?php
unset($foo->bar,);
$b = isset($foo->bar,);
list($a,$b,) = $a;
',
];
yield 'unset' => [
'<?php A::foo(1);',
'<?php A::foo(1,);',
];
yield 'anonymous_class construction' => [
'<?php new class(1, 2) {};',
'<?php new class(1, 2,) {};',
];
yield 'array/property access call' => [
'<?php
$a = [
"e" => static function(int $a): void{ echo $a;},
"d" => [
[2 => static function(int $a): void{ echo $a;}]
]
];
$a["e"](1);
$a["d"][0][2](1);
$z = new class { public static function b(int $a): void {echo $a; }};
$z::b(1);
${$e}(1);
$$e(2);
$f(0)(1);
$g["e"](1); // foo',
'<?php
$a = [
"e" => static function(int $a): void{ echo $a;},
"d" => [
[2 => static function(int $a): void{ echo $a;}]
]
];
$a["e"](1,);
$a["d"][0][2](1,);
$z = new class { public static function b(int $a): void {echo $a; }};
$z::b(1,);
${$e}(1,);
$$e(2,);
$f(0,)(1,);
$g["e"](1,); // foo',
];
yield 'do not fix' => [
'<?php
function someFunction ($p1){}
function & foo($a,$b): array { return []; }
foo (
1,
2,
);
$a = new class (
$a,
) {};
isset($a, $b);
unset($a,$b);
list($a,$b) = $a;
$a = [1,2,3,];
$a = array(1,2,3,);
function foo1(string $param = null ): void
{
}
;',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php function foo(
#[MyAttr(1, 2,)] Type $myParam,
) {}
$foo1b = function() use ($bar, ) {};
',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php $object?->method(1); strlen(...);',
'<?php $object?->method(1,); strlen(...);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/FopenFlagOrderFixerTest.php | tests/Fixer/FunctionNotation/FopenFlagOrderFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractFopenFlagFixer
* @covers \PhpCsFixer\Fixer\FunctionNotation\FopenFlagOrderFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\FopenFlagOrderFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class FopenFlagOrderFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'most simple fix case' => [
'<?php
$a = fopen($foo, \'rw+b\');
',
'<?php
$a = fopen($foo, \'brw+\');
',
];
yield '"fopen" casing insensitive' => [
'<?php
$a = \FOPen($foo, "cr+w+b");
$a = \FOPEN($foo, "crw+b");
',
'<?php
$a = \FOPen($foo, "bw+r+c");
$a = \FOPEN($foo, "bw+rc");
',
];
yield 'comments around flags' => [
'<?php
$a = fopen($foo,/*0*/\'rb\'/*1*/);
',
'<?php
$a = fopen($foo,/*0*/\'br\'/*1*/);
',
];
yield 'binary string' => [
'<?php
$a = \fopen($foo, b"cr+w+b");
$b = \fopen($foo, B"crw+b");
$c = \fopen($foo, b\'cr+w+b\');
$d = \fopen($foo, B\'crw+b\');
',
'<?php
$a = \fopen($foo, b"bw+r+c");
$b = \fopen($foo, B"bw+rc");
$c = \fopen($foo, b\'bw+r+c\');
$d = \fopen($foo, B\'bw+rc\');
',
];
yield 'common typos' => [
'<?php
$a = fopen($a, "b+r");
$b = fopen($b, \'b+w\');
',
];
// `t` cases
yield [
'<?php
$a = fopen($foo, \'rw+t\');
',
'<?php
$a = fopen($foo, \'trw+\');
',
];
yield [
'<?php
$a = \fopen($foo, \'rw+tb\');
',
'<?php
$a = \fopen($foo, \'btrw+\');
',
];
// don't fix cases
yield 'single flag' => [
'<?php
$a = fopen($foo, "r");
$a = fopen($foo, "r+");
',
];
yield 'not simple flags' => [
'<?php
$a = fopen($foo, "br+".$a);
',
];
yield 'wrong # of arguments' => [
'<?php
$b = \fopen("br+");
$c = fopen($foo, "bw+", 1, 2 , 3);
',
];
yield '"flags" is too long (must be overridden)' => [
'<?php
$d = fopen($foo, "r+w+a+x+c+etbX");
',
];
yield 'static method call' => [
'<?php
$e = A::fopen($foo, "bw+");
',
];
yield 'method call' => [
'<?php
$f = $b->fopen($foo, "br+");
',
];
yield 'comments, PHPDoc and literal' => [
'<?php
// fopen($foo, "brw");
/* fopen($foo, "brw"); */
echo("fopen($foo, \"brw\")");
',
];
yield 'invalid flag values' => [
'<?php
$a = fopen($foo, \'\');
$a = fopen($foo, \'x\'); // ok but should not mark collection as changed
$a = fopen($foo, \'k\');
$a = fopen($foo, \'kz\');
$a = fopen($foo, \'k+\');
$a = fopen($foo, \'+k\');
$a = fopen($foo, \'xc++\');
$a = fopen($foo, \'w+r+r+\');
$a = fopen($foo, \'+brw+\');
$a = fopen($foo, \'b+rw\');
$a = fopen($foo, \'bbrw+\');
$a = fopen($foo, \'brw++\');
$a = fopen($foo, \'++brw\');
$a = fopen($foo, \'ybrw+\');
$a = fopen($foo, \'rr\');
$a = fopen($foo, \'ロ\');
$a = fopen($foo, \'ロ+\');
$a = \fopen($foo, \'rロ\');
$a = \fopen($foo, \'w+ロ\');
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/DateTimeCreateFromFormatCallFixerTest.php | tests/Fixer/FunctionNotation/DateTimeCreateFromFormatCallFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\DateTimeCreateFromFormatCallFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\DateTimeCreateFromFormatCallFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class DateTimeCreateFromFormatCallFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
foreach ([\DateTime::class, \DateTimeImmutable::class] as $class) {
$lowerCaseClass = strtolower($class);
$upperCaseClass = strtoupper($class);
yield [
"<?php \\{$class}::createFromFormat('!Y-m-d', '2022-02-11');",
"<?php \\{$class}::createFromFormat('Y-m-d', '2022-02-11');",
];
yield [
"<?php use {$class}; {$class}::createFromFormat('!Y-m-d', '2022-02-11');",
"<?php use {$class}; {$class}::createFromFormat('Y-m-d', '2022-02-11');",
];
yield [
"<?php {$class}::createFromFormat('!Y-m-d', '2022-02-11');",
"<?php {$class}::createFromFormat('Y-m-d', '2022-02-11');",
];
yield [
"<?php use \\Example\\{$class}; {$class}::createFromFormat('Y-m-d', '2022-02-11');",
];
yield [
"<?php use \\Example\\{$lowerCaseClass}; {$upperCaseClass}::createFromFormat('Y-m-d', '2022-02-11');",
];
yield [
"<?php \\{$class}::createFromFormat(\"!Y-m-d\", '2022-02-11');",
"<?php \\{$class}::createFromFormat(\"Y-m-d\", '2022-02-11');",
];
yield [
"<?php \\{$class}::createFromFormat(\$foo, '2022-02-11');",
];
yield [
"<?php \\{$upperCaseClass}::createFromFormat( \"!Y-m-d\", '2022-02-11');",
"<?php \\{$upperCaseClass}::createFromFormat( \"Y-m-d\", '2022-02-11');",
];
yield [
"<?php \\{$class}::createFromFormat(/* aaa */ '!Y-m-d', '2022-02-11');",
"<?php \\{$class}::createFromFormat(/* aaa */ 'Y-m-d', '2022-02-11');",
];
yield [
"<?php /*1*//*2*/{$class}/*3*/::/*4*/createFromFormat/*5*/(/*6*/\"!Y-m-d\"/*7*/,/*8*/\"2022-02-11\"/*9*/)/*10*/ ?>",
"<?php /*1*//*2*/{$class}/*3*/::/*4*/createFromFormat/*5*/(/*6*/\"Y-m-d\"/*7*/,/*8*/\"2022-02-11\"/*9*/)/*10*/ ?>",
];
yield [
"<?php \\{$class}::createFromFormat('Y-m-d');",
];
yield [
"<?php \\{$class}::createFromFormat(\$a, \$b);",
];
yield [
"<?php \\{$class}::createFromFormat('Y-m-d', \$b, \$c);",
];
yield [
"<?php A\\{$class}::createFromFormat('Y-m-d', '2022-02-11');",
];
yield [
"<?php A\\{$class}::createFromFormat('Y-m-d'.\"a\", '2022-02-11');",
];
yield ["<?php \\{$class}::createFromFormat(123, '2022-02-11');"];
yield [
"<?php namespace {
\\{$class}::createFromFormat('!Y-m-d', '2022-02-11');
}
namespace Bar {
class {$class} extends Foo {}
{$class}::createFromFormat('Y-m-d', '2022-02-11');
}
",
"<?php namespace {
\\{$class}::createFromFormat('Y-m-d', '2022-02-11');
}
namespace Bar {
class {$class} extends Foo {}
{$class}::createFromFormat('Y-m-d', '2022-02-11');
}
",
];
yield $class.': binary string' => [
"<?php \\{$class}::createFromFormat(b'!Y-m-d', '2022-02-11');",
"<?php \\{$class}::createFromFormat(b'Y-m-d', '2022-02-11');",
];
yield $class.': empty string' => [
"<?php \\{$class}::createFromFormat('', '2022-02-11');",
];
}
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/NoUselessPrintfFixerTest.php | tests/Fixer/FunctionNotation/NoUselessPrintfFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\NoUselessPrintfFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\NoUselessPrintfFixer>
*
* @author John Paul E. Balandan, CPA <paulbalandan@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoUselessPrintfFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'single-quoted' => [
'<?php print \'bar\';',
'<?php printf(\'bar\');',
];
yield 'double-quoted' => [
'<?php print "bar\n";',
'<?php printf("bar\n");',
];
yield 'case insensitive' => [
'<?php print "bar";',
'<?php PrInTf("bar");',
];
yield 'trailing comma' => [
'<?php print "bar";',
'<?php printf("bar",);',
];
yield 'leading NS separator' => [
'<?php print "bar";',
'<?php \printf("bar");',
];
yield 'skip not global function' => [
<<<'PHP'
<?php
namespace Foo {
function printf($arg) {}
}
namespace Bar {
use function Foo\printf;
printf('bar');
}
PHP,
];
yield 'skip function cases' => [
<<<'PHP'
<?php
printf();
printf('%s%s', 'foo', 'bar');
\Foo\printf('bar');
printf(...$bar);
PHP,
];
yield 'skip class cases' => [
<<<'PHP'
<?php
class Foo {
public function printf($arg) {}
}
class Bar {
public static function printf($arg) {}
}
(new Foo())->printf('bar');
Bar::printf('bar');
PHP,
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'skip first class callable' => [
'<?php return printf(...);',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/FunctionNotation/FunctionTypehintSpaceFixerTest.php | tests/Fixer/FunctionNotation/FunctionTypehintSpaceFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\FunctionNotation;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\FunctionNotation\FunctionTypehintSpaceFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\FunctionTypehintSpaceFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class FunctionTypehintSpaceFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php function foo($param) {}',
];
yield [
'<?php function foo($param1,$param2) {}',
];
yield [
'<?php function foo(&$param) {}',
];
yield [
'<?php function foo(& $param) {}',
];
yield [
'<?php function foo(/**int*/$param) {}',
];
yield [
'<?php function foo(bool /**bla bla*/ $param) {}',
];
yield [
'<?php function foo(bool /**bla bla*/$param) {}',
'<?php function foo(bool/**bla bla*/$param) {}',
];
yield [
'<?php function foo(bool /**bla bla*/$param) {}',
'<?php function foo(bool /**bla bla*/$param) {}',
];
yield [
'<?php function foo(callable $param) {}',
'<?php function foo(callable$param) {}',
];
yield [
'<?php function foo(callable $param) {}',
'<?php function foo(callable $param) {}',
];
yield [
'<?php function foo(array &$param) {}',
'<?php function foo(array&$param) {}',
];
yield [
'<?php function foo(array &$param) {}',
'<?php function foo(array &$param) {}',
];
yield [
'<?php function foo(array & $param) {}',
'<?php function foo(array& $param) {}',
];
yield [
'<?php function foo(array & $param) {}',
'<?php function foo(array & $param) {}',
];
yield [
'<?php function foo(Bar $param) {}',
'<?php function foo(Bar$param) {}',
];
yield [
'<?php function foo(Bar $param) {}',
'<?php function foo(Bar $param) {}',
];
yield [
'<?php function foo(Bar\Baz $param) {}',
'<?php function foo(Bar\Baz$param) {}',
];
yield [
'<?php function foo(Bar\Baz $param) {}',
'<?php function foo(Bar\Baz $param) {}',
];
yield [
'<?php function foo(Bar\Baz &$param) {}',
'<?php function foo(Bar\Baz&$param) {}',
];
yield [
'<?php function foo(Bar\Baz &$param) {}',
'<?php function foo(Bar\Baz &$param) {}',
];
yield [
'<?php function foo(Bar\Baz & $param) {}',
'<?php function foo(Bar\Baz& $param) {}',
];
yield [
'<?php function foo(Bar\Baz & $param) {}',
'<?php function foo(Bar\Baz & $param) {}',
];
yield [
'<?php $foo = function(Bar\Baz $param) {};',
'<?php $foo = function(Bar\Baz$param) {};',
];
yield [
'<?php $foo = function(Bar\Baz $param) {};',
'<?php $foo = function(Bar\Baz $param) {};',
];
yield [
'<?php $foo = function(Bar\Baz &$param) {};',
'<?php $foo = function(Bar\Baz&$param) {};',
];
yield [
'<?php $foo = function(Bar\Baz &$param) {};',
'<?php $foo = function(Bar\Baz &$param) {};',
];
yield [
'<?php $foo = function(Bar\Baz & $param) {};',
'<?php $foo = function(Bar\Baz& $param) {};',
];
yield [
'<?php $foo = function(Bar\Baz & $param) {};',
'<?php $foo = function(Bar\Baz & $param) {};',
];
yield [
'<?php class Test { public function foo(Bar\Baz $param) {} }',
'<?php class Test { public function foo(Bar\Baz$param) {} }',
];
yield [
'<?php class Test { public function foo(Bar\Baz $param) {} }',
'<?php class Test { public function foo(Bar\Baz $param) {} }',
];
yield [
'<?php $foo = function(array $a,
array $b, array $c, array $d) {};',
'<?php $foo = function(array $a,
array$b, array $c, array
$d) {};',
];
yield [
'<?php $foo = function(
array $a,
$b
) {};',
];
yield [
'<?php $foo = function(
$a,
array $b
) {};',
];
yield [
'<?php function foo(...$param) {}',
];
yield [
'<?php function foo(&...$param) {}',
];
yield [
'<?php function foo(array ...$param) {}',
'<?php function foo(array...$param) {}',
];
yield [
'<?php function foo(array & ...$param) {}',
'<?php function foo(array& ...$param) {}',
];
yield ['<?php use function some\test\{fn_a, fn_b, fn_c};'];
yield ['<?php use function some\test\{fn_a, fn_b, fn_c} ?>'];
yield [
'<?php $foo = fn(Bar\Baz $param) => null;',
'<?php $foo = fn(Bar\Baz$param) => null;',
];
yield [
'<?php $foo = fn(Bar\Baz $param) => null;',
'<?php $foo = fn(Bar\Baz $param) => null;',
];
yield [
'<?php $foo = fn(Bar\Baz &$param) => null;',
'<?php $foo = fn(Bar\Baz&$param) => null;',
];
yield [
'<?php $foo = fn(Bar\Baz &$param) => null;',
'<?php $foo = fn(Bar\Baz &$param) => null;',
];
yield [
'<?php $foo = fn(Bar\Baz & $param) => null;',
'<?php $foo = fn(Bar\Baz& $param) => null;',
];
yield [
'<?php $foo = fn(Bar\Baz & $param) => null;',
'<?php $foo = fn(Bar\Baz & $param) => null;',
];
yield [
'<?php $foo = fn(array $a,
array $b, array $c, array $d) => null;',
'<?php $foo = fn(array $a,
array$b, array $c, array
$d) => null;',
];
yield [
'<?php $foo = fn(
array $a,
$b
) => null;',
];
yield [
'<?php $foo = fn(
$a,
array $b
) => null;',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, string $input): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php function foo(mixed $a){}',
'<?php function foo(mixed$a){}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocNoAliasTagFixerTest.php | tests/Fixer/Phpdoc/PhpdocNoAliasTagFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocNoAliasTagFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocNoAliasTagFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocNoAliasTagFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocNoAliasTagFixerTest extends AbstractFixerTestCase
{
/**
* @param array<string, mixed> $config
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(array $config, string $expectedMessage): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches($expectedMessage);
$this->fixer->configure($config);
}
/**
* @return iterable<int, array{array<string, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield [
['replacements' => [1 => 'abc']],
'#^\[phpdoc_no_alias_tag\] Invalid configuration: Tag to replace must be a string\.$#',
];
yield [
['replacements' => ['a' => null]],
'#^\[phpdoc_no_alias_tag\] Invalid configuration: The option "replacements" with value array is expected to be of type "string\[\]", but one of the elements is of type "null"\.$#',
];
yield [
['replacements' => ['see' => 'link*/']],
'#^\[phpdoc_no_alias_tag\] Invalid configuration: Tag "see" cannot be replaced by invalid tag "link\*\/"\.$#',
];
yield [
['foo' => 123],
'#^\[phpdoc_no_alias_tag\] Invalid configuration: The option "foo" does not exist. Defined options are: "replacements"\.$#',
];
yield [
['replacements' => [
'link' => 'see',
'a' => 'b',
'see' => 'link',
]],
'#^\[phpdoc_no_alias_tag\] Invalid configuration: Cannot change tag "link" to tag "see", as the tag "see" is configured to be replaced to "link"\.$#',
];
yield [
['replacements' => [
'b' => 'see',
'see' => 'link',
'link' => 'b',
]],
'#^\[phpdoc_no_alias_tag\] Invalid configuration: Cannot change tag "b" to tag "see", as the tag "see" is configured to be replaced to "link"\.$#',
];
yield [
['replacements' => [
'see' => 'link',
'link' => 'b',
]],
'#^\[phpdoc_no_alias_tag\] Invalid configuration: Cannot change tag "see" to tag "link", as the tag "link" is configured to be replaced to "b"\.$#',
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php /** @see https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#710-link-deprecated */',
'<?php /** @link https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#710-link-deprecated */',
];
yield [
'<?php /** @property mixed $bar */',
'<?php /** @property-write mixed $bar */',
];
yield [
'<?php /** @property mixed $bar */',
'<?php /** @property-read mixed $bar */',
];
yield [
'<?php /** @var string Hello! */',
'<?php /** @type string Hello! */',
];
yield [
'<?php
/**
*
*/',
null,
['replacements' => [
'property-read' => 'property',
'property-write' => 'property',
]],
];
yield [
'<?php
/**
* @property string $foo
*/',
'<?php
/**
* @property-read string $foo
*/',
['replacements' => [
'property-read' => 'property',
'property-write' => 'property',
]],
];
yield [
'<?php /** @property mixed $bar */',
'<?php /** @property-write mixed $bar */',
['replacements' => [
'property-read' => 'property',
'property-write' => 'property',
]],
];
yield [
'<?php
/**
*
*/',
null,
['replacements' => [
'type' => 'var',
]],
];
yield [
'<?php
/**
* @var string Hello!
*/',
'<?php
/**
* @type string Hello!
*/',
['replacements' => [
'type' => 'var',
]],
];
yield [
'<?php /** @var string Hello! */',
'<?php /** @type string Hello! */',
['replacements' => [
'type' => 'var',
]],
];
yield [
'<?php
/**
* Initializes this class with the given options.
*
* @param array $options {
* @var bool $required Whether this element is required
* @var string $label The display name for this element
* }
*/',
'<?php
/**
* Initializes this class with the given options.
*
* @param array $options {
* @type bool $required Whether this element is required
* @type string $label The display name for this element
* }
*/',
['replacements' => [
'type' => 'var',
]],
];
yield [
'<?php
/**
*
*/',
null,
['replacements' => [
'var' => 'type',
]],
];
yield [
'<?php
/**
* @type string Hello!
*/',
'<?php
/**
* @var string Hello!
*/',
['replacements' => [
'var' => 'type',
]],
];
yield [
'<?php /** @type string Hello! */',
'<?php /** @var string Hello! */',
['replacements' => [
'var' => 'type',
]],
];
yield [
'<?php
/**
* Initializes this class with the given options.
*
* @param array $options {
* @type bool $required Whether this element is required
* @type string $label The display name for this element
* }
*/',
'<?php
/**
* Initializes this class with the given options.
*
* @param array $options {
* @var bool $required Whether this element is required
* @var string $label The display name for this element
* }
*/',
['replacements' => [
'var' => 'type',
]],
];
yield [
'<?php /** @see https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#710-link-deprecated */',
'<?php /** @link https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#710-link-deprecated */',
['replacements' => [
'link' => 'see',
]],
];
yield [
<<<'PHP'
<?php
/**
* @see example.com
* @var foo
*
* @phpstan-type MyArray array{
* '@link': ?string,
* "@type": string,
* }
*/
class Foo {}
PHP,
<<<'PHP'
<?php
/**
* @link example.com
* @type foo
*
* @phpstan-type MyArray array{
* '@link': ?string,
* "@type": string,
* }
*/
class Foo {}
PHP,
];
yield [
<<<'PHP'
<?php
class Foo
{
/**
* @var int
*/
const BAR = 1;
/** @var int */
const BAZ = 2;
}
PHP,
<<<'PHP'
<?php
class Foo
{
/**
* @const int
*/
const BAR = 1;
/** @const int */
const BAZ = 2;
}
PHP,
['replacements' => ['const' => 'var']],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocArrayTypeFixerTest.php | tests/Fixer/Phpdoc/PhpdocArrayTypeFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocArrayTypeFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocArrayTypeFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocArrayTypeFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
<<<'PHP'
<?php
/** @tagNotSupportingTypes string[] */
/** @var array{'a', 'b[]', 'c'} */
/** @var 'x|T1[][]|y' */
/** @var "x|T2[][]|y" */
PHP,
];
yield [
'<?php /** @var array<int> */',
'<?php /** @var int[] */',
];
yield [
'<?php /** @param array<array<array<array<int>>>> $x */',
'<?php /** @param int[][][][] $x */',
];
yield [
'<?php /** @param array<array<array<array<int>>>> $x */',
'<?php /** @param int [][ ][] [] $x */',
];
yield [
'<?php /** @return iterable<array<int>> */',
'<?php /** @return iterable<int[]> */',
];
yield [
'<?php /** @var array<Foo\Bar> */',
'<?php /** @var Foo\Bar[] */',
];
yield [
'<?php /** @var array<Foo_Bar> */',
'<?php /** @var Foo_Bar[] */',
];
yield [
'<?php /** @var array<bool>|array<float>|array<int>|array<string> */',
'<?php /** @var array<bool>|float[]|array<int>|string[] */',
];
yield [
<<<'PHP'
<?php
/**
* @param array{foo?: array<int>} $foo
* @param callable(array<int>): array<int> $bar
*/
PHP,
<<<'PHP'
<?php
/**
* @param array{foo?: int[]} $foo
* @param callable(int[]): int[] $bar
*/
PHP,
];
yield [
<<<'PHP'
<?php
/**
* @var array<array{'a'}>
* @var \Closure(iterable<\DateTime>): array<void>
* @var array<Yes>|'No[][]'|array<array<Yes>>|'No[]'
*/
PHP,
<<<'PHP'
<?php
/**
* @var array{'a'}[]
* @var \Closure(iterable<\DateTime>): void[]
* @var Yes[]|'No[][]'|Yes[][]|'No[]'
*/
PHP,
];
yield [
<<<'PHP'
<?php
/**
* @param ?array<type> $a
* @param ?array<array<type>> $b
* @param ?'literal[]' $c
*/
PHP,
<<<'PHP'
<?php
/**
* @param ?type[] $a
* @param ?type[][] $b
* @param ?'literal[]' $c
*/
PHP,
];
yield [
'<?php /** @var array<Foo|Bar> */',
'<?php /** @var (Foo|Bar)[] */',
];
yield [
'<?php /** @var (Foo&Bar)|array<Baz> */',
'<?php /** @var (Foo&Bar)|Baz[] */',
];
$expected = $input = 'string';
for ($i = 0; $i < 32; ++$i) {
$expected = 'array<'.$expected.'>';
$input .= '[]';
}
yield [
\sprintf('<?php /** @var %s */', $expected),
\sprintf('<?php /** @var %s */', $input),
];
yield [
'<?php /** @return array<Foo<covariant TEntity>> */',
'<?php /** @return Foo<covariant TEntity>[] */',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/NoEmptyPhpdocFixerTest.php | tests/Fixer/Phpdoc/NoEmptyPhpdocFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\NoEmptyPhpdocFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\NoEmptyPhpdocFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoEmptyPhpdocFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFixCases(): iterable
{
yield 'multiple PHPdocs' => [
'<?php
/** a */
'.'
'.'
'.'
'.'
/**
* test
*/
/** *test* */
',
'<?php
/** *//** a *//** */
/**
*/
/**
*
*/
/** ***
*
******/
/**
**/
/**
* test
*/
/** *test* */
',
];
yield 'PHPDoc on its own line' => [
<<<'PHP'
<?php
echo $a;
echo $b;
PHP,
<<<'PHP'
<?php
echo $a;
/** */
echo $b;
PHP,
];
yield 'PHPDoc on its own line with empty line before' => [
<<<'PHP'
<?php
function f() {
echo $a;
echo $b;
}
PHP,
<<<'PHP'
<?php
function f() {
echo $a;
/** */
echo $b;
}
PHP,
];
yield 'PHPDoc on its own line with empty line after' => [
<<<'PHP'
<?php
echo $a;
echo $b;
PHP,
<<<'PHP'
<?php
echo $a;
/** */
echo $b;
PHP,
];
yield 'PHPDoc on its own line with empty line before and after' => [
<<<'PHP'
<?php
echo $a;
echo $b;
PHP,
<<<'PHP'
<?php
echo $a;
/** */
echo $b;
PHP,
];
yield 'PHPDoc with empty line before and content after' => [
<<<'PHP'
<?php
function f() {
echo $a;
echo $b;
}
PHP,
<<<'PHP'
<?php
function f() {
echo $a;
/** */echo $b;
}
PHP,
];
yield 'PHPDoc with content before and empty line after' => [
<<<'PHP'
<?php
function f() {
echo $a;
echo $b;
}
PHP,
<<<'PHP'
<?php
function f() {
echo $a;/** */
echo $b;
}
PHP,
];
yield 'PHPDoc after open tag - the same line' => [
'<?php '.'
foo();
',
'<?php /** */
foo();
',
];
yield 'PHPDoc after open tag - next line' => [
<<<'PHP'
<?php
foo();
PHP,
<<<'PHP'
<?php
/** */
foo();
PHP,
];
yield 'PHPDoc after open tag - next next next line' => [
<<<'PHP'
<?php
foo();
PHP,
<<<'PHP'
<?php
/** */
foo();
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/AlignMultilineCommentFixerTest.php | tests/Fixer/Phpdoc/AlignMultilineCommentFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\AlignMultilineCommentFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\AlignMultilineCommentFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\AlignMultilineCommentFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class AlignMultilineCommentFixerTest extends AbstractFixerTestCase
{
public function testInvalidConfiguration(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->fixer->configure(['a' => 1]);
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = [], ?WhitespacesFixerConfig $whitespacesConfig = null): void
{
$this->fixer->configure($configuration);
$this->fixer->setWhitespacesConfig($whitespacesConfig ?? new WhitespacesFixerConfig());
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration, 3?: WhitespacesFixerConfig}>
*/
public static function provideFixCases(): iterable
{
foreach (self::getFixCases() as $case) {
yield $case;
yield [
str_replace("\n", "\r\n", $case[0]),
isset($case[1]) ? str_replace("\n", "\r\n", $case[1]) : null,
$case[2] ?? [],
new WhitespacesFixerConfig("\t", "\r\n"),
];
}
yield [
'<?php
/*
* Doc-like Multiline comment
*
*
*/',
'<?php
/*
* Doc-like Multiline comment
*
*
*/',
['comment_type' => 'phpdocs_like'],
];
yield [
'<?php
/*
* Multiline comment with mixed content
*
Line without an asterisk
*
*/',
null,
['comment_type' => 'phpdocs_like'],
];
yield [
'<?php
/*
* Two empty lines
*
*
*/',
null,
['comment_type' => 'phpdocs_like'],
];
yield [
'<?php
/*
* Multiline comment with mixed content
*
Line without an asterisk
*
*/',
'<?php
/*
* Multiline comment with mixed content
*
Line without an asterisk
*
*/',
['comment_type' => 'all_multiline'],
];
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
private static function getFixCases(): iterable
{
yield [
'<?php
$a = 1;
/**
* Doc comment
*
*
*
* first without an asterisk
* second without an asterisk or space
*/',
'<?php
$a = 1;
/**
* Doc comment
*
*
first without an asterisk
second without an asterisk or space
*/',
];
yield [
'<?php
/**
* Document start
*/',
'<?php
/**
* Document start
*/',
];
yield [
"<?php\n \n /**\n * Two new lines\n */",
"<?php\n \n /**\n* Two new lines\n*/",
];
yield [
"<?php
\t/**
\t * Tabs as indentation
\t */",
"<?php
\t/**
* Tabs as indentation
*/",
];
yield [
'<?php
$a = 1;
/**
* Doc command without prior indentation
*/',
'<?php
$a = 1;
/**
* Doc command without prior indentation
*/',
];
yield [
'<?php
/**
* Doc command without prior indentation
* Document start
*/',
'<?php
/**
* Doc command without prior indentation
* Document start
*/',
];
// Untouched cases
yield [
'<?php
/*
* Multiline comment
*
*
*/',
];
yield [
'<?php
/** inline doc comment */',
];
yield [
'<?php
$a=1; /**
*
doc comment that doesn\'t start in a new line
*/',
];
yield [
'<?php
# Hash single line comments are untouched
#
#
#',
];
yield [
'<?php
// Slash single line comments are untouched
//
//
//',
];
yield 'uni code test' => [
'<?php
class A
{
/**
* @SWG\Get(
* path="/api/v0/cards",
* operationId="listCards",
* tags={"Банковские карты"},
* summary="Возвращает список банковских карт."
* )
*/
public function indexAction()
{
}
}',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocAlignFixerTest.php | tests/Fixer/Phpdoc/PhpdocAlignFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocAlignFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocAlignFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*
* @param _AutogeneratedInputConfiguration $configuration
*/
public function testFix(
string $expected,
?string $input,
array $configuration,
?WhitespacesFixerConfig $whitespacesConfig = null
): void {
$this->fixer->configure($configuration);
$this->fixer->setWhitespacesConfig($whitespacesConfig ?? new WhitespacesFixerConfig());
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, null|string, _AutogeneratedInputConfiguration, 3?: WhitespacesFixerConfig}>
*/
public static function provideFixCases(): iterable
{
yield 'none, one and four spaces between type and variable' => [
'<?php
/**
* @param int $a
* @param int $b
* @param int $c
*/',
'<?php
/**
* @param int $a
* @param int $b
* @param int$c
*/',
['tags' => ['param']],
];
yield 'aligning params' => [
'<?php
/**
* @param EngineInterface $templating
* @param string $format
* @param int $code An HTTP response status code
* @param bool $debug
* @param mixed &$reference A parameter passed by reference
*/
',
'<?php
/**
* @param EngineInterface $templating
* @param string $format
* @param int $code An HTTP response status code
* @param bool $debug
* @param mixed &$reference A parameter passed by reference
*/
',
['tags' => ['param']],
];
yield 'left align' => [
'<?php
/**
* @param EngineInterface $templating
* @param string $format
* @param int $code An HTTP response status code
* @param bool $debug
* @param mixed &$reference A parameter passed by reference
*/
',
'<?php
/**
* @param EngineInterface $templating
* @param string $format
* @param int $code An HTTP response status code
* @param bool $debug
* @param mixed &$reference A parameter passed by reference
*/
',
['tags' => ['param'], 'align' => PhpdocAlignFixer::ALIGN_LEFT],
];
yield 'partially untyped' => [
'<?php
/**
* @param $id
* @param $parentId
* @param int $websiteId
* @param $position
* @param int[][] $siblings
*/
',
'<?php
/**
* @param $id
* @param $parentId
* @param int $websiteId
* @param $position
* @param int[][] $siblings
*/
',
['tags' => ['param']],
];
yield 'partially untyped left align' => [
'<?php
/**
* @param $id
* @param $parentId
* @param int $websiteId
* @param $position
* @param int[][] $siblings
*/
',
'<?php
/**
* @param $id
* @param $parentId
* @param int $websiteId
* @param $position
* @param int[][] $siblings
*/
',
['tags' => ['param'], 'align' => PhpdocAlignFixer::ALIGN_LEFT],
];
yield 'multiline description' => [
'<?php
/**
* @param EngineInterface $templating
* @param string $format
* @param int $code An HTTP response status code
* See constants
* @param bool $debug
* @param bool $debug See constants
* See constants
* @param mixed &$reference A parameter passed by reference
* @property mixed $foo A foo
* See constants
* @method static baz($bop) A method that does a thing
* It does it well
*/
',
'<?php
/**
* @param EngineInterface $templating
* @param string $format
* @param int $code An HTTP response status code
* See constants
* @param bool $debug
* @param bool $debug See constants
* See constants
* @param mixed &$reference A parameter passed by reference
* @property mixed $foo A foo
* See constants
* @method static baz($bop) A method that does a thing
* It does it well
*/
',
['tags' => ['param', 'property', 'method']],
];
yield 'multiline description left align' => [
'<?php
/**
* @param EngineInterface $templating
* @param string $format
* @param int $code An HTTP response status code
* See constants
* @param bool $debug
* @param bool $debug See constants
* See constants
* @param mixed &$reference A parameter passed by reference
* @property mixed $foo A foo
* See constants
* @method static baz($bop) A method that does a thing
* It does it well
*/
',
'<?php
/**
* @param EngineInterface $templating
* @param string $format
* @param int $code An HTTP response status code
* See constants
* @param bool $debug
* @param bool $debug See constants
* See constants
* @param mixed &$reference A parameter passed by reference
* @property mixed $foo A foo
* See constants
* @method static baz($bop) A method that does a thing
* It does it well
*/
',
['tags' => ['param', 'property', 'method'], 'align' => PhpdocAlignFixer::ALIGN_LEFT],
];
yield 'multiline description with throws' => [
'<?php
/**
* @param EngineInterface $templating
* @param string $format
* @param int $code An HTTP response status code
* See constants
* @param bool $debug
* @param bool $debug See constants
* See constants
* @param mixed &$reference A parameter passed by reference
*
* @return Foo description foo
*
* @throws Foo description foo
* description foo
*/
',
'<?php
/**
* @param EngineInterface $templating
* @param string $format
* @param int $code An HTTP response status code
* See constants
* @param bool $debug
* @param bool $debug See constants
* See constants
* @param mixed &$reference A parameter passed by reference
*
* @return Foo description foo
*
* @throws Foo description foo
* description foo
*/
',
['tags' => ['param', 'return', 'throws']],
];
yield 'multiline description with throws left align' => [
'<?php
/**
* @param EngineInterface $templating
* @param string $format
* @param int $code An HTTP response status code
* See constants
* @param bool $debug
* @param bool $debug See constants
* See constants
* @param mixed &$reference A parameter passed by reference
*
* @return Foo description foo
*
* @throws Foo description foo
* description foo
*/
',
'<?php
/**
* @param EngineInterface $templating
* @param string $format
* @param int $code An HTTP response status code
* See constants
* @param bool $debug
* @param bool $debug See constants
* See constants
* @param mixed &$reference A parameter passed by reference
*
* @return Foo description foo
*
* @throws Foo description foo
* description foo
*/
',
['tags' => ['param', 'return', 'throws'], 'align' => PhpdocAlignFixer::ALIGN_LEFT],
];
yield 'return and throws' => [
'<?php
/**
* @param EngineInterface $templating
* @param mixed &$reference A parameter passed by reference
* Multiline description
* @throws Bar description bar
* @return Foo description foo
* multiline description
*/
',
'<?php
/**
* @param EngineInterface $templating
* @param mixed &$reference A parameter passed by reference
* Multiline description
* @throws Bar description bar
* @return Foo description foo
* multiline description
*/
',
['tags' => ['param', 'throws', 'return']],
];
// https://github.com/FriendsOfPhp/PHP-CS-Fixer/issues/55
yield 'three params with return' => [
'<?php
/**
* @param string $param1
* @param bool $param2 lorem ipsum
* @param string $param3 lorem ipsum
* @return int lorem ipsum
*/
',
'<?php
/**
* @param string $param1
* @param bool $param2 lorem ipsum
* @param string $param3 lorem ipsum
* @return int lorem ipsum
*/
',
['tags' => ['param', 'return']],
];
yield 'only return' => [
'<?php
/**
* @return Foo description foo
*/
',
'<?php
/**
* @return Foo description foo
*/
',
['tags' => ['return']],
];
yield 'return with $this' => [
'<?php
/**
* @param Foo $foo
* @return $this
*/
',
'<?php
/**
* @param Foo $foo
* @return $this
*/
',
['tags' => ['param', 'return']],
];
yield 'custom annotations stay untouched' => [
'<?php
/**
* @return string
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
',
'<?php
/**
* @return string
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
*/
',
['tags' => ['return']],
];
yield 'custom annotations stay untouched 2' => [
'<?php
class X
{
/**
* @var Collection<Value>|Value[]
* @ORM\ManyToMany(
* targetEntity="\Dl\Component\DomainModel\Product\Value\AbstractValue",
* inversedBy="externalAliases"
* )
*/
private $values;
}
',
null,
['tags' => ['var']],
];
yield 'left align 2' => [
'<?php
/**
* @param int $a
* @param string $b
*
* @dataProvider dataJobCreation
*/
',
'<?php
/**
* @param int $a
* @param string $b
*
* @dataProvider dataJobCreation
*/
',
['tags' => ['param'], 'align' => PhpdocAlignFixer::ALIGN_LEFT],
];
yield 'params and data provider' => [
'<?php
/**
* @param int $a
* @param string|null $b
*
* @dataProvider dataJobCreation
*/
',
'<?php
/**
* @param int $a
* @param string|null $b
*
* @dataProvider dataJobCreation
*/
',
['tags' => ['param']],
];
yield 'var' => [
'<?php
/**
* @var Type
*/
',
'<?php
/**
* @var Type
*/
',
['tags' => ['var']],
];
yield 'type' => [
'<?php
/**
* @type Type
*/
',
'<?php
/**
* @type Type
*/
',
['tags' => ['type']],
];
yield 'var and description' => [
'<?php
/**
* This is a variable.
*
* @var Type
*/
',
'<?php
/**
* This is a variable.
*
* @var Type
*/
',
['tags' => ['var']],
];
yield 'var and inline description' => [
'<?php
/**
* @var Type This is a variable.
*/
',
'<?php
/**
* @var Type This is a variable.
*/
',
['tags' => ['var']],
];
yield 'type and inline description' => [
'<?php
/**
* @type Type This is a variable.
*/
',
'<?php
/**
* @type Type This is a variable.
*/
',
['tags' => ['type']],
];
yield 'when we are not modifying a docblock, then line endings should not change' => [
"<?php\r /**\r * @param Example Hello there!\r */\r",
null,
['tags' => ['param']],
];
yield 'malformed doc block' => [
'<?php
/**
* @return string
* */
',
null,
['tags' => ['return']],
];
yield 'different indentation' => [
'<?php
/**
* @param int $limit
* @param string $more
*
* @return array
*/
/**
* @param int $limit
* @param string $more
*
* @return array
*/
',
'<?php
/**
* @param int $limit
* @param string $more
*
* @return array
*/
/**
* @param int $limit
* @param string $more
*
* @return array
*/
',
['tags' => ['param', 'return']],
];
yield 'different indentation left align' => [
'<?php
/**
* @param int $limit
* @param string $more
*
* @return array
*/
/**
* @param int $limit
* @param string $more
*
* @return array
*/
',
'<?php
/**
* @param int $limit
* @param string $more
*
* @return array
*/
/**
* @param int $limit
* @param string $more
*
* @return array
*/
',
['tags' => ['param', 'return'], 'align' => PhpdocAlignFixer::ALIGN_LEFT],
];
yield 'messy whitespaces 1' => [
"<?php\r\n\t/**\r\n\t * @type Type This is a variable.\r\n\t */",
"<?php\r\n\t/**\r\n\t * @type Type This is a variable.\r\n\t */",
['tags' => ['type']],
new WhitespacesFixerConfig("\t", "\r\n"),
];
yield 'messy whitespaces 2' => [
"<?php\r\n/**\r\n * @param int \$limit\r\n * @param string \$more\r\n *\r\n * @return array\r\n */",
"<?php\r\n/**\r\n * @param int \$limit\r\n * @param string \$more\r\n *\r\n * @return array\r\n */",
['tags' => ['param', 'return']],
new WhitespacesFixerConfig("\t", "\r\n"),
];
yield 'messy whitespaces 3' => [
"<?php\r\n/**\r\n * @param int \$limit\r\n * @param string \$more\r\n *\r\n * @return array\r\n */",
"<?php\r\n/**\r\n * @param int \$limit\r\n * @param string \$more\r\n *\r\n * @return array\r\n */",
[],
new WhitespacesFixerConfig("\t", "\r\n"),
];
yield 'messy whitespaces 4' => [
"<?php\n/**\n * @param int \$a\n * @param int \$b\n * ABC\n */",
"<?php\n/**\n * @param int \$a\n * @param int \$b\n * ABC\n */",
[],
new WhitespacesFixerConfig(' ', "\n"),
];
yield 'messy whitespaces 5' => [
"<?php\r\n/**\r\n * @param int \$z\r\n * @param int \$b\r\n * XYZ\r\n */",
"<?php\r\n/**\r\n * @param int \$z\r\n * @param int \$b\r\n * XYZ\r\n */",
[],
new WhitespacesFixerConfig(' ', "\r\n"),
];
yield 'badly formatted' => [
"<?php\n /**\n * @var Foo */\n",
null,
['tags' => ['var']],
];
yield 'unicode' => [
'<?php
/**
* Method test.
*
* @param int $foobar Description
* @param string $foo Description
* @param mixed $bar Description word_with_ą
* @param int|null $test Description
*/
$a = 1;
/**
* @return string
* @SuppressWarnings(PHPMD.UnusedLocalVariable) word_with_ą
*/
$b = 1;
',
'<?php
/**
* Method test.
*
* @param int $foobar Description
* @param string $foo Description
* @param mixed $bar Description word_with_ą
* @param int|null $test Description
*/
$a = 1;
/**
* @return string
* @SuppressWarnings(PHPMD.UnusedLocalVariable) word_with_ą
*/
$b = 1;
',
['tags' => ['param', 'return']],
];
yield 'does align property by default' => [
'<?php
/**
* @param int $foobar Description
* @return int
* @throws Exception
* @var FooBar
* @type BarFoo
* @property string $foo Hello World
*/
',
'<?php
/**
* @param int $foobar Description
* @return int
* @throws Exception
* @var FooBar
* @type BarFoo
* @property string $foo Hello World
*/
',
[],
];
yield 'aligns property' => [
'<?php
/**
* @param int $foobar Description
* @return int
* @throws Exception
* @var FooBar
* @type BarFoo
* @property string $foo Hello World
*/
',
'<?php
/**
* @param int $foobar Description
* @return int
* @throws Exception
* @var FooBar
* @type BarFoo
* @property string $foo Hello World
*/
',
['tags' => ['param', 'property', 'return', 'throws', 'type', 'var']],
];
yield 'does align method by default' => [
'<?php
/**
* @param int $foobar Description
* @return int
* @throws Exception
* @var FooBar
* @type BarFoo
* @method string foo(string $bar) Hello World
*/
',
'<?php
/**
* @param int $foobar Description
* @return int
* @throws Exception
* @var FooBar
* @type BarFoo
* @method string foo(string $bar) Hello World
*/
',
[],
];
yield 'aligns method' => [
'<?php
/**
* @param int $foobar Description
* @return int
* @throws Exception
* @var FooBar
* @type BarFoo
* @method int foo(string $bar, string ...$things, int &$baz) Description
*/
',
'<?php
/**
* @param int $foobar Description
* @return int
* @throws Exception
* @var FooBar
* @type BarFoo
* @method int foo(string $bar, string ...$things, int &$baz) Description
*/
',
['tags' => ['param', 'method', 'return', 'throws', 'type', 'var']],
];
yield 'aligns method without parameters' => [
'<?php
/**
* @property string $foo Desc
* @method int foo() Description
*/
',
'<?php
/**
* @property string $foo Desc
* @method int foo() Description
*/
',
['tags' => ['method', 'property']],
];
yield 'aligns method without parameters left align' => [
'<?php
/**
* @property string $foo Desc
* @method int foo() Description
*/
',
'<?php
/**
* @property string $foo Desc
* @method int foo() Description
*/
',
['tags' => ['method', 'property'], 'align' => PhpdocAlignFixer::ALIGN_LEFT],
];
yield 'does not format method' => [
'<?php
/**
* @method int foo( string $bar ) Description
*/
',
null,
['tags' => ['method']],
];
yield 'aligns method without return type' => [
'<?php
/**
* @property string $foo Desc
* @method int foo() Description
* @method bar() Descrip
*/
',
'<?php
/**
* @property string $foo Desc
* @method int foo() Description
* @method bar() Descrip
*/
',
['tags' => ['method', 'property']],
];
yield 'aligns methods without return type' => [
'<?php
/**
* @method fooBaz() Description
* @method bar(string $foo) Descrip
*/
',
'<?php
/**
* @method fooBaz() Description
* @method bar(string $foo) Descrip
*/
',
['tags' => ['method']],
];
yield 'aligns static and non-static methods' => [
'<?php
/**
* @property string $foo Desc1
* @property int $bar Desc2
* @method foo(string $foo) DescriptionFoo
* @method static bar(string $foo) DescriptionBar
* @method string|null baz(bool $baz) DescriptionBaz
* @method static int|false qux(float $qux) DescriptionQux
* @method static static quux(int $quux) DescriptionQuux
* @method static $this quuz(bool $quuz) DescriptionQuuz
*/
',
'<?php
/**
* @property string $foo Desc1
* @property int $bar Desc2
* @method foo(string $foo) DescriptionFoo
* @method static bar(string $foo) DescriptionBar
* @method string|null baz(bool $baz) DescriptionBaz
* @method static int|false qux(float $qux) DescriptionQux
* @method static static quux(int $quux) DescriptionQuux
* @method static $this quuz(bool $quuz) DescriptionQuuz
*/
',
['tags' => ['method', 'property']],
];
yield 'aligns static and non-static methods left align' => [
'<?php
/**
* @property string $foo Desc1
* @property int $bar Desc2
* @method foo(string $foo) DescriptionFoo
* @method static bar(string $foo) DescriptionBar
* @method string|null baz(bool $baz) DescriptionBaz
* @method static int|false qux(float $qux) DescriptionQux
* @method static static quux(int $quux) DescriptionQuux
* @method static $this quuz(bool $quuz) DescriptionQuuz
*/
',
'<?php
/**
* @property string $foo Desc1
* @property int $bar Desc2
* @method foo(string $foo) DescriptionFoo
* @method static bar(string $foo) DescriptionBar
* @method string|null baz(bool $baz) DescriptionBaz
* @method static int|false qux(float $qux) DescriptionQux
* @method static static quux(int $quux) DescriptionQuux
* @method static $this quuz(bool $quuz) DescriptionQuuz
*/
',
['tags' => ['method', 'property'], 'align' => PhpdocAlignFixer::ALIGN_LEFT],
];
yield 'aligns return static' => [
'<?php
/**
* @param string $foobar Desc1
* @param int &$baz Desc2
* @param ?Qux $qux Desc3
* @param int|float $quux Desc4
* @return static DescriptionReturn
* @throws Exception DescriptionException
*/
',
'<?php
/**
* @param string $foobar Desc1
* @param int &$baz Desc2
* @param ?Qux $qux Desc3
* @param int|float $quux Desc4
* @return static DescriptionReturn
* @throws Exception DescriptionException
*/
',
['tags' => ['param', 'return', 'throws']],
];
yield 'aligns return static left align' => [
'<?php
/**
* @param string $foobar Desc1
* @param int &$baz Desc2
* @param ?Qux $qux Desc3
* @param int|float $quux Desc4
* @return static DescriptionReturn
* @throws Exception DescriptionException
*/
',
'<?php
/**
* @param string $foobar Desc1
* @param int &$baz Desc2
* @param ?Qux $qux Desc3
* @param int|float $quux Desc4
* @return static DescriptionReturn
* @throws Exception DescriptionException
*/
',
['tags' => ['param', 'return', 'throws'], 'align' => PhpdocAlignFixer::ALIGN_LEFT],
];
yield 'does not align with empty config' => [
'<?php
/**
* @param int $foobar Description
* @return int
* @throws Exception
* @var FooBar
* @type BarFoo
* @property string $foo Hello World
* @method int bar() Description
*/
',
null,
['tags' => []],
];
yield 'variadic params 1' => [
'<?php
final class Sample
{
/**
* @param int[] $a A
* @param int &$b B
* @param array ...$c C
*/
public function sample2($a, &$b, ...$c)
{
}
}
',
'<?php
final class Sample
{
/**
* @param int[] $a A
* @param int &$b B
* @param array ...$c C
*/
public function sample2($a, &$b, ...$c)
{
}
}
',
['tags' => ['param']],
];
yield 'variadic params 2' => [
'<?php
final class Sample
{
/**
* @param int $a
* @param int $b
* @param array[] ...$c
*/
public function sample2($a, $b, ...$c)
{
}
}
',
'<?php
final class Sample
{
/**
* @param int $a
* @param int $b
* @param array[] ...$c
*/
public function sample2($a, $b, ...$c)
{
}
}
',
['tags' => ['param']],
];
yield 'variadic params 3' => [
'<?php
final class Sample
{
/**
* @param int $a
* @param int $b
* @param array[] ...$c
*/
public function sample2($a, $b, ...$c)
{
}
}
',
'<?php
final class Sample
{
/**
* @param int $a
* @param int $b
* @param array[] ...$c
*/
public function sample2($a, $b, ...$c)
{
}
}
',
['tags' => ['param'], 'align' => PhpdocAlignFixer::ALIGN_LEFT],
];
yield 'variadic params 4' => [
'<?php
/**
* @property string $myMagicProperty magic property
* @property-read string $myMagicReadProperty magic read-only property
* @property-write string $myMagicWriteProperty magic write-only property
*/
class Foo {}
',
'<?php
/**
* @property string $myMagicProperty magic property
* @property-read string $myMagicReadProperty magic read-only property
* @property-write string $myMagicWriteProperty magic write-only property
*/
class Foo {}
',
['tags' => ['property', 'property-read', 'property-write']],
];
yield 'invalid PHPDoc 1' => [
'<?php
/**
* @ Security("is_granted(\'CANCEL\', giftCard)")
*/
',
null,
['tags' => ['param', 'return', 'throws', 'type', 'var']],
];
yield 'invalid PHPDoc 2' => [
'<?php
/**
* @ Security("is_granted(\'CANCEL\', giftCard)")
*/
',
null,
['tags' => ['param', 'return', 'throws', 'type', 'var', 'method']],
];
yield 'invalid PHPDoc 3' => [
'<?php
/**
* @ Security("is_granted(\'CANCEL\', giftCard)")
* @ foo bar
* @ foo
*/
',
null,
['tags' => ['param', 'return', 'throws', 'type', 'var']],
];
yield 'types containing callables' => [
'<?php
/**
* @param callable(Foo): Bar $x Description
* @param callable(FooFoo): BarBar $yy Description
*/
',
'<?php
/**
* @param callable(Foo): Bar $x Description
* @param callable(FooFoo): BarBar $yy Description
*/
',
[],
];
yield 'types containing whitespace' => [
'<?php
/**
* @var int $key
* @var iterable<int, string> $value
*/
/**
* @param array<int, $this> $arrayOfIntegers
* @param array<string, $this> $arrayOfStrings
*/
',
null,
[],
];
yield 'closure types containing backslash' => [
'<?php
/**
* @var string $input
* @var \Closure $fn
* @var \Closure(bool):int $fn2
* @var Closure $fn3
* @var Closure(string):string $fn4
* @var array<string, array<string, mixed>> $data
*/
/**
* @param string $input
* @param \Closure $fn
* @param \Closure(bool):int $fn2
* @param Closure $fn3
* @param Closure(string):string $fn4
* @param array<string, array<string, mixed>> $data
*/
/**
* @var string $value
* @var \Closure(string): string $callback
* @var Closure(int): bool $callback2
*/
/**
* @param string $value
* @param \Closure(string): string $callback
* @param Closure(int): bool $callback2
*/
/**
* @var Closure(array<int, bool>): bool $callback1
* @var \Closure(string): string $callback2
*/
/**
* @param Closure(array<int, bool>): bool $callback1
* @param \Closure(string): string $callback2
*/
',
null,
[],
];
yield 'types parenthesized' => [
'<?php
/**
* @param list<string> $allowedTypes
* @param null|list<\Closure(mixed): (bool|null|scalar)> $allowedValues
*/
',
'<?php
/**
* @param list<string> $allowedTypes
* @param null|list<\Closure(mixed): (bool|null|scalar)> $allowedValues
*/
',
[],
];
yield 'callable types with ugly code 1' => [
'<?php
/**
* @var callable $fn
* @var callable(bool): int $fn2
* @var Closure $fn3
* @var Closure(string|object):string $fn4
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | true |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocAddMissingParamAnnotationFixerTest.php | tests/Fixer/Phpdoc/PhpdocAddMissingParamAnnotationFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\ConfigurationException\InvalidConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocAddMissingParamAnnotationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocAddMissingParamAnnotationFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocAddMissingParamAnnotationFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocAddMissingParamAnnotationFixerTest extends AbstractFixerTestCase
{
/**
* @param array<string, mixed> $configuration
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(array $configuration, string $expectedMessage): void
{
$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessageMatches($expectedMessage);
$this->fixer->configure($configuration);
}
/**
* @return iterable<string, array{array<string, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield 'unknown key' => [
['foo' => 'bar'],
'#The option "foo" does not exist\.#',
];
yield 'null' => [
['only_untyped' => null],
'#expected to be of type "bool", but is of type "(null|NULL)"\.$#',
];
yield 'int' => [
['only_untyped' => 1],
'#expected to be of type "bool", but is of type "(int|integer)"\.$#',
];
yield 'array' => [
['only_untyped' => []],
'#expected to be of type "bool", but is of type "array"\.$#',
];
yield 'float' => [
['only_untyped' => 0.1],
'#expected to be of type "bool", but is of type "(float|double)"\.$#',
];
yield 'object' => [
['only_untyped' => new \stdClass()],
'#expected to be of type "bool", but is of type "stdClass"\.$#',
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = [], ?WhitespacesFixerConfig $whitespacesConfig = null): void
{
$this->fixer->configure($configuration);
$this->fixer->setWhitespacesConfig($whitespacesConfig ?? new WhitespacesFixerConfig());
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
/**
*
*/',
null,
['only_untyped' => false],
];
yield [
'<?php
/**
* @param int $foo
* @param mixed $bar
*/
function f1($foo, $bar) {}',
'<?php
/**
* @param int $foo
*/
function f1($foo, $bar) {}',
['only_untyped' => false],
];
yield [
'<?php
/**
* @param int $bar
* @param mixed $foo
*/
function f2($foo, $bar) {}',
'<?php
/**
* @param int $bar
*/
function f2($foo, $bar) {}',
['only_untyped' => false],
];
yield [
'<?php
/**
* @return void
* @param mixed $foo
* @param mixed $bar
*/
function f3($foo, $bar) {}',
'<?php
/**
* @return void
*/
function f3($foo, $bar) {}',
['only_untyped' => false],
];
yield [
'<?php
abstract class Foo {
/**
* @param int $bar
* @param mixed $foo
*/
abstract public function f4a($foo, $bar);
}',
'<?php
abstract class Foo {
/**
* @param int $bar
*/
abstract public function f4a($foo, $bar);
}',
['only_untyped' => false],
];
yield [
'<?php
class Foo {
/**
* @param int $bar
* @param mixed $foo
*/
static final public function f4b($foo, $bar) {}
}',
'<?php
class Foo {
/**
* @param int $bar
*/
static final public function f4b($foo, $bar) {}
}',
['only_untyped' => false],
];
yield [
'<?php
class Foo {
/**
* @var int
*/
private $foo;
}',
null,
['only_untyped' => false],
];
yield [
'<?php
/**
* @param $bar No type !!
* @param mixed $foo
*/
function f5($foo, $bar) {}',
'<?php
/**
* @param $bar No type !!
*/
function f5($foo, $bar) {}',
['only_untyped' => false],
];
yield [
'<?php
/**
* @param int
* @param int $bar
* @param Foo\Bar $foo
*/
function f6(Foo\Bar $foo, $bar) {}',
'<?php
/**
* @param int
* @param int $bar
*/
function f6(Foo\Bar $foo, $bar) {}',
['only_untyped' => false],
];
yield [
'<?php
/**
* @param int $bar
* @param null|string $foo
*/
function f7(string $foo = nuLl, $bar) {}',
'<?php
/**
* @param int $bar
*/
function f7(string $foo = nuLl, $bar) {}',
['only_untyped' => false],
];
yield [
'<?php
/**
* @param int $bar
* @param mixed $baz
*
* @return void
*/
function f9(string $foo, $bar, $baz) {}',
'<?php
/**
* @param int $bar
*
* @return void
*/
function f9(string $foo, $bar, $baz) {}',
['only_untyped' => true],
];
yield [
'<?php
/**
* @param bool|bool[] $caseSensitive Line 1
* Line 2
*/
function f11($caseSensitive) {}
',
null,
['only_untyped' => false],
];
yield [
'<?php
/** @return string */
function hello($string)
{
return $string;
}',
null,
['only_untyped' => false],
];
yield [
'<?php
/** @return string
* @param mixed $string
*/
function hello($string)
{
return $string;
}',
'<?php
/** @return string
*/
function hello($string)
{
return $string;
}',
['only_untyped' => false],
];
yield [
'<?php
/**
* @param mixed $string
* @return string */
function hello($string)
{
return $string;
}',
'<?php
/**
* @return string */
function hello($string)
{
return $string;
}',
['only_untyped' => false],
];
yield [
'<?php
/**
* @param int $bar
* @param string $foo
*/
function f8(string $foo = "null", $bar) {}',
'<?php
/**
* @param int $bar
*/
function f8(string $foo = "null", $bar) {}',
['only_untyped' => false],
];
yield [
'<?php
/**
* @{inheritdoc}
*/
function f10(string $foo = "null", $bar) {}',
];
yield [
'<?php
/**
* @inheritDoc
*/
function f10(string $foo = "null", $bar) {}',
null,
['only_untyped' => false],
];
yield [
'<?php
/**
* @param int $bar
* @param ?array $foo
*/
function p1(?array $foo = null, $bar) {}',
'<?php
/**
* @param int $bar
*/
function p1(?array $foo = null, $bar) {}',
['only_untyped' => false],
];
yield [
'<?php
/**
* Foo
* @param mixed $bar
*/
function p1(?int $foo = 0, $bar) {}',
'<?php
/**
* Foo
*/
function p1(?int $foo = 0, $bar) {}',
['only_untyped' => true],
];
yield [
'<?php
/**
* Foo
* @return int
*/
function p1(?int $foo = 0) {}',
null,
['only_untyped' => true],
];
yield [
"<?php\r\n\t/**\r\n\t * @param int \$bar\r\n\t * @param null|string \$foo\r\n\t */\r\n\tfunction f7(string \$foo = nuLl, \$bar) {}",
"<?php\r\n\t/**\r\n\t * @param int \$bar\r\n\t */\r\n\tfunction f7(string \$foo = nuLl, \$bar) {}",
['only_untyped' => false],
new WhitespacesFixerConfig("\t", "\r\n"),
];
yield [
'<?php
/**
* something
* @param mixed $numbers
*/
function add(&$numbers) {}
',
'<?php
/**
* something
*/
function add(&$numbers) {}
',
['only_untyped' => false],
];
yield [
'<?php
/**
* something
* @param null|array $numbers
*/
function add(array &$numbers = null) {}
',
'<?php
/**
* something
*/
function add(array &$numbers = null) {}
',
['only_untyped' => false],
];
yield [
'<?php
/**
* something
* @param array $numbers
*/
function sum(...$numbers) {}
',
'<?php
/**
* something
*/
function sum(...$numbers) {}
',
['only_untyped' => false],
];
yield [
'<?php
/**
* @param int $a
* @param array $numbers
*/
function sum($a, ...$numbers) {}
',
'<?php
/**
* @param int $a
*/
function sum($a, ...$numbers) {}
',
['only_untyped' => false],
];
yield [
'<?php
/**
* @param \Date[] $numbers
*/
function sum(\Date ...$numbers) {}
',
'<?php
/**
*/
function sum(\Date ...$numbers) {}
',
['only_untyped' => false],
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->fixer->configure(['only_untyped' => false]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php class Foo {
/**
* @param Bar $x
* @param ?Bar $y
* @param null|Bar $z
*/
public function __construct(
public Bar $x,
protected ?Bar $y,
private null|Bar $z,
) {}
}',
'<?php class Foo {
/**
*/
public function __construct(
public Bar $x,
protected ?Bar $y,
private null|Bar $z,
) {}
}',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->fixer->configure(['only_untyped' => false]);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php class Foo {
/**
* @param Bar $bar
* @param Baz $baz
*/
public function __construct(
public readonly Bar $bar,
readonly public Baz $baz,
) {}
}',
'<?php class Foo {
/**
*/
public function __construct(
public readonly Bar $bar,
readonly public Baz $baz,
) {}
}',
];
}
/**
* @dataProvider provideFix84Cases
*
* @requires PHP 8.4
*/
public function testFix84(string $expected, ?string $input = null): void
{
$this->testFix($expected, $input, ['only_untyped' => false]);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix84Cases(): iterable
{
yield 'asymmetric visibility' => [
<<<'PHP'
<?php class Foo {
/**
* @param bool $a
* @param bool $b
* @param bool $c
*/
public function __construct(
public public(set) bool $a,
public protected(set) bool $b,
public private(set) bool $c,
) {}
}
PHP,
<<<'PHP'
<?php class Foo {
/**
*/
public function __construct(
public public(set) bool $a,
public protected(set) bool $b,
public private(set) bool $c,
) {}
}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocVarAnnotationCorrectOrderFixerTest.php | tests/Fixer/Phpdoc/PhpdocVarAnnotationCorrectOrderFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocVarAnnotationCorrectOrderFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocVarAnnotationCorrectOrderFixer>
*
* @author Kuba Werłos <werlos@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocVarAnnotationCorrectOrderFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [ // It's @param, we care only about @var
'<?php /** @param $foo Foo */',
];
yield [ // This is already fine
'<?php /** @var Foo $foo */ ',
];
yield [ // What? Two variables, I'm not touching this
'<?php /** @var $foo $bar */',
];
yield [ // Two classes are not to touch either
'<?php /** @var Foo Bar */',
];
yield ['<?php /** @var */'];
yield ['<?php /** @var $foo */'];
yield ['<?php /** @var Bar */'];
yield [
'<?php
/**
* @var Foo $foo
* @var Bar $bar
*/
',
'<?php
/**
* @var $foo Foo
* @var $bar Bar
*/
',
];
yield [
'<?php
/**
* @var Foo $foo Some description
*/
',
'<?php
/**
* @var $foo Foo Some description
*/
',
];
yield [
'<?php /** @var Foo $foo */',
'<?php /** @var $foo Foo */',
];
yield [
'<?php /** @type Foo $foo */',
'<?php /** @type $foo Foo */',
];
yield [
'<?php /** @var Foo $foo*/',
'<?php /** @var $foo Foo*/',
];
yield [
'<?php /** @var Foo[] $foos */',
'<?php /** @var $foos Foo[] */',
];
yield [
'<?php /** @Var Foo $foo */',
'<?php /** @Var $foo Foo */',
];
yield [
'<?php
/** @var Foo|Bar|mixed|int $someWeirdLongNAME__123 */
',
'<?php
/** @var $someWeirdLongNAME__123 Foo|Bar|mixed|int */
',
];
yield [
'<?php
/**
* @var Foo $bar long description
* goes here
*/
',
'<?php
/**
* @var $bar Foo long description
* goes here
*/
',
];
yield [
'<?php
/** @var array<int, int> $foo */
',
'<?php
/** @var $foo array<int, int> */
',
];
yield [
'<?php
/** @var array<int, int> $foo Array of something */
',
'<?php
/** @var $foo array<int, int> Array of something */
',
];
yield [
'<?php
/** @var Foo|array<int, int>|null $foo */
',
'<?php
/** @var $foo Foo|array<int, int>|null */
',
];
yield [
'<?php
class Foo
{
/**
* @var $bar
*/
private $bar;
}
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/GeneralPhpdocAnnotationRemoveFixerTest.php | tests/Fixer/Phpdoc/GeneralPhpdocAnnotationRemoveFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer>
*
* @author Gert de Pagter
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocAnnotationRemoveFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class GeneralPhpdocAnnotationRemoveFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'An Annotation gets removed' => [
'<?php
/**
* @internal
*/
function hello($name)
{
return "hello " . $name;
}',
'<?php
/**
* @internal
* @param string $name
*/
function hello($name)
{
return "hello " . $name;
}',
['annotations' => ['param']],
];
yield 'It removes multiple annotations' => [
'<?php
/**
* @author me
* @internal
*/
function hello($name)
{
return "hello " . $name;
}',
'<?php
/**
* @author me
* @internal
* @param string $name
* @return string
* @throws \Exception
*/
function hello($name)
{
return "hello " . $name;
}',
['annotations' => ['param', 'return', 'throws']],
];
yield 'It does nothing if no configuration is given' => [
'<?php
/**
* @author me
* @internal
* @param string $name
* @return string
* @throws \Exception
*/
function hello($name)
{
return "hello " . $name;
}',
];
yield 'It works on multiple functions' => [
'<?php
/**
* @param string $name
* @throws \Exception
*/
function hello($name)
{
return "hello " . $name;
}
/**
*/
function goodBye()
{
return 0;
}
function noComment()
{
callOtherFunction();
}',
'<?php
/**
* @author me
* @internal
* @param string $name
* @return string
* @throws \Exception
*/
function hello($name)
{
return "hello " . $name;
}
/**
* @internal
* @author Piet-Henk
* @return int
*/
function goodBye()
{
return 0;
}
function noComment()
{
callOtherFunction();
}',
['annotations' => ['author', 'return', 'internal']],
];
yield 'Nothing happens to non doc-block comments' => [
'<?php
/*
* @internal
* @param string $name
*/
function hello($name)
{
return "hello " . $name;
}',
null,
['annotations' => ['internal', 'param', 'return']],
];
yield 'Nothing happens if to be deleted annotations are not present' => [
'<?php
/**
* @internal
* @param string $name
*/
function hello($name)
{
return "hello " . $name;
}',
null,
['annotations' => ['author', 'test', 'return', 'deprecated']],
];
yield [
'<?php
while ($something = myFunction($foo)) {}
',
'<?php
/** @noinspection PhpAssignmentInConditionInspection */
while ($something = myFunction($foo)) {}
',
['annotations' => ['noinspection']],
];
yield [
'<?php
/**
* @internal
* @AuThOr Jane Doe
*/
function foo() {}',
'<?php
/**
* @internal
* @author John Doe
* @AuThOr Jane Doe
*/
function foo() {}',
['annotations' => ['author'], 'case_sensitive' => true],
];
yield [
'<?php
/**
* @internal
*/
function foo() {}',
'<?php
/**
* @internal
* @author John Doe
* @AuThOr Jane Doe
*/
function foo() {}',
['annotations' => ['author'], 'case_sensitive' => false],
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocAnnotationWithoutDotFixerTest.php | tests/Fixer/Phpdoc/PhpdocAnnotationWithoutDotFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocAnnotationWithoutDotFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocAnnotationWithoutDotFixer>
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocAnnotationWithoutDotFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
/**
* Summary.
*
* Description.
*
* @param string|null $str some string
* @param string $ip IPv4 is not lowercased
* @param string $a A
* @param string $a_string a string
* @param string $ab ab
* @param string $t34 T34
* @param string $s S§
* @param string $genrb Optional. The path to the "genrb" executable
* @param string $ellipsis1 Ellipsis is this: ...
* @param string $ellipsis2 Ellipsis is this: 。。。
* @param string $ellipsis3 Ellipsis is this: …
* @param bool $isStr Is it a string?
* @param int $int Some single-line description. With many dots.
* @param int $int Some multiline
* description. With many dots.
*
* @return array result array
*
* @SomeCustomAnnotation This is important sentence that must not be modified.
*/',
'<?php
/**
* Summary.
*
* Description.
*
* @param string|null $str Some string.
* @param string $ip IPv4 is not lowercased.
* @param string $a A.
* @param string $a_string A string.
* @param string $ab Ab.
* @param string $t34 T34.
* @param string $s S§.
* @param string $genrb Optional. The path to the "genrb" executable
* @param string $ellipsis1 Ellipsis is this: ...
* @param string $ellipsis2 Ellipsis is this: 。。。
* @param string $ellipsis3 Ellipsis is this: …
* @param bool $isStr Is it a string?
* @param int $int Some single-line description. With many dots.
* @param int $int Some multiline
* description. With many dots.
*
* @return array Result array。
*
* @SomeCustomAnnotation This is important sentence that must not be modified.
*/',
];
yield [
// invalid char inside line won't crash the fixer
'<?php
/**
* @var string this: '.\chr(174).' is an odd character
* @var string This: '.\chr(174).' is an odd character 2nd time。
*/',
'<?php
/**
* @var string This: '.\chr(174).' is an odd character.
* @var string This: '.\chr(174).' is an odd character 2nd time。
*/',
];
yield [
'<?php
/**
* @deprecated since version 2. Use emergency() which is PSR-3 compatible.
*/',
];
yield [
'<?php
/**
* @internal This method is public to be usable as callback. It should not
* be used in user code.
*/',
];
yield [
'<?php
/**
* @deprecated this is
* deprecated
*/',
'<?php
/**
* @deprecated This is
* deprecated.
*/',
];
yield [
'<?php
/**
* @return bool|null returns `true` if the class has a single-column ID
* and Returns `false` otherwise
*/',
'<?php
/**
* @return bool|null Returns `true` if the class has a single-column ID
* and Returns `false` otherwise.
*/',
];
yield [
'<?php
/**
* @throws \Exception having whitespaces after dot, yet I am fixed
*/',
'<?php
/**
* @throws \Exception having whitespaces after dot, yet I am fixed. '.'
*/',
];
yield [
'<?php
/**
* @throws \Exception having tabs after dot, yet I am fixed
*/',
'<?php
/**
* @throws \Exception having tabs after dot, yet I am fixed. '.'
*/',
];
yield [
'<?php
/**
* Dispatches an event to all registered listeners.
*
* @param string $eventName The name of the event to dispatch. The name of the event is
* the name of the method that is invoked on listeners.
* @param EventArgs $eventArgs The event arguments to pass to the event handlers/listeners.
* If not supplied, the single empty EventArgs instance is used.
*
* @return bool
*/
function dispatchEvent($eventName, EventArgs $eventArgs = null) {}
/**
* Extract the `object_to_populate` field from the context if it exists
* and is an instance of the provided $class.
*
* @param string $class The class the object should be
* @param array $context The denormalization context
* @param string $key Key in which to look for the object to populate.
* Keeps backwards compatibility with `AbstractNormalizer`.
*
* @return null|object an object if things check out, null otherwise
*/
function extractObjectToPopulate($class, array $context, $key = null) {}
',
];
yield [
'<?php
/**
* This is a broken phpdoc - missing asterisk
* @param string $str As it is broken, let us not apply the rule to description of parameter.
*/
function foo($str) {}',
];
yield [
'<?php
/**
* @return bool|null Returns `true` if the class has a single-column ID.
Returns `false` otherwise.
That was multilined comment. With plenty of sentenced.
*/
function nothingToDo() {}',
];
yield [
'<?php
/**
* @param string $bar τάχιστη
*/
function foo ($bar) {}
',
'<?php
/**
* @param string $bar Τάχιστη.
*/
function foo ($bar) {}
',
];
yield [
'<?php
/**
* @param Url $url URL is not lowercased
*/',
'<?php
/**
* @param Url $url URL is not lowercased.
*/',
];
yield [
'<?php
/**
* @param Url URL is not lowercased and note that name of param is ommited
*/',
'<?php
/**
* @param Url URL is not lowercased and note that name of param is ommited.
*/',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocVarWithoutNameFixerTest.php | tests/Fixer/Phpdoc/PhpdocVarWithoutNameFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocVarWithoutNameFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocVarWithoutNameFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocVarWithoutNameFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
$expected = str_replace('@var', '@type', $expected);
if (null !== $input) {
$input = str_replace('@var', '@type', $input);
}
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'testFixVar' => [
<<<'EOF'
<?php
class Foo
{
/**
* @var string Hello!
*/
public $foo;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/**
* @var string $foo Hello!
*/
public $foo;
}
EOF,
];
yield 'testFixType' => [
<<<'EOF'
<?php
class Foo
{
/**
* @var int|null
*/
public $bar;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/**
* @var int|null $bar
*/
public $bar;
}
EOF,
];
yield 'testDoNothing' => [
<<<'EOF'
<?php
class Foo
{
/**
* @var Foo\Bar This is a variable.
*/
public $bar;
}
EOF,
];
yield 'testFixVarWithNestedKeys' => [
<<<'EOF'
<?php
class Foo
{
/**
* @var array {
* @var bool $required Whether this element is required
* @var string $label The display name for this element
* }
*/
public $options;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/**
* @var array $options {
* @var bool $required Whether this element is required
* @var string $label The display name for this element
* }
*/
public $options;
}
EOF,
];
yield 'testSingleLine' => [
<<<'EOF'
<?php
class Foo
{
/** @var Foo\Bar */
public $bar;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/** @var Foo\Bar $bar */
public $bar;
}
EOF,
];
yield 'testSingleLineProtected' => [
<<<'EOF'
<?php
class Foo
{
/** @var Foo\Bar */
protected $bar;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/** @var Foo\Bar $bar */
protected $bar;
}
EOF,
];
yield 'testSingleLinePrivate' => [
<<<'EOF'
<?php
class Foo
{
/** @var Foo\Bar */
private $bar;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/** @var Foo\Bar $bar */
private $bar;
}
EOF,
];
yield 'testSingleLineVar' => [
<<<'EOF'
<?php
class Foo
{
/** @var Foo\Bar */
var $bar;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/** @var Foo\Bar $bar */
var $bar;
}
EOF,
];
yield 'testSingleLineStatic' => [
<<<'EOF'
<?php
class Foo
{
/** @var Foo\Bar */
static public $bar;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/** @var Foo\Bar $bar */
static public $bar;
}
EOF,
];
yield 'testSingleLineNoSpace' => [
<<<'EOF'
<?php
class Foo
{
/** @var Foo\Bar*/
public $bar;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/** @var Foo\Bar $bar*/
public $bar;
}
EOF,
];
yield 'testInlineDoc' => [
<<<'EOF'
<?php
class Foo
{
/**
* Initializes this class with the given options.
*
* @param array $options {
* @var bool $required Whether this element is required
* @var string $label The display name for this element
* }
*/
public function init($options)
{
// Do something
}
}
EOF,
];
yield 'testSingleLineNoProperty' => [
<<<'EOF'
<?php
/** @var Foo\Bar $bar */
$bar;
EOF,
];
yield 'testMultiLineNoProperty' => [
<<<'EOF'
<?php
/**
* @var Foo\Bar $bar
*/
$bar;
EOF,
];
yield 'testVeryNestedInlineDoc' => [
<<<'EOF'
<?php
class Foo
{
/**
* @var array {
* @var array $secondLevelOne {
* {@internal This should not break}
* @var int $thirdLevel
* }
* @var array $secondLevelTwo {
* @var array $thirdLevel {
* @var string $fourthLevel
* }
* @var int $moreThirdLevel
* }
* @var int $secondLevelThree
* }
*/
public $nestedFoo;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/**
* @var array $nestedFoo {
* @var array $secondLevelOne {
* {@internal This should not break}
* @var int $thirdLevel
* }
* @var array $secondLevelTwo {
* @var array $thirdLevel {
* @var string $fourthLevel
* }
* @var int $moreThirdLevel
* }
* @var int $secondLevelThree
* }
*/
public $nestedFoo;
}
EOF,
];
yield [
'<?php
class Foo
{
/**
* @no_candidate string Hello!
*/
public $foo;
}
',
];
yield [
'<?php
class Foo{}
/** */',
];
yield 'anonymousClass' => [
<<<'EOF'
<?php
class Anon
{
public function getNewAnon()
{
return new class()
{
/**
* @var string
*/
public $stringVar;
public function getNewAnon()
{
return new class()
{
/**
* @var string
*/
public $stringVar;
};
}
};
}
}
EOF,
<<<'EOF'
<?php
class Anon
{
public function getNewAnon()
{
return new class()
{
/**
* @var $stringVar string
*/
public $stringVar;
public function getNewAnon()
{
return new class()
{
/**
* @var $stringVar string
*/
public $stringVar;
};
}
};
}
}
EOF,
];
yield [
'<?php
/**
* Header
*/
class A {} // for the candidate check
/**
* @var ClassLoader $loader
*/
$loader = require __DIR__.\'/../vendor/autoload.php\';
/**
* @var \Foo\Bar $bar
*/
$bar->doSomething(1);
/**
* @var $bar \Foo\Bar
*/
$bar->doSomething(2);
/**
* @var User $bar
*/
($bar = tmp())->doSomething(3);
/**
* @var User $bar
*/
list($bar) = a();
',
];
yield 'const are not handled by this fixer' => [
'<?php
class A
{
/**
* @var array<string, true> SKIPPED_TYPES
*/
private const SKIPPED_TYPES = ["a" => true];
}
',
];
yield 'trait' => [
'<?php
trait StaticExample {
/**
* @var string Hello!
*/
public static $static = "foo";
}',
'<?php
trait StaticExample {
/**
* @var string $static Hello!
*/
public static $static = "foo";
}',
];
yield 'complex type with union containing callable that has `$this` in signature' => [
<<<'EOF'
<?php
class Foo
{
/**
* @var array<string, string|array{ string|\Closure(mixed, string, $this): int|float }>|false Hello!
*/
public $foo;
/** @var int Hello! */
public $foo2;
/** @var int Hello! */
public $foo3;
/** @var int Hello! */
public $foo4;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/**
* @var array<string, string|array{ string|\Closure(mixed, string, $this): int|float }>|false $foo Hello!
*/
public $foo;
/** @var int $thi Hello! */
public $foo2;
/** @var int $thiss Hello! */
public $foo3;
/** @var int $this2 Hello! */
public $foo4;
}
EOF,
];
yield 'testFixMultibyteVariableName' => [
<<<'EOF'
<?php
class Foo
{
/** @var int Hello! */
public $foo;
/** @var 🚀 🚀 */
public $foo2;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/** @var int $my🚀 Hello! */
public $foo;
/** @var 🚀 $my 🚀 */
public $foo2;
}
EOF,
];
yield '@var with callable syntax' => [
<<<'EOF'
<?php
class Foo
{
/** @var array<callable(string, Buzz): void> */
protected $bar;
}
EOF,
<<<'EOF'
<?php
class Foo
{
/** @var array<callable(string $baz, Buzz $buzz): void> */
protected $bar;
}
EOF,
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'readonly' => [
'<?php
class Foo
{
/** @var Foo */
public $bar1;
/** @var Foo */
public readonly int $bar2;
/** @var Foo */
readonly public int $bar3;
/** @var Foo */
readonly int $bar4;
}',
'<?php
class Foo
{
/** @var Foo $bar1 */
public $bar1;
/** @var Foo $bar2 */
public readonly int $bar2;
/** @var Foo $bar3 */
readonly public int $bar3;
/** @var Foo $bar4 */
readonly int $bar4;
}',
];
yield 'final public const are not handled by this fixer' => [
'<?php
class A
{
/**
* @var array<string, true> SKIPPED_TYPES
*/
final public const SKIPPED_TYPES = ["a" => true];
}
',
];
}
/**
* @dataProvider provideFix84Cases
*
* @requires PHP 8.4
*/
public function testFix84(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix84Cases(): iterable
{
yield 'asymmetric visibility' => [
<<<'PHP'
<?php class Foo
{
/** @var bool */
public(set) bool $a;
/** @var bool */
protected(set) bool $b;
/** @var bool */
private(set) bool $c;
}
PHP,
<<<'PHP'
<?php class Foo
{
/** @var bool $a */
public(set) bool $a;
/** @var bool $b */
protected(set) bool $b;
/** @var bool $c */
private(set) bool $c;
}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocParamOrderFixerTest.php | tests/Fixer/Phpdoc/PhpdocParamOrderFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocParamOrderFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocParamOrderFixer>
*
* @author Jonathan Gruber <gruberjonathan@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocParamOrderFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'no changes' => [<<<'EOT'
<?php
class C {
/**
* @param $a
*/
public function m($a) {}
}
EOT];
yield 'no changes multiline' => [<<<'EOT'
<?php
class C {
/**
* @param string $a
* @param bool $b
*/
public function m($a, $b) {}
}
EOT];
yield 'only params untyped' => [
<<<'EOT'
<?php
class C {
/**
* @param $a
* @param $b
* @param $c
* @param $d
* @param $e
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* @param $b
* @param $e
* @param $a
* @param $c
* @param $d
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
];
yield 'only params untyped mixed' => [
<<<'EOT'
<?php
class C {
/**
* @param int $a
* @param $b
* @param $c
* @param bool $d
* @param $e
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* @param $c
* @param $e
* @param int $a
* @param $b
* @param bool $d
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
];
yield 'only params typed' => [
<<<'EOT'
<?php
class C {
/**
* @param string $a
* @param bool $b
* @param string $c
* @param string $d
* @param int $e
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* @param bool $b
* @param string $a
* @param string $c
* @param int $e
* @param string $d
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
];
yield 'only params undocumented' => [
<<<'EOT'
<?php
class C {
/**
* @param $a
* @param $b
* @param $c
* @param $d
*/
public function m($a, $b, $c, $d, $e, $f) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* @param $a
* @param $c
* @param $d
* @param $b
*/
public function m($a, $b, $c, $d, $e, $f) {}
}
EOT,
];
yield 'only params superfluous annotation' => [<<<'EOT'
<?php
class C {
/**
* @param $a
* @param $b
* @param $c
* @param $superfluous
*/
public function m($a, $b, $c) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* @param $a
* @param $superfluous
* @param $b
* @param $c
*/
public function m($a, $b, $c) {}
}
EOT,
];
yield 'only params superfluous annotations' => [
<<<'EOT'
<?php
class C {
/**
* @param $a
* @param $b
* @param $c
* @param $superfluous2
* @param $superfluous1
* @param $superfluous3
*/
public function m($a, $b, $c) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* @param $a
* @param $superfluous2
* @param $b
* @param $superfluous1
* @param $c
* @param $superfluous3
*/
public function m($a, $b, $c) {}
}
EOT,
];
yield 'params untyped' => [<<<'EOT'
<?php
class C {
/**
* Some function
*
* @param $a
* @param $b
* @param $c
*
* @throws \Exception
*
* @return bool
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* Some function
*
* @param $b
* @param $c
* @param $a
*
* @throws \Exception
*
* @return bool
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
];
yield 'params typed' => [
<<<'EOT'
<?php
class C {
/**
* Some function
*
* @param Foo $a
* @param int $b
* @param bool $c
*
* @throws \Exception
*
* @return bool
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* Some function
*
* @param int $b
* @param bool $c
* @param Foo $a
*
* @throws \Exception
*
* @return bool
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
];
yield 'params description' => [<<<'EOT'
<?php
class C {
/**
* Some function
*
* @param Foo $a A parameter
* @param int $b B parameter
* @param bool $c C parameter
*
* @throws \Exception
*
* @return bool
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* Some function
*
* @param int $b B parameter
* @param bool $c C parameter
* @param Foo $a A parameter
*
* @throws \Exception
*
* @return bool
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
];
yield 'params multiline description' => [<<<'EOT'
<?php
class C {
/**
* Some function
*
* @param Foo $a A parameter
* @param int $b B parameter
* @param bool $c Another multiline, longer
* description of C parameter
* @param bool $d Multiline description
* of D parameter
*
* @throws \Exception
*
* @return bool
*/
public function m($a, $b, $c, $d) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* Some function
*
* @param int $b B parameter
* @param bool $d Multiline description
* of D parameter
* @param bool $c Another multiline, longer
* description of C parameter
* @param Foo $a A parameter
*
* @throws \Exception
*
* @return bool
*/
public function m($a, $b, $c, $d) {}
}
EOT,
];
yield 'complex types' => [<<<'EOT'
<?php
class C {
/**
* @param Foo[]|\Bar\Baz $a
* @param Foo|Bar $b
* @param array<int, FooInterface>|string $c
* @param array<array{int, int}> $d
* @param ?Foo $e
* @param \Closure(( $b is Bar ? bool : int)): $this $f
*/
public function m($a, $b, $c, $d, $e, $f) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* @param array<int, FooInterface>|string $c
* @param Foo|Bar $b
* @param array<array{int, int}> $d
* @param ?Foo $e
* @param \Closure(( $b is Bar ? bool : int)): $this $f
* @param Foo[]|\Bar\Baz $a
*/
public function m($a, $b, $c, $d, $e, $f) {}
}
EOT,
];
yield 'various method declarations' => [<<<'EOT'
<?php
abstract class C {
/**
* @param Foo $a
* @param array $b
* @param $c
* @param mixed $d
*/
final public static function m1(Foo $a, array $b, $c, $d) {}
/**
* @param array $a
* @param $b
* @throws Exception
*
* @return bool
*/
abstract public function m2(array $a, $b);
/**
* Description of
* method
*
* @param int $a
* @param Foo $b
* @param $c
* @param Bar $d
* @param string $e
*/
protected static function m3($a, Foo $b, $c, Bar $d, $e) {}
/**
* @see Something
*
* @param callable $a
* @param $b
* @param array $c
* @param array $d
*
* @return int
*
* Text
*/
final protected function m4(Callable $a, $b, array $c, array $d) {}
/**
* @param Bar $a
* @param Bar $b
* @param $c
* @param int $d
* @param array $e
* @param $f
*
* @return Foo|null
*/
abstract protected function m5(Bar $a, Bar $b, $c, $d, array $e, $f);
/**
* @param array $a
* @param $b
*/
private function m6(array $a, $b) {}
/**
* @param Foo $a
* @param array $b
* @param mixed $c
*/
private static function m7(Foo $a, array $b, $c) {}
}
EOT,
<<<'EOT'
<?php
abstract class C {
/**
* @param array $b
* @param Foo $a
* @param mixed $d
* @param $c
*/
final public static function m1(Foo $a, array $b, $c, $d) {}
/**
* @param $b
* @param array $a
* @throws Exception
*
* @return bool
*/
abstract public function m2(array $a, $b);
/**
* Description of
* method
*
* @param string $e
* @param int $a
* @param Foo $b
* @param Bar $d
* @param $c
*/
protected static function m3($a, Foo $b, $c, Bar $d, $e) {}
/**
* @see Something
*
* @param $b
* @param array $d
* @param array $c
* @param callable $a
*
* @return int
*
* Text
*/
final protected function m4(Callable $a, $b, array $c, array $d) {}
/**
* @param Bar $b
* @param $f
* @param int $d
* @param array $e
* @param $c
* @param Bar $a
*
* @return Foo|null
*/
abstract protected function m5(Bar $a, Bar $b, $c, $d, array $e, $f);
/**
* @param $b
* @param array $a
*/
private function m6(array $a, $b) {}
/**
* @param array $b
* @param mixed $c
* @param Foo $a
*/
private static function m7(Foo $a, array $b, $c) {}
}
EOT,
];
yield 'params with other annotations in between' => [<<<'EOT'
<?php
/**
* [c1] Method description
* [c2] over multiple lines
*
* @see Baz
*
* @param int $a Long param
* description
* @param mixed $b
* @param mixed $superfluous1 With text
* @param int $superfluous2
* @return array Long return
* description
* @throws Exception
* @throws FooException
*/
function foo($a, $b) {}
EOT,
<<<'EOT'
<?php
/**
* [c1] Method description
* [c2] over multiple lines
*
* @see Baz
*
* @param mixed $b
* @param mixed $superfluous1 With text
* @return array Long return
* description
* @param int $superfluous2
* @throws Exception
* @param int $a Long param
* description
* @throws FooException
*/
function foo($a, $b) {}
EOT,
];
yield 'params blank lines' => [<<<'EOT'
<?php
class C {
/**
* Some function
*
* @param $a
* @param $b
*
* @param $c
*
*
* @param $d
*
* @param $e
*
* @throws \Exception
*
* @return bool
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* Some function
*
* @param $b
* @param $e
*
* @param $c
*
*
* @param $a
*
* @param $d
*
* @throws \Exception
*
* @return bool
*/
public function m($a, $b, $c, $d, $e) {}
}
EOT,
];
yield 'nested phpdoc' => [<<<'EOT'
<?php
/**
* @param string[] $array
* @param callable $callback {
* @param string $value
* @param int $key
* @param mixed $userdata
* }
* @param mixed $userdata
*
* @return bool
*/
function string_array_walk(array &$array, callable $callback, $userdata = null) {}
EOT,
<<<'EOT'
<?php
/**
* @param callable $callback {
* @param string $value
* @param int $key
* @param mixed $userdata
* }
* @param mixed $userdata
* @param string[] $array
*
* @return bool
*/
function string_array_walk(array &$array, callable $callback, $userdata = null) {}
EOT,
];
yield 'multi nested phpdoc' => [<<<'EOT'
<?php
/**
* @param string[] $a
* @param callable $b {
* @param string $a
* @param callable {
* @param string $d
* @param int $a
* @param callable $c {
* $param string $e
* }
* }
* @param mixed $b2
* }
* @param mixed $c
* @param int $d
*
* @return bool
*/
function m(array &$a, callable $b, $c = null, $d) {}
EOT,
<<<'EOT'
<?php
/**
* @param mixed $c
* @param callable $b {
* @param string $a
* @param callable {
* @param string $d
* @param int $a
* @param callable $c {
* $param string $e
* }
* }
* @param mixed $b2
* }
* @param int $d
* @param string[] $a
*
* @return bool
*/
function m(array &$a, callable $b, $c = null, $d) {}
EOT,
];
yield 'multiple nested phpdoc' => [<<<'EOT'
<?php
/**
* @param string[] $array
* @param callable $callback {
* @param string $value
* @param int $key
* @param mixed $userdata {
* $param array $array
* }
* }
* @param mixed $userdata
* @param callable $foo {
* @param callable {
* @param string $inner1
* @param int $inner2
* }
* @param mixed $userdata
* }
* @param $superfluous1 Superfluous
* @param $superfluous2 Superfluous
*
* @return bool
*/
function string_array_walk(array &$array, callable $callback, $userdata = null, $foo) {}
EOT,
<<<'EOT'
<?php
/**
* @param $superfluous1 Superfluous
* @param callable $callback {
* @param string $value
* @param int $key
* @param mixed $userdata {
* $param array $array
* }
* }
* @param $superfluous2 Superfluous
* @param callable $foo {
* @param callable {
* @param string $inner1
* @param int $inner2
* }
* @param mixed $userdata
* }
* @param mixed $userdata
* @param string[] $array
*
* @return bool
*/
function string_array_walk(array &$array, callable $callback, $userdata = null, $foo) {}
EOT,
];
yield 'non-matching param name' => [<<<'EOT'
<?php
/**
* @param Foo $fooBar
* @param $fooSomethingNotMatchingTheName
* @param OtherClassLorem $x
*/
function f(Foo $fooBar, Payment $foo, OtherClassLoremIpsum $y) {}
EOT,
<<<'EOT'
<?php
/**
* @param $fooSomethingNotMatchingTheName
* @param Foo $fooBar
* @param OtherClassLorem $x
*/
function f(Foo $fooBar, Payment $foo, OtherClassLoremIpsum $y) {}
EOT,
];
yield 'plain function' => [<<<'EOT'
<?php
/**
* A plain function
*
* @param $a
* @param $b
* @param $c
* @param $d
*/
function m($a, $b, $c, $d) {}
EOT,
<<<'EOT'
<?php
/**
* A plain function
*
* @param $c
* @param $b
* @param $d
* @param $a
*/
function m($a, $b, $c, $d) {}
EOT,
];
yield 'comments in signature' => [<<<'EOT'
<?php
class C {
/**
* @param $a
* @param $b
* @param $c
* @param $d
*/
public/*1*/function/*2*/m/*3*/(/*4*/$a, $b,/*5*/$c, $d){}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* @param $d
* @param $a
* @param $b
* @param $c
*/
public/*1*/function/*2*/m/*3*/(/*4*/$a, $b,/*5*/$c, $d){}
}
EOT,
];
yield 'closure' => [<<<'EOT'
<?php
/**
* @param array $a
* @param $b
* @param Foo $c
* @param int $d
*/
$closure = function (array $a, $b, Foo $c, $d) {};
EOT,
<<<'EOT'
<?php
/**
* @param $b
* @param int $d
* @param Foo $c
* @param array $a
*/
$closure = function (array $a, $b, Foo $c, $d) {};
EOT,
];
yield 'arrow function' => [<<<'EOT'
<?php
/**
* @param array $a
* @param $b
* @param Foo $c
* @param int $d
*/
$closure = fn (array $a, $b, Foo $c, $d) => null;
EOT,
<<<'EOT'
<?php
/**
* @param $b
* @param int $d
* @param Foo $c
* @param array $a
*/
$closure = fn (array $a, $b, Foo $c, $d) => null;
EOT,
];
yield 'interface' => [<<<'EOT'
<?php
Interface I
{
/**
* @param string $a
* @param array $b
* @param Foo $c
*
* @return int|null
*/
public function foo($a, array $b, Foo $c);
/**
* @param array $a
* @param $b
*
* @return bool
*/
public static function bar(array $a, $b);
}
EOT,
<<<'EOT'
<?php
Interface I
{
/**
* @param Foo $c
* @param string $a
* @param array $b
*
* @return int|null
*/
public function foo($a, array $b, Foo $c);
/**
* @param $b
* @param array $a
*
* @return bool
*/
public static function bar(array $a, $b);
}
EOT,
];
yield 'PHP 7 param types' => [<<<'EOT'
<?php
class C {
/**
* @param array $a
* @param $b
* @param bool $c
*/
public function m(array $a, $b, bool $c) {}
}
EOT,
<<<'EOT'
<?php
class C {
/**
* @param $b
* @param bool $c
* @param array $a
*/
public function m(array $a, $b, bool $c) {}
}
EOT,
];
yield 'call-site generic variance' => [
<<<'PHP'
<?php
/**
* @param Foo $a
* @param Bar<covariant A, covariant B> $b
* @param Foo $c
* @param Bar<contravariant C, contravariant D> $d
* @param Foo $e
*/
function f($a, $b, $c, $d, $e) {}
PHP,
<<<'PHP'
<?php
/**
* @param Bar<contravariant C, contravariant D> $d
* @param Bar<covariant A, covariant B> $b
* @param Foo $e
* @param Foo $a
* @param Foo $c
*/
function f($a, $b, $c, $d, $e) {}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocOrderByValueFixerTest.php | tests/Fixer/Phpdoc/PhpdocOrderByValueFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocOrderByValueFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocOrderByValueFixer>
*
* @author Filippo Tessarotto <zoeslam@gmail.com>
* @author Andreas Möller <am@localheinz.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocOrderByValueFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocOrderByValueFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideInvalidConfigurationCases
*
* @param mixed $annotation
*/
public function testInvalidConfiguration($annotation): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->fixer->configure([
'annotations' => [
$annotation,
],
]);
}
/**
* @return iterable<string, array{mixed}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield 'null' => [null];
yield 'false' => [false];
yield 'true' => [true];
yield 'int' => [0];
yield 'float' => [3.14];
yield 'array' => [[]];
yield 'object' => [new \stdClass()];
yield 'unknown' => ['foo'];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'author - skip on 1 or 0 occurrences' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase {
/**
* @author Bob
* @params bool $bool
* @return void
*/
public function testMe() {}
/**
* @params bool $bool
* @return void
*/
public function testMe2() {}
}
',
null,
['annotations' => ['author']],
];
yield 'author - base case' => [
'<?php
/**
* @author Alice
* @author Bob
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @author Bob
* @author Alice
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['author']],
];
yield 'author - preserve positions if other docblock parts are present' => [
'<?php
/**
* Comment 1
* @author Alice
* Comment 3
* @author Bob
* Comment 2
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* Comment 1
* @author Bob
* Comment 2
* @author Alice
* Comment 3
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['author']],
];
yield 'author - case-insensitive' => [
'<?php
/**
* @author Alice
* @author chris
* @author Daniel
* @author Erna
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @author Alice
* @author Erna
* @author chris
* @author Daniel
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['author']],
];
yield 'author - data provider' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @author Alice
* @dataProvider provide
* @author Bob
*/
public function testMe() {}
}
',
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @author Bob
* @dataProvider provide
* @author Alice
*/
public function testMe() {}
}
',
['annotations' => ['author']],
];
yield 'covers - skip on 1 or 0 occurrences' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase {
/**
* @covers Foo
* @params bool $bool
* @return void
*/
public function testMe() {}
/**
* @params bool $bool
* @return void
*/
public function testMe2() {}
}
',
null,
['annotations' => ['covers']],
];
yield 'covers - base case' => [
'<?php
/**
* @covers Bar
* @covers Foo
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @covers Foo
* @covers Bar
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['covers']],
];
yield 'covers - preserve positions if other docblock parts are present' => [
'<?php
/**
* Comment 1
* @covers Bar
* Comment 3
* @covers Foo
* Comment 2
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* Comment 1
* @covers Foo
* Comment 2
* @covers Bar
* Comment 3
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['covers']],
];
yield 'covers - case-insensitive' => [
'<?php
/**
* @covers A
* @covers c
* @covers D
* @covers E
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @covers A
* @covers E
* @covers c
* @covers D
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['covers']],
];
yield 'covers - data provider' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Bar
* @dataProvider provide
* @covers Foo
*/
public function testMe() {}
}
',
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @covers Foo
* @dataProvider provide
* @covers Bar
*/
public function testMe() {}
}
',
['annotations' => ['covers']],
];
yield 'coversNothing - skip on 1 or 0 occurrences' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase {
/**
* @coversNothing Foo
* @params bool $bool
* @return void
*/
public function testMe() {}
/**
* @params bool $bool
* @return void
*/
public function testMe2() {}
}
',
null,
['annotations' => ['coversNothing']],
];
yield 'coversNothing - base case' => [
'<?php
/**
* @coversNothing Bar
* @coversNothing Foo
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @coversNothing Foo
* @coversNothing Bar
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['coversNothing']],
];
yield 'coversNothing - preserve positions if other docblock parts are present' => [
'<?php
/**
* Comment 1
* @coversNothing Bar
* Comment 3
* @coversNothing Foo
* Comment 2
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* Comment 1
* @coversNothing Foo
* Comment 2
* @coversNothing Bar
* Comment 3
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['coversNothing']],
];
yield 'coversNothing - case-insensitive' => [
'<?php
/**
* @coversNothing A
* @coversNothing c
* @coversNothing D
* @coversNothing E
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @coversNothing A
* @coversNothing E
* @coversNothing c
* @coversNothing D
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['coversNothing']],
];
yield 'coversNothing - data provider' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @coversNothing Bar
* @dataProvider provide
* @coversNothing Foo
*/
public function testMe() {}
}
',
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @coversNothing Foo
* @dataProvider provide
* @coversNothing Bar
*/
public function testMe() {}
}
',
['annotations' => ['coversNothing']],
];
yield 'dataProvider - skip on 1 or 0 occurrences' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase {
/**
* @dataProvider Foo::provideData()
* @params bool $bool
* @return void
*/
public function testMe() {}
/**
* @params bool $bool
* @return void
*/
public function testMe2() {}
}
',
null,
['annotations' => ['dataProvider']],
];
yield 'dataProvider - base case' => [
'<?php
/**
* @dataProvider Bar::provideData()
* @dataProvider Foo::provideData()
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @dataProvider Foo::provideData()
* @dataProvider Bar::provideData()
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['dataProvider']],
];
yield 'dataProvider - preserve positions if other docblock parts are present' => [
'<?php
/**
* Comment 1
* @dataProvider Bar::provideData()
* Comment 3
* @dataProvider Foo::provideData()
* Comment 2
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* Comment 1
* @dataProvider Foo::provideData()
* Comment 2
* @dataProvider Bar::provideData()
* Comment 3
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['dataProvider']],
];
yield 'dataProvider - case-insensitive' => [
'<?php
/**
* @dataProvider A::provideData()
* @dataProvider c::provideData()
* @dataProvider D::provideData()
* @dataProvider E::provideData()
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @dataProvider A::provideData()
* @dataProvider E::provideData()
* @dataProvider c::provideData()
* @dataProvider D::provideData()
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['dataProvider']],
];
yield 'dataProvider - data provider' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider Bar::provideData()
* @dataProvider dataProvider
* @dataProvider Foo::provideData()
*/
public function testMe() {}
}
',
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider Foo::provideData()
* @dataProvider dataProvider
* @dataProvider Bar::provideData()
*/
public function testMe() {}
}
',
['annotations' => ['dataProvider']],
];
yield 'depends - skip on 1 or 0 occurrences' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase {
/**
* @depends testFoo
* @params bool $bool
* @return void
*/
public function testMe() {}
/**
* @params bool $bool
* @return void
*/
public function testMe2() {}
}
',
null,
['annotations' => ['depends']],
];
yield 'depends - base case' => [
'<?php
/**
* @depends testBar
* @depends testFoo
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @depends testFoo
* @depends testBar
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['depends']],
];
yield 'depends - preserve positions if other docblock parts are present' => [
'<?php
/**
* Comment 1
* @depends testBar
* Comment 3
* @depends testFoo
* Comment 2
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* Comment 1
* @depends testFoo
* Comment 2
* @depends testBar
* Comment 3
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['depends']],
];
yield 'depends - case-insensitive' => [
'<?php
/**
* @depends testA
* @depends testc
* @depends testD
* @depends testE
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @depends testA
* @depends testE
* @depends testc
* @depends testD
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['depends']],
];
yield 'depends - data provider' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @depends testBar
* @dataProvider provide
* @depends testFoo
*/
public function testMe() {}
}
',
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @depends testFoo
* @dataProvider provide
* @depends testBar
*/
public function testMe() {}
}
',
['annotations' => ['depends']],
];
yield 'group - skip on 1 or 0 occurrences' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase {
/**
* @group slow
* @params bool $bool
* @return void
*/
public function testMe() {}
/**
* @params bool $bool
* @return void
*/
public function testMe2() {}
}
',
null,
['annotations' => ['group']],
];
yield 'group - base case' => [
'<?php
/**
* @group fast
* @group slow
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @group slow
* @group fast
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['group']],
];
yield 'group - preserve positions if other docblock parts are present' => [
'<?php
/**
* Comment 1
* @group fast
* Comment 3
* @group slow
* Comment 2
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* Comment 1
* @group slow
* Comment 2
* @group fast
* Comment 3
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['group']],
];
yield 'group - case-insensitive' => [
'<?php
/**
* @group A
* @group c
* @group D
* @group E
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @group A
* @group E
* @group c
* @group D
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['group']],
];
yield 'group - data provider' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @group fast
* @dataProvider provide
* @group slow
*/
public function testMe() {}
}
',
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @group slow
* @dataProvider provide
* @group fast
*/
public function testMe() {}
}
',
['annotations' => ['group']],
];
yield 'internal - skip on 1 or 0 occurrences' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase {
/**
* @internal Foo
* @params bool $bool
* @return void
*/
public function testMe() {}
/**
* @params bool $bool
* @return void
*/
public function testMe2() {}
}
',
null,
['annotations' => ['internal']],
];
yield 'internal - base case' => [
'<?php
/**
* @internal Bar
* @internal Foo
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @internal Foo
* @internal Bar
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['internal']],
];
yield 'internal - preserve positions if other docblock parts are present' => [
'<?php
/**
* Comment 1
* @internal Bar
* Comment 3
* @internal Foo
* Comment 2
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* Comment 1
* @internal Foo
* Comment 2
* @internal Bar
* Comment 3
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['internal']],
];
yield 'internal - case-insensitive' => [
'<?php
/**
* @internal A
* @internal c
* @internal D
* @internal E
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
'<?php
/**
* @internal A
* @internal E
* @internal c
* @internal D
*/
class FooTest extends \PHPUnit_Framework_TestCase {}
',
['annotations' => ['internal']],
];
yield 'internal - data provider' => [
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @internal Bar
* @dataProvider provide
* @internal Foo
*/
public function testMe() {}
}
',
'<?php
class FooTest extends \PHPUnit_Framework_TestCase
{
/**
* @internal Foo
* @dataProvider provide
* @internal Bar
*/
public function testMe() {}
}
',
['annotations' => ['internal']],
];
yield 'method - skip on 1 or 0 occurrences' => [
'<?php
/**
* @method int foo(array $b)
*/
class Foo {}
',
null,
['annotations' => ['method']],
];
yield 'method - base case' => [
'<?php
/**
* @method bool bar(int $a, bool $b)
* @method array|null baz()
* @method int foo(array $b)
*/
class Foo {}
',
'<?php
/**
* @method int foo(array $b)
* @method bool bar(int $a, bool $b)
* @method array|null baz()
*/
class Foo {}
',
['annotations' => ['method']],
];
yield 'method - preserve positions if other docblock parts are present' => [
'<?php
/**
* Comment 1
* @method bool bar(int $a, bool $b)
* Comment 3
* @method int foo(array $b)
* Comment 2
*/
class Foo {}
',
'<?php
/**
* Comment 1
* @method int foo(array $b)
* Comment 2
* @method bool bar(int $a, bool $b)
* Comment 3
*/
class Foo {}
',
['annotations' => ['method']],
];
yield 'method - case-insensitive' => [
'<?php
/**
* @method int A()
* @method bool b()
* @method array|null c()
* @method float D()
*/
class Foo {}
',
'<?php
/**
* @method array|null c()
* @method float D()
* @method bool b()
* @method int A()
*/
class Foo {}
',
['annotations' => ['method']],
];
yield 'method - with-possibly-unexpected-comparable' => [
'<?php
/**
* @method int foo(Z $b)
* @method int fooA( $b)
*/
class Foo {}
',
'<?php
/**
* @method int fooA( $b)
* @method int foo(Z $b)
*/
class Foo {}
',
['annotations' => ['method']],
];
yield 'method - with-and-without-types' => [
'<?php
/**
* @method int A()
* @method b()
* @method array|null c()
* @method D()
*/
class Foo {}
',
'<?php
/**
* @method array|null c()
* @method D()
* @method b()
* @method int A()
*/
class Foo {}
',
['annotations' => ['method']],
];
yield 'mixin - skip on 1 or 0 occurrences' => [
'<?php
/**
* @package SomePackage
* @mixin Bar
* @license MIT
*/
class Foo {
}
/**
* @package SomePackage
* @license MIT
*/
class Foo2 {
}
',
null,
['annotations' => ['mixin']],
];
yield 'mixin - base case' => [
'<?php
/**
* @mixin Bar1
* @mixin Bar2
* @mixin Bar3
*/
class Foo {
}
',
'<?php
/**
* @mixin Bar2
* @mixin Bar3
* @mixin Bar1
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | true |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocTagNoNamedArgumentsFixerTest.php | tests/Fixer/Phpdoc/PhpdocTagNoNamedArgumentsFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocTagNoNamedArgumentsFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocTagNoNamedArgumentsFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocTagNoNamedArgumentsFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocTagNoNamedArgumentsFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = [], ?WhitespacesFixerConfig $whitespacesConfig = null): void
{
$this->fixer->configure($configuration);
$this->fixer->setWhitespacesConfig($whitespacesConfig ?? new WhitespacesFixerConfig());
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'do not add for anonymous class' => [
<<<'PHP'
<?php
new class () {};
PHP,
];
yield 'do not add for internal class' => [
<<<'PHP'
<?php
/**
* @internal
*/
class Foo {}
PHP,
null,
['fix_internal' => false],
];
yield 'add for internal class by default' => [
<<<'PHP'
<?php
/**
* @internal
* @no-named-arguments
*/
class Foo {}
PHP,
<<<'PHP'
<?php
/**
* @internal
*/
class Foo {}
PHP,
['fix_internal' => true],
];
yield 'create PHPDoc comment' => [
<<<'PHP'
<?php
/**
* @no-named-arguments
*/
class Foo {}
PHP,
<<<'PHP'
<?php
class Foo {}
PHP,
];
yield 'create PHPDoc with description' => [
<<<'PHP'
<?php
/**
* @no-named-arguments The description
*/
class Foo {}
PHP,
<<<'PHP'
<?php
class Foo {}
PHP,
['description' => 'The description'],
];
yield 'add description' => [
<<<'PHP'
<?php
/**
* @no-named-arguments New description
*/
class Foo {}
PHP,
<<<'PHP'
<?php
/**
* @no-named-arguments
*/
class Foo {}
PHP,
['description' => 'New description'],
];
yield 'change description' => [
<<<'PHP'
<?php
/**
* @no-named-arguments New description
*/
class Foo {}
PHP,
<<<'PHP'
<?php
/**
* @no-named-arguments Old description
*/
class Foo {}
PHP,
['description' => 'New description'],
];
yield 'remove description' => [
<<<'PHP'
<?php
/**
* @no-named-arguments
*/
class Foo {}
PHP,
<<<'PHP'
<?php
/**
* @no-named-arguments Description to remove
*/
class Foo {}
PHP,
];
yield 'multiple classes' => [
<<<'PHP'
<?php
/**
* @no-named-arguments
*/
class Foo {}
new class {};
/**
* @no-named-arguments
*/
class Bar {}
PHP,
<<<'PHP'
<?php
class Foo {}
new class {};
class Bar {}
PHP,
];
yield 'tabs and Windows Line endings' => [
str_replace(
[' ', "\n"],
["\t", "\r\n"],
<<<'PHP'
<?php
/**
* @no-named-arguments
*/
class Foo {}
PHP,
),
str_replace(
[' ', "\n"],
["\t", "\r\n"],
<<<'PHP'
<?php
class Foo {}
PHP,
),
[],
new WhitespacesFixerConfig("\t", "\r\n"),
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix80Cases
*
* @requires PHP >= 8.0
*/
public function testFix80(string $expected, ?string $input = null, array $configuration = []): void
{
$this->testFix($expected, $input, $configuration);
}
/**
* @return iterable<string, array{string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix80Cases(): iterable
{
yield 'do not add for attribute class' => [
<<<'PHP'
<?php
#[Attribute(flags: Attribute::TARGET_METHOD)]
final class MyAttributeClass {}
PHP,
null,
['fix_attribute' => false],
];
yield 'add for attribute class' => [
<<<'PHP'
<?php
/**
* @no-named-arguments
*/
#[Attribute(flags: Attribute::TARGET_METHOD)]
final class MyAttributeClass {}
PHP,
<<<'PHP'
<?php
#[Attribute(flags: Attribute::TARGET_METHOD)]
final class MyAttributeClass {}
PHP,
['fix_attribute' => true],
];
yield 'do not add for attribute class (with alias)' => [
<<<'PHP'
<?php
namespace Foo;
use Attribute as TheAttributeClass;
#[TheAttributeClass(flags: TheAttributeClass::TARGET_METHOD)]
final class MyAttributeClass {}
PHP,
null,
['fix_attribute' => false],
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP >= 8.1
*/
public function testFix81(string $expected, ?string $input = null): void
{
$this->testFix($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'enum' => [
<<<'PHP'
<?php
/**
* @no-named-arguments
*/
enum Size {
case Small;
case Medium;
case Large;
public static function fromLength(int $cm) {
return match(true) {
$cm < 50 => static::Small,
$cm < 100 => static::Medium,
default => static::Large,
};
}
}
PHP,
<<<'PHP'
<?php
enum Size {
case Small;
case Medium;
case Large;
public static function fromLength(int $cm) {
return match(true) {
$cm < 50 => static::Small,
$cm < 100 => static::Medium,
default => static::Large,
};
}
}
PHP,
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFix82Cases
*
* @requires PHP >= 8.2
*/
public function testFix82(string $expected, ?string $input = null, array $configuration = []): void
{
$this->testFix($expected, $input, $configuration);
}
/**
* @return iterable<string, array{string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix82Cases(): iterable
{
yield 'do not add for attribute (readonly) class' => [
<<<'PHP'
<?php
/**
* @no-named-arguments
*/
#[FooAttribute]
final readonly class NotAttributeClass1 {}
#[Attribute(flags: Attribute::TARGET_METHOD)]
final readonly class MyAttributeClass {}
/**
* @no-named-arguments
*/
final readonly class NoAttributes {}
/**
* @no-named-arguments
*/
#[FooAttribute]
#[BarAttribute]
#[BazAttribute]
final readonly class NotAttributeClass2 {}
PHP,
<<<'PHP'
<?php
#[FooAttribute]
final readonly class NotAttributeClass1 {}
#[Attribute(flags: Attribute::TARGET_METHOD)]
final readonly class MyAttributeClass {}
final readonly class NoAttributes {}
#[FooAttribute]
#[BarAttribute]
#[BazAttribute]
final readonly class NotAttributeClass2 {}
PHP,
['fix_attribute' => false],
];
}
/**
* @dataProvider provideFixPre85Cases
*
* @requires PHP ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0
*/
public function testFixPre85(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixPre85Cases(): iterable
{
// the below case is fatal error in PHP 8.5+ (https://github.com/php/php-src/pull/19154)
yield 'always add for abstract attribute class' => [
<<<'PHP'
<?php
namespace Foo;
/**
* @no-named-arguments
*/
#[\Attribute(flags: \Attribute::TARGET_METHOD)]
abstract class MyAttributeClass {}
PHP,
<<<'PHP'
<?php
namespace Foo;
#[\Attribute(flags: \Attribute::TARGET_METHOD)]
abstract class MyAttributeClass {}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocToCommentFixerTest.php | tests/Fixer/Phpdoc/PhpdocToCommentFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocToCommentFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocToCommentFixer>
*
* @author Ceeram <ceeram@cakephp.org>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocToCommentFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocToCommentFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
/**
* Do not convert this
*/
namespace Docs;
/**
* Do not convert this
*/
class DocBlocks
{
/**
* Do not convert this
*/
use TestTrait;
/**
* Do not convert this
*/
const STRUCTURAL = true;
/**
* Do not convert this
*/
protected $indent = false;
/**
* Do not convert this
*/
var $oldPublicStyle;
/**
* Do not convert this
*/
public function test() {}
/**
* Do not convert this
*/
private function testPrivate() {}
/**
* Do not convert this
*/
function testNoVisibility() {}
}',
];
yield [
'<?php namespace Docs;
/**
* Do not convert this
*/
/**
* Do not convert this
*/
class DocBlocks{}
',
];
yield [
'<?php
/**
* Do not convert this
*/
namespace Foo;
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/**
* Do not convert this
*/
abstract class DocBlocks
{
/**
* Do not convert this
*/
abstract public function test();
}',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/**
* Do not convert this
*/
interface DocBlocks
{
public function test();
}',
];
yield [
'<?php
namespace NS;
/**
* Do not
*/
final class Foo
{
}',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/**
* Do not convert this
*/
require "require.php";
/**
* Do not convert this
*/
require_once "require_once.php";
/**
* Do not convert this
*/
include "include.php";
/**
* Do not convert this
*/
include_once "include_once.php";
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/**
* Do not convert this
*
* @var int
*/
$a = require "require.php";
/**
* Do not convert this
*
* @var int
*/
$b = require_once "require_once.php";
/**
* Do not convert this
*
* @var int
*/
$c = include "include.php";
/**
* Do not convert this
*
* @var int
*/
$d = include_once "include_once.php";
/**
* @var Composer\Autoload\ClassLoader $loader
*/
$loader = require_once __DIR__."/vendor/autoload.php";
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/**
* @var ClassLoader $loader
*/
$loader = require_once __DIR__."/../app/autoload.php";
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/**
* Do not convert this
*
* @var Foo
*/
$foo = createFoo();
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/**
* Do not convert this
*
* @var bool $local
*/
$local = true;
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/**
* Comment
*/
$local = true;
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** @var \Sqlite3 $sqlite */
foreach($connections as $sqlite) {
$sqlite->open($path);
}',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** @var \Sqlite3 $sqlite */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** @var int $key */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/* This should not be a docblock */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** This should not be a docblock */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/* there should be no docblock here */
$sqlite1->open($path);
',
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** there should be no docblock here */
$sqlite1->open($path);
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/* there should be no docblock here */
$i++;
',
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** there should be no docblock here */
$i++;
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** @var int $index */
$index = $a[\'number\'];
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** @var string $two */
list($one, $two) = explode("," , $csvLines);
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/* This should be a comment */
list($one, $two) = explode("," , $csvLines);
',
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** This should be a comment */
list($one, $two) = explode("," , $csvLines);
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** @var int $index */
foreach ($foo->getPairs($c->bar(), $bar) as $index => list($a, $b)) {
// Do something with $index, $a and $b
}
/** @var \Closure $value */
if (!$value = $this->getValue()) {
return false;
}
/** @var string $name */
switch ($name = $this->getName()) {
case "John":
return false;
case "Jane":
return true;
}
/** @var string $content */
while ($content = $this->getContent()) {
$name .= $content;
}
/** @var int $size */
for($i = 0, $size = count($people); $i < $size; ++$i) {
$people[$i][\'salt\'] = mt_rand(000000, 999999);
}',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/* @var int $wrong */
foreach ($foo->getPairs($c->bar(), $bar) as $index => list($a, $b)) {
// Do something with $index, $a and $b
}
/* @var \Closure $notValue */
if (!$value = $this->getValue()) {
return false;
}
/* @var string $notName */
switch ($name = $this->getName()) {
case "John":
return false;
case "Jane":
return true;
}
/* @var string $notContent */
while ($content = $this->getContent()) {
$name .= $content;
}
/* @var int $notSize */
for($i = 0, $size = count($people); $i < $size; ++$i) {
$people[$i][\'salt\'] = mt_rand(000000, 999999);
}',
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** @var int $wrong */
foreach ($foo->getPairs($c->bar(), $bar) as $index => list($a, $b)) {
// Do something with $index, $a and $b
}
/** @var \Closure $notValue */
if (!$value = $this->getValue()) {
return false;
}
/** @var string $notName */
switch ($name = $this->getName()) {
case "John":
return false;
case "Jane":
return true;
}
/** @var string $notContent */
while ($content = $this->getContent()) {
$name .= $content;
}
/** @var int $notSize */
for($i = 0, $size = count($people); $i < $size; ++$i) {
$people[$i][\'salt\'] = mt_rand(000000, 999999);
}',
];
yield [
'<?php
/* This should be a comment */
',
'<?php
/** This should be a comment */
',
];
yield [
'<?php
/**
* This is a page level docblock should stay untouched
*/
echo "Some string";
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** @var \NumberFormatter $formatter */
static $formatter;
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
function getNumberFormatter()
{
/** @var \NumberFormatter $formatter */
static $formatter;
}
',
];
yield [
'<?php
class A
{
public function b()
{
/** @var int $c */
print($c = 0);
}
}
',
];
yield ['<?php
/** header */
echo 123;
/** @var int $bar1 */
(print($bar1 = 0));
',
];
yield [
'<?php
/** header */
echo 123;
/** @var ClassLoader $loader */
$loader = require __DIR__.\'/../vendor/autoload.php\';
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** @todo Do not convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
null,
['ignored_tags' => ['todo']],
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/* Convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}
/** @todo Do not convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** Convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}
/** @todo Do not convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
['ignored_tags' => ['todo']],
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/* Convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}
/** @fix-me Do not convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** Convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}
/** @fix-me Do not convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
['ignored_tags' => ['fix-me']],
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/* @todoNot Convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}
/** @TODO Do not convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** @todoNot Convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}
/** @TODO Do not convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
['ignored_tags' => ['todo']],
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/* Convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}
/**
* @deprecated This tag is not in the list but the next one is
* @todo This should be a PHPDoc as the tag is on "ignored_tags" list
*/
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** Convert this */
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}
/**
* @deprecated This tag is not in the list but the next one is
* @todo This should be a PHPDoc as the tag is on "ignored_tags" list
*/
foreach($connections as $key => $sqlite) {
$sqlite->open($path);
}',
['ignored_tags' => ['todo']],
];
yield 'do not convert before fn' => [
'<?php // needed because by default first comment is never fixed
/** @param int $x */
fn ($x) => $x + 42;
',
];
yield 'convert before return without option' => [
'<?php
function doSomething()
{
/* @var void */
return;
}
',
'<?php
function doSomething()
{
/** @var void */
return;
}
',
['allow_before_return_statement' => false],
];
yield 'do not convert before return with option' => [
'<?php
function doSomething()
{
/** @var void */
return;
}
',
null,
['allow_before_return_statement' => true],
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/**
* Do not convert this
*/
trait DocBlocks
{
public function test() {}
}',
];
yield [
'<?php
/** header */
echo 123;
/** @var User $bar3 */
($bar3 = tmp())->doSomething();
/** @var Session $session */ # test
$session = new Session();
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** @var int $a */
[$a] = $b;
/* @var int $c */
[$a] = $c;
',
'<?php
$first = true;// needed because by default first docblock is never fixed.
/** @var int $a */
[$a] = $b;
/** @var int $c */
[$a] = $c;
',
];
yield [
'<?php
$first = true;// needed because by default first docblock is never fixed.
/**
* @var int $a
*/
[$a] = $b;
',
];
yield [
'<?php
class Foo {
/**
* Do not convert this
*/
private int $foo;
}',
];
yield [
'<?php
class Foo {
/**
* Do not convert this
*/
protected ?string $foo;
}',
];
yield [
'<?php
class Foo {
/**
* Do not convert this
*/
public ? float $foo;
}',
];
yield [
'<?php
class Foo {
/**
* Do not convert this
*/
var ? Foo\Bar $foo;
}',
];
yield [
'<?php
class Foo {
/**
* Do not convert this
*/
var ? array $foo;
}',
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*/
public function testFix80(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: string}>
*/
public static function provideFix80Cases(): iterable
{
yield [
'<?php
/**
* @Annotation
*/
#[CustomAnnotationA]
Class MyAnnotation3
{
/**
* @Annotation
*/
#[CustomAnnotationB]
#[CustomAnnotationC]
public function foo() {}
/**
* @Annotation
*/
#[CustomAnnotationD]
public $var;
/*
* end of class
*/
}',
'<?php
/**
* @Annotation
*/
#[CustomAnnotationA]
Class MyAnnotation3
{
/**
* @Annotation
*/
#[CustomAnnotationB]
#[CustomAnnotationC]
public function foo() {}
/**
* @Annotation
*/
#[CustomAnnotationD]
public $var;
/**
* end of class
*/
}',
];
yield [
'<?php
class Foo
{
public function __construct(
/** @var string Do not convert this */
public string $bar
) {
}
}
',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected): void
{
$this->doTest($expected);
}
/**
* @return iterable<string, array{string}>
*/
public static function provideFix81Cases(): iterable
{
yield 'enum' => [
'<?php
declare(strict_types=1);
namespace PhpCsFixer\Tests\Tokenizer\Analyzer;
/** Before enum */
enum Foo {
//
}',
];
yield 'phpDoc over enum case' => [
'<?php
enum Foo: int
{
/**
* @deprecated do not convert this
*/
case BAR = 1;
}
',
];
}
/**
* @dataProvider provideFix84Cases
*
* @requires PHP 8.4
*/
public function testFix84(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: string}>
*/
public static function provideFix84Cases(): iterable
{
yield 'asymmetric visibility' => [
<<<'PHP'
<?php class Foo {
/** @var int */
public(set) int $a;
/** @var int */
protected(set) int $b;
/** @var int */
private(set) int $c;
}
PHP,
];
yield 'property hooks' => [
<<<'PHP'
<?php
class Foo
{
public int $a {
/** @var int */
get => $this->a;
/** @var int */
set => $this->a = $value;
}
public int $b {
/** @var int */
GET => $this->b;
/** @var int */
SET => $this->b = $value;
}
}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocInlineTagNormalizerFixerTest.php | tests/Fixer/Phpdoc/PhpdocInlineTagNormalizerFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocInlineTagNormalizerFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocInlineTagNormalizerFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocInlineTagNormalizerFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocInlineTagNormalizerFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
/**
* {link} { LINK }
* { test }
* {@inheritDoc rire éclatant des écoliers qui décontenança®¶ñ¿}
* test other comment
* {@inheritdoc test} a
* {@inheritdoc test} b
* {@inheritdoc test} c
* {@inheritdoc foo bar.} d
* {@inheritdoc foo bar.} e
* {@inheritdoc test} f
* end comment {@inheritdoc here we are done} @foo {1}
*/
',
'<?php
/**
* {link} { LINK }
* { test }
* {@inheritDoc rire éclatant des écoliers qui décontenança®¶ñ¿ }
* test other comment
* @{inheritdoc test} a
* {{@inheritdoc test}} b
* {@ inheritdoc test} c
* { @inheritdoc foo bar. } d
* {@ inheritdoc foo bar. } e
* @{{inheritdoc test}} f
* end comment {@inheritdoc here we are done} @foo {1}
*/
',
];
yield [
'<?php
/**
* {@foo}
* @{ bar }
*/',
'<?php
/**
* @{ foo }
* @{ bar }
*/',
[
'tags' => ['foo'],
],
];
yield [
'<?php
/**
* @inheritDoc
* {@inheritDoc}
*/',
'<?php
/**
* @inheritDoc
* @{ inheritDoc }
*/',
];
foreach (['example', 'id', 'internal', 'inheritdoc', 'link', 'source', 'toc', 'tutorial'] as $tag) {
yield [
\sprintf("<?php\n /**\n * {@%s}a\n */\n", $tag),
\sprintf("<?php\n /**\n * @{%s}a\n */\n", $tag),
];
yield [
\sprintf("<?php\n /**\n * {@%s} b\n */\n", $tag),
\sprintf("<?php\n /**\n * {{@%s}} b\n */\n", $tag),
];
yield [
\sprintf("<?php\n /**\n * c {@%s}\n */\n", $tag),
\sprintf("<?php\n /**\n * c @{{%s}}\n */\n", $tag),
];
yield [
\sprintf("<?php\n /**\n * c {@%s test}\n */\n", $tag),
\sprintf("<?php\n /**\n * c @{{%s test}}\n */\n", $tag),
];
// test unbalanced { tags
yield [
\sprintf("<?php\n /**\n * c {@%s test}\n */\n", $tag),
\sprintf("<?php\n /**\n * c {@%s test}}\n */\n", $tag),
];
yield [
\sprintf("<?php\n /**\n * c {@%s test}\n */\n", $tag),
\sprintf("<?php\n /**\n * c {{@%s test}\n */\n", $tag),
];
yield [
\sprintf("<?php\n /**\n * c {@%s test}\n */\n", $tag),
\sprintf("<?php\n /**\n * c @{{%s test}}}\n */\n", $tag),
];
}
// don't auto inline tags
foreach (['example', 'id', 'internal', 'inheritdoc', 'foo', 'link', 'source', 'toc', 'tutorial'] as $tag) {
yield [
\sprintf("<?php\n /**\n * @%s\n */\n", $tag),
];
}
// don't touch well formatted tags
foreach (['example', 'id', 'internal', 'inheritdoc', 'foo', 'link', 'source', 'toc', 'tutorial'] as $tag) {
yield [
\sprintf("<?php\n /**\n * {@%s}\n */\n", $tag),
];
}
// invalid syntax
yield [
'<?php
/**
* {@link https://symfony.com/rfc/rfc1035.text)
*/
$someVar = "hello";',
];
yield 'do not rename tags' => [
'<?php
/**
* { @iDontWantToBeChanged }
* @Route("/conventions/@{idcc}", name="api_v1_convention_read_by_idcc")
*
* { @ids }
*/
',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocTypesOrderFixerTest.php | tests/Fixer/Phpdoc/PhpdocTypesOrderFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocTypesOrderFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocTypesOrderFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocTypesOrderFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocTypesOrderFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php /** @var null|int|string */',
'<?php /** @var string|int|null */',
];
yield [
'<?php /** @param Bar|\Foo */',
'<?php /** @param \Foo|Bar */',
];
yield [
'<?php /** @property-read \Bar|Foo */',
'<?php /** @property-read Foo|\Bar */',
];
yield [
'<?php /** @property-write bar|Foo */',
'<?php /** @property-write Foo|bar */',
];
yield [
'<?php /** @return Bar|foo */',
'<?php /** @return foo|Bar */',
];
yield [
'<?php /** @method \Bar|Foo foo(\Bar|Foo $foo, \Bar|Foo $bar) */',
'<?php /** @method Foo|\Bar foo(Foo|\Bar $foo, Foo|\Bar $bar) */',
];
yield [
'<?php /** @var null|array|Bar\Baz|bool[]|false|Foo|int|object|resource|string|string[] */',
'<?php /** @var string[]|resource|false|object|null|Foo|Bar\Baz|bool[]|string|array|int */',
];
yield [
'<?php /** @var null|array<int, string> Foo */',
];
yield [
'<?php /** @var null|array<int, array<string>> Foo */',
];
yield [
'<?php /** @var NULL|int|string */',
'<?php /** @var string|int|NULL */',
];
yield [
'<?php /** @var ?Bar|Foo */',
'<?php /** @var Foo|?Bar */',
];
yield [
'<?php /** @var Bar|?Foo */',
'<?php /** @var ?Foo|Bar */',
];
yield [
'<?php /** @var ?\Bar|Foo */',
'<?php /** @var Foo|?\Bar */',
];
yield [
'<?php /** @var Bar|?\Foo */',
'<?php /** @var ?\Foo|Bar */',
];
yield [
'<?php /** @var array<null|int|string> */',
'<?php /** @var array<string|int|null> */',
];
yield [
'<?php /** @var array<int|string, null|int|string> */',
'<?php /** @var array<string|int, string|int|null> */',
];
yield [
'<?php /** @var array<int|string, array<int|string, null|int|string>> */',
'<?php /** @var array<string|int, array<string|int, string|int|null>> */',
];
yield [
'<?php /** @var null|null */',
];
yield [
'<?php /** @var null|\null */',
];
yield [
'<?php /** @var \null|null */',
];
yield [
'<?php /** @var \null|\null */',
];
yield [
'<?php /** @var array<\null|int|string> */',
'<?php /** @var array<string|\null|int> */',
];
yield [
'<?php /** @var array<int, array<int, array<int, array<int, array<null|OutputInterface>>>>> */',
'<?php /** @var array<int, array<int, array<int, array<int, array<OutputInterface|null>>>>> */',
];
yield [
'<?php /** @var null|Foo|Foo[]|Foo\Bar|Foo_Bar */',
'<?php /** @var Foo[]|null|Foo|Foo\Bar|Foo_Bar */',
];
yield [
'<?php /** @return array<array<string, int>> */',
];
yield [
'<?php /** @return array<int, callable(array<string, null|string> , DateTime): bool> */',
];
yield [
'<?php /** @var null|string */',
'<?php /** @var string|null */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @param null|string $foo */',
'<?php /** @param string|null $foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @property null|string $foo */',
'<?php /** @property string|null $foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @property-read null|string $foo */',
'<?php /** @property-read string|null $foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @property-write null|string $foo */',
'<?php /** @property-write string|null $foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @method null|string foo(null|int $foo, null|string $bar) */',
'<?php /** @method string|null foo(int|null $foo, string|null $bar) */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @return null|string */',
'<?php /** @return string|null */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var null|string[]|resource|false|object|Foo|Bar\Baz|bool[]|string|array|int */',
'<?php /** @var string[]|resource|false|object|null|Foo|Bar\Baz|bool[]|string|array|int */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var null|array<int, string> Foo */',
'<?php /** @var array<int, string>|null Foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var null|array<int, array<string>> Foo */',
'<?php /** @var array<int, array<string>>|null Foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var NULL|string */',
'<?php /** @var string|NULL */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var Foo|?Bar */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var ?Foo|Bar */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var array<null|string> */',
'<?php /** @var array<string|null> */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var array<int, null|string> */',
'<?php /** @var array<int, string|null> */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var array<int, array<null|int|string>> */',
'<?php /** @var array<int, array<int|string|null>> */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var null|null */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var null|\null */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var \null|null */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var \null|\null */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var \null|int */',
'<?php /** @var int|\null */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var array<\null|int> */',
'<?php /** @var array<int|\null> */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var array<int, array<int, array<int, array<int, array<null|OutputInterface>>>>> */',
'<?php /** @var array<int, array<int, array<int, array<int, array<OutputInterface|null>>>>> */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var null|Foo[]|Foo|Foo\Bar|Foo_Bar */',
'<?php /** @var Foo[]|null|Foo|Foo\Bar|Foo_Bar */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @method null|Y|X setOrder(array<string, null|array{Y,X,null|Z}> $by) */',
'<?php /** @method Y|null|X setOrder(array<string, array{Y,X,Z|null}|null> $by) */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield '@method with invalid 2nd phpdoc' => [
'<?php /** @method null|X setOrder($$by) */',
'<?php /** @method X|null setOrder($$by) */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var array<array<int, int>, OutputInterface> */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var iterable<array{names:array<string>, surname:string}> */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var iterable<array{surname:string, names:array<string>}> */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @return array<array{level:string, message:string, context:array<mixed>}> */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @return Data<array{enabled: string[], all: array<string, string>}> */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @return array<int, callable(array<string, null|string> , DateTime): bool> */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @param null|callable(array<string>): array<string, T> $callback */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @return Closure(Iterator<TKey, T>): Generator<int, array<TKey, T>> */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var Closure(Iterator<TKey, T>): Generator<int, array<TKey, T>> $pipe */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @return Generator<int, Promise<mixed>, mixed, Identity> */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @param null|callable(null|foo, null|bar): array<string, T> $callback */',
'<?php /** @param null|callable(foo|null, bar|null): array<string, T> $callback */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @param null|string$foo */',
'<?php /** @param string|null$foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_first'],
];
yield [
'<?php /** @var string|null */',
'<?php /** @var null|string */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @param string|null $foo */',
'<?php /** @param null|string $foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @property string|null $foo */',
'<?php /** @property null|string $foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @property-read string|null $foo */',
'<?php /** @property-read null|string $foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @property-write string|null $foo */',
'<?php /** @property-write null|string $foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @method string|null foo(int|null $foo, string|null $bar) */',
'<?php /** @method null|string foo(null|int $foo, null|string $bar) */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @return string|null */',
'<?php /** @return null|string */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var string[]|resource|false|object|Foo|Bar\Baz|bool[]|string|array|int|null */',
'<?php /** @var string[]|resource|false|object|null|Foo|Bar\Baz|bool[]|string|array|int */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int, string>|null Foo */',
'<?php /** @var null|array<int, string> Foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int, array<string>>|null Foo */',
'<?php /** @var null|array<int, array<string>> Foo */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var string|NULL */',
'<?php /** @var NULL|string */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var Foo|?Bar */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var ?Foo|Bar */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var Foo|?\Bar */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var ?\Foo|Bar */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<string|null> */',
'<?php /** @var array<null|string> */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int, string|null> */',
'<?php /** @var array<int, null|string> */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int, array<int|string|null>> */',
'<?php /** @var array<int, array<null|int|string>> */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var null|null */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var null|\null */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var \null|null */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var \null|\null */',
null,
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var int|\null */',
'<?php /** @var \null|int */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int|\null> */',
'<?php /** @var array<\null|int> */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int, array<int, array<int, array<int, array<OutputInterface|null>>>>> */',
'<?php /** @var array<int, array<int, array<int, array<int, array<null|OutputInterface>>>>> */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var Foo[]|Foo|Foo\Bar|Foo_Bar|null */',
'<?php /** @var Foo[]|null|Foo|Foo\Bar|Foo_Bar */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @return array<int, callable(array<string, string|null> , DateTime): bool> */',
'<?php /** @return array<int, callable(array<string, null|string> , DateTime): bool> */',
['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var int|null|string */',
'<?php /** @var string|int|null */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @param Bar|\Foo */',
'<?php /** @param \Foo|Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @property-read \Bar|Foo */',
'<?php /** @property-read Foo|\Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @property-write bar|Foo */',
'<?php /** @property-write Foo|bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @return Bar|foo */',
'<?php /** @return foo|Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @method \Bar|Foo foo(\Bar|Foo $foo, \Bar|Foo $bar) */',
'<?php /** @method Foo|\Bar foo(Foo|\Bar $foo, Foo|\Bar $bar) */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var array|Bar\Baz|bool[]|false|Foo|int|null|object|resource|string|string[] */',
'<?php /** @var string[]|resource|false|object|null|Foo|Bar\Baz|bool[]|string|array|int */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var array<int, string>|null Foo */',
'<?php /** @var null|array<int, string> Foo */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var array<int, array<string>>|null Foo */',
'<?php /** @var null|array<int, array<string>> Foo */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var ?Bar|Foo */',
'<?php /** @var Foo|?Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var Bar|?Foo */',
'<?php /** @var ?Foo|Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var ?\Bar|Foo */',
'<?php /** @var Foo|?\Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var Bar|?\Foo */',
'<?php /** @var ?\Foo|Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var array<null|string> */',
'<?php /** @var array<string|null> */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var array<int|string, null|string> */',
'<?php /** @var array<string|int, string|null> */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var array<int|string, array<int|string, null|string>> */',
'<?php /** @var array<string|int, array<string|int, string|null>> */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var null|null */',
null,
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var null|\null */',
null,
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var \null|null */',
null,
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var \null|\null */',
null,
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var int|\null|string */',
'<?php /** @var string|\null|int */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var array<int|\null|string> */',
'<?php /** @var array<string|\null|int> */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var array<int, array<int, array<int, array<int, array<null|OutputInterface>>>>> */',
'<?php /** @var array<int, array<int, array<int, array<int, array<OutputInterface|null>>>>> */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var Foo|Foo[]|Foo\Bar|Foo_Bar|null */',
'<?php /** @var Foo[]|null|Foo|Foo\Bar|Foo_Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @return array<int, callable(array<string, null|string> , DateTime): bool> */',
null,
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @return A&B&C */',
'<?php /** @return A&C&B */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @return array<A&B&C> */',
'<?php /** @return array<A&C&B> */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @return array<A&B&C>|bool|string */',
'<?php /** @return bool|array<A&B&C>|string */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @return A&B<X|Y|Z>&C&D */',
'<?php /** @return A&D&B<X|Y|Z>&C */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @param A|(B&C) */',
'<?php /** @param (C&B)|A */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @param A|((A&B)|(B&C)) */',
'<?php /** @param ((B&C)|(B&A))|A */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @param A&(B&C) */',
'<?php /** @param (C&B)&A */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @param (A&C)|(B&C)|(C&D) */',
'<?php /** @param (C&A)|(C&B)|(C&D) */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @param \A|(\B&\C)|D */',
'<?php /** @param D|\A|(\C&\B) */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @param A|((B&C)|D) */',
'<?php /** @param (D|(C&B))|A */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var Closure<T>(T): T|null|string */',
'<?php /** @var string|Closure<T>(T): T|null */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var \Closure<T of Model, T2, T3>(A|T, T3, T2): (T|T2)|null|string */',
'<?php /** @var string|\Closure<T of Model, T2, T3>(T|A, T3, T2): (T2|T)|null */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var Closure<Closure_can_be_regular_class>|null|string */',
'<?php /** @var string|Closure<Closure_can_be_regular_class>|null */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'none'],
];
yield [
'<?php /** @var int|string|null */',
'<?php /** @var string|int|null */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @param Bar|\Foo */',
'<?php /** @param \Foo|Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @property-read \Bar|Foo */',
'<?php /** @property-read Foo|\Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @property-write bar|Foo */',
'<?php /** @property-write Foo|bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @return Bar|foo */',
'<?php /** @return foo|Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @method \Bar|Foo foo(\Bar|Foo $foo, \Bar|Foo $bar) */',
'<?php /** @method Foo|\Bar foo(Foo|\Bar $foo, Foo|\Bar $bar) */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array|Bar\Baz|bool[]|false|Foo|int|object|resource|string|string[]|null */',
'<?php /** @var string[]|resource|false|object|null|Foo|Bar\Baz|bool[]|string|array|int */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int, string>|null Foo */',
'<?php /** @var null|array<int, string> Foo */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int, array<string>>|null Foo */',
'<?php /** @var null|array<int, array<string>> Foo */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var int|string|NULL */',
'<?php /** @var string|int|NULL */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var ?Bar|Foo */',
'<?php /** @var Foo|?Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var Bar|?Foo */',
'<?php /** @var ?Foo|Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var ?\Bar|Foo */',
'<?php /** @var Foo|?\Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var Bar|?\Foo */',
'<?php /** @var ?\Foo|Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int|string|null> */',
'<?php /** @var array<string|int|null> */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int|string, int|string|null> */',
'<?php /** @var array<string|int, string|int|null> */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int|string, array<int|string, int|string|null>> */',
'<?php /** @var array<string|int, array<string|int, string|int|null>> */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var null|null */',
null,
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var null|\null */',
null,
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var \null|null */',
null,
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var \null|\null */',
null,
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int|string|\null> */',
'<?php /** @var array<string|\null|int> */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var array<int, array<int, array<int, array<int, array<OutputInterface|null>>>>> */',
'<?php /** @var array<int, array<int, array<int, array<int, array<null|OutputInterface>>>>> */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var Foo|Foo[]|Foo\Bar|Foo_Bar|null */',
'<?php /** @var Foo[]|null|Foo|Foo\Bar|Foo_Bar */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @return array<int, callable(array<string, string|null> , DateTime): bool> */',
'<?php /** @return array<int, callable(array<string, null|string> , DateTime): bool> */',
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
'<?php /** @var ?Deferred<TestLocations> */',
null,
['sort_algorithm' => 'alpha', 'null_adjustment' => 'always_last'],
];
yield [
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | true |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixerTest.php | tests/Fixer/Phpdoc/NoSuperfluousPhpdocTagsFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\NoSuperfluousPhpdocTagsFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\NoSuperfluousPhpdocTagsFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\NoSuperfluousPhpdocTagsFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoSuperfluousPhpdocTagsFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'no type declaration' => [
'<?php
class Foo {
/**
* @param Bar $bar
*
* @return Baz
*/
public function doFoo($bar) {}
}',
];
yield 'same type declaration' => [
'<?php
class Foo {
/**
*/
public function doFoo(Bar $bar) {}
}',
'<?php
class Foo {
/**
* @param Bar $bar
*/
public function doFoo(Bar $bar) {}
}',
];
yield 'same optional type declaration' => [
'<?php
class Foo {
/**
*/
public function doFoo(Bar $bar = NULL) {}
}',
'<?php
class Foo {
/**
* @param Bar|null $bar
*/
public function doFoo(Bar $bar = NULL) {}
}',
];
yield 'same type declaration with description' => [
'<?php
class Foo {
/**
* @param Bar $bar an instance of Bar
*/
public function doFoo(Bar $bar) {}
}',
];
yield 'allow_mixed=>false' => [
'<?php
class Foo {
/**
*
*/
public function doFoo($bar) {}
}',
'<?php
class Foo {
/**
* @param mixed $bar
*
* @return mixed
*/
public function doFoo($bar) {}
}',
['allow_mixed' => false],
];
yield 'allow_mixed=>true' => [
'<?php
class Foo {
/**
*
*/
public function doFoo($bar) {}
}',
null,
['allow_mixed' => true],
];
yield 'allow_mixed=>false on property' => [
'<?php
class Foo {
/**
*/
private $bar;
}',
'<?php
class Foo {
/**
* @var mixed
*/
private $bar;
}',
['allow_mixed' => false],
];
yield 'allow_mixed=>false on property with var' => [
'<?php
class Foo {
/**
*/
private $bar;
}',
'<?php
class Foo {
/**
* @var mixed $bar
*/
private $bar;
}',
['allow_mixed' => false],
];
yield 'allow_mixed=>false on property but with comment' => [
'<?php
class Foo {
/**
* @var mixed comment
*/
private $bar;
}',
null,
['allow_mixed' => false],
];
yield 'allow_unused_params=>true' => [
'<?php
class Foo {
/**
* @param string|int $c
*/
public function doFoo($bar /*, $c = 0 */) {}
}',
null,
['allow_unused_params' => true],
];
yield 'multiple different types' => [
'<?php
class Foo {
/**
* @param SubclassOfBar1|SubclassOfBar2 $bar
*/
public function doFoo(Bar $bar) {}
}',
];
yield 'same type declaration with different casing' => [
'<?php
class Foo {
/**
*/
public function doFoo(Bar $bar) {}
}',
'<?php
class Foo {
/**
* @param bar $bar
*/
public function doFoo(Bar $bar) {}
}',
];
yield 'same type declaration with leading backslash - global' => [
'<?php
class Foo {
/**
*/
public function doFoo(Bar $bar) {}
}',
'<?php
class Foo {
/**
* @param \Bar $bar
*/
public function doFoo(Bar $bar) {}
}',
];
yield 'same type declaration with leading backslash - namespaced' => [
'<?php
namespace Xxx;
class Foo {
/**
*/
public function doFoo(Model\Invoice $bar) {}
}',
'<?php
namespace Xxx;
class Foo {
/**
* @param \Xxx\Model\Invoice $bar
*/
public function doFoo(Model\Invoice $bar) {}
}',
];
yield 'same type declaration without leading backslash - global' => [
'<?php
class Foo {
/**
*/
public function doFoo(\Bar $bar) {}
}',
'<?php
class Foo {
/**
* @param Bar $bar
*/
public function doFoo(\Bar $bar) {}
}',
];
yield 'same type declaration without leading backslash - namespaced' => [
'<?php
namespace Xxx;
class Foo {
/**
*/
public function doFoo(\Xxx\Bar $bar) {}
}',
'<?php
namespace Xxx;
class Foo {
/**
* @param Bar $bar
*/
public function doFoo(\Xxx\Bar $bar) {}
}',
];
yield 'same type declaration with null implied from native type - param type' => [
'<?php
class Foo {
/**
*/
public function setAttribute(?string $value, string $value2 = null): void
{
}
}',
'<?php
class Foo {
/**
* @param string $value
* @param string $value2
*/
public function setAttribute(?string $value, string $value2 = null): void
{
}
}',
];
yield 'same type declaration with null implied from native type - return type' => [
'<?php
class Foo {
/**
*/
public function getX(): ?X
{
}
}',
'<?php
class Foo {
/**
* @return X
*/
public function getX(): ?X
{
}
}',
];
yield 'same type declaration with null implied from native type - property' => [
'<?php
class Foo {
/** */
public ?bool $enabled;
}',
'<?php
class Foo {
/** @var bool */
public ?bool $enabled;
}',
];
yield 'same type declaration with null but native type without null - invalid phpdoc must be kept unfixed' => [
'<?php
class Foo {
/** @var bool|null */
public bool $enabled;
}',
];
yield 'multiple arguments' => [
'<?php
class Foo {
/**
* @param SubclassOfBar1|SubclassOfBar2 $bar
*/
public function doFoo(Bar $bar, Baz $baz = null) {}
}',
'<?php
class Foo {
/**
* @param SubclassOfBar1|SubclassOfBar2 $bar
* @param Baz|null $baz
*/
public function doFoo(Bar $bar, Baz $baz = null) {}
}',
];
yield 'with import' => [
'<?php
use Foo\Bar;
/**
*/
function foo(Bar $bar) {}',
'<?php
use Foo\Bar;
/**
* @param Bar $bar
*/
function foo(Bar $bar) {}',
];
yield 'with root symbols' => [
'<?php
/**
*/
function foo(\Foo\Bar $bar) {}',
'<?php
/**
* @param \Foo\Bar $bar
*/
function foo(\Foo\Bar $bar) {}',
];
yield 'with mix of imported and fully qualified symbols' => [
'<?php
use Foo\Bar;
use Foo\Baz;
/**
*/
function foo(Bar $bar, \Foo\Baz $baz) {}',
'<?php
use Foo\Bar;
use Foo\Baz;
/**
* @param \Foo\Bar $bar
* @param Baz $baz
*/
function foo(Bar $bar, \Foo\Baz $baz) {}',
];
yield 'with aliased import' => [
'<?php
use Foo\Bar as Baz;
/**
*/
function foo(Baz $bar) {}',
'<?php
use Foo\Bar as Baz;
/**
* @param \Foo\Bar $bar
*/
function foo(Baz $bar) {}',
];
yield 'with unmapped param' => [
'<?php
use Foo\Bar;
/**
* @param Bar
*/
function foo(Bar $bar) {}',
];
yield 'with param superfluous but not return' => [
'<?php
class Foo {
/**
*
* @return Baz
*/
public function doFoo(Bar $bar) {}
}',
'<?php
class Foo {
/**
* @param Bar $bar
*
* @return Baz
*/
public function doFoo(Bar $bar) {}
}',
];
yield 'with not all params superfluous' => [
'<?php
class Foo {
/**
* @param Bax|Baz $baxz
*/
public function doFoo(Bar $bar, $baxz) {}
}',
'<?php
class Foo {
/**
* @param Bar $bar
* @param Bax|Baz $baxz
*/
public function doFoo(Bar $bar, $baxz) {}
}',
];
yield 'with special type declarations' => [
'<?php
class Foo {
/**
*/
public function doFoo(array $bar, callable $baz) {}
}',
'<?php
class Foo {
/**
* @param array $bar
* @param callable $baz
*/
public function doFoo(array $bar, callable $baz) {}
}',
];
yield 'PHPDoc at the end of file' => [
'<?php
/**
* Foo
*/',
];
yield 'with_variable_in_description' => [
'<?php
class Foo {
/**
* @param $foo Some description that includes a $variable
*/
public function doFoo($foo) {}
}',
];
yield 'with_null' => [
'<?php
class Foo {
/**
* @param null $foo
* @return null
*/
public function doFoo($foo) {}
}',
];
yield 'inheritdoc' => [
'<?php
class Foo {
/**
* @inheritDoc
*/
public function doFoo($foo) {}
}',
];
yield 'inline_inheritdoc' => [
'<?php
class Foo {
/**
* {@inheritdoc}
*/
public function doFoo($foo) {}
}',
];
yield 'dont_remove_inheritdoc' => [
'<?php
class Foo {
/**
* @inheritDoc
*/
public function doFoo($foo) {}
}',
null,
['remove_inheritdoc' => false],
];
yield 'dont_remove_inline_inheritdoc' => [
'<?php
class Foo {
/**
* {@inheritdoc}
*/
public function doFoo($foo) {}
}',
null,
['remove_inheritdoc' => false],
];
yield 'remove_inheritdoc' => [
'<?php
class Foo {
/**
*
*/
public function doFoo($foo) {}
}',
'<?php
class Foo {
/**
* @inheritDoc
*/
public function doFoo($foo) {}
}',
['remove_inheritdoc' => true],
];
yield 'remove_inline_inheritdoc' => [
'<?php
class Foo {
/**
*
*/
public function doFoo($foo) {}
}',
'<?php
class Foo {
/**
* {@inheritdoc}
*/
public function doFoo($foo) {}
}',
['remove_inheritdoc' => true],
];
yield 'dont_remove_inheritdoc_when_surrounded_by_text' => [
'<?php
class Foo {
/**
* Foo.
*
* @inheritDoc
*
* Bar.
*/
public function doFoo($foo) {}
}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_inheritdoc_when_preceded_by_text' => [
'<?php
class Foo {
/**
* Foo.
*
* @inheritDoc
*/
public function doFoo($foo) {}
}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_inheritdoc_when_followed_by_text' => [
'<?php
class Foo {
/**
* @inheritDoc
*
* Bar.
*/
public function doFoo($foo) {}
}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_inline_inheritdoc_inside_text' => [
'<?php
class Foo {
/**
* Foo {@inheritDoc} Bar.
*/
public function doFoo($foo) {}
}',
null,
['remove_inheritdoc' => true],
];
yield 'inheritdocs' => [
'<?php
class Foo {
/**
* @inheritDocs
*/
public function doFoo($foo) {}
}',
];
yield 'inline_inheritdocs' => [
'<?php
class Foo {
/**
* {@inheritdocs}
*/
public function doFoo($foo) {}
}',
];
yield 'dont_remove_inheritdocs' => [
'<?php
class Foo {
/**
* @inheritDocs
*/
public function doFoo($foo) {}
}',
null,
['remove_inheritdoc' => false],
];
yield 'dont_remove_inline_inheritdocs' => [
'<?php
class Foo {
/**
* {@inheritdocs}
*/
public function doFoo($foo) {}
}',
null,
['remove_inheritdoc' => false],
];
yield 'remove_inheritdocs' => [
'<?php
class Foo {
/**
*
*/
public function doFoo($foo) {}
}',
'<?php
class Foo {
/**
* @inheritDocs
*/
public function doFoo($foo) {}
}',
['remove_inheritdoc' => true],
];
yield 'remove_inline_inheritdocs' => [
'<?php
class Foo {
/**
*
*/
public function doFoo($foo) {}
}',
'<?php
class Foo {
/**
* {@inheritdocs}
*/
public function doFoo($foo) {}
}',
['remove_inheritdoc' => true],
];
yield 'dont_remove_inheritdocs_when_surrounded_by_text' => [
'<?php
class Foo {
/**
* Foo.
*
* @inheritDocs
*
* Bar.
*/
public function doFoo($foo) {}
}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_inheritdocs_when_preceded_by_text' => [
'<?php
class Foo {
/**
* Foo.
*
* @inheritDocs
*/
public function doFoo($foo) {}
}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_inheritdocs_when_followed_by_text' => [
'<?php
class Foo {
/**
* @inheritDocs
*
* Bar.
*/
public function doFoo($foo) {}
}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_inline_inheritdocs_inside_text' => [
'<?php
class Foo {
/**
* Foo {@inheritDocs} Bar.
*/
public function doFoo($foo) {}
}',
null,
['remove_inheritdoc' => true],
];
yield 'property_inheritdoc' => [
'<?php
class Foo {
/**
* @inheritDoc
*/
private $foo;
}',
];
yield 'inline_property_inheritdoc' => [
'<?php
class Foo {
/**
* {@inheritdoc}
*/
private $foo;
}',
];
yield 'dont_remove_property_inheritdoc' => [
'<?php
class Foo {
/**
* @inheritDoc
*/
private $foo;
}',
null,
['remove_inheritdoc' => false],
];
yield 'dont_remove_property_inline_inheritdoc' => [
'<?php
class Foo {
/**
* {@inheritdoc}
*/
private $foo;
}',
null,
['remove_inheritdoc' => false],
];
yield 'remove_property_inheritdoc' => [
'<?php
class Foo {
/**
*
*/
private $foo;
}',
'<?php
class Foo {
/**
* @inheritDoc
*/
private $foo;
}',
['remove_inheritdoc' => true],
];
yield 'remove_inline_property_inheritdoc' => [
'<?php
class Foo {
/**
*
*/
private $foo;
}',
'<?php
class Foo {
/**
* {@inheritdoc}
*/
private $foo;
}',
['remove_inheritdoc' => true],
];
yield 'dont_remove_property_inheritdoc_when_surrounded_by_text' => [
'<?php
class Foo {
/**
* Foo.
*
* @inheritDoc
*
* Bar.
*/
private $foo;
}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_property_inheritdoc_when_preceded_by_text' => [
'<?php
class Foo {
/**
* Foo.
*
* @inheritDoc
*/
private $foo;
}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_property_inheritdoc_when_followed_by_text' => [
'<?php
class Foo {
/**
* @inheritDoc
*
* Bar.
*/
private $foo;
}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_property_inline_inheritdoc_inside_text' => [
'<?php
class Foo {
/**
* Foo {@inheritDoc} Bar.
*/
private $foo;
}',
null,
['remove_inheritdoc' => true],
];
yield 'property_inheritdocs' => [
'<?php
class Foo {
/**
* @inheritDocs
*/
private $foo;
}',
];
yield 'inline_property_inheritdocs' => [
'<?php
class Foo {
/**
* {@inheritdocs}
*/
private $foo;
}',
];
yield 'dont_remove_property_inheritdocs' => [
'<?php
class Foo {
/**
* @inheritDocs
*/
private $foo;
}',
null,
['remove_inheritdoc' => false],
];
yield 'dont_remove_inline_property_inheritdocs' => [
'<?php
class Foo {
/**
* {@inheritdocs}
*/
private $foo;
}',
null,
['remove_inheritdoc' => false],
];
yield 'remove_property_property_inheritdoc' => [
'<?php
class Foo {
/**
*
*/
private $foo;
}',
'<?php
class Foo {
/**
* @inheritDocs
*/
private $foo;
}',
['remove_inheritdoc' => true],
];
yield 'remove_inline_property_inheritdocs' => [
'<?php
class Foo {
/**
*
*/
private $foo;
}',
'<?php
class Foo {
/**
* {@inheritdocs}
*/
private $foo;
}',
['remove_inheritdoc' => true],
];
yield 'dont_remove_property_inheritdocs_when_surrounded_by_text' => [
'<?php
class Foo {
/**
* Foo.
*
* @inheritDocs
*
* Bar.
*/
private $foo;
}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_property_inheritdocs_when_preceded_by_text' => [
'<?php
class Foo {
/**
* Foo.
*
* @inheritDocs
*/
private $foo;
}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_property_inheritdocs_when_followed_by_text' => [
'<?php
class Foo {
/**
* @inheritDocs
*
* Bar.
*/
private $foo;
}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_inline_property_inheritdocs_inside_text' => [
'<?php
class Foo {
/**
* Foo {@inheritDocs} Bar.
*/
private $foo;
}',
null,
['remove_inheritdoc' => true],
];
yield 'class_inheritdoc' => [
'<?php
/**
* @inheritDoc
*/
class Foo {}',
];
yield 'dont_remove_class_inheritdoc' => [
'<?php
/**
* @inheritDoc
*/
class Foo {}',
null,
['remove_inheritdoc' => false],
];
yield 'remove_class_inheritdoc' => [
'<?php
/**
*
*/
class Foo {}',
'<?php
/**
* @inheritDoc
*/
class Foo {}',
['remove_inheritdoc' => true],
];
yield 'remove_interface_inheritdoc' => [
'<?php
/**
*
*/
interface Foo {}',
'<?php
/**
* @inheritDoc
*/
interface Foo {}',
['remove_inheritdoc' => true],
];
yield 'dont_remove_class_inheritdoc_when_surrounded_by_text' => [
'<?php
/**
* Foo.
*
* @inheritDoc
*
* Bar.
*/
class Foo {}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_class_inheritdoc_when_preceded_by_text' => [
'<?php
/**
* Foo.
*
* @inheritDoc
*/
class Foo {}',
null,
['remove_inheritdoc' => true],
];
yield 'dont_remove_class_inheritdoc_when_followed_by_text' => [
'<?php
/**
* @inheritDoc
*
* Bar.
*/
class Foo {}',
null,
['remove_inheritdoc' => true],
];
yield 'remove_inheritdoc_after_other_tag' => [
'<?php
class Foo {
/**
* @param int $foo an integer
*
*
*/
public function doFoo($foo) {}
}',
'<?php
class Foo {
/**
* @param int $foo an integer
*
* @inheritDoc
*/
public function doFoo($foo) {}
}',
['remove_inheritdoc' => true],
];
yield 'remove_only_inheritdoc_line' => [
'<?php
class Foo {
/**
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
public function doFoo($foo) {}
}',
'<?php
class Foo {
/**
*
*
*
*
*
*
* @inheritDoc
*
*
*
*
*
*
*/
public function doFoo($foo) {}
}',
['remove_inheritdoc' => true],
];
yield 'remove_single_line_inheritdoc' => [
'<?php
class Foo {
/** */
public function doFoo($foo) {}
}',
'<?php
class Foo {
/** @inheritDoc */
public function doFoo($foo) {}
}',
['remove_inheritdoc' => true],
];
yield 'remove_inheritdoc_on_first_line' => [
'<?php
class Foo {
/**
*/
public function doFoo($foo) {}
}',
'<?php
class Foo {
/** @inheritDoc
*/
public function doFoo($foo) {}
}',
['remove_inheritdoc' => true],
];
yield 'remove_inheritdoc_on_last_line' => [
'<?php
class Foo {
/**
* */
public function doFoo($foo) {}
}',
'<?php
class Foo {
/**
* @inheritDoc */
public function doFoo($foo) {}
}',
['remove_inheritdoc' => true],
];
yield 'remove_inheritdoc_non_structural_element_it_does_not_inherit' => [
'<?php
/**
*
*/
$foo = 1;',
'<?php
/**
* @inheritDoc
*/
$foo = 1;',
['remove_inheritdoc' => true],
];
yield 'property with unsupported type' => [
'<?php
class Foo {
/**
* @var foo:bar
*/
private $foo;
}',
];
yield 'method with unsupported types' => [
'<?php
class Foo {
/**
* @param foo:bar $foo
* @return foo:bar
*/
public function foo($foo) {}
}',
];
yield 'with constant values as type' => [
'<?php
class Foo {
/**
* @var Bar::A|Bar::B|Baz::*|null
*/
private $foo;
/**
* @var 1|\'a\'|\'b\'
*/
private $bar;
}',
];
yield 'same type declaration (with extra empty line)' => [
'<?php
class Foo {
/**
*
*/
public function doFoo(Bar $bar): Baz {}
}',
'<?php
class Foo {
/**
* @param Bar $bar
*
* @return Baz
*/
public function doFoo(Bar $bar): Baz {}
}',
];
yield 'same type declaration (with return type) with description' => [
'<?php
class Foo {
/**
* @param Bar $bar an instance of Bar
*
* @return Baz an instance of Baz
*/
public function doFoo(Bar $bar): Baz {}
}',
];
yield 'multiple different types (with return type)' => [
'<?php
class Foo {
/**
* @param SubclassOfBar1|SubclassOfBar2 $bar
*
* @return SubclassOfBaz1|SubclassOfBaz2 $bar
*/
public function doFoo(Bar $bar): Baz {}
}',
];
yield 'with import (with return type)' => [
'<?php
use Foo\Bar;
use Foo\Baz;
/**
*/
function foo(Bar $bar): Baz {}',
'<?php
use Foo\Bar;
use Foo\Baz;
/**
* @param Bar $bar
* @return Baz
*/
function foo(Bar $bar): Baz {}',
];
yield 'with root symbols (with return type)' => [
'<?php
/**
*/
function foo(\Foo\Bar $bar): \Foo\Baz {}',
'<?php
/**
* @param \Foo\Bar $bar
* @return \Foo\Baz
*/
function foo(\Foo\Bar $bar): \Foo\Baz {}',
];
yield 'with mix of imported and fully qualified symbols (with return type)' => [
'<?php
use Foo\Bar;
use Foo\Baz;
use Foo\Qux;
/**
*/
function foo(Bar $bar, \Foo\Baz $baz): \Foo\Qux {}',
'<?php
use Foo\Bar;
use Foo\Baz;
use Foo\Qux;
/**
* @param \Foo\Bar $bar
* @param Baz $baz
* @return Qux
*/
function foo(Bar $bar, \Foo\Baz $baz): \Foo\Qux {}',
];
yield 'with aliased import (with return type)' => [
'<?php
use Foo\Bar as Baz;
/**
*/
function foo(Baz $bar): Baz {}',
'<?php
use Foo\Bar as Baz;
/**
* @param \Foo\Bar $bar
* @return \Foo\Bar
*/
function foo(Baz $bar): Baz {}',
];
yield 'with scalar type declarations' => [
'<?php
class Foo {
/**
*
*/
public function doFoo(int $bar, string $baz): bool {}
}',
'<?php
class Foo {
/**
* @param int $bar
* @param string $baz
*
* @return bool
*/
public function doFoo(int $bar, string $baz): bool {}
}',
];
yield 'really long one' => [
'<?php
/**
* "Sponsored" by https://github.com/PrestaShop/PrestaShop/blob/1.6.1.24/tools/tcpdf/tcpdf.php (search for "Get page dimensions from format name")
* @see
* @param $number - it can be:
* '.implode("\n * ", range(1, 1_000)).'
*/
function display($number) {}
',
];
yield 'return with @inheritDoc in description' => [
'<?php
/**
*/
function foo(): bool {}
',
'<?php
/**
* @return bool @inheritDoc
*/
function foo(): bool {}
',
['remove_inheritdoc' => true],
];
yield 'remove_trait_inheritdoc' => [
'<?php
/**
*
*/
trait Foo {}',
'<?php
/**
* @inheritDoc
*/
trait Foo {}',
['remove_inheritdoc' => true],
];
yield 'same nullable type declaration' => [
'<?php
class Foo {
/**
*
*/
public function doFoo(?Bar $bar): ?Baz {}
}',
'<?php
class Foo {
/**
* @param Bar|null $bar
*
* @return Baz|null
*/
public function doFoo(?Bar $bar): ?Baz {}
}',
];
yield 'same nullable type declaration reversed' => [
'<?php
class Foo {
/**
*
*/
public function doFoo(?Bar $bar): ?Baz {}
}',
'<?php
class Foo {
/**
* @param null|Bar $bar
*
* @return null|Baz
*/
public function doFoo(?Bar $bar): ?Baz {}
}',
];
yield 'same ?type declaration' => [
'<?php
class Foo {
/**
*
*/
public function doFoo(?Bar $bar): ?Baz {}
}',
'<?php
class Foo {
/**
* @param ?Bar $bar
*
* @return ?Baz
*/
public function doFoo(?Bar $bar): ?Baz {}
}',
];
yield 'same nullable type declaration with description' => [
'<?php
class Foo {
/**
* @param Bar|null $bar an instance of Bar
*
* @return Baz|null an instance of Baz
*/
public function doFoo(?Bar $bar): ?Baz {}
}',
];
yield 'same optional nullable type declaration' => [
'<?php
class Foo {
/**
*/
public function doFoo(?Bar $bar = null) {}
}',
'<?php
class Foo {
/**
* @param Bar|null $bar
*/
public function doFoo(?Bar $bar = null) {}
}',
];
yield 'multiple different types (nullable)' => [
'<?php
class Foo {
/**
* @param SubclassOfBar1|SubclassOfBar2|null $bar
*
* @return SubclassOfBaz1|SubclassOfBaz2|null $bar
*/
public function doFoo(?Bar $bar): ?Baz {}
}',
];
yield 'with nullable import' => [
'<?php
use Foo\Bar;
use Foo\Baz;
/**
*/
function foo(?Bar $bar): ?Baz {}',
'<?php
use Foo\Bar;
use Foo\Baz;
/**
* @param Bar|null $bar
* @return Baz|null
*/
function foo(?Bar $bar): ?Baz {}',
];
yield 'with nullable root symbols' => [
'<?php
/**
*/
function foo(?\Foo\Bar $bar): ?\Foo\Baz {}',
'<?php
/**
* @param \Foo\Bar|null $bar
* @return \Foo\Baz|null
*/
function foo(?\Foo\Bar $bar): ?\Foo\Baz {}',
];
yield 'with nullable mix of imported and fully qualified symbols' => [
'<?php
use Foo\Bar;
use Foo\Baz;
use Foo\Qux;
/**
*/
function foo(?Bar $bar, ?\Foo\Baz $baz): ?\Foo\Qux {}',
'<?php
use Foo\Bar;
use Foo\Baz;
use Foo\Qux;
/**
* @param \Foo\Bar|null $bar
* @param Baz|null $baz
* @return Qux|null
*/
function foo(?Bar $bar, ?\Foo\Baz $baz): ?\Foo\Qux {}',
];
yield 'with nullable aliased import' => [
'<?php
use Foo\Bar as Baz;
/**
*/
function foo(?Baz $bar): ?Baz {}',
'<?php
use Foo\Bar as Baz;
/**
* @param \Foo\Bar|null $bar
* @return \Foo\Bar|null
*/
function foo(?Baz $bar): ?Baz {}',
];
yield 'with nullable special type declarations' => [
'<?php
class Foo {
/**
*
*/
public function doFoo(iterable $bar, ?int $baz): ?array {}
}',
'<?php
class Foo {
/**
* @param iterable $bar
* @param int|null $baz
*
* @return array|null
*/
public function doFoo(iterable $bar, ?int $baz): ?array {}
}',
];
yield 'remove abstract annotation in function' => [
'<?php
abstract class Foo {
/**
*/
public abstract function doFoo();
}',
'<?php
abstract class Foo {
/**
* @abstract
*/
public abstract function doFoo();
}', ];
yield 'dont remove abstract annotation in function' => [
'<?php
class Foo {
/**
* @abstract
*/
public function doFoo() {}
}', ];
yield 'remove final annotation in function' => [
'<?php
class Foo {
/**
*/
public final function doFoo() {}
}',
'<?php
class Foo {
/**
* @final
*/
public final function doFoo() {}
}', ];
yield 'dont remove final annotation in function' => [
'<?php
class Foo {
/**
* @final
*/
public function doFoo() {}
}', ];
yield 'remove abstract annotation in class' => [
'<?php
/**
*/
abstract class Foo {
}',
'<?php
/**
* @abstract
*/
abstract class Foo {
}', ];
yield 'dont remove abstract annotation in class' => [
'<?php
abstract class Bar{}
/**
* @abstract
*/
class Foo {
}', ];
yield 'remove final annotation in class' => [
'<?php
/**
*/
final class Foo {
}',
'<?php
/**
* @final
*/
final class Foo {
}', ];
yield 'dont remove final annotation in class' => [
'<?php
final class Bar{}
/**
* @final
*/
class Foo {
}', ];
yield 'remove when used with reference' => [
'<?php class Foo {
/**
*/
function f1(string &$x) {}
/**
*/
function f2(string &$x) {}
/**
*/
function f3(string &$x) {}
}',
'<?php class Foo {
/**
* @param string $x
*/
function f1(string &$x) {}
/**
* @param string &$x
*/
function f2(string &$x) {}
/**
* @param string $y Description
*/
function f3(string &$x) {}
}',
];
yield 'dont remove when used with reference' => [
'<?php class Foo {
/**
* @param string &$x Description
*/
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | true |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocLineSpanFixerTest.php | tests/Fixer/Phpdoc/PhpdocLineSpanFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocLineSpanFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocLineSpanFixer>
*
* @author Gert de Pagter <BackEndTea@gmail.com>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocLineSpanFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocLineSpanFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*
* @param _AutogeneratedInputConfiguration $configuration
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'It does not change doc blocks if not needed' => [
'<?php
class Foo
{
/**
* Important
*/
const FOO_BAR = "foobar";
/**
* @var bool
*/
public $variable = true;
/**
* @var bool
*/
private $var = false;
/**
* @return void
*/
public function hello() {}
}
',
];
yield 'It does change doc blocks to multi by default' => [
'<?php
class Foo
{
/**
* Important
*/
const FOO_BAR = "foobar";
/**
* @var bool
*/
public $variable = true;
/**
* @var bool
*/
private $var = false;
/**
* @return void
*/
public function hello() {}
}
',
'<?php
class Foo
{
/** Important */
const FOO_BAR = "foobar";
/** @var bool */
public $variable = true;
/** @var bool */
private $var = false;
/** @return void */
public function hello() {}
}
',
];
yield 'It does change doc blocks to single if configured to do so' => [
'<?php
class Foo
{
/** Important */
const FOO_BAR = "foobar";
/** @var bool */
public $variable = true;
/** @var bool */
private $var = false;
/** @return void */
public function hello() {}
}
',
'<?php
class Foo
{
/**
* Important
*/
const FOO_BAR = "foobar";
/**
* @var bool
*/
public $variable = true;
/**
* @var bool
*/
private $var = false;
/**
* @return void
*/
public function hello() {}
}
',
[
'property' => 'single',
'const' => 'single',
'method' => 'single',
],
];
yield 'It does change complicated doc blocks to single if configured to do so' => [
'<?php
class Foo
{
/** @var bool */
public $variable1 = true;
/** @var bool */
public $variable2 = true;
/** @Assert\File(mimeTypes={ "image/jpeg", "image/png" }) */
public $imageFileObject;
}
',
'<?php
class Foo
{
/**
* @var bool */
public $variable1 = true;
/** @var bool
*/
public $variable2 = true;
/**
* @Assert\File(mimeTypes={ "image/jpeg", "image/png" })
*/
public $imageFileObject;
}
',
[
'property' => 'single',
],
];
yield 'It does not changes doc blocks from single if configured to do so' => [
'<?php
class Foo
{
/** Important */
const FOO_BAR = "foobar";
/** @var bool */
public $variable = true;
/** @var bool */
private $var = false;
/** @return void */
public function hello() {}
}
',
null,
[
'property' => 'single',
'const' => 'single',
'method' => 'single',
],
];
yield 'It can be configured to change certain elements to single line' => [
'<?php
class Foo
{
/**
* Important
*/
const FOO_BAR = "foobar";
/** @var bool */
public $variable = true;
/** @var bool */
private $var = false;
/**
* @return void
*/
public function hello() {}
}
',
'<?php
class Foo
{
/**
* Important
*/
const FOO_BAR = "foobar";
/**
* @var bool
*/
public $variable = true;
/**
* @var bool
*/
private $var = false;
/**
* @return void
*/
public function hello() {}
}
',
[
'property' => 'single',
],
];
yield 'It wont change a doc block to single line if it has multiple useful lines' => [
'<?php
class Foo
{
/**
* Important
* Really important
*/
const FOO_BAR = "foobar";
}
',
null,
[
'const' => 'single',
],
];
yield 'It updates doc blocks correctly, even with more indentation' => [
'<?php
if (false) {
class Foo
{
/** @var bool */
public $var = true;
/**
* @return void
*/
public function hello () {}
}
}
',
'<?php
if (false) {
class Foo
{
/**
* @var bool
*/
public $var = true;
/** @return void */
public function hello () {}
}
}
',
[
'property' => 'single',
],
];
yield 'It can convert empty doc blocks' => [
'<?php
class Foo
{
/**
*
*/
const FOO = "foobar";
/** */
private $foo;
}',
'<?php
class Foo
{
/** */
const FOO = "foobar";
/**
*
*/
private $foo;
}',
[
'property' => 'single',
],
];
yield 'It can update doc blocks of static properties' => [
'<?php
class Bar
{
/**
* Important
*/
public static $variable = "acme";
}
',
'<?php
class Bar
{
/** Important */
public static $variable = "acme";
}
',
];
yield 'It can update doc blocks of properties that use the var keyword instead of public' => [
'<?php
class Bar
{
/**
* Important
*/
var $variable = "acme";
}
',
'<?php
class Bar
{
/** Important */
var $variable = "acme";
}
',
];
yield 'It can update doc blocks of static that do not declare visibility' => [
'<?php
class Bar
{
/**
* Important
*/
static $variable = "acme";
}
',
'<?php
class Bar
{
/** Important */
static $variable = "acme";
}
',
];
yield 'It does not change method doc blocks if configured to do so' => [
'<?php
class Foo
{
/** @return mixed */
public function bar() {}
/**
* @return void
*/
public function baz() {}
}',
null,
[
'method' => null,
],
];
yield 'It does not change property doc blocks if configured to do so' => [
'<?php
class Foo
{
/**
* @var int
*/
public $foo;
/** @var mixed */
public $bar;
}',
null,
[
'property' => null,
],
];
yield 'It does not change const doc blocks if configured to do so' => [
'<?php
class Foo
{
/**
* @var int
*/
public const FOO = 1;
/** @var mixed */
public const BAR = null;
}',
null,
[
'const' => null,
],
];
yield 'It can handle constants with visibility, does not crash on trait imports' => [
'<?php
trait Bar
{}
class Foo
{
/** whatever */
use Bar;
/**
*
*/
public const FOO = "foobar";
/** */
private $foo;
}',
'<?php
trait Bar
{}
class Foo
{
/** whatever */
use Bar;
/** */
public const FOO = "foobar";
/**
*
*/
private $foo;
}',
[
'property' => 'single',
],
];
yield 'It can handle properties with type declaration' => [
'<?php
class Foo
{
/** */
private ?string $foo;
}',
'<?php
class Foo
{
/**
*
*/
private ?string $foo;
}',
[
'property' => 'single',
],
];
yield 'It can handle properties with array type declaration' => [
'<?php
class Foo
{
/** @var string[] */
private array $foo;
}',
'<?php
class Foo
{
/**
* @var string[]
*/
private array $foo;
}',
[
'property' => 'single',
],
];
}
/**
* @dataProvider provideFix80Cases
*
* @requires PHP 8.0
*
* @param _AutogeneratedInputConfiguration $configuration
*/
public function testFix80(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix80Cases(): iterable
{
yield 'It handles constructor property promotion' => [
'<?php
class Foo
{
public function __construct(
/** @var string[] */
#[Attribute1]
private array $foo1,
/** @var string[] */
private array $foo2,
) {}
}',
'<?php
class Foo
{
public function __construct(
/**
* @var string[]
*/
#[Attribute1]
private array $foo1,
/**
* @var string[]
*/
private array $foo2,
) {}
}',
[
'property' => 'single',
],
];
yield 'It detects attributes between docblock and token' => [
'<?php
class Foo
{
/** @var string[] */
#[Attribute1]
private array $foo1;
/** @var string[] */
#[Attribute1]
#[Attribute2]
private array $foo2;
/** @var string[] */
#[Attribute1, Attribute2]
public array $foo3;
}',
'<?php
class Foo
{
/**
* @var string[]
*/
#[Attribute1]
private array $foo1;
/**
* @var string[]
*/
#[Attribute1]
#[Attribute2]
private array $foo2;
/**
* @var string[]
*/
#[Attribute1, Attribute2]
public array $foo3;
}',
[
'property' => 'single',
],
];
yield 'It handles class constants correctly' => [
'<?php
class Foo
{
/**
* 0
*/
#[Attribute1]
const B0 = "0";
/**
* 1
*/
#[Attribute1]
#[Attribute2]
public const B1 = "1";
/**
* 2
*/
#[Attribute1, Attribute2]
public const B2 = "2";
}
',
'<?php
class Foo
{
/** 0 */
#[Attribute1]
const B0 = "0";
/** 1 */
#[Attribute1]
#[Attribute2]
public const B1 = "1";
/** 2 */
#[Attribute1, Attribute2]
public const B2 = "2";
}
',
];
yield 'It handles class functions correctly' => [
'<?php
class Foo
{
/**
* @return void
*/
#[Attribute1]
public function hello1() {}
/**
* @return void
*/
#[Attribute1]
#[Attribute2]
public function hello2() {}
/**
* @return void
*/
#[Attribute1, Attribute2]
public function hello3() {}
}
',
'<?php
class Foo
{
/** @return void */
#[Attribute1]
public function hello1() {}
/** @return void */
#[Attribute1]
#[Attribute2]
public function hello2() {}
/** @return void */
#[Attribute1, Attribute2]
public function hello3() {}
}
',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*
* @param _AutogeneratedInputConfiguration $configuration
*/
public function testFix81(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFix81Cases(): iterable
{
yield 'It handles readonly properties correctly' => [
'<?php
class Foo
{
/** @var string[] */
private readonly array $foo1;
/** @var string[] */
readonly private array $foo2;
/** @var string[] */
readonly array $foo3;
}',
'<?php
class Foo
{
/**
* @var string[]
*/
private readonly array $foo1;
/**
* @var string[]
*/
readonly private array $foo2;
/**
* @var string[]
*/
readonly array $foo3;
}',
[
'property' => 'single',
],
];
yield 'It handles class constant correctly' => [
'<?php
class Foo
{
/**
* 0
*/
const B0 = "0";
/**
* 1
*/
final public const B1 = "1";
/**
* 2
*/
public final const B2 = "2";
/**
* 3
*/
final const B3 = "3";
}
',
'<?php
class Foo
{
/** 0 */
const B0 = "0";
/** 1 */
final public const B1 = "1";
/** 2 */
public final const B2 = "2";
/** 3 */
final const B3 = "3";
}
',
];
yield 'It handles enum functions correctly' => [
'<?php
enum Foo
{
/**
* @return void
*/
public function hello() {}
}
',
'<?php
enum Foo
{
/** @return void */
public function hello() {}
}
',
];
yield 'It handles enum function with attributes correctly' => [
'<?php
enum Foo
{
/**
* @return void
*/
#[Attribute1]
public function hello1() {}
/**
* @return void
*/
#[Attribute1]
#[Attribute2]
public function hello2() {}
/**
* @return void
*/
#[Attribute1, Attribute2]
public function hello3() {}
}
',
'<?php
enum Foo
{
/** @return void */
#[Attribute1]
public function hello1() {}
/** @return void */
#[Attribute1]
#[Attribute2]
public function hello2() {}
/** @return void */
#[Attribute1, Attribute2]
public function hello3() {}
}
',
];
}
/**
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, string}>
*/
public static function provideFix82Cases(): iterable
{
yield 'constant in trait' => [
<<<'PHP'
<?php
trait Foo {
/**
* @var string
*/
const Foo = 'foo';
}
PHP,
<<<'PHP'
<?php
trait Foo {
/** @var string */
const Foo = 'foo';
}
PHP,
];
}
/**
* @dataProvider provideFix84Cases
*
* @requires PHP 8.4
*/
public function testFix84(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFix84Cases(): iterable
{
yield 'asymmetric visibility' => [
<<<'PHP'
<?php class Foo
{
/**
* @var bool
*/
public public(set) bool $a;
/**
* @var bool
*/
public protected(set) bool $b;
/**
* @var bool
*/
public private(set) bool $c;
}
PHP,
<<<'PHP'
<?php class Foo
{
/** @var bool */
public public(set) bool $a;
/** @var bool */
public protected(set) bool $b;
/** @var bool */
public private(set) bool $c;
}
PHP,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/NoBlankLinesAfterPhpdocFixerTest.php | tests/Fixer/Phpdoc/NoBlankLinesAfterPhpdocFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\NoBlankLinesAfterPhpdocFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\NoBlankLinesAfterPhpdocFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class NoBlankLinesAfterPhpdocFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'simple example is not changed' => [<<<'EOF'
<?php
/**
* This is the bar class.
*/
class Bar
{
/**
* @return void
*/
public function foo()
{
//
}
}
EOF];
yield 'complex example is not changed' => [<<<'EOF'
<?php
/**
* This is the hello function.
* Yeh, this layout should be allowed.
* We're fixing lines following a docblock.
*/
function hello($foo) {}
/**
* This is the bar class.
*/
final class Bar
{
/**
* @return void
*/
public static function foo()
{
//
}
/**
* @return void
*/
static private function bar123() {}
/*
* This T_COMMENT should not be moved
*
* Only T_DOC_COMMENT should be moved
*/
final protected
// mixin' it up a bit
function baz() {
}
/*
* This T_COMMENT should not be moved
*
* Only T_DOC_COMMENT should be moved
*/
public function cool() {}
/**
* This is the first docblock
*
* Not removing blank line here.
* No element is being documented
*/
/**
* Another docblock
*/
public function silly() {}
}
EOF];
yield 'comments are not changed' => [<<<'EOF'
<?php
/*
* This file is part of xyz.
*
* License etc...
*/
namespace Foo\Bar;
EOF];
yield 'line before declare is not removed' => [<<<'EOF'
<?php
/**
* This is some license header.
*/
declare(strict_types=1);
EOF];
yield 'line before use statement is not removed' => [<<<'EOF'
<?php
/**
* This is some license header.
*/
use Foo\Bar;
EOF];
yield 'line before include is not removed' => [
<<<'EOF'
<?php
/**
* This describes what my script does.
*/
include 'vendor/autoload.php';
EOF,
];
yield 'line before include_once is not removed' => [
<<<'EOF'
<?php
/**
* This describes what my script does.
*/
include_once 'vendor/autoload.php';
EOF,
];
yield 'line before require is not removed' => [
<<<'EOF'
<?php
/**
* This describes what my script does.
*/
require 'vendor/autoload.php';
EOF,
];
yield 'line before require_once is not removed' => [
<<<'EOF'
<?php
/**
* This describes what my script does.
*/
require_once 'vendor/autoload.php';
EOF,
];
yield 'line with spaces is removed When next token is indented' => [
'<?php
/**
* PHPDoc with a line with space
*/
class Foo {}',
'<?php
/**
* PHPDoc with a line with space
*/
'.'
class Foo {}',
];
yield 'line With spaces is removed when next token is not indented' => [
'<?php
/**
* PHPDoc with a line with space
*/
class Foo {}',
'<?php
/**
* PHPDoc with a line with space
*/
'.'
class Foo {}',
];
yield 'simple class' => [
<<<'EOF'
<?php
/**
* This is the bar class.
*/
class Bar {}
EOF,
<<<'EOF'
<?php
/**
* This is the bar class.
*/
class Bar {}
EOF,
];
yield 'indented class' => [
<<<'EOF'
<?php
/**
*
*/
class Foo {
private $a;
}
EOF,
<<<'EOF'
<?php
/**
*
*/
class Foo {
private $a;
}
EOF,
];
yield 'others' => [
<<<'EOF'
<?php
/**
* Constant!
*/
const test = 'constant';
/**
* Foo!
*/
$foo = 123;
EOF,
<<<'EOF'
<?php
/**
* Constant!
*/
const test = 'constant';
/**
* Foo!
*/
$foo = 123;
EOF,
];
yield 'whitespace in docblock above namespace is not touched' => [<<<'EOF'
<?php
/**
* This is a file-level docblock.
*/
namespace Foo\Bar\Baz;
EOF];
yield 'windows style' => [
"<?php\r\n /** * Constant! */\n \$foo = 123;",
"<?php\r\n /** * Constant! */\r\n\r\n\r\n \$foo = 123;",
];
yield 'inline typehinting docs before flow break 1' => [
<<<'EOF'
<?php
function parseTag($tag)
{
$tagClass = get_class($tag);
if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
/** @var DocBlock\Tag\VarTag $tag */
return $tag->getDescription();
}
}
EOF,
];
yield 'inline typehinting docs before flow break 2' => [
<<<'EOF'
<?php
function parseTag($tag)
{
$tagClass = get_class($tag);
if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
/** @var DocBlock\Tag\VarTag $tag */
throw new Exception($tag->getDescription());
}
}
EOF,
];
yield 'inline typehinting docs before flow break 3' => [
<<<'EOF'
<?php
function parseTag($tag)
{
$tagClass = get_class($tag);
if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
/** @var DocBlock\Tag\VarTag $tag */
goto FOO;
}
FOO:
}
EOF,
];
yield 'inline typehinting docs before flow break 4' => [
<<<'EOF'
<?php
function parseTag($tag)
{
while (true) {
$tagClass = get_class($tag);
if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
/** @var DocBlock\Tag\VarTag $tag */
continue;
}
}
}
EOF,
];
yield 'inline typehinting docs before flow break 5' => [
<<<'EOF'
<?php
function parseTag($tag)
{
while (true) {
$tagClass = get_class($tag);
if ('phpDocumentor\Reflection\DocBlock\Tag\VarTag' === $tagClass) {
/** @var DocBlock\Tag\VarTag $tag */
break;
}
}
}
EOF,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocTrimConsecutiveBlankLineSeparationFixerTest.php | tests/Fixer/Phpdoc/PhpdocTrimConsecutiveBlankLineSeparationFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocTrimConsecutiveBlankLineSeparationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocTrimConsecutiveBlankLineSeparationFixer>
*
* @author Nobu Funaki <nobu.funaki@gmail.com>
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocTrimConsecutiveBlankLineSeparationFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'no changes' => ['<?php /** Summary. */'];
yield 'only Summary and Description' => [
'<?php
/**
* Summary.
*
* Description.
*
*
*
*/',
'<?php
/**
* Summary.
*
*
* Description.
*
*
*
*/',
];
yield 'basic phpdoc' => [
'<?php
/**
* Summary.
*
* Description.
*
* @var int
*
* @return int
*
* foo
*
* bar
*
*
*/',
'<?php
/**
* Summary.
*
*
* Description.
*
*
* @var int
*
*
*
*
* @return int
*
*
* foo
*
*
* bar
*
*
*/',
];
yield 'extra blank lines in description' => [
'<?php
/**
* Summary.
*
* Description has multiple blank lines:
*
*
*
* End.
*
* @var int
*/',
];
yield 'extra blank lines after annotation' => [
'<?php
/**
* Summary without description.
*
* @var int
*
* This is still @var annotation description...
*
* But this is not!
*
* @internal
*/',
'<?php
/**
* Summary without description.
*
*
* @var int
*
* This is still @var annotation description...
*
*
*
*
* But this is not!
*
*
*
*
*
* @internal
*/',
];
yield 'extra blank lines between annotations when no Summary no Description' => [
'<?php
/**
* @param string $expected
* @param string $input
*
* @dataProvider provideFix56Cases
*/',
'<?php
/**
* @param string $expected
* @param string $input
*
*
* @dataProvider provideFix56Cases
*/',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocNoPackageFixerTest.php | tests/Fixer/Phpdoc/PhpdocNoPackageFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocNoPackageFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocNoPackageFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocNoPackageFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'package' => [
<<<'EOF'
<?php
/**
*/
EOF,
<<<'EOF'
<?php
/**
* @package Foo\Bar
*/
EOF,
];
yield 'subpackage' => [
<<<'EOF'
<?php
/**
*/
EOF,
<<<'EOF'
<?php
/**
* @subpackage Foo\Bar\Baz
*/
EOF,
];
yield 'many' => [
<<<'EOF'
<?php
/**
* Hello!
*/
EOF,
<<<'EOF'
<?php
/**
* Hello!
* @package
* @subpackage
*/
EOF,
];
yield 'do nothing' => [
<<<'EOF'
<?php
/**
* @var package
*/
EOF,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocOrderFixerTest.php | tests/Fixer/Phpdoc/PhpdocOrderFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocOrderFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocOrderFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
* @author Jakub Kwaśniewski <jakub@zero-85.pl>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocOrderFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocOrderFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideInvalidConfigurationCases
*
* @param array<string, mixed> $configuration
*/
public function testInvalidConfiguration(array $configuration, string $expectedExceptionMessage): void
{
self::expectException(InvalidFixerConfigurationException::class);
self::expectExceptionMessage($expectedExceptionMessage);
$this->fixer->configure($configuration);
}
/**
* @return iterable<string, array{array<string, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield 'empty order' => [
['order' => []],
'The option "order" value is invalid. Minimum two tags are required.',
];
yield 'invalid order' => [
['order' => ['param']],
'The option "order" value is invalid. Minimum two tags are required.',
];
yield 'duplicated tag' => [
['order' => ['param', 'return', 'throws', 'return']],
'The option "order" value is invalid. Tag "return" is duplicated.',
];
yield 'duplicated tags' => [
['order' => ['param', 'return', 'throws', 'param', 'return', 'throws']],
'The option "order" value is invalid. Tags "param", "return" and "throws" are duplicated.',
];
}
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: ?string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'no changes' => [<<<'EOF'
<?php
/**
* Do some cool stuff.
*
* @param EngineInterface $templating
* @param string $name
*
* @throws Exception
*
* @return void|bar
*/
EOF];
yield 'only params 1' => [
<<<'EOF'
<?php
/**
* @param EngineInterface $templating
* @param string $name
*/
EOF,
null,
['order' => ['param', 'throw', 'return']],
];
yield 'only params 2' => [
<<<'EOF'
<?php
/**
* @param EngineInterface $templating
* @param string $name
*/
EOF,
null,
['order' => ['param', 'return', 'throw']],
];
yield 'only return 1' => [
<<<'EOF'
<?php
/**
*
* @return void|bar
*
*/
EOF,
null,
['order' => ['param', 'throw', 'return']],
];
yield 'only return 2' => [
<<<'EOF'
<?php
/**
*
* @return void|bar
*
*/
EOF,
null,
['order' => ['param', 'return', 'throw']],
];
yield 'empty 1' => [
'/***/',
null,
['order' => ['param', 'throw', 'return']],
];
yield 'empty 2' => [
'/***/',
null,
['order' => ['param', 'return', 'throw']],
];
yield 'no annotations 1' => [
<<<'EOF'
<?php
/**
*
*
*
*/
EOF,
null,
['order' => ['param', 'throw', 'return']],
];
yield 'no annotations 2' => [
<<<'EOF'
<?php
/**
*
*
*
*/
EOF,
null,
['order' => ['param', 'return', 'throw']],
];
yield 'basic case' => [
<<<'EOF'
<?php
/**
* @param string $foo
* @throws Exception
* @return bool
*/
EOF,
<<<'EOF'
<?php
/**
* @throws Exception
* @return bool
* @param string $foo
*/
EOF,
];
yield 'complete case' => [
<<<'EOF'
<?php
/**
* Hello there!
*
* Long description
* goes here.
*
* @internal
*
*
* @custom Test!
* asldnaksdkjasdasd
*
*
*
* @param string $foo
* @param bool $bar Bar
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
* @return bool Return false on failure.
* @return int Return the number of changes.
*/
EOF,
<<<'EOF'
<?php
/**
* Hello there!
*
* Long description
* goes here.
*
* @internal
*
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
*
* @custom Test!
* asldnaksdkjasdasd
*
*
* @return bool Return false on failure.
* @return int Return the number of changes.
*
* @param string $foo
* @param bool $bar Bar
*/
EOF,
];
yield 'example from Symfony' => [
<<<'EOF'
<?php
/**
* Renders a template.
*
* @param mixed $name A template name
* @param array $parameters An array of parameters to pass to the template
*
*
* @throws \InvalidArgumentException if the template does not exist
* @throws \RuntimeException if the template cannot be rendered
* @return string The evaluated template as a string
*/
EOF,
<<<'EOF'
<?php
/**
* Renders a template.
*
* @param mixed $name A template name
* @param array $parameters An array of parameters to pass to the template
*
* @return string The evaluated template as a string
*
* @throws \InvalidArgumentException if the template does not exist
* @throws \RuntimeException if the template cannot be rendered
*/
EOF,
];
yield 'no changes with Laravel style' => [
<<<'EOF'
<?php
/**
* Do some cool stuff.
*
* @param EngineInterface $templating
* @param string $name
*
* @return void|bar
*
* @throws Exception
*/
EOF,
null,
['order' => ['param', 'return', 'throws']],
];
yield 'basic case with Laravel style' => [
<<<'EOF'
<?php
/**
* @param string $foo
* @return bool
* @throws Exception
*/
EOF,
<<<'EOF'
<?php
/**
* @throws Exception
* @return bool
* @param string $foo
*/
EOF,
['order' => ['param', 'return', 'throws']],
];
yield 'complete case with Laravel style' => [
<<<'EOF'
<?php
/**
* Hello there!
*
* Long description
* goes here.
*
* @internal
*
*
* @custom Test!
* asldnaksdkjasdasd
*
*
*
* @param string $foo
* @param bool $bar Bar
* @return bool Return false on failure.
* @return int Return the number of changes.
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
*/
EOF,
<<<'EOF'
<?php
/**
* Hello there!
*
* Long description
* goes here.
*
* @internal
*
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
*
* @custom Test!
* asldnaksdkjasdasd
*
*
* @return bool Return false on failure.
* @return int Return the number of changes.
*
* @param string $foo
* @param bool $bar Bar
*/
EOF,
['order' => ['param', 'return', 'throws']],
];
yield 'example from Symfony with Laravel style' => [
<<<'EOF'
<?php
/**
* Renders a template.
*
* @param mixed $name A template name
* @param array $parameters An array of parameters to pass to the template
*
* @return string The evaluated template as a string
*
* @throws \InvalidArgumentException if the template does not exist
* @throws \RuntimeException if the template cannot be rendered
*/
EOF,
null,
['order' => ['param', 'return', 'throws']],
];
yield 'basic case with different order 1' => [
<<<'EOF'
<?php
/**
* @return bool
* @throws Exception
* @param string $foo
*/
EOF,
<<<'EOF'
<?php
/**
* @throws Exception
* @return bool
* @param string $foo
*/
EOF,
['order' => ['return', 'throws', 'param']],
];
yield 'basic case with different order 2' => [
<<<'EOF'
<?php
/**
* @throws Exception
* @return bool
* @param string $foo
*/
EOF,
null,
['order' => ['throws', 'return', 'param']],
];
yield 'complete case with custom order' => [
<<<'EOF'
<?php
/**
* Hello there!
*
* Long description
* goes here.
*
*
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
*
*
*
* @return bool Return false on failure.
* @return int Return the number of changes.
*
* @param string $foo
* @param bool $bar Bar
* @custom Test!
* asldnaksdkjasdasd
* @internal
*/
EOF,
<<<'EOF'
<?php
/**
* Hello there!
*
* Long description
* goes here.
*
* @internal
*
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
*
* @custom Test!
* asldnaksdkjasdasd
*
*
* @return bool Return false on failure.
* @return int Return the number of changes.
*
* @param string $foo
* @param bool $bar Bar
*/
EOF,
['order' => [
'throws',
'return',
'param',
'custom',
'internal',
]],
];
yield 'intepacuthre' => [
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @template T of Extension\Extension
* @param string $foo
* @param bool $bar Bar
* @param class-string<T> $id
* @custom Test!
* asldnaksdkjasdasd
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
* @return bool Return false on failure
* @return int Return the number of changes.
**/
EOF,
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @param string $foo
* @param bool $bar Bar
* @param class-string<T> $id
* @custom Test!
* asldnaksdkjasdasd
* @template T of Extension\Extension
* @return bool Return false on failure
* @return int Return the number of changes.
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
**/
EOF,
['order' => ['internal', 'template', 'param', 'custom', 'throws', 'return']],
];
yield 'pare' => [
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @custom Test!
* asldnaksdkjasdasd
* @template T of Extension\Extension
* @param string $foo
* @param bool $bar Bar
* @param class-string<T> $id
* @return bool Return false on failure
* @return int Return the number of changes.
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
**/
EOF,
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @return bool Return false on failure
* @return int Return the number of changes.
* @custom Test!
* asldnaksdkjasdasd
* @template T of Extension\Extension
* @param string $foo
* @param bool $bar Bar
* @param class-string<T> $id
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
**/
EOF,
['order' => ['param', 'return']],
];
yield 'pareth' => [
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @custom Test!
* asldnaksdkjasdasd
* @template T of Extension\Extension
* @param string $foo
* @param bool $bar Bar
* @param class-string<T> $id
* @return bool Return false on failure
* @return int Return the number of changes.
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
**/
EOF,
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @return bool Return false on failure
* @return int Return the number of changes.
* @custom Test!
* asldnaksdkjasdasd
* @template T of Extension\Extension
* @param string $foo
* @param bool $bar Bar
* @param class-string<T> $id
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
**/
EOF,
['order' => ['param', 'return', 'throws']],
];
yield 'pathre' => [
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @custom Test!
* asldnaksdkjasdasd
* @template T of Extension\Extension
* @param string $foo
* @param bool $bar Bar
* @param class-string<T> $id
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
* @return bool Return false on failure
* @return int Return the number of changes.
**/
EOF,
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @return bool Return false on failure
* @return int Return the number of changes.
* @custom Test!
* asldnaksdkjasdasd
* @template T of Extension\Extension
* @param string $foo
* @param bool $bar Bar
* @param class-string<T> $id
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
**/
EOF,
['order' => ['param', 'throws', 'return']],
];
yield 'tepathre' => [
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @template T of Extension\Extension
* @param string $foo
* @param bool $bar Bar
* @param class-string<T> $id
* @custom Test!
* asldnaksdkjasdasd
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
* @return bool Return false on failure
* @return int Return the number of changes.
**/
EOF,
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @return bool Return false on failure
* @return int Return the number of changes.
* @param string $foo
* @param bool $bar Bar
* @param class-string<T> $id
* @template T of Extension\Extension
* @custom Test!
* asldnaksdkjasdasd
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
**/
EOF,
['order' => ['template', 'param', 'throws', 'return']],
];
yield 'tepathre2' => [
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @template T of Extension\Extension
* @param string $foo
* @param bool $bar Bar
* @param class-string<T> $id
* @custom Test!
* asldnaksdkjasdasd
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
* @return bool Return false on failure
* @return int Return the number of changes.
**/
EOF,
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @param string $foo
* @param bool $bar Bar
* @param class-string<T> $id
* @return bool Return false on failure
* @return int Return the number of changes.
* @template T of Extension\Extension
* @custom Test!
* asldnaksdkjasdasd
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
**/
EOF,
['order' => ['template', 'param', 'throws', 'return']],
];
yield 'multiline phpstan-return when phpstan order specified' => [
<<<'EOF'
<?php
/**
* Returns next token if it's type equals to expected.
*
* @return array
* @phpstan-param TTokenType $type
* @phpstan-return (
* $type is 'TableRow'
* ? TTableRowToken
* : TOtherToken
* )
* @throws ParserException
**/
EOF,
null,
['order' => ['param', 'return', 'phpstan-param', 'phpstan-return', 'throws']],
];
yield 'multiple phpstan-param when phpstan order specified' => [
<<<'EOF'
<?php
/**
* @phpstan-param non-empty-list<TGitHubReleaseArray> $releases
* @phpstan-param TCurrentVersionArray $currentVersion
* @phpstan-return TGitHubReleaseArray|false
*/
EOF,
null,
['order' => ['param', 'return', 'phpstan-param', 'phpstan-return', 'throws']],
];
yield 'mixed param, phpstan-param and psalm-param with order specified' => [
<<<'EOF'
<?php
/**
* @param array $releases
* @param array $currentVersion
* @psalm-param list<array> $releases
* @psalm-param array<string> $releases
* @phpstan-param non-empty-list<TGitHubReleaseArray> $releases
* @phpstan-param TCurrentVersionArray $currentVersion
* @return array|false
* @phpstan-return TGitHubReleaseArray|false
*/
EOF,
null,
['order' => ['param', 'psalm-param', 'phpstan-param', 'return']],
];
yield 'phpstan- / psalm- annotations follow specified order for non-prefixed version by default' => [
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @phpstan-template T of Extension\Extension
* @param string $foo
* @param bool $bar Bar
* @param string $id
* @phpstan-param TFooType $foo
* @custom Test!
* asldnaksdkjasdasd
* @phpstan-param class-string<T> $id
* @psalm-param 'foo'|'bar' $foo
* @psalm-param class-string<T> $id
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
* @return array Some array of results
* @phpstan-return TSomeResultArray
* @psalm-return array{
* foo: string,
* bar: ?int
* } Some array of results
**/
EOF,
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @param string $foo
* @phpstan-return TSomeResultArray
* @return array Some array of results
* @param bool $bar Bar
* @psalm-return array{
* foo: string,
* bar: ?int
* } Some array of results
* @param string $id
* @phpstan-template T of Extension\Extension
* @psalm-param 'foo'|'bar' $foo
* @phpstan-param TFooType $foo
* @psalm-param class-string<T> $id
* @custom Test!
* asldnaksdkjasdasd
* @phpstan-param class-string<T> $id
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
**/
EOF,
['order' => ['template', 'param', 'throws', 'return']],
];
yield 'retains phpstan- / psalm- annotations that are not prefixed versions of configured order' => [
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @psalm-ignore-nullable-return
* @param string $id
* @phpstan-param class-string<T> $id
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
* @return array Some array of results
* @psalm-return TSomeResultArray
* @phpstan-pure
**/
EOF,
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @internal
* @psalm-ignore-nullable-return
* @return array Some array of results
* @phpstan-param class-string<T> $id
* @param string $id
* @psalm-return TSomeResultArray
* @throws Exception|RuntimeException dfsdf
* jkaskdnaksdnkasndansdnansdajsdnkasd
* @phpstan-pure
**/
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | true |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php | tests/Fixer/Phpdoc/PhpdocNoEmptyReturnFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocNoEmptyReturnFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocNoEmptyReturnFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocNoEmptyReturnFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield 'void' => [
<<<'EOF'
<?php
/**
*/
EOF,
<<<'EOF'
<?php
/**
* @return void
*/
EOF,
];
yield 'null' => [
<<<'EOF'
<?php
/**
*/
EOF,
<<<'EOF'
<?php
/**
* @return null
*/
EOF,
];
yield 'null with end on the same line' => [
<<<'EOF'
<?php
/**
*/
EOF,
<<<'EOF'
<?php
/**
* @return null */
EOF,
];
yield 'null with end on the same line no space' => [
<<<'EOF'
<?php
/**
*/
EOF,
<<<'EOF'
<?php
/**
* @return null*/
EOF,
];
yield 'void case insensitive' => [
<<<'EOF'
<?php
/**
*/
EOF,
<<<'EOF'
<?php
/**
* @return vOId
*/
EOF,
];
yield 'null case insensitive' => [
<<<'EOF'
<?php
/**
*/
EOF,
<<<'EOF'
<?php
/**
* @return nULl
*/
EOF,
];
yield 'full' => [
<<<'EOF'
<?php
/**
* Hello!
*
* @param string $foo
*/
EOF,
<<<'EOF'
<?php
/**
* Hello!
*
* @param string $foo
* @return void
*/
EOF,
];
yield 'do nothing' => [<<<'EOF'
<?php
/**
* @var null
*/
EOF];
yield 'do nothing again' => [<<<'EOF'
<?php
/**
* @return null|int
*/
EOF];
yield 'other do nothing' => [<<<'EOF'
<?php
/**
* @return int|null
*/
EOF];
yield 'yet another do nothing' => [<<<'EOF'
<?php
/**
* @return null[]|string[]
*/
EOF];
yield 'handle single line phpdoc' => [
<<<'EOF'
<?php
EOF,
<<<'EOF'
<?php
/** @return null */
EOF,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocReturnSelfReferenceFixerTest.php | tests/Fixer/Phpdoc/PhpdocReturnSelfReferenceFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocReturnSelfReferenceFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocReturnSelfReferenceFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php interface A{/** @return $this */public function test();}',
'<?php interface A{/** @return this */public function test();}',
];
yield [
'<?php interface B{/** @return self|int */function test();}',
'<?php interface B{/** @return $SELF|int */function test();}',
];
yield [
'<?php class D {} /** @return {@this} */ require_once($a);echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;',
];
yield [
'<?php /** @return this */ require_once($a);echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1;echo 1; class E {}',
];
yield [
'<?php
trait SomeTrait
{
/** @return $this */
public function someTest(): self
{
return $this;
}
}
// class Foo { use Bla; } $a = (new Foo())->someTest();',
'<?php
trait SomeTrait
{
/** @return this */
public function someTest(): self
{
return $this;
}
}
// class Foo { use Bla; } $a = (new Foo())->someTest();',
];
yield [
'<?php interface C{/** @return $self|int */function test();}',
null,
['replacements' => ['$static' => 'static']],
];
foreach (self::provideReplacements() as [$expected,$input]) {
yield [
\sprintf('<?php
/**
* Please do not use @return %s|static|self|this|$static|$self|@static|@self|@this as return type declaration
*/
class F
{
/**
* @param %s
*
* @return %s
*/
public function AB($self)
{
return $this; // %s
}
}
', $input, $input, $expected, $input),
\sprintf('<?php
/**
* Please do not use @return %s|static|self|this|$static|$self|@static|@self|@this as return type declaration
*/
class F
{
/**
* @param %s
*
* @return %s
*/
public function AB($self)
{
return $this; // %s
}
}
', $input, $input, $input, $input),
['replacements' => [$input => $expected]],
];
}
yield 'anonymous class' => [
'<?php
$a = new class() {
/** @return $this */
public function a() {
}
};
class C
{
public function A()
{
$a = new class() {
/** @return $this */
public function a() {}
};
}
}
',
'<?php
$a = new class() {
/** @return @this */
public function a() {
}
};
class C
{
public function A()
{
$a = new class() {
/** @return @this */
public function a() {}
};
}
}
',
];
}
/**
* @param array<string, mixed> $configuration
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(array $configuration, string $message): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches(\sprintf('/^\[phpdoc_return_self_reference\] %s$/', preg_quote($message, '/')));
$this->fixer->configure($configuration);
}
/**
* @return iterable<int, array{array<string, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield [
['replacements' => [1 => 'a']],
'Invalid configuration: Unknown key "integer#1", expected any of "this", "@this", "$self", "@self", "$static" and "@static".',
];
yield [
['replacements' => [
'this' => 'foo',
]],
'Invalid configuration: Unknown value "string#foo", expected any of "$this", "static" and "self".',
];
}
/**
* @dataProvider provideFix81Cases
*
* @requires PHP 8.1
*/
public function testFix81(string $expected, string $input): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, string}>
*/
public static function provideFix81Cases(): iterable
{
yield [
'<?php
enum Foo {
case CAT;
/** @return $this */
public function test(): self {
return $this;
}
}
var_dump(Foo::CAT->test());
',
'<?php
enum Foo {
case CAT;
/** @return this */
public function test(): self {
return $this;
}
}
var_dump(Foo::CAT->test());
',
];
}
/**
* @return iterable<int, array{string, string}>
*/
private static function provideReplacements(): iterable
{
yield ['$this', 'this'];
yield ['$this', '@this'];
yield ['self', '$self'];
yield ['self', '@self'];
yield ['static', '$static'];
yield ['static', '@STATIC'];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocTrimFixerTest.php | tests/Fixer/Phpdoc/PhpdocTrimFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocTrimFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocTrimFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocTrimFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield [
<<<'EOF'
<?php
/**
* @param EngineInterface $templating
*
* @return void
*/
EOF,
];
yield [
'<?php
/**
* @return int количество деактивированных
*/
function deactivateCompleted()
{
return 0;
}',
];
yield [
(string) mb_convert_encoding('
<?php
/**
* Test à
*/
function foo(){}
', 'Windows-1252', 'UTF-8'),
];
yield [
<<<'EOF'
<?php
/**
* Hello there!
* @internal
*@param string $foo
*@throws Exception
*
*
*
* @return bool
*/
EOF,
<<<'EOF'
<?php
/**
*
*
* Hello there!
* @internal
*@param string $foo
*@throws Exception
*
*
*
* @return bool
*
*
*/
EOF,
];
yield [
<<<'EOF'
<?php
namespace Foo;
/**
* This is a class that does classy things.
*
* @internal
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
* @author Graham Campbell <hello@gjcampbell.co.uk>
*/
class Bar {}
EOF,
<<<'EOF'
<?php
namespace Foo;
/**
*
*
* This is a class that does classy things.
*
* @internal
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
* @author Graham Campbell <hello@gjcampbell.co.uk>
*
*
*
*/
class Bar {}
EOF,
];
yield 'empty doc block' => [<<<'EOF'
<?php
/**
*
*/
EOF];
yield 'empty larger doc block' => [
<<<'EOF'
<?php
/**
*
*/
EOF,
<<<'EOF'
<?php
/**
*
*
*
*
*/
EOF,
];
yield 'super simple doc block start' => [
<<<'EOF'
<?php
/**
* Test.
*/
EOF,
<<<'EOF'
<?php
/**
*
* Test.
*/
EOF,
];
yield 'super simple doc block end' => [
<<<'EOF'
<?php
/**
* Test.
*/
EOF,
<<<'EOF'
<?php
/**
* Test.
*
*/
EOF,
];
yield 'with lines without asterisk' => [<<<'EOF'
<?php
/**
* Foo
Baz
*/
class Foo
{
}
EOF];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/GeneralPhpdocTagRenameFixerTest.php | tests/Fixer/Phpdoc/GeneralPhpdocTagRenameFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocTagRenameFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocTagRenameFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\GeneralPhpdocTagRenameFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class GeneralPhpdocTagRenameFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
/**
* @inheritdocs
* @inheritDocs
* {@inheritdocs}
* {@inheritDocs}
* @see Foo::bar()
* {@see Foo::bar()}
*/',
];
yield [
'<?php
/**
* @inheritDoc
* @inheritDoc
* {@inheritDoc}
* {@inheritDoc}
* @see Foo::bar()
* {@see Foo::bar()}
*/',
'<?php
/**
* @inheritdocs
* @inheritDocs
* {@inheritdocs}
* {@inheritDocs}
* @see Foo::bar()
* {@see Foo::bar()}
*/',
[
'replacements' => ['inheritDocs' => 'inheritDoc'],
],
];
yield [
'<?php
/**
* @inheritdoc
* @inheritdoc
* {@inheritdoc}
* {@inheritdoc}
* @see Foo::bar()
* {@see Foo::bar()}
*/',
'<?php
/**
* @inheritdocs
* @inheritDocs
* {@inheritdocs}
* {@inheritDocs}
* @see Foo::bar()
* {@see Foo::bar()}
*/',
[
'fix_annotation' => true,
'fix_inline' => true,
'replacements' => ['inheritdocs' => 'inheritdoc'],
'case_sensitive' => false,
],
];
yield [
'<?php
/**
* @inheritDoc
* @inheritDoc
* {@inheritdocs}
* {@inheritDocs}
* @see Foo::bar()
* {@see Foo::bar()}
*/',
'<?php
/**
* @inheritdocs
* @inheritDocs
* {@inheritdocs}
* {@inheritDocs}
* @see Foo::bar()
* {@see Foo::bar()}
*/',
[
'fix_inline' => false,
'replacements' => ['inheritDocs' => 'inheritDoc'],
],
];
yield [
'<?php
/**
* @inheritdocs
* @inheritDocs
* {@inheritDoc}
* {@inheritDoc}
* @see Foo::bar()
* {@see Foo::bar()}
*/',
'<?php
/**
* @inheritdocs
* @inheritDocs
* {@inheritdocs}
* {@inheritDocs}
* @see Foo::bar()
* {@see Foo::bar()}
*/',
[
'fix_annotation' => false,
'replacements' => ['inheritDocs' => 'inheritDoc'],
],
];
yield [
'<?php
/**
* @inheritdocs
* @inheritDoc
* {@inheritdocs}
* {@inheritDoc}
* @see Foo::bar()
* {@see Foo::bar()}
*/',
'<?php
/**
* @inheritdocs
* @inheritDocs
* {@inheritdocs}
* {@inheritDocs}
* @see Foo::bar()
* {@see Foo::bar()}
*/',
[
'case_sensitive' => true,
'replacements' => ['inheritDocs' => 'inheritDoc'],
],
];
yield [
'<?php
/**
* @inheritdoc
* @inheritdoc
* {@inheritdoc}
* {@inheritdoc}
* @see Foo::bar()
* {@see Foo::bar()}
*/',
'<?php
/**
* @inheritdocs
* @inheritDocs
* {@inheritdocs}
* {@inheritDocs}
* @link Foo::bar()
* {@link Foo::bar()}
*/',
[
'replacements' => [
'inheritdocs' => 'inheritdoc',
'link' => 'see',
],
],
];
yield [
'<?php
/**
* @var int $foo
* @Annotation("@type")
*/',
'<?php
/**
* @type int $foo
* @Annotation("@type")
*/',
[
'fix_annotation' => true,
'fix_inline' => true,
'replacements' => [
'type' => 'var',
],
],
];
yield [
'<?php
/**
* @var int $foo
* @Annotation(\'@type\')
* @Annotation("@type")
*/',
'<?php
/**
* @type int $foo
* @Annotation(\'@type\')
* @Annotation("@type")
*/',
[
'fix_annotation' => true,
'fix_inline' => false,
'replacements' => [
'type' => 'var',
],
],
];
}
/**
* @param array<string, mixed> $config
*
* @dataProvider provideInvalidConfigurationCases
*/
public function testInvalidConfiguration(array $config, string $message): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessage($message);
$this->fixer->configure($config);
}
/**
* @return iterable<array{array<string, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield 'invalid option' => [
['replacements' => true],
'[general_phpdoc_tag_rename] Invalid configuration: The option "replacements" with value true is expected to be of type "string[]", but is of type "bool".',
];
yield 'unknown option' => [
['foo' => true],
'[general_phpdoc_tag_rename] Invalid configuration: The option "foo" does not exist. Defined options are: "case_sensitive", "fix_annotation", "fix_inline", "replacements".',
];
yield [
[
'replacements' => [1 => 'abc'],
'case_sensitive' => true,
],
'[general_phpdoc_tag_rename] Invalid configuration: Tag to replace must be a string.',
];
yield [
[
'replacements' => ['a' => null],
'case_sensitive' => true,
],
'[general_phpdoc_tag_rename] Invalid configuration: The option "replacements" with value array is expected to be of type "string[]", but one of the elements is of type "null".',
];
yield [
[
'replacements' => ['see' => 'link*/'],
'case_sensitive' => true,
],
'[general_phpdoc_tag_rename] Invalid configuration: Tag "see" cannot be replaced by invalid tag "link*/".',
];
yield [
[
'replacements' => ['link' => 'see', 'a' => 'b', 'see' => 'link'],
'case_sensitive' => true,
],
'[general_phpdoc_tag_rename] Invalid configuration: Cannot change tag "link" to tag "see", as the tag "see" is configured to be replaced to "link".',
];
yield [
[
'replacements' => ['b' => 'see', 'see' => 'link', 'link' => 'b'],
'case_sensitive' => true,
],
'[general_phpdoc_tag_rename] Invalid configuration: Cannot change tag "b" to tag "see", as the tag "see" is configured to be replaced to "link".',
];
yield [
[
'replacements' => ['see' => 'link', 'link' => 'b'],
'case_sensitive' => true,
],
'[general_phpdoc_tag_rename] Invalid configuration: Cannot change tag "see" to tag "link", as the tag "link" is configured to be replaced to "b".',
];
yield [
[
'replacements' => ['Foo' => 'bar', 'foo' => 'baz'],
'case_sensitive' => false,
],
'[general_phpdoc_tag_rename] Invalid configuration: Tag "foo" cannot be configured to be replaced with several different tags when case sensitivity is off.',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocSummaryFixerTest.php | tests/Fixer/Phpdoc/PhpdocSummaryFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
use PhpCsFixer\WhitespacesFixerConfig;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocSummaryFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocSummaryFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocSummaryFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, ?WhitespacesFixerConfig $whitespacesConfig = null): void
{
$this->fixer->setWhitespacesConfig($whitespacesConfig ?? new WhitespacesFixerConfig());
$this->doTest($expected, $input);
}
/**
* @return iterable<string, array{0: string, 1?: string, 2?: WhitespacesFixerConfig}>
*/
public static function provideFixCases(): iterable
{
yield 'with trailing space' => [
'<?php
/**
* Test.
*/',
'<?php
/**
* Test '.'
*/',
];
yield 'with period' => [
<<<'EOF'
<?php
/**
* Hello there.
*/
EOF,
<<<'EOF'
<?php
/**
* Hello there
*/
EOF,
];
yield 'with question mark' => [<<<'EOF'
<?php
/**
* Hello?
*/
EOF];
yield 'with exclamation mark' => [<<<'EOF'
<?php
/**
* Hello!
*/
EOF];
yield 'with inverted question mark' => [<<<'EOF'
<?php
/**
* Hello¿
*/
EOF];
yield 'with inverted exclamation mark' => [<<<'EOF'
<?php
/**
* Hello¡
*/
EOF];
yield 'with unicode question mark' => [<<<'EOF'
<?php
/**
* ハロー?
*/
EOF];
yield 'with unicode exclamation mark' => [<<<'EOF'
<?php
/**
* ハロー!
*/
EOF];
yield 'with Japanese period' => [<<<'EOF'
<?php
/**
* ハロー。
*/
EOF];
yield 'with inc blank' => [
<<<'EOF'
<?php
/**
* Hi.
*
*/
EOF,
<<<'EOF'
<?php
/**
* Hi
*
*/
EOF,
];
yield 'multiline' => [<<<'EOF'
<?php
/**
* Hello
* there.
*/
EOF,
<<<'EOF'
<?php
/**
* Hello
* there
*/
EOF,
];
yield 'with list' => [<<<'EOF'
<?php
/**
* Options:
* * a: aaa
* * b: bbb
* * c: ccc
*/
/**
* Options:
*
* * a: aaa
* * b: bbb
* * c: ccc
*/
EOF];
yield 'with tags' => [
<<<'EOF'
<?php
/**
* Hello there.
*
* @param string $foo
*
* @return bool
*/
EOF,
<<<'EOF'
<?php
/**
* Hello there
*
* @param string $foo
*
* @return bool
*/
EOF,
];
yield 'with long description' => [
<<<'EOF'
<?php
/**
* Hello there.
*
* Long description
* goes here.
*
* @return bool
*/
EOF,
<<<'EOF'
<?php
/**
* Hello there
*
* Long description
* goes here.
*
* @return bool
*/
EOF,
];
yield 'crazy multiline comments' => [
<<<'EOF'
<?php
/**
* Clients accept an array of constructor parameters.
*
* Here's an example of creating a client using a URI template for the
* client's base_url and an array of default request options to apply
* to each request:
*
* $client = new Client([
* 'base_url' => [
* 'https://www.foo.com/{version}/',
* ['version' => '123']
* ],
* 'defaults' => [
* 'timeout' => 10,
* 'allow_redirects' => false,
* 'proxy' => '192.168.16.1:10'
* ]
* ]);
*
* @param _AutogeneratedInputConfiguration $config Client configuration settings
* - base_url: Base URL of the client that is merged into relative URLs.
* Can be a string or an array that contains a URI template followed
* by an associative array of expansion variables to inject into the
* URI template.
* - handler: callable RingPHP handler used to transfer requests
* - message_factory: Factory used to create request and response object
* - defaults: Default request options to apply to each request
* - emitter: Event emitter used for request events
* - fsm: (internal use only) The request finite state machine. A
* function that accepts a transaction and optional final state. The
* function is responsible for transitioning a request through its
* lifecycle events.
* @param string $foo
*/
EOF,
<<<'EOF'
<?php
/**
* Clients accept an array of constructor parameters
*
* Here's an example of creating a client using a URI template for the
* client's base_url and an array of default request options to apply
* to each request:
*
* $client = new Client([
* 'base_url' => [
* 'https://www.foo.com/{version}/',
* ['version' => '123']
* ],
* 'defaults' => [
* 'timeout' => 10,
* 'allow_redirects' => false,
* 'proxy' => '192.168.16.1:10'
* ]
* ]);
*
* @param _AutogeneratedInputConfiguration $config Client configuration settings
* - base_url: Base URL of the client that is merged into relative URLs.
* Can be a string or an array that contains a URI template followed
* by an associative array of expansion variables to inject into the
* URI template.
* - handler: callable RingPHP handler used to transfer requests
* - message_factory: Factory used to create request and response object
* - defaults: Default request options to apply to each request
* - emitter: Event emitter used for request events
* - fsm: (internal use only) The request finite state machine. A
* function that accepts a transaction and optional final state. The
* function is responsible for transitioning a request through its
* lifecycle events.
* @param string $foo
*/
EOF,
];
yield 'with no description' => [<<<'EOF'
<?php
/**
* @return bool
*/
EOF];
yield 'inheritdoc in braces' => [
'<?php
/**
* {@inheritdoc}
*/
',
];
yield 'inheritdoc' => [
'<?php
/**
* @inheritDoc
*/
',
];
yield 'empty doc block' => [<<<'EOF'
<?php
/**
*
*/
EOF];
yield 'tabs and windows line endings' => [
"<?php\r\n\t/**\r\n\t * Hello there.\r\n\t */",
"<?php\r\n\t/**\r\n\t * Hello there\r\n\t */",
new WhitespacesFixerConfig("\t", "\r\n"),
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocListTypeFixerTest.php | tests/Fixer/Phpdoc/PhpdocListTypeFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocListTypeFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocListTypeFixer>
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocListTypeFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{string, 1?: string}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php /** @tagNotSupportingTypes string[] */'];
yield ['<?php /** @var array<string, int> */'];
yield ['<?php /** @var array<int, array<string, bool>> */'];
yield ['<?php /** @var array<class-string<Foo>, bool> */'];
yield ['<?php /** @var array<int<1, 10>, bool> */'];
yield ['<?php /** @var array<Foo::BAR_*, bool> */'];
yield ['<?php /** @var array<\'foo\'|\'bar\', bool> */'];
yield ['<?php /** @var array{} */'];
yield ['<?php /** @var array{string, string, string} */'];
yield ['<?php /** @var array{"a", "b[]", "c"} */'];
yield ["<?php /** @var array{'a', 'b[]', 'c'} */"];
yield [
'<?php /** @var list<Foo> */',
'<?php /** @var array<Foo> */',
];
yield [
'<?php /** @var list<Foo> */',
'<?php /** @var ARRAY<Foo> */',
];
yield [
'<?php /** @var ?list<Foo> */',
'<?php /** @var ?array<Foo> */',
];
yield [
'<?php /** @var list<bool>|list<float>|list<int>|list<string> */',
'<?php /** @var array<bool>|list<float>|array<int>|list<string> */',
];
yield [
'<?php /** @var non-empty-list<string> */',
'<?php /** @var non-empty-array<string> */',
];
yield [
'<?php /** @var array{string, list<array{Foo, list<int>, Bar}>} */',
'<?php /** @var array{string, array<array{Foo, array<int>, Bar}>} */',
];
yield [
'<?php /** @var list<int<1, 10>> */',
'<?php /** @var array<int<1, 10>> */',
];
yield [<<<'EOD'
<?php
/** @var list<Foo> */
/** @var \SplFixedArray<Foo> */
/** @var \KůňArray<Foo> */
/** @var \My_Array<Foo> */
/** @var \My2Array<Foo> */
EOD, <<<'EOD'
<?php
/** @var array<Foo> */
/** @var \SplFixedArray<Foo> */
/** @var \KůňArray<Foo> */
/** @var \My_Array<Foo> */
/** @var \My2Array<Foo> */
EOD,
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocSeparationFixerTest.php | tests/Fixer/Phpdoc/PhpdocSeparationFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocSeparationFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocSeparationFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
* @author Jakub Kwaśniewski <jakub@zero-85.pl>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocSeparationFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocSeparationFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield ['<?php
/** @param EngineInterface $templating
*@return void
*/'];
yield [
<<<'EOF'
<?php
/**
* @param EngineInterface $templating
*
* @return void
*/
EOF,
<<<'EOF'
<?php
/**
* @param EngineInterface $templating
* @return void
*/
EOF,
];
yield 'more tags' => [
<<<'EOF'
<?php
/**
* Hello there!
*
* @internal
*
* @param string $foo
*
* @throws Exception
*
* @return bool
*/
EOF,
<<<'EOF'
<?php
/**
* Hello there!
* @internal
* @param string $foo
* @throws Exception
*
*
*
* @return bool
*/
EOF,
];
yield 'spread out' => [
<<<'EOF'
<?php
/**
* Hello there!
*
* Long description
* goes here.
*
* @param string $foo
* @param bool $bar Bar
*
* @throws Exception|RuntimeException
*
* @return bool
*/
EOF,
<<<'EOF'
<?php
/**
* Hello there!
*
* Long description
* goes here.
* @param string $foo
*
*
* @param bool $bar Bar
*
*
*
* @throws Exception|RuntimeException
*
*
*
*
* @return bool
*/
EOF,
];
yield 'multiline comments' => [
<<<'EOF'
<?php
/**
* Hello there!
*
* Long description
* goes here.
*
* @param string $foo test 123
* asdasdasd
* @param bool $bar qwerty
*
* @throws Exception|RuntimeException
*
* @return bool
*/
EOF,
<<<'EOF'
<?php
/**
* Hello there!
*
* Long description
* goes here.
* @param string $foo test 123
* asdasdasd
* @param bool $bar qwerty
* @throws Exception|RuntimeException
* @return bool
*/
EOF,
];
yield 'crazy multiline comments' => [
<<<'EOF'
<?php
/**
* Clients accept an array of constructor parameters.
*
* Here's an example of creating a client using a URI template for the
* client's base_url and an array of default request options to apply
* to each request:
*
* $client = new Client([
* 'base_url' => [
* 'https://www.foo.com/{version}/',
* ['version' => '123']
* ],
* 'defaults' => [
* 'timeout' => 10,
* 'allow_redirects' => false,
* 'proxy' => '192.168.16.1:10'
* ]
* ]);
*
* @param _AutogeneratedInputConfiguration $config Client configuration settings
* - base_url: Base URL of the client that is merged into relative URLs.
* Can be a string or an array that contains a URI template followed
* by an associative array of expansion variables to inject into the
* URI template.
* - handler: callable RingPHP handler used to transfer requests
* - message_factory: Factory used to create request and response object
* - defaults: Default request options to apply to each request
* - emitter: Event emitter used for request events
* - fsm: (internal use only) The request finite state machine. A
* function that accepts a transaction and optional final state. The
* function is responsible for transitioning a request through its
* lifecycle events.
* @param string $foo
*/
EOF,
];
yield 'Doctrine example' => [
<<<'EOF'
<?php
/**
* PersistentObject base class that implements getter/setter methods for all mapped fields and associations
* by overriding __call.
*
* This class is a forward compatible implementation of the PersistentObject trait.
*
* Limitations:
*
* 1. All persistent objects have to be associated with a single ObjectManager, multiple
* ObjectManagers are not supported. You can set the ObjectManager with `PersistentObject#setObjectManager()`.
* 2. Setters and getters only work if a ClassMetadata instance was injected into the PersistentObject.
* This is either done on `postLoad` of an object or by accessing the global object manager.
* 3. There are no hooks for setters/getters. Just implement the method yourself instead of relying on __call().
* 4. Slower than handcoded implementations: An average of 7 method calls per access to a field and 11 for an association.
* 5. Only the inverse side associations get autoset on the owning side as well. Setting objects on the owning side
* will not set the inverse side associations.
*
* @example
*
* PersistentObject::setObjectManager($em);
*
* class Foo extends PersistentObject
* {
* private $id;
* }
*
* $foo = new Foo();
* $foo->getId(); // method exists through __call
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
EOF,
];
yield 'Symfony example' => [<<<'EOF'
<?php
/**
* Constructor.
*
* Depending on how you want the storage driver to behave you probably
* want to override this constructor entirely.
*
* List of options for $options array with their defaults.
*
* @see https://php.net/session.configuration for options
*
* but we omit 'session.' from the beginning of the keys for convenience.
*
* ("auto_start", is not supported as it tells PHP to start a session before
* PHP starts to execute user-land code. Setting during runtime has no effect).
*
* cache_limiter, "nocache" (use "0" to prevent headers from being sent entirely).
* cookie_domain, ""
* cookie_httponly, ""
* cookie_lifetime, "0"
* cookie_path, "/"
* cookie_secure, ""
* entropy_file, ""
* entropy_length, "0"
* gc_divisor, "100"
* gc_maxlifetime, "1440"
* gc_probability, "1"
* hash_bits_per_character, "4"
* hash_function, "0"
* name, "PHPSESSID"
* referer_check, ""
* serialize_handler, "php"
* use_cookies, "1"
* use_only_cookies, "1"
* use_trans_sid, "0"
* upload_progress.enabled, "1"
* upload_progress.cleanup, "1"
* upload_progress.prefix, "upload_progress_"
* upload_progress.name, "PHP_SESSION_UPLOAD_PROGRESS"
* upload_progress.freq, "1%"
* upload_progress.min-freq, "1"
* url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
*
* @param array $options Session configuration options.
* @param AbstractProxy|NativeSessionHandler|\SessionHandlerInterface|null $handler
* @param MetadataBag $metaBag MetadataBag.
*/
EOF
];
yield '@deprecated and @see tags' => [
<<<'EOF'
<?php
/**
* Hi!
*
* @author Bar Baz <foo@example.com>
*
* @deprecated As of some version.
* @see Replacement
* described here.
*
* @param string $foo test 123
* @param bool $bar qwerty
*
* @return void
*/
EOF,
<<<'EOF'
<?php
/**
* Hi!
*
* @author Bar Baz <foo@example.com>
* @deprecated As of some version.
*
* @see Replacement
* described here.
* @param string $foo test 123
* @param bool $bar qwerty
*
* @return void
*/
EOF,
];
yield 'property tags' => [
<<<'EOF'
<?php
/**
* @author Bar Baz <foo@example.com>
*
* @property int $foo
* @property-read int $foo
* @property-write int $bar
*/
EOF,
<<<'EOF'
<?php
/**
* @author Bar Baz <foo@example.com>
* @property int $foo
*
* @property-read int $foo
*
* @property-write int $bar
*/
EOF,
];
yield 'class doc block' => [
<<<'EOF'
<?php
namespace Foo;
/**
* This is a class that does classy things.
*
* @internal
*
* @package Foo
* @subpackage Foo\Bar
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
* @author Graham Campbell <hello@gjcampbell.co.uk>
* @copyright Foo Bar
* @license MIT
*/
class Bar {}
EOF,
<<<'EOF'
<?php
namespace Foo;
/**
* This is a class that does classy things.
* @internal
* @package Foo
*
*
* @subpackage Foo\Bar
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
*
* @copyright Foo Bar
*
*
* @license MIT
*/
class Bar {}
EOF,
];
yield 'poor alignment' => [
<<<'EOF'
<?php
namespace Foo;
/**
* This is a class that does classy things.
*
* @internal
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*@author Graham Campbell <hello@gjcampbell.co.uk>
*/
class Bar {}
EOF,
<<<'EOF'
<?php
namespace Foo;
/**
* This is a class that does classy things.
*
* @internal
*
*
*
* @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
*
*@author Graham Campbell <hello@gjcampbell.co.uk>
*/
class Bar {}
EOF,
];
yield 'move unknown annotations' => [
<<<'EOF'
<?php
/**
* @expectedException Exception
*
* @expectedExceptionMessage Oh Noes!
* Something when wrong!
*
* @Hello\Test\Foo(asd)
*
* @Method("GET")
*
* @param string $expected
* @param string $input
*/
EOF,
<<<'EOF'
<?php
/**
* @expectedException Exception
* @expectedExceptionMessage Oh Noes!
* Something when wrong!
*
*
* @Hello\Test\Foo(asd)
* @Method("GET")
*
* @param string $expected
*
* @param string $input
*/
EOF,
];
yield [
'<?php
/**
* {@inheritdoc}
*
* @param string $expected
* @param string $input
*/
',
'<?php
/**
* {@inheritdoc}
* @param string $expected
* @param string $input
*/
',
];
yield [
'<?php
/**
* {@inheritDoc}
*
* @param string $expected
* @param string $input
*/
',
'<?php
/**
* {@inheritDoc}
* @param string $expected
* @param string $input
*/
',
];
yield 'empty doc block' => [<<<'EOF'
<?php
/**
*
*/
EOF];
yield 'larger empty doc block' => [<<<'EOF'
<?php
/**
*
*
*
*
*/
EOF];
yield 'one line doc block' => [<<<'EOF'
<?php
/** Foo */
const Foo = 1;
EOF];
yield 'messy whitespaces' => [
"<?php\t/**\r\n\t * @param string \$text\r\n\t *\r\n\t * @return string\r\n\t */",
"<?php\t/**\r\n\t * @param string \$text\r\n\t * @return string\r\n\t */",
];
yield 'with spacing' => [
'<?php
/**
* Foo
*
* @bar 123
*
* {@inheritdoc} '.'
*
* @param string $expected
* @param string $input
*/',
'<?php
/**
* Foo
* @bar 123
*
* {@inheritdoc} '.'
* @param string $expected
* @param string $input
*/',
];
yield 'Laravel groups' => [
<<<'EOF'
<?php
/**
* Attempt to authenticate using HTTP Basic Auth.
*
* @param string $field
* @param array $extraConditions
* @return \Symfony\Component\HttpFoundation\Response|null
*
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*/
EOF,
<<<'EOF'
<?php
/**
* Attempt to authenticate using HTTP Basic Auth.
*
* @param string $field
* @param array $extraConditions
* @return \Symfony\Component\HttpFoundation\Response|null
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*/
EOF,
['groups' => [
['param', 'return'],
['throws'],
['deprecated', 'link', 'see', 'since'],
['author', 'copyright', 'license'],
['category', 'package', 'subpackage'],
['property', 'property-read', 'property-write'],
]],
];
yield 'various groups' => [
<<<'EOF'
<?php
/**
* Attempt to authenticate using HTTP Basic Auth.
*
* @link https://example.com/link
* @see https://doc.example.com/link
* @copyright by John Doe 2001
* @author John Doe
*
* @property-custom string $prop
*
* @param string $field
* @param array $extraConditions
* @return \Symfony\Component\HttpFoundation\Response|null
*
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*/
EOF,
<<<'EOF'
<?php
/**
* Attempt to authenticate using HTTP Basic Auth.
*
* @link https://example.com/link
*
*
* @see https://doc.example.com/link
* @copyright by John Doe 2001
* @author John Doe
* @property-custom string $prop
* @param string $field
* @param array $extraConditions
*
* @return \Symfony\Component\HttpFoundation\Response|null
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*/
EOF,
[
'groups' => [
['deprecated', 'link', 'see', 'since', 'author', 'copyright', 'license'],
['category', 'package', 'subpackage'],
['property', 'property-read', 'property-write'],
['return', 'param'],
],
],
];
yield 'various additional groups' => [
<<<'EOF'
<?php
/**
* Attempt to authenticate using HTTP Basic Auth.
*
* @link https://example.com/link
* @see https://doc.example.com/link
* @copyright by John Doe 2001
* @author John Doe
*
* @param string $field
* @param array $extraConditions
* @return \Symfony\Component\HttpFoundation\Response|null
*
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*/
EOF,
<<<'EOF'
<?php
/**
* Attempt to authenticate using HTTP Basic Auth.
*
* @link https://example.com/link
*
*
* @see https://doc.example.com/link
* @copyright by John Doe 2001
* @author John Doe
* @param string $field
* @param array $extraConditions
*
* @return \Symfony\Component\HttpFoundation\Response|null
* @throws \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException
*/
EOF,
[
'groups' => [
['deprecated', 'link', 'see', 'since', 'author', 'copyright', 'license'],
['category', 'package', 'subpackage'],
['property', 'property-read', 'property-write'],
['return', 'param'],
],
],
];
yield 'laravel' => [
<<<'EOF'
<?php
/**
* Hello there!
*
* @author John Doe
*
* @custom Test!
*
* @throws Exception|RuntimeException foo
*
* @param string $foo
* @param bool $bar Bar
* @return int Return the number of changes.
*/
EOF,
<<<'EOF'
<?php
/**
* Hello there!
*
* @author John Doe
* @custom Test!
* @throws Exception|RuntimeException foo
* @param string $foo
* @param bool $bar Bar
*
* @return int Return the number of changes.
*/
EOF,
['groups' => [
['param', 'return'],
['throws'],
['deprecated', 'link', 'see', 'since'],
['author', 'copyright', 'license'],
['category', 'package', 'subpackage'],
['property', 'property-read', 'property-write'],
]],
];
yield 'all_tags' => [
<<<'EOF'
<?php
/**
* Hello there!
*
* @author John Doe
* @custom Test!
* @throws Exception|RuntimeException foo
*
* @param string $foo
* @param bool $bar Bar
* @return int Return the number of changes.
*/
EOF,
<<<'EOF'
<?php
/**
* Hello there!
*
* @author John Doe
* @custom Test!
* @throws Exception|RuntimeException foo
* @param string $foo
* @param bool $bar Bar
*
* @return int Return the number of changes.
*/
EOF,
['groups' => [['author', 'throws', 'custom'], ['return', 'param']]],
];
yield 'default_groups_standard_tags' => [
<<<'EOF'
<?php
/**
* Hello there!
*
* @author John Doe
*
* @throws Exception|RuntimeException foo
*
* @custom Test!
*
* @param string $foo
* @param bool $bar Bar
*
* @return int Return the number of changes.
*/
EOF,
<<<'EOF'
<?php
/**
* Hello there!
* @author John Doe
* @throws Exception|RuntimeException foo
* @custom Test!
* @param string $foo
* @param bool $bar Bar
* @return int Return the number of changes.
*/
EOF,
];
yield 'Separated unlisted tags with default config' => [
<<<'EOF'
<?php
/**
* @not-in-any-group1
*
* @not-in-any-group2
*
* @not-in-any-group3
*/
EOF,
<<<'EOF'
<?php
/**
* @not-in-any-group1
* @not-in-any-group2
* @not-in-any-group3
*/
EOF,
];
yield 'Skip unlisted tags' => [
<<<'EOF'
<?php
/**
* @in-group-1
* @in-group-1-too
*
* @not-in-any-group1
*
* @not-in-any-group2
* @not-in-any-group3
*/
EOF,
<<<'EOF'
<?php
/**
* @in-group-1
*
* @in-group-1-too
* @not-in-any-group1
*
* @not-in-any-group2
* @not-in-any-group3
*/
EOF,
[
'groups' => [['in-group-1', 'in-group-1-too']],
'skip_unlisted_annotations' => true,
],
];
yield 'Doctrine annotations' => [
<<<'EOF'
<?php
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*/
EOF,
<<<'EOF'
<?php
/**
* @ORM\Id
*
* @ORM\Column(type="integer")
*
* @ORM\GeneratedValue
*/
EOF,
['groups' => [
['ORM\Id', 'ORM\Column', 'ORM\GeneratedValue'],
]],
];
yield 'With wildcard' => [
<<<'EOF'
<?php
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue
*
* @Assert\NotNull
* @Assert\Type("string")
*/
EOF,
<<<'EOF'
<?php
/**
* @ORM\Id
*
* @ORM\Column(type="integer")
*
* @ORM\GeneratedValue
* @Assert\NotNull
*
* @Assert\Type("string")
*/
EOF,
['groups' => [
['ORM\*'],
['Assert\*'],
]],
];
}
/**
* @dataProvider provideInvalidConfigurationCases
*
* @param array<string, mixed> $configuration
*/
public function testInvalidConfiguration(array $configuration, string $expectedExceptionMessage): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessage($expectedExceptionMessage);
$this->fixer->configure($configuration);
}
/**
* @return iterable<string, array{array<string, mixed>, string}>
*/
public static function provideInvalidConfigurationCases(): iterable
{
yield 'tag in two groups' => [
['groups' => [['param', 'return'], ['param', 'throws']]],
'The option "groups" value is invalid. The "param" tag belongs to more than one group.',
];
yield 'tag specified two times in group' => [
['groups' => [['param', 'return', 'param', 'throws']]],
'The option "groups" value is invalid. The "param" tag is specified more than once.',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocScalarFixerTest.php | tests/Fixer/Phpdoc/PhpdocScalarFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\AbstractPhpdocTypesFixer
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocScalarFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocScalarFixer>
*
* @author Graham Campbell <hello@gjcampbell.co.uk>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocScalarFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocScalarFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield 'basic fix' => [
'<?php
/**
* @return int
*/
',
'<?php
/**
* @return integer
*/
',
];
yield 'property fix' => [
'<?php
/**
* @method int foo()
* @property int $foo
* @property callable $foo
* @property-read bool $bar
* @property-write float $baz
*/
',
'<?php
/**
* @method integer foo()
* @property integer $foo
* @property callback $foo
* @property-read boolean $bar
* @property-write double $baz
*/
',
];
yield 'do not modify variables' => [
'<?php
/**
* @param int $integer
*/
',
'<?php
/**
* @param integer $integer
*/
',
];
yield 'fix with tabs on one line' => [
"<?php /**\t@return\tbool\t*/",
"<?php /**\t@return\tboolean\t*/",
];
yield 'fix more things' => [
'<?php
/**
* Hello there mr integer!
*
* @param int|float $integer
* @param int|int[] $foo
* @param string|null $bar
*
* @return string|bool
*/
',
'<?php
/**
* Hello there mr integer!
*
* @param integer|real $integer
* @param int|integer[] $foo
* @param str|null $bar
*
* @return string|boolean
*/
',
];
yield 'fix var' => [
'<?php
/**
* @var int Some integer value.
*/
',
'<?php
/**
* @var integer Some integer value.
*/
',
];
yield 'fix var with more stuff' => [
'<?php
/**
* @var bool|int|Double Booleans, integers and doubles.
*/
',
'<?php
/**
* @var boolean|integer|Double Booleans, integers and doubles.
*/
',
];
yield 'fix type' => [
'<?php
/**
* @type float
*/
',
'<?php
/**
* @type real
*/
',
];
yield 'do not fix' => [
'<?php
/**
* @var notaboolean
*/
',
];
yield 'complex mix' => [
'<?php
/**
* @var notabooleanthistime|bool|integerr
*/
',
'<?php
/**
* @var notabooleanthistime|boolean|integerr
*/
',
];
yield 'do not modify complex tag' => [
'<?php
/**
* @Type("boolean")
*/
',
];
yield 'do not modify strings' => [
"<?php
\$string = '
/**
* @var boolean
*/
';
",
];
yield 'empty DocBlock' => [
'<?php
/**
*
*/
',
];
yield 'wrong cased Phpdoc tag is not altered' => [
'<?php
/**
* @Param boolean
*
* @Return int
*/
',
];
yield 'inline doc' => [
'<?php
/**
* Does stuff with stuffs.
*
* @param array $stuffs {
* @type bool $foo
* @type int $bar
* }
*/
',
'<?php
/**
* Does stuff with stuffs.
*
* @param array $stuffs {
* @type boolean $foo
* @type integer $bar
* }
*/
',
];
yield 'fix callback' => [
'<?php
/**
* @method int foo()
* @property int $foo
* @property callable $foo
* @property-read bool $bar
* @property-write float $baz
*/
',
'<?php
/**
* @method integer foo()
* @property integer $foo
* @property callback $foo
* @property-read boolean $bar
* @property-write double $baz
*/
',
['types' => ['boolean', 'callback', 'double', 'integer', 'real', 'str']],
];
yield 'fix Windows line endings' => [
str_replace("\n", "\r\n", '<?php
/**
* @return int
*/
'),
str_replace("\n", "\r\n", '<?php
/**
* @return integer
*/
'),
];
yield [
'<?php /** @var array<int, bool> */',
'<?php /** @var array<integer, boolean> */',
];
yield [
'<?php /** @var array{bool, int, string} */',
'<?php /** @var array{boolean, integer, str} */',
];
yield [
'<?php /** @var array{int, array{string, bool}} */',
'<?php /** @var array{integer, array{str, boolean}} */',
];
yield [
'<?php /** @var array{index: int, isRequired: bool} */',
'<?php /** @var array{index: integer, isRequired: boolean} */',
];
yield [
'<?php /** @return never */',
'<?php /** @return no-return */',
['types' => ['no-return']],
];
yield [
'<?php /** @return never */',
'<?php /** @return never-return */',
['types' => ['never-return']],
];
yield [
'<?php /** @return never */',
'<?php /** @return never-returns */',
['types' => ['never-returns']],
];
yield [
'<?php /** @return Collection<int, covariant Foo> */',
'<?php /** @return Collection<integer, covariant Foo> */',
];
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
PHP-CS-Fixer/PHP-CS-Fixer | https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/63b024eaaf8a48445494964bd21a6c38c788f40f/tests/Fixer/Phpdoc/PhpdocTagTypeFixerTest.php | tests/Fixer/Phpdoc/PhpdocTagTypeFixerTest.php | <?php
declare(strict_types=1);
/*
* This file is part of PHP CS Fixer.
*
* (c) Fabien Potencier <fabien@symfony.com>
* Dariusz Rumiński <dariusz.ruminski@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace PhpCsFixer\Tests\Fixer\Phpdoc;
use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
/**
* @internal
*
* @covers \PhpCsFixer\Fixer\Phpdoc\PhpdocTagTypeFixer
*
* @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\PhpdocTagTypeFixer>
*
* @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\PhpdocTagTypeFixer
*
* @no-named-arguments Parameter names are not covered by the backward compatibility promise.
*/
final class PhpdocTagTypeFixerTest extends AbstractFixerTestCase
{
/**
* @param _AutogeneratedInputConfiguration $configuration
*
* @dataProvider provideFixCases
*/
public function testFix(string $expected, ?string $input = null, array $configuration = []): void
{
$this->fixer->configure($configuration);
$this->doTest($expected, $input);
}
/**
* @return iterable<int, array{0: string, 1?: null|string, 2?: _AutogeneratedInputConfiguration}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
/**
* @api
* @author
* @copyright
* @deprecated
* @example
* @global
* @inheritDoc
* @internal
* @license
* @method
* @package
* @param
* @property
* @return
* @see
* @since
* @throws
* @todo
* @uses
* @var
* @version
*/',
'<?php
/**
* {@api}
* {@author}
* {@copyright}
* {@deprecated}
* {@example}
* {@global}
* {@inheritDoc}
* {@internal}
* {@license}
* {@method}
* {@package}
* {@param}
* {@property}
* {@return}
* {@see}
* {@since}
* {@throws}
* {@todo}
* {@uses}
* {@var}
* {@version}
*/',
];
yield [
'<?php
/**
* @api
* @author
* @copyright
* @deprecated
* @example
* @global
* @inheritDoc
* @internal
* @license
* @method
* @package
* @param
* @property
* @return
* @see
* @since
* @throws
* @todo
* @uses
* @var
* @version
*/',
'<?php
/**
* {@api}
* {@author}
* {@copyright}
* {@deprecated}
* {@example}
* {@global}
* {@inheritDoc}
* {@internal}
* {@license}
* {@method}
* {@package}
* {@param}
* {@property}
* {@return}
* {@see}
* {@since}
* {@throws}
* {@todo}
* {@uses}
* {@var}
* {@version}
*/',
['tags' => [
'api' => 'annotation',
'author' => 'annotation',
'copyright' => 'annotation',
'deprecated' => 'annotation',
'example' => 'annotation',
'global' => 'annotation',
'inheritDoc' => 'annotation',
'internal' => 'annotation',
'license' => 'annotation',
'method' => 'annotation',
'package' => 'annotation',
'param' => 'annotation',
'property' => 'annotation',
'return' => 'annotation',
'see' => 'annotation',
'since' => 'annotation',
'throws' => 'annotation',
'todo' => 'annotation',
'uses' => 'annotation',
'var' => 'annotation',
'version' => 'annotation',
]],
];
yield [
'<?php
/**
* {@api}
* {@author}
* {@copyright}
* {@deprecated}
* {@example}
* {@global}
* {@inheritDoc}
* {@internal}
* {@license}
* {@method}
* {@package}
* {@param}
* {@property}
* {@return}
* {@see}
* {@since}
* {@throws}
* {@todo}
* {@uses}
* {@var}
* {@version}
*/',
'<?php
/**
* @api
* @author
* @copyright
* @deprecated
* @example
* @global
* @inheritDoc
* @internal
* @license
* @method
* @package
* @param
* @property
* @return
* @see
* @since
* @throws
* @todo
* @uses
* @var
* @version
*/',
['tags' => [
'api' => 'inline',
'author' => 'inline',
'copyright' => 'inline',
'deprecated' => 'inline',
'example' => 'inline',
'global' => 'inline',
'inheritDoc' => 'inline',
'internal' => 'inline',
'license' => 'inline',
'method' => 'inline',
'package' => 'inline',
'param' => 'inline',
'property' => 'inline',
'return' => 'inline',
'see' => 'inline',
'since' => 'inline',
'throws' => 'inline',
'todo' => 'inline',
'uses' => 'inline',
'var' => 'inline',
'version' => 'inline',
]],
];
yield [
'<?php
/** @api */',
'<?php
/** {@api} */',
];
yield [
'<?php
/**
* @deprecated since version X
*/',
'<?php
/**
* {@deprecated since version X}
*/',
];
yield [
'<?php
/**
* {@deprecated since version X}
*/',
'<?php
/**
* @deprecated since version X
*/',
['tags' => ['deprecated' => 'inline']],
];
yield [
'<?php
/** {@deprecated since version X} */',
'<?php
/** @deprecated since version X */',
['tags' => ['deprecated' => 'inline']],
];
yield [
'<?php
/**
* @inheritDoc
*/',
'<?php
/**
* {@inheritDoc}
*/',
];
yield [
'<?php
/**
* @inheritdoc
*/',
'<?php
/**
* {@inheritdoc}
*/',
];
yield [
'<?php
/**
* {@inheritdoc}
*/',
'<?php
/**
* @inheritdoc
*/',
['tags' => ['inheritDoc' => 'inline']],
];
yield [
'<?php
/**
* Some summary.
*
* {@inheritdoc}
*/',
];
yield [
'<?php
/**
* Some summary.
*
* Some description.
*
* {@inheritdoc}
*
* More description.
*
* @param Foo $foo
*/',
];
yield [
'<?php
/**
* {@inheritdoc}
*
* More description.
*/',
];
yield [
'<?php
/**
*
* @inheritdoc
*
*/',
'<?php
/**
*
* {@inheritdoc}
*
*/',
];
yield [
'<?php
/**
* @return array{0: float, 1: int}
*/',
];
yield [
'<?php
/** @internal Please use {@see Foo} instead */',
'<?php
/** {@internal Please use {@see Foo} instead} */',
];
yield [
'<?php
/**
* @internal Please use {@see Foo} instead
*/',
'<?php
/**
* {@internal Please use {@see Foo} instead}
*/',
];
yield [
'<?php
/**
*
* @internal Please use {@see Foo} instead
*
*/',
'<?php
/**
*
* {@internal Please use {@see Foo} instead}
*
*/',
];
yield [
'<?php
/** @internal Foo Bar {@see JsonSerializable} */',
];
}
public function testInvalidConfiguration(): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches('#^\[phpdoc_tag_type\] Invalid configuration: Unknown tag type "foo"\.#');
$this->fixer->configure([ // @phpstan-ignore-line
'tags' => ['inheritDoc' => 'foo'],
]);
}
}
| php | MIT | 63b024eaaf8a48445494964bd21a6c38c788f40f | 2026-01-04T15:02:54.911792Z | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.