Skip to content

Commit

Permalink
Files containing exclusively non-relevant code are ok
Browse files Browse the repository at this point in the history
As far as all the non-relevant code (namespace, uses, declare and
defines) is also code without side effects, these files don't need
the MOODLE_INTERNAL check).

Covered with tests.

This needs to be merged after #190.

Fixes #188
  • Loading branch information
stronk7 committed Feb 24, 2022
1 parent ed95c2b commit 73cebc6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
8 changes: 6 additions & 2 deletions phpcs/moodle/Sniffs/Files/MoodleInternalSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ public function process(File $file, $pointer) {

// Find where real code is and check from there.
$pointer = $this->get_position_of_relevant_code($file, $pointer);
if (!$pointer) {
// The file only contains non-relevant (and non side-effects) code. We are done.
return;
}

if ($this->is_config_php_incluson($file, $pointer)) {
// We are requiring config.php. This file is good, hurrah!
Expand Down Expand Up @@ -136,8 +140,8 @@ public function process(File $file, $pointer) {
}

/**
* Finds the position of the first bit of relevant code (ignoring namespaces
* and define statements).
* Finds the position of the first bit of relevant code (ignoring namespaces,
* uses, declares and define statements).
*
* @param File $file The file being scanned.
* @param int $pointer The position in the stack.
Expand Down
14 changes: 13 additions & 1 deletion phpcs/moodle/tests/files_moodleinternal_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,20 @@ public function test_moodle_files_moodleinternal_old_style_if_die() {
$this->verify_cs_results();
}

public function test_moodle_files_moodleinternal_no_relevant_ok() {
// Files that only contain non-relevant (and no side-effects) code.
$this->set_standard('moodle');
$this->set_sniff('moodle.Files.MoodleInternal');
$this->set_fixture(__DIR__ . '/fixtures/files/moodleinternal/no_relevant_ok.php');

$this->set_errors([]);
$this->set_warnings([]);

$this->verify_cs_results();
}

public function test_moodle_files_moodleinternal_unexpected() {
// Old style if statement MOODLE_INTERNAL check.
// Unexpected (not needed) check.
$this->set_standard('moodle');
$this->set_sniff('moodle.Files.MoodleInternal');
$this->set_fixture(__DIR__ . '/fixtures/files/moodleinternal/unexpected.php');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?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/>.

/**
* A file that only contains non-relevant (and no side effects) code.
*/

declare(strict_types=1);
declare(encoding='UTF-8');
declare(ticks=1);

namespace local_codechecker;;

use something;
use various, things;
use something as elsething;

define('ONE', 1);
define('TWO', 2);

0 comments on commit 73cebc6

Please sign in to comment.