Skip to content

Commit 237cf60

Browse files
committed
Move getObject* helpers to their own Util
1 parent 88f207d commit 237cf60

File tree

3 files changed

+170
-40
lines changed

3 files changed

+170
-40
lines changed

moodle/Sniffs/Commenting/PackageSniff.php

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919

2020
use MoodleHQ\MoodleCS\moodle\Util\MoodleUtil;
2121
use MoodleHQ\MoodleCS\moodle\Util\Docblocks;
22+
use MoodleHQ\MoodleCS\moodle\Util\Tokens;
2223
use PHP_CodeSniffer\Sniffs\Sniff;
2324
use PHP_CodeSniffer\Files\File;
24-
use PHPCSUtils\Utils\ObjectDeclarations;
2525

2626
/**
2727
* Checks that all test classes and global functions have appropriate @package tags.
@@ -89,43 +89,6 @@ public function process(File $phpcsFile, $stackPtr) {
8989
}
9090
}
9191

92-
/**
93-
* Get the human-readable object type.
94-
*
95-
* @param File $phpcsFile
96-
* @param int $stackPtr
97-
* @return string
98-
*/
99-
protected function getObjectType(
100-
File $phpcsFile,
101-
int $stackPtr
102-
): string {
103-
$tokens = $phpcsFile->getTokens();
104-
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
105-
return 'file';
106-
}
107-
return $tokens[$stackPtr]['content'];
108-
}
109-
110-
/**
111-
* Get the human readable object name.
112-
*
113-
* @param File $phpcsFile
114-
* @param int $stackPtr
115-
* @return string
116-
*/
117-
protected function getObjectName(
118-
File $phpcsFile,
119-
int $stackPtr
120-
): string {
121-
$tokens = $phpcsFile->getTokens();
122-
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
123-
return basename($phpcsFile->getFilename());
124-
}
125-
126-
return ObjectDeclarations::getName($phpcsFile, $stackPtr);
127-
}
128-
12992
/**
13093
* Check the docblock for a @package tag.
13194
*
@@ -140,8 +103,8 @@ protected function checkDocblock(
140103
array $docblock
141104
): bool {
142105
$tokens = $phpcsFile->getTokens();
143-
$objectName = $this->getObjectName($phpcsFile, $stackPtr);
144-
$objectType = $this->getObjectType($phpcsFile, $stackPtr);
106+
$objectName = Tokens::getObjectName($phpcsFile, $stackPtr);
107+
$objectType = Tokens::getObjectType($phpcsFile, $stackPtr);
145108
$expectedPackage = MoodleUtil::getMoodleComponent($phpcsFile, true);
146109

147110
// Nothing to do if we have been unable to determine the package
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
// This file is part of Moodle - https://moodle.org/
4+
//
5+
// Moodle is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Moodle is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
17+
18+
namespace MoodleHQ\MoodleCS\moodle\Tests\Util;
19+
20+
use MoodleHQ\MoodleCS\moodle\Tests\MoodleCSBaseTestCase;
21+
use MoodleHQ\MoodleCS\moodle\Util\TokenUtil;
22+
use PHP_CodeSniffer\Config;
23+
use PHP_CodeSniffer\Ruleset;
24+
use PHP_CodeSniffer\Files\DummyFile;
25+
26+
/**
27+
* Test the Tokens specific utilities class
28+
*
29+
* @copyright 2021 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
30+
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31+
*
32+
* @covers \MoodleHQ\MoodleCS\moodle\Util\TokenUtil
33+
*/
34+
class TokenUtilTest extends MoodleCSBaseTestCase
35+
{
36+
/**
37+
* @dataProvider objectPropertiesProvider
38+
*/
39+
public function testGetObjectProperties(
40+
string $content,
41+
int $type,
42+
string $expectedType,
43+
string $expectedName
44+
): void {
45+
$config = new Config([]);
46+
$ruleset = new Ruleset($config);
47+
48+
$phpcsFile = new DummyFile($content, $ruleset, $config);
49+
$phpcsFile->process();
50+
51+
$stackPtr = $phpcsFile->findNext($type, 0);
52+
53+
$this->assertEquals($expectedType, TokenUtil::getObjectType($phpcsFile, $stackPtr));
54+
$this->assertEquals($expectedName, TokenUtil::getObjectName($phpcsFile, $stackPtr));
55+
}
56+
57+
public static function objectPropertiesProvider(): array {
58+
$cases = [
59+
'Class name' => [
60+
'<?php class Example {}',
61+
T_CLASS,
62+
'class',
63+
'Example',
64+
],
65+
'File name' => [
66+
// Setting the first line of the file to phpcs_input_file: pathname will set the file name of the dummy file.
67+
<<<EOF
68+
phpcs_input_file: /path/to/file/example.php
69+
<?php class Example {}
70+
EOF,
71+
T_OPEN_TAG,
72+
'file',
73+
'example.php',
74+
],
75+
'Trait name' => [
76+
'<?php trait ExampleTrait {}',
77+
T_TRAIT,
78+
'trait',
79+
'ExampleTrait',
80+
],
81+
'Interface name' => [
82+
'<?php interface ExampleInterface {}',
83+
T_INTERFACE,
84+
'interface',
85+
'ExampleInterface',
86+
],
87+
'Function name' => [
88+
'<?php function exampleFunction(): void {}',
89+
T_FUNCTION,
90+
'function',
91+
'exampleFunction',
92+
],
93+
];
94+
95+
if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
96+
$cases['Enum name'] = [
97+
'<?php enum ExampleEnum {}',
98+
T_ENUM,
99+
'enum',
100+
'ExampleEnum',
101+
];
102+
}
103+
104+
return $cases;
105+
}
106+
}

moodle/Util/TokenUtil.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
// This file is part of Moodle - https://moodle.org/
4+
//
5+
// Moodle is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// Moodle is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU General Public License
16+
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
17+
18+
namespace MoodleHQ\MoodleCS\moodle\Util;
19+
20+
use PHP_CodeSniffer\Files\File;
21+
use PHPCSUtils\Utils\ObjectDeclarations;
22+
23+
class TokenUtil
24+
{
25+
/**
26+
* Get the human-readable object type.
27+
*
28+
* @param File $phpcsFile
29+
* @param int $stackPtr
30+
* @return string
31+
*/
32+
public static function getObjectType(
33+
File $phpcsFile,
34+
int $stackPtr
35+
): string {
36+
$tokens = $phpcsFile->getTokens();
37+
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
38+
return 'file';
39+
}
40+
return $tokens[$stackPtr]['content'];
41+
}
42+
43+
/**
44+
* Get the human readable object name.
45+
*
46+
* @param File $phpcsFile
47+
* @param int $stackPtr
48+
* @return string
49+
*/
50+
public static function getObjectName(
51+
File $phpcsFile,
52+
int $stackPtr
53+
): string {
54+
$tokens = $phpcsFile->getTokens();
55+
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
56+
return basename($phpcsFile->getFilename());
57+
}
58+
59+
return ObjectDeclarations::getName($phpcsFile, $stackPtr);
60+
}
61+
}

0 commit comments

Comments
 (0)