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

Fix: PHPCS Errors for PHP files #143

Merged
merged 10 commits into from
Feb 8, 2023
2 changes: 1 addition & 1 deletion src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,6 @@ public function define_services(): void {
*
* @since 1.0.0
*/
do_action( 'rtcamp.google_login_services', $this );
do_action( 'rtcamp.google_login_services', $this ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.
Copy link
Member

Choose a reason for hiding this comment

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

What about disabling this rule in phpcs config?

<rule ref="WordPress-Core">
	<exclude name="Generic.Arrays.DisallowShortArraySyntax" />
	<exclude name="Generic.Commenting.DocComment.MissingShort" />
	<exclude name="WordPress.PHP.DisallowShortTernary" />
+	<exclude name="WordPress.NamingConventions.ValidHookName.UseUnderscores" />
</rule>

Probably include a todo comment as well?

<!-- @todo: Remove this once we rename the hooks with underscores and deprecate the old ones. -->

Copy link
Author

Choose a reason for hiding this comment

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

Yes, We can do exclude it from there with a todo for future, works well. 👍

}
}
1 change: 1 addition & 0 deletions src/Interfaces/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @package WpGuruDev\OrderExport\Interfaces
*/
interface Container {

/**
* Define services in container.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Modules/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function render_login_button( $attributes ): string {
* @since 1.2.3
*/
$force_display = $attributes['forceDisplay'] ?? false;
if ( $force_display || ! is_user_logged_in() || apply_filters( 'rtcamp.google_login_button_display', false ) ) {
if ( $force_display || ! is_user_logged_in() || apply_filters( 'rtcamp.google_login_button_display', false ) ) { // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.
$markup = $this->markup(
[
'login_url' => $this->client->authorization_url(),
Expand Down
4 changes: 2 additions & 2 deletions src/Modules/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function authenticate( $user = null ) {
*
* @param WP_User $user WP User object.
*/
do_action( 'rtcamp.google_user_authenticated', $user );
do_action( 'rtcamp.google_user_authenticated', $user ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.

return $user;
}
Expand Down Expand Up @@ -205,7 +205,7 @@ public function state_redirect( array $state ): array {
*
* @param string $admin_url Admin URL address.
*/
$state['redirect_to'] = $redirect_to ?? apply_filters( 'rtcamp.google_default_redirect', admin_url() );
$state['redirect_to'] = $redirect_to ?? apply_filters( 'rtcamp.google_default_redirect', admin_url() ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.

return $state;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Modules/OneTapLogin.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,9 @@ public function validate_token(): void {
*
* @since 1.0.16
*/
do_action( 'rtcamp.id_token_verified' );
do_action( 'rtcamp.id_token_verified' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.

$redirect_to = apply_filters( 'rtcamp.google_default_redirect', admin_url() );
$redirect_to = apply_filters( 'rtcamp.google_default_redirect', admin_url() ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.
$state = Helper::filter_input( INPUT_POST, 'state', FILTER_SANITIZE_STRING );
$decoded_state = $state ? (array) ( json_decode( base64_decode( $state ) ) ) : null;

Expand Down
1 change: 0 additions & 1 deletion src/Modules/Shortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ public function scan_shortcode( string $output, string $tag, $attrs ): string {
return $output;
}


/**
* Filter redirect URL as per shortcode param.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function run(): void {
*
* @since 1.0.0
*/
$this->active_modules = apply_filters( 'rtcamp.google_login_modules', $this->active_modules );
$this->active_modules = apply_filters( 'rtcamp.google_login_modules', $this->active_modules ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.

$this->container()->define_services();
$this->activate_modules();
Expand Down
6 changes: 3 additions & 3 deletions src/Utils/Authenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function authenticate( stdClass $user ): WP_User {
* @param WP_User $user_wp WP User data object.
* @param stdClass $user User data object returned by Google.
*/
do_action( 'rtcamp.google_user_logged_in', $user_wp, $user );
do_action( 'rtcamp.google_user_logged_in', $user_wp, $user ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.

return $user_wp;
}
Expand All @@ -80,7 +80,7 @@ public function authenticate( stdClass $user ): WP_User {
* @param stdClass $user User object from google.
* @since 1.0.0
*/
return apply_filters( 'rtcamp.google_register_user', $this->maybe_create_username( $user ) );
return apply_filters( 'rtcamp.google_register_user', $this->maybe_create_username( $user ) ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.
}

/**
Expand Down Expand Up @@ -115,7 +115,7 @@ public function register( stdClass $user ): ?WP_User {
/**
* Fires once the user has been registered successfully.
*/
do_action( 'rtcamp.google_user_created', $uid, $user );
do_action( 'rtcamp.google_user_created', $uid, $user ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.

return get_user_by( 'id', $uid );
}
Expand Down
9 changes: 4 additions & 5 deletions src/Utils/GoogleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function set_access_token( string $code ): self {
* @return string
*/
public function gt_redirect_url(): string {
return apply_filters( 'rtcamp.google_redirect_url', $this->redirect_uri );
return apply_filters( 'rtcamp.google_redirect_url', $this->redirect_uri ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.
}

/**
Expand Down Expand Up @@ -152,7 +152,7 @@ public function authorization_url(): string {
*
* @param array $scope List of scopes.
*/
$scope = apply_filters( 'rtcamp.google_scope', $scope );
$scope = apply_filters( 'rtcamp.google_scope', $scope ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.

$client_args = [
'client_id' => $this->client_id,
Expand All @@ -170,7 +170,7 @@ public function authorization_url(): string {
*
* @param array $client_args List of query arguments to send to Google OAuth.
*/
$client_args = apply_filters( 'rtcamp.google_client_args', $client_args );
$client_args = apply_filters( 'rtcamp.google_client_args', $client_args ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.

return self::AUTHORIZE_URL . '?' . http_build_query( $client_args );
}
Expand Down Expand Up @@ -245,10 +245,9 @@ public function user(): \stdClass {
*/
public function state(): string {
$state_data['nonce'] = wp_create_nonce( 'login_with_google' );
hbhalodia marked this conversation as resolved.
Show resolved Hide resolved
$state_data = apply_filters( 'rtcamp.google_login_state', $state_data );
$state_data = apply_filters( 'rtcamp.google_login_state', $state_data ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.
$state_data['provider'] = 'google';

return base64_encode( wp_json_encode( $state_data ) );
}

}
4 changes: 2 additions & 2 deletions src/Utils/TokenVerifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static function get_supported_algorithm( string $algo = '' ) {
$find_algo = array_key_exists( $algo, self::SUPPORTED_ALGORITHMS );

if ( ! $find_algo ) {
return apply_filters( 'rtcamp.default_algorithm', OPENSSL_ALGO_SHA256, $algo );
return apply_filters( 'rtcamp.default_algorithm', OPENSSL_ALGO_SHA256, $algo ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.
}

return self::SUPPORTED_ALGORITHMS[ $algo ];
Expand All @@ -103,7 +103,7 @@ public function verify_token( string $token ): bool {
return true;
} catch ( Exception $e ) {

do_action( 'rtcamp.login_with_google_exception', $e );
do_action( 'rtcamp.login_with_google_exception', $e ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores -- Ignore as currently cannot change because of backward compatibility.

throw $e;
}
Expand Down
2 changes: 1 addition & 1 deletion templates/google-login-button.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

if ( is_user_logged_in() ) {
$button_text = __( 'Log out', 'login-with-google' );
$button_url = wp_logout_url( get_permalink() );
$button_url = wp_logout_url( get_permalink() );
}
?>
<div class="wp_google_login">
Expand Down
110 changes: 62 additions & 48 deletions tests/php/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
*
* @author Guido Scialfa <[email protected]>
*/
class TestCase extends WPMockTestCase
{
class TestCase extends WPMockTestCase {

/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
public function setUp(): void
{
public function setUp(): void {

parent::setUp();
\WP_Mock::setUp();
}
Expand All @@ -35,8 +35,8 @@ public function setUp(): void
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*/
public function tearDown(): void
{
public function tearDown(): void {

parent::tearDown();
\WP_Mock::tearDown();
}
Expand All @@ -46,27 +46,28 @@ public function tearDown(): void
*
* Basic configuration available for all of the testee objects, call `getMock` to get the mock.
*
* @param string $className
* @param array $constructorArguments
* @param array $methods
* @param string $sutMethod
* @param string $class_name
* @param array $constructor_arguments
* @param array $methods
* @param string $sut_method
*
* @return PHPUnit_Framework_MockObject_MockBuilder
*/
protected function buildTesteeMock(
string $className,
array $constructorArguments,
string $class_name,
array $constructor_arguments,
array $methods,
string $sutMethod
): object {
string $sut_method
): object { // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewReturnTypeDeclarations.objectFound -- Ignoring because assuming php application version greater than 7.1.

$testee = $this->getMockBuilder( $class_name );

$testee = $this->getMockBuilder($className);
$constructorArguments
? $testee->setConstructorArgs($constructorArguments)
$constructor_arguments
? $testee->setConstructorArgs( $constructor_arguments )
: $testee->disableOriginalConstructor();

$methods and $testee->setMethods($methods);
$sutMethod and $testee->setMethodsExcept([$sutMethod]);
$methods && $testee->setMethods( $methods );
$sut_method && $testee->setMethodsExcept( [ $sut_method ] );

return $testee;
}
Expand All @@ -75,31 +76,36 @@ protected function buildTesteeMock(
* Retrieve a Testee Mock to Test Protected Methods
*
* return MockBuilder
* @param string $className
* @param array $constructorArguments
*
* @param string $class_name
* @param array $constructor_arguments
* @param string $method
* @param array $methods
* @param array $methods
*
* @return array
*
* @throws ReflectionException
*/
protected function buildTesteeMethodMock(
string $className,
array $constructorArguments,
string $class_name,
array $constructor_arguments,
string $method,
array $methods
): array {

$testee = $this->buildTesteeMock(
$className,
$constructorArguments,
$class_name,
$constructor_arguments,
$methods,
''
)->getMock();
$reflectionMethod = new ReflectionMethod($className, $method);
$reflectionMethod->setAccessible(true);

$reflection_method = new ReflectionMethod( $class_name, $method );
$reflection_method->setAccessible( true );

return [
$testee,
$reflectionMethod,
$reflection_method,
];
}

Expand All @@ -112,13 +118,14 @@ protected function buildTesteeMethodMock(
//phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
protected function getTesteeProperty(
string $property,
object $object
object $object // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewParamTypeDeclarations.objectFound -- Ignoring because assuming php application version greater than 7.1.
) {

$reflection = new \ReflectionClass($object);
$reflectionProperty = $reflection->getProperty($property);
$reflectionProperty->setAccessible(true);
return $reflectionProperty->getValue($object);
$reflection = new \ReflectionClass( $object );
$reflection_property = $reflection->getProperty( $property );

$reflection_property->setAccessible( true );
return $reflection_property->getValue( $object );
}

/**
Expand All @@ -131,12 +138,17 @@ protected function getTesteeProperty(
* @throws ReflectionException
*/
// phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
protected function setTesteeProperty(object $object, string $property, $value): void
{
$reflection = new \ReflectionClass($object);
$reflectionProperty = $reflection->getProperty($property);
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, $value);
protected function setTesteeProperty(
object $object, // phpcs:ignore PHPCompatibility.FunctionDeclarations.NewParamTypeDeclarations.objectFound -- Ignoring because assuming php application version greater than 7.1.
string $property,
$value
): void {

$reflection = new \ReflectionClass( $object );
$reflection_property = $reflection->getProperty( $property );

$reflection_property->setAccessible( true );
$reflection_property->setValue( $object, $value );
}

/**
Expand All @@ -146,30 +158,32 @@ protected function setTesteeProperty(object $object, string $property, $value):
* @param string $function_name
* @param mixed $args
* @param int $times
*
* @param mixed $return
*/
protected function wpMockFunction(
string $functionName,
string $function_name,
$args = [],
int $times = 1,
$return = null
) {

$funcArgs = [
$func_args = [
'times' => $times,
];

if (!empty($args)) {
$funcArgs['args'] = $args;
if ( ! empty( $args ) ) {
$func_args['args'] = $args;
}

if (!empty($return)) {
$funcArgs['return'] = $return;
if ( ! empty( $return ) ) {
$func_args['return'] = $return;
}

\WP_Mock::userFunction(
$functionName,
$funcArgs
$function_name,
$func_args
);

}
}
Loading