Skip to content

Commit 5e146d9

Browse files
committed
fix - invalid visibility - kokororin/vscode-phpfmt#197
1 parent f59e47c commit 5e146d9

7 files changed

Lines changed: 63 additions & 3 deletions

fmt.stub.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7214,16 +7214,22 @@ public function format($source)
72147214
$this->appendCode($text);
72157215
break;
72167216
case T_STATIC:
7217-
if (! $this->leftTokenIs(T_DOUBLE_COLON)) {
7217+
// `static` can be used as a type (e.g. `instanceof static`, `function f(): static`).
7218+
// Do not treat it as a member modifier in those contexts.
7219+
if ($this->leftUsefulTokenIs([T_INSTANCEOF, ST_COLON])) {
7220+
$this->appendCode($text);
7221+
break;
7222+
}
7223+
if (! $this->leftUsefulTokenIs(T_DOUBLE_COLON)) {
72187224
if (! is_null($visibility)) {
72197225
$static = $text;
72207226
$skipWhitespaces = true;
72217227
break;
7222-
} elseif ($this->leftTokenIs([T_FINAL])) {
7228+
} elseif ($this->leftUsefulTokenIs([T_FINAL])) {
72237229
$static = $text;
72247230
$visibility = 'public';
72257231
break;
7226-
} elseif (! $this->rightTokenIs([T_VARIABLE, T_DOUBLE_COLON]) && ! $this->leftTokenIs([T_NEW, ST_COMMA])) {
7232+
} elseif (! $this->rightTokenIs([T_VARIABLE, T_DOUBLE_COLON]) && ! $this->leftUsefulTokenIs([T_NEW, ST_COMMA])) {
72277233
$static = $text;
72287234
$skipWhitespaces = true;
72297235
break;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
//passes:PSR2ModifierVisibilityStaticOrder
3+
4+
class Example {
5+
public function check(object $object) {
6+
return $object instanceof static;
7+
}
8+
}
9+
10+
$example = new Example();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
//passes:PSR2ModifierVisibilityStaticOrder
3+
4+
class Example {
5+
public function check(object $object) {
6+
return $object instanceof static;
7+
}
8+
}
9+
10+
$example = new Example();

tests/Original/541-new-static.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
//passes:PSR2ModifierVisibilityStaticOrder
3+
4+
class Example {
5+
public function make() {
6+
return new static();
7+
}
8+
}

tests/Original/541-new-static.out

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
//passes:PSR2ModifierVisibilityStaticOrder
3+
4+
class Example {
5+
public function make() {
6+
return new static();
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
//version:8.0
3+
//passes:PSR2ModifierVisibilityStaticOrder
4+
5+
class Example {
6+
public function me(): static {
7+
return $this;
8+
}
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
//version:8.0
3+
//passes:PSR2ModifierVisibilityStaticOrder
4+
5+
class Example {
6+
public function me(): static {
7+
return $this;
8+
}
9+
}

0 commit comments

Comments
 (0)