forked from moodlehq/moodle-cs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check namespaces and class imports for leading backslash
Fixes moodlehq#20
- Loading branch information
1 parent
9292e56
commit 41d22bd
Showing
10 changed files
with
203 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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/>. | ||
|
||
/** | ||
* Checks that each file contains the standard GPL comment. | ||
* | ||
* @package moodle-cs | ||
* @copyright 2023 Andrew Lyons <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace MoodleHQ\MoodleCS\moodle\Sniffs\Namespaces; | ||
|
||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
use PHP_CodeSniffer\Util\Tokens; | ||
|
||
// phpcs:disable moodle.NamingConventions | ||
|
||
class NoLeadingSlashSniff implements Sniff { | ||
public function register() | ||
{ | ||
return [ | ||
T_NAMESPACE, | ||
]; | ||
} | ||
|
||
public function process(File $file, $stackPtr) | ||
{ | ||
$tokens = $file->getTokens(); | ||
// Format should be: | ||
// - T_NAMESPACE | ||
// - T_WHITESPACE | ||
// - T_STRING | ||
|
||
$checkPtr = $stackPtr + 2; | ||
$token = $tokens[$checkPtr]; | ||
if ($token['code'] === T_NS_SEPARATOR) { | ||
$fqdn = ''; | ||
$stop = $file->findNext(Tokens::$emptyTokens, ($stackPtr + 2)); | ||
for ($i = $stackPtr + 2; $i < $stop; $i++) { | ||
$fqdn .= $tokens[$i]['content']; | ||
} | ||
$fix = $file->addFixableError( | ||
'Namespace should not start with a slash: %s', | ||
$checkPtr, | ||
'NamespaceStartsWithSlash', | ||
[$fqdn] | ||
); | ||
|
||
if ($fix) { | ||
$file->fixer->beginChangeset(); | ||
$file->fixer->replaceToken($checkPtr, ''); | ||
$file->fixer->endChangeset(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
moodle/Tests/Sniffs/Namespaces/NoLeadingSlashSniffTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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/>. | ||
|
||
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Namespaces; | ||
|
||
use MoodleHQ\MoodleCS\moodle\Tests\MoodleCSBaseTestCase; | ||
|
||
// phpcs:disable moodle.NamingConventions | ||
|
||
/** | ||
* Test the NoLeadingSlash sniff. | ||
* | ||
* @package moodle-cs | ||
* @category test | ||
* @copyright 2023 Andrew Lyons <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* | ||
* @covers \MoodleHQ\MoodleCS\moodle\Sniffs\Namespaces\NoLeadingSlashSniff | ||
*/ | ||
class NoLeadingSlashSniffTest extends MoodleCSBaseTestCase | ||
{ | ||
public static function no_leading_slash_provider(): array | ||
{ | ||
return [ | ||
[ | ||
'fixture' => 'correct_namespace', | ||
'warnings' => [], | ||
'errors' => [], | ||
], | ||
[ | ||
'fixture' => 'leading_backslash', | ||
'warnings' => [], | ||
'errors' => [ | ||
3 => 'Namespace should not start with a slash: \MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Namespaces', | ||
], | ||
], | ||
[ | ||
'fixture' => 'curly_namespace', | ||
'warnings' => [], | ||
'errors' => [ | ||
3 => 'Namespace should not start with a slash: \MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Namespaces', | ||
], | ||
], | ||
]; | ||
} | ||
/** | ||
* @dataProvider no_leading_slash_provider | ||
*/ | ||
public function test_no_leading_slash( | ||
string $fixture, | ||
array $warnings, | ||
array $errors | ||
): void | ||
{ | ||
$this->set_standard('moodle'); | ||
$this->set_sniff('moodle.Namespaces.NoLeadingSlash'); | ||
$this->set_fixture(sprintf("%s/fixtures/%s.php", __DIR__, $fixture)); | ||
$this->set_warnings($warnings); | ||
$this->set_errors($errors); | ||
|
||
$this->verify_cs_results(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
moodle/Tests/Sniffs/Namespaces/fixtures/correct_namespace.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Namespaces; | ||
|
||
defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures. | ||
|
||
class correct_test extends base_test { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace \MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Namespaces { | ||
class leading_backslash {} | ||
} |
5 changes: 5 additions & 0 deletions
5
moodle/Tests/Sniffs/Namespaces/fixtures/curly_namespace.php.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Namespaces { | ||
class leading_backslash {} | ||
} |
5 changes: 5 additions & 0 deletions
5
moodle/Tests/Sniffs/Namespaces/fixtures/curly_namespace_correct.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php | ||
|
||
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Namespaces { | ||
class leading_backslash {} | ||
} |
8 changes: 8 additions & 0 deletions
8
moodle/Tests/Sniffs/Namespaces/fixtures/leading_backslash.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace \MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Namespaces; | ||
|
||
defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures. | ||
|
||
class leading_backslash extends base_test { | ||
} |
8 changes: 8 additions & 0 deletions
8
moodle/Tests/Sniffs/Namespaces/fixtures/leading_backslash.php.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace MoodleHQ\MoodleCS\moodle\Tests\Sniffs\Namespaces; | ||
|
||
defined('MOODLE_INTERNAL') || die(); // Make this always the 1st line in all CS fixtures. | ||
|
||
class leading_backslash extends base_test { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters