Skip to content

Commit

Permalink
PHP 8.3 Support: Arbitrary static variable initializers (Part 2)
Browse files Browse the repository at this point in the history
- apache#6701
- apache#8073
- https://wiki.php.net/rfc/arbitrary_static_variable_initializers
- Fix the formatter
- Add unit tests
  - Formatter
  - Code Completion
  - GotoDeclaration and MarkOccurrences
  • Loading branch information
junichi11 committed Dec 20, 2024
1 parent d0f037d commit 561a96e
Show file tree
Hide file tree
Showing 33 changed files with 447 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,18 @@ public void visit(StaticStatement node) {
List<Expression> expressions = node.getExpressions();
for (Expression expression : expressions) {
addAllUntilOffset(expression.getStartOffset());
// e.g. static $variable = new class() {}
boolean addIndent = !(expression instanceof Assignment
&& isAnonymousClass(((Assignment) expression).getRightHandSide()));
if (moveNext() && lastIndex < ts.index()) {
addFormatToken(formatTokens); // add the first token of the expression and then add the indentation
formatTokens.add(new FormatToken.IndentToken(ts.offset() + ts.token().length(), options.continualIndentSize));
if (addIndent) {
formatTokens.add(new FormatToken.IndentToken(ts.offset() + ts.token().length(), options.continualIndentSize));
}
scan(expression);
formatTokens.add(new FormatToken.IndentToken(expression.getEndOffset(), -1 * options.continualIndentSize));
if (addIndent) {
formatTokens.add(new FormatToken.IndentToken(expression.getEndOffset(), -1 * options.continualIndentSize));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
function test(): void {
}

class Example {
private int $field = 1;

public function method(): int {
return 1;
}

public function run(int $param1) : void {
static $example1 = test();
static $example2 = $param1;
static $example3 = $this->field;
static $example4 = $this->method();
static $example5 = new class() {};
static $example6 = new stdClass(...[0]);
static $example6 = new stdClass($param1);
static $example7 = new (Test);
static $example8 = new static;
static $example9 = $param1 <= 100 ? run($param1 + 1) : "Test $param1";
static $example10 = test(), $example11 = test();
}
}

$variable = 1;
static $example1 = test();
static $example2 = $variable;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code completion result for source line:
static $example1 = te|st();
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
METHOD test() [PUBLIC] testArbitraryStaticVariableInitializers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code completion result for source line:
static $example2 = $para|m1;
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
VARIABLE int $param1 [PUBLIC] testArbitraryStaticVariableInitializers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code completion result for source line:
static $example3 = $this->fi|eld;
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
VARIABLE int field [PRIVATE] Example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code completion result for source line:
static $example4 = $this->metho|d();
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
METHOD method() [PUBLIC] Example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code completion result for source line:
static $example6 = new stdClass($param|1);
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
VARIABLE int $param1 [PUBLIC] testArbitraryStaticVariableInitializers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code completion result for source line:
static $example9 = $param1 <= 100 ? ru|n($param1 + 1) : "Test $param1";
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
METHOD run(int $param1) [PUBLIC] Example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code completion result for source line:
static $example9 = $param1 <= 100 ? run($param1 + 1) : "Test $param|1";
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
VARIABLE int $param1 [PUBLIC] testArbitraryStaticVariableInitializers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code completion result for source line:
static $example10 = tes|t(), $example11 = test();
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
METHOD test() [PUBLIC] testArbitraryStaticVariableInitializers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code completion result for source line:
static $example10 = test(), $example11 = tes|t();
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
METHOD test() [PUBLIC] testArbitraryStaticVariableInitializers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code completion result for source line:
static $example1 = te|st();
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
METHOD test() [PUBLIC] testArbitraryStaticVariableInitializers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Code completion result for source line:
static $example2 = $variab|le;
(QueryType=COMPLETION, prefixSearch=true, caseSensitive=true)
VARIABLE int $variable [PUBLIC] testArbitraryStaticVariableInitializers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class Example {
private int $field = 1;

public function method(): int {
return 1;
}

public function run(int $param1) : void {
static $example1 = rand();
static $example2 = $param1;
static $example3 = $this->field;
static $example4 = $this-> method();
static $example5 = new class() {};
static $example6 = new stdClass(...[0]);
static $example6 = new stdClass($param1);
static $example7 = new (Test);
static $example8 = new static;
static $example9 = $param1 <= 100 ? run($param1 + 1) : "Test $param1";
static $example10 = rand(), $example11 = rand();
}
}

$variable = 1;
static $example1 = rand();
static $example2 = $variable;
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class Example {

private int $field = 1;

public function method(): int {
return 1;
}

public function run(int $param1): void {
static $example1 = rand();
static $example2 = $param1;
static $example3 = $this->field;
static $example4 = $this->method();
static $example5 = new class() {

};
static $example6 = new stdClass(...[0]);
static $example6 = new stdClass($param1);
static $example7 = new (Test);
static $example8 = new static;
static $example9 = $param1 <= 100 ? run($param1 + 1) : "Test $param1";
static $example10 = rand(), $example11 = rand();
}
}

$variable = 1;
static $example1 = rand();
static $example2 = $variable;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class Example {
private int $field = 1;

public function method(): int {
return 1;
}

public function run(int $param1) : void {
static $example1 = rand();
static $example2 = $param1;
static $example3 = $this->field;
static $example4 = $this->method();
static $example5 = new class() {};
static $example6 = new stdClass(...[0]);
static $example6 = new stdClass($param1);
static $example7 = new (Test);
static $example8 = new static;
static $example9 = $param1 <= 100 ? run($param1 + 1) : "Test $param1";
static $example10 = rand(), $example11 = rand();
}
}

$variable = 1;
static $example1 = rand();
static $example2 = $variable;
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class Example {
private int $field = 1;

public function method(): int {
return 1;
}

public function run(int $param1) : void {
static $example1 = rand();
static $example2 = $param1;
static $example3 = $this->field;
static $example4 = $this->method();
static $example5 = new class() {};
static $example6 = new stdClass(...[0]);
static $example6 = new stdClass($param1);
static $example7 = new (Test);
static $example8 = new static;
static $example9 = $param1 <= 100 ? run($param1 + 1) : "Test $param1";
static $example10 = rand(), $example11 = rand();
}
}

$variable = 1;
static $example1 = rand();
static $example2 = $variable;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public function run(int $|>MARK_OCCURRENCES:par^am1<|) : void {
static $example2 = $|>MARK_OCCURRENCES:param1<|;
static $example6 = new stdClass($|>MARK_OCCURRENCES:param1<|);
static $example9 = $|>MARK_OCCURRENCES:param1<| <= 100 ? run($|>MARK_OCCURRENCES:param1<| + 1) : "Test $|>MARK_OCCURRENCES:param1<|";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public function run(int $|>MARK_OCCURRENCES:param1<|) : void {
static $example2 = $|>MARK_OCCURRENCES:para^m1<|;
static $example6 = new stdClass($|>MARK_OCCURRENCES:param1<|);
static $example9 = $|>MARK_OCCURRENCES:param1<| <= 100 ? run($|>MARK_OCCURRENCES:param1<| + 1) : "Test $|>MARK_OCCURRENCES:param1<|";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public function run(int $|>MARK_OCCURRENCES:param1<|) : void {
static $example2 = $|>MARK_OCCURRENCES:param1<|;
static $example6 = new stdClass($|>MARK_OCCURRENCES:para^m1<|);
static $example9 = $|>MARK_OCCURRENCES:param1<| <= 100 ? run($|>MARK_OCCURRENCES:param1<| + 1) : "Test $|>MARK_OCCURRENCES:param1<|";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public function run(int $|>MARK_OCCURRENCES:param1<|) : void {
static $example2 = $|>MARK_OCCURRENCES:param1<|;
static $example6 = new stdClass($|>MARK_OCCURRENCES:param1<|);
static $example9 = $|>MARK_OCCURRENCES:para^m1<| <= 100 ? run($|>MARK_OCCURRENCES:param1<| + 1) : "Test $|>MARK_OCCURRENCES:param1<|";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public function run(int $|>MARK_OCCURRENCES:param1<|) : void {
static $example2 = $|>MARK_OCCURRENCES:param1<|;
static $example6 = new stdClass($|>MARK_OCCURRENCES:param1<|);
static $example9 = $|>MARK_OCCURRENCES:param1<| <= 100 ? run($|>MARK_OCCURRENCES:p^aram1<| + 1) : "Test $|>MARK_OCCURRENCES:param1<|";
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public function run(int $|>MARK_OCCURRENCES:param1<|) : void {
static $example2 = $|>MARK_OCCURRENCES:param1<|;
static $example6 = new stdClass($|>MARK_OCCURRENCES:param1<|);
static $example9 = $|>MARK_OCCURRENCES:param1<| <= 100 ? run($|>MARK_OCCURRENCES:param1<| + 1) : "Test $|>MARK_OCCURRENCES:para^m1<|";
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
private int $|>MARK_OCCURRENCES:fie^ld<| = 1;
static $example3 = $this->|>MARK_OCCURRENCES:field<|;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
private int $|>MARK_OCCURRENCES:field<| = 1;
static $example3 = $this->|>MARK_OCCURRENCES:fiel^d<|;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public function |>MARK_OCCURRENCES:met^hod<|(): int {
static $example4 = $this->|>MARK_OCCURRENCES:method<|();
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public function |>MARK_OCCURRENCES:method<|(): int {
static $example4 = $this->|>MARK_OCCURRENCES:meth^od<|();
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$|>MARK_OCCURRENCES:vari^able<| = 1;
static $example2 = $|>MARK_OCCURRENCES:variable<|;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
$|>MARK_OCCURRENCES:variable<| = 1;
static $example2 = $|>MARK_OCCURRENCES:varia^ble<|;
Loading

0 comments on commit 561a96e

Please sign in to comment.