Skip to content

Commit

Permalink
ObjectDeclarations::getDeclared*(): use the new exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfnl committed May 14, 2024
1 parent e62d6bd commit d3dc01a
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 34 deletions.
54 changes: 36 additions & 18 deletions PHPCSUtils/Utils/ObjectDeclarations.php
Expand Up @@ -386,8 +386,9 @@ private static function findNames(File $phpcsFile, $stackPtr, $keyword, array $a
* If no constants are found or a parse error is encountered,
* an empty array is returned.
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token doesn't exist or
* is not an OO keyword token.
* @throws \PHPCSUtils\Exceptions\TypeError If the $stackPtr parameter is not an integer.
* @throws \PHPCSUtils\Exceptions\OutOfBoundsStackPtr If the token passed does not exist in the $phpcsFile.
* @throws \PHPCSUtils\Exceptions\UnexpectedTokenType If the token passed is not an OO keyword token.
*/
public static function getDeclaredConstants(File $phpcsFile, $stackPtr)
{
Expand All @@ -407,14 +408,23 @@ public static function getDeclaredConstants(File $phpcsFile, $stackPtr)
* If no cases are found or a parse error is encountered,
* an empty array is returned.
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token doesn't exist or
* is not a T_ENUM token.
* @throws \PHPCSUtils\Exceptions\TypeError If the $stackPtr parameter is not an integer.
* @throws \PHPCSUtils\Exceptions\OutOfBoundsStackPtr If the token passed does not exist in the $phpcsFile.
* @throws \PHPCSUtils\Exceptions\UnexpectedTokenType If the token passed is not a T_ENUM token.
*/
public static function getDeclaredEnumCases(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();
if (isset($tokens[$stackPtr]) === false || $tokens[$stackPtr]['code'] !== \T_ENUM) {
throw new RuntimeException('$stackPtr must be of type T_ENUM');
if (\is_int($stackPtr) === false) {
throw TypeError::create(2, '$stackPtr', 'integer', $stackPtr);
}

if (isset($tokens[$stackPtr]) === false) {
throw OutOfBoundsStackPtr::create(2, '$stackPtr', $stackPtr);
}

if ($tokens[$stackPtr]['code'] !== \T_ENUM) {
throw UnexpectedTokenType::create(2, '$stackPtr', 'T_ENUM', $tokens[$stackPtr]['type']);
}

return self::analyzeOOStructure($phpcsFile, $stackPtr)['cases'];
Expand All @@ -436,8 +446,9 @@ public static function getDeclaredEnumCases(File $phpcsFile, $stackPtr)
* If no properties are found or a parse error is encountered,
* an empty array is returned.
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token doesn't exist or
* is not an OO keyword token.
* @throws \PHPCSUtils\Exceptions\TypeError If the $stackPtr parameter is not an integer.
* @throws \PHPCSUtils\Exceptions\OutOfBoundsStackPtr If the token passed does not exist in the $phpcsFile.
* @throws \PHPCSUtils\Exceptions\UnexpectedTokenType If the token passed is not an OO keyword token.
*/
public static function getDeclaredProperties(File $phpcsFile, $stackPtr)
{
Expand All @@ -457,8 +468,9 @@ public static function getDeclaredProperties(File $phpcsFile, $stackPtr)
* If no methods are found or a parse error is encountered,
* an empty array is returned.
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token doesn't exist or
* is not an OO keyword token.
* @throws \PHPCSUtils\Exceptions\TypeError If the $stackPtr parameter is not an integer.
* @throws \PHPCSUtils\Exceptions\OutOfBoundsStackPtr If the token passed does not exist in the $phpcsFile.
* @throws \PHPCSUtils\Exceptions\UnexpectedTokenType If the token passed is not an OO keyword token.
*/
public static function getDeclaredMethods(File $phpcsFile, $stackPtr)
{
Expand All @@ -481,19 +493,25 @@ public static function getDeclaredMethods(File $phpcsFile, $stackPtr)
* Each index holds an associative array with the name of the "thing"
* as the key and the stack pointer to the related token as the value.
*
* @throws \PHP_CodeSniffer\Exceptions\RuntimeException If the specified token doesn't exist or
* is not an OO keyword token.
* @throws \PHPCSUtils\Exceptions\TypeError If the $stackPtr parameter is not an integer.
* @throws \PHPCSUtils\Exceptions\OutOfBoundsStackPtr If the token passed does not exist in the $phpcsFile.
* @throws \PHPCSUtils\Exceptions\UnexpectedTokenType If the token passed is not an OO keyword token.
*/
private static function analyzeOOStructure(File $phpcsFile, $stackPtr)
{
$tokens = $phpcsFile->getTokens();

if (isset($tokens[$stackPtr]) === false
|| isset(Tokens::$ooScopeTokens[$tokens[$stackPtr]['code']]) === false
) {
throw new RuntimeException(
'$stackPtr must be of type T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT or T_ENUM'
);
if (\is_int($stackPtr) === false) {
throw TypeError::create(2, '$stackPtr', 'integer', $stackPtr);
}

if (isset($tokens[$stackPtr]) === false) {
throw OutOfBoundsStackPtr::create(2, '$stackPtr', $stackPtr);
}

if (isset(Tokens::$ooScopeTokens[$tokens[$stackPtr]['code']]) === false) {
$acceptedTokens = 'T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT or T_ENUM';
throw UnexpectedTokenType::create(2, '$stackPtr', $acceptedTokens, $tokens[$stackPtr]['type']);
}

// Set defaults.
Expand Down
Expand Up @@ -12,7 +12,7 @@

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Internal\Cache;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tests\PolyfilledTestCase;
use PHPCSUtils\Utils\ObjectDeclarations;

/**
Expand All @@ -23,7 +23,7 @@
*
* @since 1.1.0
*/
final class GetDeclaredConstantsTest extends UtilityMethodTestCase
final class GetDeclaredConstantsTest extends PolyfilledTestCase
{

/**
Expand All @@ -46,14 +46,31 @@ public static function setUpTestFile()
parent::setUpTestFile();
}

/**
* Test receiving an expected exception when a non-integer token is passed.
*
* @return void
*/
public function testNonIntegerToken()
{
$this->expectException('PHPCSUtils\Exceptions\TypeError');
$this->expectExceptionMessage('Argument #2 ($stackPtr) must be of type integer, boolean given');

ObjectDeclarations::getDeclaredConstants(self::$phpcsFile, false);
}

/**
* Test receiving an expected exception when a non-existent token is passed.
*
* @return void
*/
public function testNonExistentToken()
{
$this->expectPhpcsException('$stackPtr must be of type T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT or T_ENUM');
$this->expectException('PHPCSUtils\Exceptions\OutOfBoundsStackPtr');
$this->expectExceptionMessage(
'Argument #2 ($stackPtr) must be a stack pointer which exists in the $phpcsFile object, 100000 given'
);

ObjectDeclarations::getDeclaredConstants(self::$phpcsFile, 100000);
}

Expand All @@ -64,7 +81,10 @@ public function testNonExistentToken()
*/
public function testNotTargetToken()
{
$this->expectPhpcsException('$stackPtr must be of type T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT or T_ENUM');
$this->expectException('PHPCSUtils\Exceptions\UnexpectedTokenType');
$this->expectExceptionMessage(
'Argument #2 ($stackPtr) must be of type T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT or T_ENUM;'
);

$stackPtr = $this->getTargetToken('/* testUnacceptableToken */', \T_FUNCTION);
ObjectDeclarations::getDeclaredConstants(self::$phpcsFile, $stackPtr);
Expand Down
Expand Up @@ -12,7 +12,7 @@

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Internal\Cache;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tests\PolyfilledTestCase;
use PHPCSUtils\Utils\ObjectDeclarations;

/**
Expand All @@ -23,7 +23,7 @@
*
* @since 1.1.0
*/
final class GetDeclaredEnumCasesTest extends UtilityMethodTestCase
final class GetDeclaredEnumCasesTest extends PolyfilledTestCase
{

/**
Expand All @@ -46,14 +46,31 @@ public static function setUpTestFile()
parent::setUpTestFile();
}

/**
* Test receiving an expected exception when a non-integer token is passed.
*
* @return void
*/
public function testNonIntegerToken()
{
$this->expectException('PHPCSUtils\Exceptions\TypeError');
$this->expectExceptionMessage('Argument #2 ($stackPtr) must be of type integer, boolean given');

ObjectDeclarations::getDeclaredEnumCases(self::$phpcsFile, false);
}

/**
* Test receiving an expected exception when a non-existent token is passed.
*
* @return void
*/
public function testNonExistentToken()
{
$this->expectPhpcsException('$stackPtr must be of type T_ENUM');
$this->expectException('PHPCSUtils\Exceptions\OutOfBoundsStackPtr');
$this->expectExceptionMessage(
'Argument #2 ($stackPtr) must be a stack pointer which exists in the $phpcsFile object, 100000 given'
);

ObjectDeclarations::getDeclaredEnumCases(self::$phpcsFile, 100000);
}

Expand All @@ -64,7 +81,8 @@ public function testNonExistentToken()
*/
public function testNotTargetToken()
{
$this->expectPhpcsException('$stackPtr must be of type T_ENUM');
$this->expectException('PHPCSUtils\Exceptions\UnexpectedTokenType');
$this->expectExceptionMessage('Argument #2 ($stackPtr) must be of type T_ENUM;');

$stackPtr = $this->getTargetToken('/* testUnacceptableToken */', \T_FUNCTION);
ObjectDeclarations::getDeclaredEnumCases(self::$phpcsFile, $stackPtr);
Expand Down
Expand Up @@ -12,7 +12,7 @@

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Internal\Cache;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tests\PolyfilledTestCase;
use PHPCSUtils\Utils\ObjectDeclarations;

/**
Expand All @@ -23,7 +23,7 @@
*
* @since 1.1.0
*/
final class GetDeclaredMethodsTest extends UtilityMethodTestCase
final class GetDeclaredMethodsTest extends PolyfilledTestCase
{

/**
Expand All @@ -46,14 +46,31 @@ public static function setUpTestFile()
parent::setUpTestFile();
}

/**
* Test receiving an expected exception when a non-integer token is passed.
*
* @return void
*/
public function testNonIntegerToken()
{
$this->expectException('PHPCSUtils\Exceptions\TypeError');
$this->expectExceptionMessage('Argument #2 ($stackPtr) must be of type integer, boolean given');

ObjectDeclarations::getDeclaredMethods(self::$phpcsFile, false);
}

/**
* Test receiving an expected exception when a non-existent token is passed.
*
* @return void
*/
public function testNonExistentToken()
{
$this->expectPhpcsException('$stackPtr must be of type T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT or T_ENUM');
$this->expectException('PHPCSUtils\Exceptions\OutOfBoundsStackPtr');
$this->expectExceptionMessage(
'Argument #2 ($stackPtr) must be a stack pointer which exists in the $phpcsFile object, 100000 given'
);

ObjectDeclarations::getDeclaredMethods(self::$phpcsFile, 100000);
}

Expand All @@ -64,7 +81,10 @@ public function testNonExistentToken()
*/
public function testNotTargetToken()
{
$this->expectPhpcsException('$stackPtr must be of type T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT or T_ENUM');
$this->expectException('PHPCSUtils\Exceptions\UnexpectedTokenType');
$this->expectExceptionMessage(
'Argument #2 ($stackPtr) must be of type T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT or T_ENUM;'
);

$stackPtr = $this->getTargetToken('/* testUnacceptableToken */', \T_FUNCTION);
ObjectDeclarations::getDeclaredMethods(self::$phpcsFile, $stackPtr);
Expand Down
Expand Up @@ -12,7 +12,7 @@

use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Internal\Cache;
use PHPCSUtils\TestUtils\UtilityMethodTestCase;
use PHPCSUtils\Tests\PolyfilledTestCase;
use PHPCSUtils\Utils\ObjectDeclarations;

/**
Expand All @@ -23,7 +23,7 @@
*
* @since 1.1.0
*/
final class GetDeclaredPropertiesTest extends UtilityMethodTestCase
final class GetDeclaredPropertiesTest extends PolyfilledTestCase
{

/**
Expand All @@ -46,14 +46,31 @@ public static function setUpTestFile()
parent::setUpTestFile();
}

/**
* Test receiving an expected exception when a non-integer token is passed.
*
* @return void
*/
public function testNonIntegerToken()
{
$this->expectException('PHPCSUtils\Exceptions\TypeError');
$this->expectExceptionMessage('Argument #2 ($stackPtr) must be of type integer, boolean given');

ObjectDeclarations::getDeclaredProperties(self::$phpcsFile, false);
}

/**
* Test receiving an expected exception when a non-existent token is passed.
*
* @return void
*/
public function testNonExistentToken()
{
$this->expectPhpcsException('$stackPtr must be of type T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT or T_ENUM');
$this->expectException('PHPCSUtils\Exceptions\OutOfBoundsStackPtr');
$this->expectExceptionMessage(
'Argument #2 ($stackPtr) must be a stack pointer which exists in the $phpcsFile object, 100000 given'
);

ObjectDeclarations::getDeclaredProperties(self::$phpcsFile, 100000);
}

Expand All @@ -64,7 +81,10 @@ public function testNonExistentToken()
*/
public function testNotTargetToken()
{
$this->expectPhpcsException('$stackPtr must be of type T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT or T_ENUM');
$this->expectException('PHPCSUtils\Exceptions\UnexpectedTokenType');
$this->expectExceptionMessage(
'Argument #2 ($stackPtr) must be of type T_CLASS, T_ANON_CLASS, T_INTERFACE, T_TRAIT or T_ENUM;'
);

$stackPtr = $this->getTargetToken('/* testUnacceptableToken */', \T_FUNCTION);
ObjectDeclarations::getDeclaredProperties(self::$phpcsFile, $stackPtr);
Expand Down

0 comments on commit d3dc01a

Please sign in to comment.