Small improvements in the code hacker

- Convert a hardcoded number to a constant
- Add more information to an exception message
- Remove unnecessary PHPCS disabling
- Throw exception on PHP<7.0
This commit is contained in:
Nestor Soriano 2020-05-18 10:07:20 +02:00
parent 8a7d955253
commit 0726f71aac
3 changed files with 31 additions and 12 deletions

View File

@ -31,9 +31,10 @@ use \ReflectionException;
*
* For using with PHPUnit, see CodeHackerTestHook.
*/
class CodeHacker {
final class CodeHacker {
const PROTOCOL = 'file';
const PROTOCOL = 'file';
const HACK_CALLBACK_ARGUMENT_COUNT = 2;
/**
* Value of "context" parameter to be passed to the native PHP filesystem related functions.
@ -132,11 +133,11 @@ class CodeHacker {
*/
public static function add_hack( $hack, $persistent = false ) {
if ( ! is_callable( $hack ) && ! is_object( $hack ) ) {
throw new \Exception( "Hacks must be either functions, or objects having a 'process(\$text, \$path)' method." );
throw new \Exception( "CodeHacker::addhack: Hacks must be either functions, or objects having a 'process(\$text, \$path)' method." );
}
if ( ! self::is_valid_hack_callback( $hack ) && ! self::is_valid_hack_object( $hack ) ) {
throw new \Exception( "CodeHacker::addhack: hacks must be either a function with a 'hack(\$code,\$path)' signature, or an object containing a public method 'hack' with that signature. " );
throw new \Exception( "CodeHacker::addhack: Hacks must be either a function with a 'hack(\$code,\$path)' signature, or an object containing a public method 'hack' with that signature. " );
}
if ( $persistent ) {
@ -154,7 +155,7 @@ class CodeHacker {
* @throws ReflectionException Error when instantiating ReflectionFunction.
*/
private static function is_valid_hack_callback( $callback ) {
return is_callable( $callback ) && 2 === ( new ReflectionFunction( $callback ) )->getNumberOfRequiredParameters();
return is_callable( $callback ) && HACK_CALLBACK_ARGUMENT_COUNT === ( new ReflectionFunction( $callback ) )->getNumberOfRequiredParameters();
}
/**
@ -162,7 +163,7 @@ class CodeHacker {
*
* @param mixed $callback Argument to check.
*
* @return boolt rue if the argument is a valid hack object, false otherwise.
* @return bool rue if the argument is a valid hack object, false otherwise.
*/
private static function is_valid_hack_object( $callback ) {
if ( ! is_object( $callback ) ) {

View File

@ -5,8 +5,6 @@
* @package WooCommerce/Testing
*/
// phpcs:disable Squiz.Commenting.FunctionComment.Missing, PHPCompatibility.FunctionDeclarations
namespace Automattic\WooCommerce\Testing\CodeHacking;
use PHPUnit\Runner\BeforeTestHook;
@ -64,12 +62,27 @@ use Exception;
*/
final class CodeHackerTestHook implements BeforeTestHook, AfterTestHook {
// phpcs:disable PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.voidFound
/**
* Runs after each test.
*
* @param string $test "TestClass::TestMethod".
* @param float $time The time it took the test to run, in seconds.
*/
public function executeAfterTest( string $test, float $time ): void {
if ( ! CodeHacker::has_persistent_hacks() ) {
CodeHacker::disable();
}
}
/**
* Runs before each test.
*
* @param string $test "TestClass::TestMethod".
*
* @throws \ReflectionException Thrown by execute_before_methods.
*/
public function executeBeforeTest( string $test ): void {
/**
* Possible formats of $test:
@ -79,7 +92,7 @@ final class CodeHackerTestHook implements BeforeTestHook, AfterTestHook {
*/
$parts = explode( '::', $test );
if ( count( $parts ) < 2 ) {
return;
return; // "Warning" was supplied as argument
}
$class_name = $parts[0];
$method_name = explode( ' ', $parts[1] )[0];
@ -95,6 +108,8 @@ final class CodeHackerTestHook implements BeforeTestHook, AfterTestHook {
}
}
// phpcs:enable PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.voidFound
/**
* Apply hacks defined in @hack annotations.
*
@ -164,4 +179,3 @@ final class CodeHackerTestHook implements BeforeTestHook, AfterTestHook {
}
}
// phpcs:enable Squiz.Commenting.FunctionComment.Missing, PHPCompatibility.FunctionDeclarations

View File

@ -29,10 +29,14 @@ abstract class CodeHack {
*
* @param string $code PHP code to tokenize.
* @return array Tokenized code.
* @throws \Exception PHP version is less than 7.0.
*/
protected function tokenize( $code ) {
//phpcs:ignore PHPCompatibility.FunctionUse.NewFunctionParameters, PHPCompatibility.Constants.NewConstants
return PHP_VERSION_ID >= 70000 ? token_get_all( $code, TOKEN_PARSE ) : token_get_all( $code );
if ( PHP_VERSION_ID < 70000 ) {
throw new \Exception( 'The code hacker can be used in PHP 7.0+ only.' );
}
return token_get_all( $code, TOKEN_PARSE );
}
/**