Skip to content

Commit 1f538ad

Browse files
committed
PHP 8.4 Support: Property hooks (Part 7)
- apache#8035 - https://wiki.php.net/rfc#php_84 - https://wiki.php.net/rfc/property-hooks - Fix `ModifiersCheckHintError` - Add unit tests Example: ```php class PropertyHooksClass { private $invalidPrivateFinal { final get{} // cannot use final } private $invalidPublic01 { public get; // cannot use public } public $invalidStatic01 { static get {} // cannot use static } public static $invalidStatic02 { // cannot use static get{} set{} } public $invalidNonAbstract { get; // need a body set; // need a body } public readonly int $invalidReadonly { get{} set{} } // cannot use readonly } interface PropertyHookInterface { abstract public $invalid01 { get; set; } // cannot use abstract protected $invalid02 {get; set;} // cannot use protected private $invalid03 { // cannot use private get; set; } public $invalid04 { final get; // cannot use final } final public $invalid05 { // cannot use final get; set; } public $invalid06 { get {} // cannot has a body } public private(set) int $invalid07 { // cannot use private(set) set; } final public int $invalid08 { // cannot use final set; } public readonly int $invalid09 { // cannot use readonly get; } } ``` - Fix a formatting issue Example: ``` interface TestInterface { public int $prop1 {get; set;} public int $prop2 {} ^^^^^^^ selected here } // before interface TestInterface { public int $prop1 {get; set;} public int $prop2 {} } // after interface TestInterface { public int $prop1 {get; set;} public int $prop2 {} } ```
1 parent baf05ae commit 1f538ad

File tree

67 files changed

+3342
-53
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+3342
-53
lines changed

php/php.editor/src/org/netbeans/modules/php/editor/indent/TokenFormatter.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2923,7 +2923,16 @@ private int replaceString(BaseDocument document, int offset, int indexInFormatTo
29232923
&& newText != null && newText.indexOf('\n') > -1) {
29242924
// will be formatted new line that the first one has to be special
29252925
previousNewIndentText = newText;
2926-
previousOldIndentText = oldText;
2926+
// e.g.
2927+
// class C {
2928+
// public function example1(): void {} : in this case, oldText is an empty string("")
2929+
// public function example2(): void {}
2930+
// ^^^^^^^ format
2931+
//
2932+
// public $hooked1 { get; set; } : in this case, oldText is " "
2933+
// public $hooked2 {}
2934+
// ^^^^^^^ format
2935+
previousOldIndentText = oldText.indexOf('\n') > -1 ? oldText : newText;
29272936
}
29282937
if (newText != null && (!oldText.equals(newText)
29292938
|| (startOffset > 0 && (startOffset - oldText.length()) == offset))) {

php/php.editor/src/org/netbeans/modules/php/editor/verification/ModifiersCheckHintError.java

Lines changed: 358 additions & 48 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
interface TestInterface {
21+
public int $prop1 {get; set;}
22+
/*FORMAT_START*/public /*FORMAT_END*/int $prop2 {}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
interface TestInterface {
21+
public int $prop1 {get; set;}
22+
public int $prop2 {}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
interface TestInterface {
21+
public int $prop1 { get; set; }
22+
/*FORMAT_START*/public /*FORMAT_END*/int $prop2 {}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
interface TestInterface {
21+
public int $prop1 { get; set; }
22+
public int $prop2 {}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
interface TestInterface {
21+
public int $prop1 {}
22+
/*FORMAT_START*/public /*FORMAT_END*/int $prop2 {}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
interface TestInterface {
21+
public int $prop1 {}
22+
public int $prop2 {}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
class TestClass {
21+
public function emptyBody01() {}
22+
/*FORMAT_START*/public /*FORMAT_END*/ function emptyBody02() {}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
class TestClass {
21+
public function emptyBody01() {}
22+
public function emptyBody02() {}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
class TestClass {
21+
public function emptyBody01() { }
22+
/*FORMAT_START*/public /*FORMAT_END*/ function emptyBody02() {}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
class TestClass {
21+
public function emptyBody01() { }
22+
public function emptyBody02() {}
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
class TestClass {
21+
public function emptyBody01() {
22+
}
23+
/*FORMAT_START*/public /*FORMAT_END*/ function emptyBody02() {}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
class TestClass {
21+
public function emptyBody01() {
22+
}
23+
public function emptyBody02() {}
24+
}

php/php.editor/test/unit/data/testfiles/verification/ModifiersCheckHintError/testModifiersCheckHint.php.testModifiersCheckHint.hints

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class ClassMethods {
22
------------
3-
HINT:Class "ClassMethods" contains abstract methods and must be declared abstract
3+
HINT:Class "ClassMethods" contains abstract members and must be declared abstract
44
FIX:Add modifier: abstract
55
abstract private function classAbstractPrivate();
66
--------------------
@@ -24,11 +24,11 @@ HINT:Interface method "ifaceFinalMethod" can not be declared final
2424
FIX:Remove modifier: final
2525
class PossibleAbstract {
2626
----------------
27-
HINT:Class "PossibleAbstract" contains abstract methods and must be declared abstract
27+
HINT:Class "PossibleAbstract" contains abstract members and must be declared abstract
2828
FIX:Add modifier: abstract
2929
final class FinalAbstract {
3030
-------------
31-
HINT:Class "FinalAbstract" contains abstract methods and can not be declared final
31+
HINT:Class "FinalAbstract" contains abstract members and can not be declared final
3232
FIX:Remove modifier: final
33-
HINT:Class "FinalAbstract" contains abstract methods and must be declared abstract
33+
HINT:Class "FinalAbstract" contains abstract members and must be declared abstract
3434
FIX:Add modifier: abstract
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/*
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
*/
20+
abstract class AbstractClass {
21+
// invalid properties
22+
public abstract $invalid01 { // invalid1
23+
get{}
24+
set{}
25+
}
26+
abstract private int $invalid02 { get; set; } // invalid2
27+
}
28+
29+
class NonAbstractClass { // invalid3
30+
public abstract int $invalid03 { get; set; }
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
get{}
2+
---
3+
HINT:Abstract property hook must specify at least one abstract hook
4+
set{}
5+
---
6+
HINT:Abstract property hook must specify at least one abstract hook
7+
abstract private int $invalid02 { get; set; } // invalid2
8+
---------
9+
HINT:Hooked property cannot be both private and abstract
10+
FIX:Remove modifier: private
11+
FIX:Remove modifier: abstract
12+
class NonAbstractClass { // invalid3
13+
----------------
14+
HINT:Class "NonAbstractClass" contains abstract members and must be declared abstract
15+
FIX:Add modifier: abstract

0 commit comments

Comments
 (0)