Skip to content

Commit

Permalink
feature: Support for constants in traits (#6607)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmajor authored Oct 11, 2022
1 parent 932d60f commit 9b070d0
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/Fixer/ClassNotation/ClassAttributesSeparationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2356,4 +2356,31 @@ protected function test2() {
]],
];
}

/**
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

public function provideFix82Cases(): iterable
{
yield [
'<?php
trait Foo {
const Bar = 1;
const Baz = 2;
}',
'<?php
trait Foo {
const Bar = 1;
const Baz = 2;
}',
];
}
}
18 changes: 18 additions & 0 deletions tests/Fixer/ClassNotation/OrderedClassElementsFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1492,4 +1492,22 @@ function qux() {
',
];
}

/**
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

public function provideFix82Cases(): iterable
{
yield [
'<?php trait Foo { const C1 = 1; protected $abc = "abc"; }',
'<?php trait Foo { protected $abc = "abc"; const C1 = 1; }',
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -944,4 +944,22 @@ enum Foo: string {
var_dump(Foo::A.Foo::B);",
];
}

/**
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

public function provideFix82Cases(): iterable
{
yield [
'<?php trait Foo { public const Bar = 1; public const Baz = 1; }',
'<?php trait Foo { public const Bar = 1, Baz = 1; }',
];
}
}
18 changes: 18 additions & 0 deletions tests/Fixer/ClassNotation/VisibilityRequiredFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -893,4 +893,22 @@ function test(): self { return $this; }
var_dump(Foo::CAT->test());',
];
}

/**
* @requires PHP 8.2
*
* @dataProvider provideFix82Cases
*/
public function testFix82(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

public function provideFix82Cases(): iterable
{
yield [
'<?php trait Foo { public const Bar = 1; }',
'<?php trait Foo { const Bar = 1; }',
];
}
}
32 changes: 32 additions & 0 deletions tests/Fixer/Phpdoc/PhpdocLineSpanFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,4 +865,36 @@ public function hello3() {}
',
];
}

/**
* @dataProvider provideFix82Cases
*
* @requires PHP 8.2
*/
public function testFix82(string $expected, ?string $input = null): void
{
$this->doTest($expected, $input);
}

public 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,
];
}
}
48 changes: 48 additions & 0 deletions tests/Tokenizer/TokensAnalyzerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,54 @@ function qux() {
];
}

/**
* @param array<int, array{classIndex: int, type: string}> $expected
*
* @dataProvider provideGetClassyElements82Cases
*
* @requires PHP 8.2
*/
public function testGetClassyElements82(array $expected, string $source): void
{
$tokens = Tokens::fromCode($source);
$tokensAnalyzer = new TokensAnalyzer($tokens);
$elements = $tokensAnalyzer->getClassyElements();

array_walk(
$expected,
static function (array &$element, $index) use ($tokens): void {
$element['token'] = $tokens[$index];
ksort($element);
},
);

static::assertSame($expected, $elements);
}

public function provideGetClassyElements82Cases(): iterable
{
yield 'constant in trait' => [
[
7 => [
'classIndex' => 1,
'type' => 'const',
],
18 => [
'classIndex' => 1,
'type' => 'const',
],
],
<<<'PHP'
<?php
trait Foo
{
const BAR = 0;
final const BAZ = 1;
}
PHP,
];
}

/**
* @param array<int, bool> $expected
*
Expand Down

0 comments on commit 9b070d0

Please sign in to comment.