Skip to content

Commit ce5588d

Browse files
committed
fix - visibility - kokororin/vscode-phpfmt#193
1 parent 59c2b8f commit ce5588d

8 files changed

Lines changed: 78 additions & 9 deletions

fmt.stub.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7168,22 +7168,37 @@ public function format($source)
71687168
break;
71697169
case T_FINAL:
71707170
case T_ABSTRACT:
7171-
if (! $this->rightTokenIs([T_CLASS]) && ! $this->leftTokenIs(T_DOUBLE_COLON)) {
7172-
$finalOrAbstract = $text;
7173-
$skipWhitespaces = true;
7171+
if ($this->leftTokenIs(T_DOUBLE_COLON)) {
7172+
$this->appendCode($text);
71747173
break;
71757174
}
7176-
$this->appendCode($text);
7175+
7176+
$nextIdx = $this->rightUsefulTokenIdx();
7177+
if (isset($this->tkns[$nextIdx]) && T_READONLY === $this->tkns[$nextIdx][0]) {
7178+
$nextIdx = $this->rightTokenSubsetAtIdx($this->tkns, $nextIdx, $this->ignoreFutileTokens);
7179+
}
7180+
7181+
if (isset($this->tkns[$nextIdx]) && in_array($this->tkns[$nextIdx][0], [T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM], true)) {
7182+
$this->appendCode($text);
7183+
break;
7184+
}
7185+
7186+
$finalOrAbstract = $text;
7187+
$skipWhitespaces = true;
71777188
break;
71787189
case T_READONLY:
71797190
if (! $this->leftTokenIs(T_DOUBLE_COLON)) {
7180-
if (! is_null($visibility)) {
7191+
// `readonly` can be a class modifier (PHP 8.2): `readonly class Foo {}`
7192+
// Handle it here so combinations like `final readonly class` and
7193+
// `abstract readonly class` keep the modifier on the class.
7194+
if ($this->rightUsefulTokenIs([T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM])) {
71817195
$readonly = $text;
71827196
$skipWhitespaces = true;
71837197
break;
7184-
} elseif ($this->leftTokenIs([T_FINAL])) { // ??
7185-
$readonly = $text;
7186-
$visibility = 'public';
7198+
}
7199+
if (! is_null($visibility)) {
7200+
$readonly = $text;
7201+
$skipWhitespaces = true;
71877202
break;
71887203
} elseif (! $this->rightTokenIs([T_VARIABLE, T_DOUBLE_COLON]) && ! $this->leftTokenIs([T_NEW, ST_COMMA])) {
71897204
$readonly = $text;

tests/Original/494-missing-static.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ class a {
99
private static readonly string $endpoint4 = '';
1010
public static readonly string $endpoint5 = '';
1111
static readonly string $endpoint6 = '';
12-
final public readonly string $endpoint7 = '';
12+
final readonly string $endpoint7 = '';
1313
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
//version:8.2
3+
//passes:PSR2ModifierVisibilityStaticOrder
4+
5+
abstract readonly class Example {
6+
public function example() {
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
//version:8.2
3+
//passes:PSR2ModifierVisibilityStaticOrder
4+
5+
abstract readonly class Example {
6+
public function example() {
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
//version:8.2
3+
//passes:PSR2ModifierVisibilityStaticOrder
4+
5+
final readonly class Example {
6+
public function example() {
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
//version:8.2
3+
//passes:PSR2ModifierVisibilityStaticOrder
4+
5+
final readonly class Example {
6+
public function example() {
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
//version:8.2
3+
//passes:PSR2ModifierVisibilityStaticOrder
4+
5+
interface IFoo {
6+
}
7+
8+
abstract readonly class Example implements IFoo {
9+
public function example() {
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
//version:8.2
3+
//passes:PSR2ModifierVisibilityStaticOrder
4+
5+
interface IFoo {
6+
}
7+
8+
abstract readonly class Example implements IFoo {
9+
public function example() {
10+
}
11+
}

0 commit comments

Comments
 (0)