Skip to content

Commit

Permalink
Demote the symbol name exception to an error. This will be removed at…
Browse files Browse the repository at this point in the history
… a later date.
  • Loading branch information
johnbillion committed Sep 9, 2024
1 parent 3d09569 commit 42a5ceb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
50 changes: 44 additions & 6 deletions src/Rules/SinceVersionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
final class SinceVersionRule implements Rule {
private static string $functionIdentifier = 'WPCompat.functionNotAvailable';
private static string $methodIdentifier = 'WPCompat.methodNotAvailable';
private static string $errorIdentifier = 'WPCompat.error';

/**
* @var array<string, array{since: string}>
Expand Down Expand Up @@ -83,7 +84,13 @@ public function processNode( Node $node, Scope $scope ): array {
* @return list<RuleError>
*/
private function processFuncCall( FuncCall $node, Scope $scope ): array {
$name = self::getFunctionName( $node );
try {
$name = self::getFunctionName( $node, $scope );
} catch ( \RuntimeException $e ) {
return [
RuleErrorBuilder::message( $e->getMessage() )->identifier( self::$errorIdentifier )->build(),
];
}

if ( $scope->isInFunctionExists( $name ) ) {
return [];
Expand Down Expand Up @@ -142,10 +149,18 @@ private function processMethodCall( CallLike $node, Scope $scope ): array {
}

foreach ( $allClassNames as $className ) {
try {
$methodName = self::getMethodName( $node, $scope );
} catch ( \RuntimeException $e ) {
return [
RuleErrorBuilder::message( $e->getMessage() )->identifier( self::$errorIdentifier )->build(),
];
}

$name = sprintf(
'%s::%s',
$className,
self::getMethodName( $node ),
$methodName,
);

if ( isset( $this->symbols[ $name ] ) ) {
Expand Down Expand Up @@ -182,19 +197,25 @@ private function processMethodVersion( string $name, array $symbol ): array {
/**
* @throws \RuntimeException
*/
private static function getFunctionName( FuncCall $node ): string {
private static function getFunctionName( FuncCall $node, Scope $scope ): string {
if ( $node->name instanceof Name ) {
return $node->name->toString();
}

throw new \RuntimeException( 'Failed to get function name' );
throw new \RuntimeException(
self::error(
'Failed to get function name from %s:%d. Please report this to https://github.com/johnbillion/wp-compat/issues.',
$node,
$scope,
)
);
}

/**
* @param MethodCall|StaticCall $node
* @throws \RuntimeException
*/
private static function getMethodName( CallLike $node ): string {
private static function getMethodName( CallLike $node, Scope $scope ): string {
if ( ( $node instanceof MethodCall ) && ( $node->name instanceof Identifier ) ) {
return $node->name->toString();
}
Expand All @@ -203,6 +224,23 @@ private static function getMethodName( CallLike $node ): string {
return $node->name->toString();
}

throw new \RuntimeException( 'Failed to get method name' );
throw new \RuntimeException(
self::error(
'Failed to get method name from %s:%d. Please report this to https://github.com/johnbillion/wp-compat/issues.',
$node,
$scope,
)
);
}

private static function error( string $message, Node $node, Scope $scope ): string {
$filename = $scope->getFile();
$filename = str_replace( getcwd() . '/', '', $filename );

return sprintf(
$message,
$filename,
$node->getStartLine(),
);
}
}
4 changes: 4 additions & 0 deletions tests/Rules/SinceVersionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public function testRule(): void{
'WP_Date_Query::sanitize_relation() is only available since WordPress version 6.0.3.',
42,
],
[
'Failed to get function name from tests/Rules/data/SinceVersion.php:50. Please report this to https://github.com/johnbillion/wp-compat/issues.',
50,
],
],
);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Rules/data/SinceVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function foo() {
$query = new Another_Date_Query( [] );
$query->foo();

// Temporary test while we look for all the weird ways to call a function:
$function_name = $_GET['foo'];
$function_name();


// ============= //
// Passing usage //
Expand Down

0 comments on commit 42a5ceb

Please sign in to comment.