Skip to content

Commit 4de827f

Browse files
committed
Add support for readonly constructor properties
1 parent 90c24e5 commit 4de827f

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

file.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,16 @@ public function &get_functions() {
423423
$argtokens = array_values($argtokens);
424424

425425
for ($j = 0; $j < count($argtokens); $j++) {
426+
if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
427+
// T_READONLY introduced in PHP 8.1.
428+
if ($argtokens[$j][0] === T_READONLY) {
429+
continue;
430+
}
431+
}
426432
switch ($argtokens[$j][0]) {
427433
// Skip any whitespace, or argument visibility.
434+
case T_COMMENT:
435+
case T_DOC_COMMENT:
428436
case T_WHITESPACE:
429437
case T_PUBLIC:
430438
case T_PROTECTED:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace local_moodlecheck;
18+
19+
use cm_info;
20+
use stdClass;
21+
22+
/**
23+
* A fixture to verify phpdoc tags used in constructor property promotion.
24+
*
25+
* @package local_moodlecheck
26+
* @copyright 2023 Andrew Lyons <[email protected]>
27+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28+
*/
29+
class constructor_property_promotion {
30+
/**
31+
* An example of a constructor using constructor property promotion.
32+
*
33+
* @param stdClass|cm_info $cm The course module data
34+
* @param string $name The name
35+
* @param int|float $size The size
36+
* @param null|string $description The description
37+
* @param ?string $content The content
38+
*/
39+
public function __construct(
40+
private stdClass|cm_info $cm,
41+
/** @var string The name of the course module */
42+
public readonly string $name,
43+
/** @var float|int The size */
44+
public readonly float|int $size,
45+
/** @var null|string The description */
46+
protected readonly ?string $description = null,
47+
protected ?string $content = null
48+
) {
49+
}
50+
}

tests/moodlecheck_rules_test.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,33 @@ public function test_phpdoc_constructor_property_promotion(): void {
219219
$this->assertStringNotContainsString('constructor_property_promotion::__construct has incomplete parameters list', $result);
220220
}
221221

222+
/**
223+
* Verify that constructor property promotion supports readonly properties.
224+
*
225+
* @covers ::local_moodlecheck_functionarguments
226+
* @requires PHP >= 8.1
227+
*/
228+
public function test_phpdoc_constructor_property_promotion_readonly(): void {
229+
global $PAGE;
230+
$output = $PAGE->get_renderer('local_moodlecheck');
231+
$path = new local_moodlecheck_path(
232+
'local/moodlecheck/tests/fixtures/phpdoc_constructor_property_promotion_readonly.php',
233+
null
234+
);
235+
$result = $output->display_path($path, 'xml');
236+
237+
// Convert results to XML Objext.
238+
$xmlresult = new \DOMDocument();
239+
$xmlresult->loadXML($result);
240+
241+
// Let's verify we have received a xml with file top element and 8 children.
242+
$xpath = new \DOMXpath($xmlresult);
243+
$found = $xpath->query("//file/error");
244+
245+
$this->assertCount(0, $found);
246+
$this->assertStringNotContainsString('constructor_property_promotion::__construct has incomplete parameters list', $result);
247+
}
248+
222249
/**
223250
* Verify that constructor property promotion is supported.
224251
*

0 commit comments

Comments
 (0)