Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common::getSniffCode(): add tests, more defensive coding and minor simplification #524

Merged
merged 5 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -873,9 +873,13 @@ protected function addMessage($error, $message, $line, $column, $code, $data, $s
$parts = explode('.', $code);
if ($parts[0] === 'Internal') {
// An internal message.
$listenerCode = Common::getSniffCode($this->activeListener);
$sniffCode = $code;
$checkCodes = [$sniffCode];
$listenerCode = '';
if ($this->activeListener !== '') {
$listenerCode = Common::getSniffCode($this->activeListener);
}

$sniffCode = $code;
$checkCodes = [$sniffCode];
} else {
if ($parts[0] !== $code) {
// The full message code has been passed in.
Expand Down
33 changes: 25 additions & 8 deletions src/Util/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace PHP_CodeSniffer\Util;

use InvalidArgumentException;
use Phar;

class Common
Expand Down Expand Up @@ -529,25 +530,41 @@ public static function suggestType($varType)
* @param string $sniffClass The fully qualified sniff class name.
*
* @return string
*
* @throws \InvalidArgumentException When $sniffClass is not a non-empty string.
* @throws \InvalidArgumentException When $sniffClass is not a FQN for a sniff(test) class.
*/
public static function getSniffCode($sniffClass)
{
$parts = explode('\\', $sniffClass);
$sniff = array_pop($parts);
if (is_string($sniffClass) === false || $sniffClass === '') {
throw new InvalidArgumentException('The $sniffClass parameter must be a non-empty string');
}

$parts = explode('\\', $sniffClass);
$partsCount = count($parts);
if ($partsCount < 4) {
throw new InvalidArgumentException(
'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received: '.$sniffClass
);
}

$sniff = $parts[($partsCount - 1)];

if (substr($sniff, -5) === 'Sniff') {
// Sniff class name.
$sniff = substr($sniff, 0, -5);
} else {
} else if (substr($sniff, -8) === 'UnitTest') {
// Unit test class name.
$sniff = substr($sniff, 0, -8);
} else {
throw new InvalidArgumentException(
'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received: '.$sniffClass
);
}

$category = array_pop($parts);
$sniffDir = array_pop($parts);
$standard = array_pop($parts);
$code = $standard.'.'.$category.'.'.$sniff;
return $code;
$standard = $parts[($partsCount - 4)];
$category = $parts[($partsCount - 2)];
return $standard.'.'.$category.'.'.$sniff;

}//end getSniffCode()

Expand Down
172 changes: 172 additions & 0 deletions tests/Core/Util/Common/GetSniffCodeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php
/**
* Tests for the \PHP_CodeSniffer\Util\Common::getSniffCode() method.
*
* @author Juliette Reinders Folmer <[email protected]>
* @copyright 2024 PHPCSStandards and contributors
* @license https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
*/

namespace PHP_CodeSniffer\Tests\Core\Util\Common;

use PHP_CodeSniffer\Util\Common;
use PHPUnit\Framework\TestCase;

/**
* Tests for the \PHP_CodeSniffer\Util\Common::getSniffCode() method.
*
* @covers \PHP_CodeSniffer\Util\Common::getSniffCode
*/
final class GetSniffCodeTest extends TestCase
{


/**
* Test receiving an expected exception when the $sniffClass parameter is not passed a string value or is passed an empty string.
*
* @param string $input NOT a fully qualified sniff class name.
*
* @dataProvider dataGetSniffCodeThrowsExceptionOnInvalidInput
*
* @return void
*/
public function testGetSniffCodeThrowsExceptionOnInvalidInput($input)
{
$exception = 'InvalidArgumentException';
$message = 'The $sniffClass parameter must be a non-empty string';

if (method_exists($this, 'expectException') === true) {
// PHPUnit 5+.
$this->expectException($exception);
$this->expectExceptionMessage($message);
} else {
// PHPUnit 4.
$this->setExpectedException($exception, $message);
}

Common::getSniffCode($input);

}//end testGetSniffCodeThrowsExceptionOnInvalidInput()


/**
* Data provider.
*
* @see testGetSniffCodeThrowsExceptionOnInvalidInput()
*
* @return array<string, array<string>>
*/
public static function dataGetSniffCodeThrowsExceptionOnInvalidInput()
{
return [
'Class name is not a string' => [true],
'Class name is empty' => [''],
];

}//end dataGetSniffCodeThrowsExceptionOnInvalidInput()


/**
* Test receiving an expected exception when the $sniffClass parameter is not passed a value which
* could be a fully qualified sniff(test) class name.
*
* @param string $input String input which can not be a fully qualified sniff(test) class name.
*
* @dataProvider dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass
*
* @return void
*/
public function testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass($input)
{
$exception = 'InvalidArgumentException';
$message = 'The $sniffClass parameter was not passed a fully qualified sniff(test) class name. Received:';

if (method_exists($this, 'expectException') === true) {
// PHPUnit 5+.
$this->expectException($exception);
$this->expectExceptionMessage($message);
} else {
// PHPUnit 4.
$this->setExpectedException($exception, $message);
}

Common::getSniffCode($input);

}//end testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()


/**
* Data provider.
*
* @see testGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()
*
* @return array<string, array<string>>
*/
public static function dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()
{
return [
'Unqualified class name' => ['ClassName'],
'Fully qualified class name, not enough parts' => ['Fully\\Qualified\\ClassName'],
'Fully qualified class name, doesn\'t end on Sniff or UnitTest' => ['Fully\\Sniffs\\Qualified\\ClassName'],
];

}//end dataGetSniffCodeThrowsExceptionOnInputWhichIsNotASniffTestClass()


/**
* Test transforming a sniff class name to a sniff code.
*
* @param string $fqnClass A fully qualified sniff class name.
* @param string $expected Expected function output.
*
* @dataProvider dataGetSniffCode
*
* @return void
*/
public function testGetSniffCode($fqnClass, $expected)
{
$this->assertSame($expected, Common::getSniffCode($fqnClass));

}//end testGetSniffCode()


/**
* Data provider.
*
* @see testGetSniffCode()
*
* @return array<string, array<string, string>>
*/
public static function dataGetSniffCode()
{
return [
'PHPCS native sniff' => [
'fqnClass' => 'PHP_CodeSniffer\\Standards\\Generic\\Sniffs\\Arrays\\ArrayIndentSniff',
'expected' => 'Generic.Arrays.ArrayIndent',
],
'Class is a PHPCS native test class' => [
'fqnClass' => 'PHP_CodeSniffer\\Standards\\Generic\\Tests\\Arrays\\ArrayIndentUnitTest',
'expected' => 'Generic.Arrays.ArrayIndent',
],
'Sniff in external standard without namespace prefix' => [
'fqnClass' => 'MyStandard\\Sniffs\\PHP\\MyNameSniff',
'expected' => 'MyStandard.PHP.MyName',
],
'Test in external standard without namespace prefix' => [
'fqnClass' => 'MyStandard\\Tests\\PHP\\MyNameSniff',
'expected' => 'MyStandard.PHP.MyName',
],
'Sniff in external standard with namespace prefix' => [
'fqnClass' => 'Vendor\\Package\\MyStandard\\Sniffs\\Category\\AnalyzeMeSniff',
'expected' => 'MyStandard.Category.AnalyzeMe',
],
'Test in external standard with namespace prefix' => [
'fqnClass' => 'Vendor\\Package\\MyStandard\\Tests\\Category\\AnalyzeMeUnitTest',
'expected' => 'MyStandard.Category.AnalyzeMe',
],
];

}//end dataGetSniffCode()


}//end class