Skip to content

Commit

Permalink
Merge branch 'master' into category-validation-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 authored Sep 9, 2020
2 parents a3d11e0 + 0c3bb18 commit f672d65
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 2 deletions.
25 changes: 24 additions & 1 deletion file.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,30 @@ public function &get_classes() {
$this->classes = array();
$tokens = &$this->get_tokens();
for ($tid = 0; $tid < $this->tokenscount; $tid++) {
if (($this->tokens[$tid][0] == T_CLASS) && ($this->previous_nonspace_token($tid) !== "::")) {
if ($this->tokens[$tid][0] == T_CLASS) {
if ($this->previous_nonspace_token($tid) === "::") {
// Skip use of the ::class special constant.
continue;
}

if ($this->previous_nonspace_token($tid) == 'new') {
// This looks to be an anonymous class.

if ($this->next_nonspace_token($tid) == '{') {
// An anonymous class in the format `new class {`.
continue;
}

if ($this->next_nonspace_token($tid) == 'extends') {
// An anonymous class in the format `new class extends otherclasses {`.
continue;
}

if ($this->next_nonspace_token($tid) == 'implements') {
// An anonymous class in the format `new class implements someinterface {`.
continue;
}
}
$class = new stdClass();
$class->tid = $tid;
$class->name = $this->next_nonspace_token($tid);
Expand Down
28 changes: 28 additions & 0 deletions tests/fixtures/anonymous/anonymous.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for a fixture file in moodlecheck.
*
* @package local_moodlecheck
* @copyright 2020 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

return new class {
};
28 changes: 28 additions & 0 deletions tests/fixtures/anonymous/assigned.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for a fixture file in moodlecheck.
*
* @package local_moodlecheck
* @copyright 2020 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$value = new class {
};
28 changes: 28 additions & 0 deletions tests/fixtures/anonymous/extends.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for a fixture file in moodlecheck.
*
* @package local_moodlecheck
* @copyright 2020 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

return new class extends parentclass {
};
28 changes: 28 additions & 0 deletions tests/fixtures/anonymous/extendsandimplements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for a fixture file in moodlecheck.
*
* @package local_moodlecheck
* @copyright 2020 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

return new class extends parentclass implements someinterface {
};
28 changes: 28 additions & 0 deletions tests/fixtures/anonymous/implements.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for a fixture file in moodlecheck.
*
* @package local_moodlecheck
* @copyright 2020 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

return new class implements someinterface {
};
28 changes: 28 additions & 0 deletions tests/fixtures/anonymous/named.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for a fixture file in moodlecheck.
*
* @package local_moodlecheck
* @copyright 2020 Andrew Nicols <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace local_moodlecheck\test\fixtures\anonymous;

class someclass extends parentclass {
}
57 changes: 56 additions & 1 deletion tests/moodlecheck_rules_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

defined('MOODLE_INTERNAL') || die(); // Remove this to use me out from Moodle.


class local_moodlecheck_rules_testcase extends advanced_testcase {

public function setUp() {
Expand Down Expand Up @@ -214,4 +213,60 @@ public function test_local_moodlecheck_get_categories() {
$this->assertContains($category, $allowed);
}
}

/**
* Verify that anonymous classes do not require phpdoc class blocks.
*
* @dataProvider anonymous_class_provider
* @param string $path
* @param bool $expectclassesdocumentedfail Whether the
*/
public function test_phpdoc_anonymous_class_docblock(string $path, bool $expectclassesdocumentedfail) {
global $PAGE;

$output = $PAGE->get_renderer('local_moodlecheck');
$checkpath = new local_moodlecheck_path($path, null);
$result = $output->display_path($checkpath, 'xml');

if ($expectclassesdocumentedfail) {
$this->assertContains('classesdocumented', $result);
} else {
$this->assertNotContains('classesdocumented', $result);
}
}

/**
* Data provider for anonymous classes tests.
*
* @return array
*/
public function anonymous_class_provider(): array {
$rootpath = 'local/moodlecheck/tests/fixtures/anonymous';
return [
'return new class {' => [
"{$rootpath}/anonymous.php",
false,
],
'return new class extends parentclass {' => [
"{$rootpath}/extends.php",
false,
],
'return new class implements someinterface {' => [
"{$rootpath}/implements.php",
false,
],
'return new class extends parentclass implements someinterface {' => [
"{$rootpath}/extendsandimplements.php",
false,
],
'$value = new class {' => [
"{$rootpath}/assigned.php",
false,
],
'class someclass extends parentclass {' => [
"{$rootpath}/named.php",
true,
],
];
}
}

0 comments on commit f672d65

Please sign in to comment.