Skip to content

Commit

Permalink
PHP 8.4 Support: Asymmetric Visibility v2 (Part 3)
Browse files Browse the repository at this point in the history
- #8035
- https://wiki.php.net/rfc#php_84
- https://wiki.php.net/rfc/asymmetric-visibility-v2

- Fix hints
  - `IncorrectConstructorPropertyPromotionHintError`
  - `UnusedVariableHint`
- Add/Fix unit tests
  • Loading branch information
junichi11 committed Jan 23, 2025
1 parent 1aa9e8c commit ca1f310
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public void visit(MethodDeclaration node) {
if (CancelSupport.getDefault().isCancelled()) {
return;
}
if (BodyDeclaration.Modifier.isVisibilityModifier(parameter.getModifier())) {
if (BodyDeclaration.Modifier.isVisibilityModifier(parameter.getModifier())
|| BodyDeclaration.Modifier.isSetVisibilityModifier(parameter.getModifier())) {
Block body = function.getBody();
if (body == null
|| (!body.isCurly() && body.getStatements().isEmpty())) {
Expand Down Expand Up @@ -184,7 +185,8 @@ private void checkParameters(List<FormalParameter> parameters) {
if (CancelSupport.getDefault().isCancelled()) {
return;
}
if (BodyDeclaration.Modifier.isVisibilityModifier(parameter.getModifier())) {
if (BodyDeclaration.Modifier.isVisibilityModifier(parameter.getModifier())
|| BodyDeclaration.Modifier.isSetVisibilityModifier(parameter.getModifier())) {
// other than a constructor
// e.g.
// public function freeMethod(private $field) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,8 @@ public void visit(FormalParameter node) {
if (CancelSupport.getDefault().isCancelled()) {
return;
}
if (BodyDeclaration.Modifier.isVisibilityModifier(node.getModifier())) {
if (BodyDeclaration.Modifier.isVisibilityModifier(node.getModifier())
|| BodyDeclaration.Modifier.isSetVisibilityModifier(node.getModifier())) {
// [NETBEANS-4443] PHP 8.0 Construcotr Property Promotion
forceVariableAsUsed = true;
scan(node.getParameterName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,48 @@

abstract class AbstractClassExample1 {

abstract public function __construct(private ?int $incorrect1 = 1);
abstract public function __construct(private ?int $incorrect1 = 1); // error1

}

abstract class AbstractClassExample2 {

abstract public function __construct(
$test,
private $incorrect1,
private ?int $incorrect2 = 1
private $incorrect1, // error2
private ?int $incorrect2 = 1 // error3
);

}

interface InterfaceExample1 {
public function __construct(public int|string $incorrect1);
public function __construct(public int|string $incorrect1); // error4
}

interface InterfaceExample2 {
public function __construct(int $test, protected $incorrect1);
public function __construct(int $test, protected $incorrect1); // error5
}

abstract class AbstractClassSetVisibilityExample1 {

abstract public function __construct(private(set) ?int $incorrect1 = 1); // error6

}

abstract class AbstractClassSetVisibilityExample2 {

abstract public function __construct(
$test,
private(set) $incorrect1, // error7
private(set) ?int $incorrect2 = 1 // error8
);

}

interface InterfaceSetVisibilityExample1 {
public function __construct(public(set) int|string $incorrect1); // error9
}

interface InterfaceSetVisibilityExample2 {
public function __construct(int $test, protected(set) $incorrect1); // error10
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
abstract public function __construct(private ?int $incorrect1 = 1);
abstract public function __construct(private ?int $incorrect1 = 1); // error1
----------------------------
HINT:Can't declare a promoted property in an abstract constructor.
private $incorrect1,
private $incorrect1, // error2
-------------------
HINT:Can't declare a promoted property in an abstract constructor.
private ?int $incorrect2 = 1
private ?int $incorrect2 = 1 // error3
----------------------------
HINT:Can't declare a promoted property in an abstract constructor.
public function __construct(public int|string $incorrect1);
public function __construct(public int|string $incorrect1); // error4
-----------------------------
HINT:Can't declare a promoted property in an abstract constructor.
public function __construct(int $test, protected $incorrect1);
public function __construct(int $test, protected $incorrect1); // error5
---------------------
HINT:Can't declare a promoted property in an abstract constructor.
abstract public function __construct(private(set) ?int $incorrect1 = 1); // error6
---------------------------------
HINT:Can't declare a promoted property in an abstract constructor.
private(set) $incorrect1, // error7
------------------------
HINT:Can't declare a promoted property in an abstract constructor.
private(set) ?int $incorrect2 = 1 // error8
---------------------------------
HINT:Can't declare a promoted property in an abstract constructor.
public function __construct(public(set) int|string $incorrect1); // error9
----------------------------------
HINT:Can't declare a promoted property in an abstract constructor.
public function __construct(int $test, protected(set) $incorrect1); // error10
--------------------------
HINT:Can't declare a promoted property in an abstract constructor.
Original file line number Diff line number Diff line change
Expand Up @@ -19,43 +19,85 @@
*/

class ClassExample1 {
public function test1(private $incorrect1): void {
public function test1(private $incorrect1): void { // error1
}

public static function test2(int $param1, public int $incorrect) {
public static function test2(int $param1, public int $incorrect) { // error2
}
}

abstract class AbstractClassExample1 {

abstract public function test1(private ?int $incorrect = 1);
abstract public function test1(private ?int $incorrect = 1); // error3
abstract public function test2(
public $incorrect1,
private ?int $incorrect2 = 1
public $incorrect1, // error4
private ?int $incorrect2 = 1 // error5
);

}

interface InterfaceExample1 {
public function test(public int|string $incorrect1): void;
public function test(
public function test(public int|string $incorrect1): void; // error6
public function test2(
int $param,
public int|string $incorrect1,
protected $incorrect2 = "",
public int|string $incorrect1, // error7
protected $incorrect2 = "", // error8
);
}

$lambda1 = function (private $incorrect1) {};
$lambda1 = function (private $incorrect1) {}; // error9
$lambda2 = function (
string $test,
private $incorrect1,
protected int $incorrect2 = 1,
private $incorrect1, // error10
protected int $incorrect2 = 1, // error11
) {};
$lambda3 = static function (public ?string $incorrect1 = "test"){};
$lambda3 = static function (public ?string $incorrect1 = "test"){}; // error12

$arrow1 = fn(int $test, private ?int $incorrect1,): int => 1;
$arrow1 = fn(int $test, private ?int $incorrect1,): int => 1; // error13
$arrow2 = fn(
int $test,
private ?int $incorrect1,
private string|int $incorrect2
private ?int $incorrect1, // error14
private string|int $incorrect2 // error15
): int => 1;

class ClassSetVisibilityExample1 {
public function test1(private(set) $incorrect1): void { // error16
}

public static function test2(int $param1, public(set) int $incorrect) { // error17
}
}

abstract class AbstractClassSetVisibilityExample1 {

abstract public function test1(private(set) ?int $incorrect = 1); // error18
abstract public function test2(
public(set) $incorrect1, // error19
private(set) ?int $incorrect2 = 1 // error20
);

}

interface InterfaceSetVisibilityExample1 {
public function test(public(set) int|string $incorrect1): void; // error21
public function test2(
int $param,
public(set) int|string $incorrect1, // error22
protected(set) $incorrect2 = "", // error23
);
}

$lambda1 = function (private(set) $incorrect1) {}; // error24
$lambda2 = function (
string $test,
private(set) $incorrect1, // error25
protected(set) int $incorrect2 = 1, // error26
) {};
$lambda3 = static function (public(set) ?string $incorrect1 = "test"){}; // error27

$arrow1 = fn(int $test, private(set) ?int $incorrect1,): int => 1; // error28
$arrow2 = fn(
int $test,
private(set) ?int $incorrect1, // error29
private(set) string|int $incorrect2 // error30
): int => 1;
Original file line number Diff line number Diff line change
@@ -1,45 +1,90 @@
public function test1(private $incorrect1): void {
public function test1(private $incorrect1): void { // error1
-------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
public static function test2(int $param1, public int $incorrect) {
public static function test2(int $param1, public int $incorrect) { // error2
---------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
abstract public function test1(private ?int $incorrect = 1);
abstract public function test1(private ?int $incorrect = 1); // error3
---------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
public $incorrect1,
public $incorrect1, // error4
------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
private ?int $incorrect2 = 1
private ?int $incorrect2 = 1 // error5
----------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
public function test(public int|string $incorrect1): void;
public function test(public int|string $incorrect1): void; // error6
-----------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
public int|string $incorrect1,
public int|string $incorrect1, // error7
-----------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
protected $incorrect2 = "",
protected $incorrect2 = "", // error8
--------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
$lambda1 = function (private $incorrect1) {};
$lambda1 = function (private $incorrect1) {}; // error9
-------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
private $incorrect1,
private $incorrect1, // error10
-------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
protected int $incorrect2 = 1,
protected int $incorrect2 = 1, // error11
-----------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
$lambda3 = static function (public ?string $incorrect1 = "test"){};
$lambda3 = static function (public ?string $incorrect1 = "test"){}; // error12
-----------------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
$arrow1 = fn(int $test, private ?int $incorrect1,): int => 1;
$arrow1 = fn(int $test, private ?int $incorrect1,): int => 1; // error13
------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
private ?int $incorrect1,
private ?int $incorrect1, // error14
------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
private string|int $incorrect2
private string|int $incorrect2 // error15
------------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
public function test1(private(set) $incorrect1): void { // error16
------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
public static function test2(int $param1, public(set) int $incorrect) { // error17
--------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
abstract public function test1(private(set) ?int $incorrect = 1); // error18
--------------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
public(set) $incorrect1, // error19
-----------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
private(set) ?int $incorrect2 = 1 // error20
---------------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
public function test(public(set) int|string $incorrect1): void; // error21
----------------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
public(set) int|string $incorrect1, // error22
----------------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
protected(set) $incorrect2 = "", // error23
-------------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
$lambda1 = function (private(set) $incorrect1) {}; // error24
------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
private(set) $incorrect1, // error25
------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
protected(set) int $incorrect2 = 1, // error26
----------------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
$lambda3 = static function (public(set) ?string $incorrect1 = "test"){}; // error27
----------------------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
$arrow1 = fn(int $test, private(set) ?int $incorrect1,): int => 1; // error28
-----------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
private(set) ?int $incorrect1, // error29
-----------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
private(set) string|int $incorrect2 // error30
-----------------------------------
HINT:Can't declare a promoted property in a function/method other than a constructor.
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,44 @@

class ClassExample1 {
public function __construct(
private ...$incorrect1,
private ...$incorrect1, // error1
) {
}
}

class ClassExample2 {
public function __construct(
int $test1,
private string ...$incorrect1,
private string ...$incorrect1, // error2
) {
}
}

$anon1 = new class(1) {
public function __construct(
protected string ...$incorrect1,
protected(set) string ...$incorrect1, // error3
) {
}
};
};

class ClassSetVisibilityExample1 {
public function __construct(
private(set) ...$incorrect1, // error4
) {
}
}

class ClassSetVisibilityExample2 {
public function __construct(
int $test1,
private(set) string ...$incorrect1, // error5
) {
}
}

$anon1 = new class(1) {
public function __construct(
protected(set) string ...$incorrect1, // error6
) {
}
};
Loading

0 comments on commit ca1f310

Please sign in to comment.