Skip to content

Commit

Permalink
Move getObject* helpers to their own Util
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Mar 14, 2024
1 parent 88f207d commit 237cf60
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 40 deletions.
43 changes: 3 additions & 40 deletions moodle/Sniffs/Commenting/PackageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

use MoodleHQ\MoodleCS\moodle\Util\MoodleUtil;
use MoodleHQ\MoodleCS\moodle\Util\Docblocks;
use MoodleHQ\MoodleCS\moodle\Util\Tokens;
use PHP_CodeSniffer\Sniffs\Sniff;
use PHP_CodeSniffer\Files\File;
use PHPCSUtils\Utils\ObjectDeclarations;

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

/**
* Get the human-readable object type.
*
* @param File $phpcsFile
* @param int $stackPtr
* @return string
*/
protected function getObjectType(
File $phpcsFile,
int $stackPtr
): string {
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
return 'file';
}
return $tokens[$stackPtr]['content'];
}

/**
* Get the human readable object name.
*
* @param File $phpcsFile
* @param int $stackPtr
* @return string
*/
protected function getObjectName(
File $phpcsFile,
int $stackPtr
): string {
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
return basename($phpcsFile->getFilename());
}

return ObjectDeclarations::getName($phpcsFile, $stackPtr);
}

/**
* Check the docblock for a @package tag.
*
Expand All @@ -140,8 +103,8 @@ protected function checkDocblock(
array $docblock
): bool {
$tokens = $phpcsFile->getTokens();
$objectName = $this->getObjectName($phpcsFile, $stackPtr);
$objectType = $this->getObjectType($phpcsFile, $stackPtr);
$objectName = Tokens::getObjectName($phpcsFile, $stackPtr);
$objectType = Tokens::getObjectType($phpcsFile, $stackPtr);
$expectedPackage = MoodleUtil::getMoodleComponent($phpcsFile, true);

// Nothing to do if we have been unable to determine the package
Expand Down
106 changes: 106 additions & 0 deletions moodle/Tests/Util/TokenUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

namespace MoodleHQ\MoodleCS\moodle\Tests\Util;

use MoodleHQ\MoodleCS\moodle\Tests\MoodleCSBaseTestCase;
use MoodleHQ\MoodleCS\moodle\Util\TokenUtil;
use PHP_CodeSniffer\Config;
use PHP_CodeSniffer\Ruleset;
use PHP_CodeSniffer\Files\DummyFile;

/**
* Test the Tokens specific utilities class
*
* @copyright 2021 onwards Eloy Lafuente (stronk7) {@link https://stronk7.com}
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @covers \MoodleHQ\MoodleCS\moodle\Util\TokenUtil
*/
class TokenUtilTest extends MoodleCSBaseTestCase
{
/**
* @dataProvider objectPropertiesProvider
*/
public function testGetObjectProperties(
string $content,
int $type,
string $expectedType,
string $expectedName
): void {
$config = new Config([]);
$ruleset = new Ruleset($config);

$phpcsFile = new DummyFile($content, $ruleset, $config);
$phpcsFile->process();

$stackPtr = $phpcsFile->findNext($type, 0);

$this->assertEquals($expectedType, TokenUtil::getObjectType($phpcsFile, $stackPtr));
$this->assertEquals($expectedName, TokenUtil::getObjectName($phpcsFile, $stackPtr));
}

public static function objectPropertiesProvider(): array {
$cases = [
'Class name' => [
'<?php class Example {}',
T_CLASS,
'class',
'Example',
],
'File name' => [
// Setting the first line of the file to phpcs_input_file: pathname will set the file name of the dummy file.
<<<EOF
phpcs_input_file: /path/to/file/example.php
<?php class Example {}
EOF,
T_OPEN_TAG,
'file',
'example.php',
],
'Trait name' => [
'<?php trait ExampleTrait {}',
T_TRAIT,
'trait',
'ExampleTrait',
],
'Interface name' => [
'<?php interface ExampleInterface {}',
T_INTERFACE,
'interface',
'ExampleInterface',
],
'Function name' => [
'<?php function exampleFunction(): void {}',
T_FUNCTION,
'function',
'exampleFunction',
],
];

if (version_compare(PHP_VERSION, '8.1.0') >= 0) {
$cases['Enum name'] = [
'<?php enum ExampleEnum {}',
T_ENUM,
'enum',
'ExampleEnum',
];
}

return $cases;
}
}
61 changes: 61 additions & 0 deletions moodle/Util/TokenUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

namespace MoodleHQ\MoodleCS\moodle\Util;

use PHP_CodeSniffer\Files\File;
use PHPCSUtils\Utils\ObjectDeclarations;

class TokenUtil
{
/**
* Get the human-readable object type.
*
* @param File $phpcsFile
* @param int $stackPtr
* @return string
*/
public static function getObjectType(
File $phpcsFile,
int $stackPtr
): string {
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
return 'file';
}
return $tokens[$stackPtr]['content'];
}

/**
* Get the human readable object name.
*
* @param File $phpcsFile
* @param int $stackPtr
* @return string
*/
public static function getObjectName(
File $phpcsFile,
int $stackPtr
): string {
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr]['code'] === T_OPEN_TAG) {
return basename($phpcsFile->getFilename());
}

return ObjectDeclarations::getName($phpcsFile, $stackPtr);
}
}

5 comments on commit 237cf60

@ewallah
Copy link

@ewallah ewallah commented on 237cf60 Apr 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function ObjectDeclarations::getName can return null: public static getName(File $phpcsFile, int $stackPtr) : string|null
So line 60 of moodle/Util/TokenUtil.php also can return null, which throws a PHP Fatal error: Uncaught TypeError: MoodleHQ\MoodleCS\moodle\Util\TokenUtil::getObjectName(): Return value must be of type string, null returned in phar:///moodle-plugin-ci.phar/vendor/moodlehq/moodle-cs/moodle/Util/TokenUtil.php:60

To get rid of the error, I changed line 59 into return ObjectDeclarations::getName($phpcsFile, $stackPtr) ?? '';

@ewallah
Copy link

@ewallah ewallah commented on 237cf60 Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error now also pops up in GitHub actions.
Adding a question mark in line 53 also fixes the problem.

): ?string {

@andrewnicols
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a testcase I can use to identify and fix this?

@ewallah
Copy link

@ewallah ewallah commented on 237cf60 Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is related to the coverage.php file.
Sample => error
Sample => no error

@andrewnicols
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect thanks - #147

Please sign in to comment.