Applied coding standards.

This commit is contained in:
Vedanshu Jain 2023-01-25 12:43:14 +05:30
parent 7619c335ff
commit 8fadc81cf7
9 changed files with 53 additions and 55 deletions

View File

@ -41,7 +41,7 @@ class OrderCacheController {
* @param OrderCache $order_cache The order cache engine to use.
*/
final public function init( OrderCache $order_cache ) {
$this->order_cache = $order_cache;
$this->order_cache = $order_cache;
}
/**

View File

@ -12,6 +12,8 @@ interface CacheEngine {
* Retrieves an object cached under a given key.
*
* @param string $key They key under which the object to retrieve is cached.
* @param string $group The group under which the object is cached.
*
* @return array|object|null The cached object, or null if there's no object cached under the passed key.
*/
public function get_cached_object( string $key, string $group = '' );
@ -22,6 +24,8 @@ interface CacheEngine {
* @param string $key The key under which the object will be cached.
* @param array|object $object The object to cache.
* @param int $expiration Expiration for the cached object, in seconds.
* @param string $group The group under which the object will be cached.
*
* @return bool True if the object is cached successfully, false otherwise.
*/
public function cache_object( string $key, $object, int $expiration, string $group = '' ): bool;
@ -30,6 +34,8 @@ interface CacheEngine {
* Removes a cached object from the cache.
*
* @param string $key They key under which the object is cached.
* @param string $group The group under which the object is cached.
*
* @return bool True if the object is removed from the cache successfully, false otherwise (because the object wasn't cached or for other reason).
*/
public function delete_cached_object( string $key, string $group = '' ): bool;
@ -38,6 +44,8 @@ interface CacheEngine {
* Checks if an object is cached under a given key.
*
* @param string $key The key to verify.
* @param string $group The group under which the object is cached.
*
* @return bool True if there's an object cached under the given key, false otherwise.
*/
public function is_cached( string $key, string $group = '' ): bool;

View File

@ -149,7 +149,7 @@ abstract class ObjectCache {
}
}
$id = $this->get_id_from_object_if_null( $object, $id );
$id = $this->get_id_from_object_if_null( $object, $id );
$this->last_cached_data = $object;
return $this->get_cache_engine()->cache_object(

View File

@ -9,10 +9,12 @@ class WPCacheEngine implements CacheEngine {
use CacheNameSpaceTrait;
/**
* Gets an object from the cache.
* Retrieves an object cached under a given key.
*
* @param string $key The key under which the object is cached.
* @return array|object|null The cached object, or null if the object is not cached.
* @param string $key They key under which the object to retrieve is cached.
* @param string $group The group under which the object is cached.
*
* @return array|object|null The cached object, or null if there's no object cached under the passed key.
*/
public function get_cached_object( string $key, string $group = '' ) {
$prefixed_key = self::get_prefixed_key( $key, $group );
@ -26,6 +28,8 @@ class WPCacheEngine implements CacheEngine {
* @param string $key The key under which the object will be cached.
* @param array|object $object The object to cache.
* @param int $expiration Expiration for the cached object, in seconds.
* @param string $group The group under which the object will be cached.
*
* @return bool True if the object is cached successfully, false otherwise.
*/
public function cache_object( string $key, $object, int $expiration, string $group = '' ): bool {
@ -37,6 +41,8 @@ class WPCacheEngine implements CacheEngine {
* Removes a cached object from the cache.
*
* @param string $key They key under which the object is cached.
* @param string $group The group under which the object is cached.
*
* @return bool True if the object is removed from the cache successfully, false otherwise (because the object wasn't cached or for other reason).
*/
public function delete_cached_object( string $key, string $group = '' ): bool {
@ -48,6 +54,8 @@ class WPCacheEngine implements CacheEngine {
* Checks if an object is cached under a given key.
*
* @param string $key The key to verify.
* @param string $group The group under which the object is cached.
*
* @return bool True if there's an object cached under the given key, false otherwise.
*/
public function is_cached( string $key, string $group = '' ): bool {

View File

@ -119,6 +119,7 @@ class CustomOrdersTableController {
self::add_filter( DataSynchronizer::PENDING_SYNCHRONIZATION_FINISHED_ACTION, array( $this, 'process_sync_finished' ), 10, 0 );
self::add_action( 'woocommerce_update_options_advanced_custom_data_stores', array( $this, 'process_options_updated' ), 10, 0 );
self::add_action( 'woocommerce_after_register_post_type', array( $this, 'register_post_type_for_order_placeholders' ), 10, 0 );
self::add_action( 'woocommerce_delete_order', array( $this, 'after_order_deleted_or_trashed' ), 10, 1 );
self::add_action( FeaturesController::FEATURE_ENABLED_CHANGED_ACTION, array( $this, 'handle_feature_enabled_changed' ), 10, 2 );
}
@ -485,7 +486,8 @@ class CustomOrdersTableController {
* @throws \Exception Attempt to change the authoritative orders table while orders sync is pending.
*/
private function process_pre_update_option( $value, $option, $old_value ) {
if ( self::CUSTOM_ORDERS_TABLE_USAGE_ENABLED_OPTION !== $option || $value === $old_value || false === $old_value ) {
if ( DataSynchronizer::ORDERS_DATA_SYNC_ENABLED_OPTION === $option && $value !== $old_value ) {
$this->order_cache->flush();
return $value;
}
@ -605,14 +607,5 @@ class CustomOrdersTableController {
);
}
/**
* Handle the order deleted/trashed hooks.
*
* @param int $order_id The id of the order deleted or trashed.
*/
private function after_order_deleted_or_trashed( int $order_id ): void {
if ( $this->features_controller->feature_is_enabled( OrderCacheController::FEATURE_NAME ) ) {
$this->order_cache->remove( $order_id );
}
}
}

View File

@ -50,7 +50,16 @@ class OrdersDataStoreServiceProvider extends AbstractServiceProvider {
$this->share( OrdersTableDataStoreMeta::class );
$this->share( OrdersTableDataStore::class )->addArguments( array( OrdersTableDataStoreMeta::class, DatabaseUtil::class, LegacyProxy::class ) );
$this->share( DataSynchronizer::class )->addArguments( array( OrdersTableDataStore::class, DatabaseUtil::class, PostsToOrdersMigrationController::class, LegacyProxy::class ) );
$this->share( DataSynchronizer::class )->addArguments(
array(
OrdersTableDataStore::class,
DatabaseUtil::class,
PostsToOrdersMigrationController::class,
OrderCache::class,
OrderCacheController::class,
LegacyProxy::class,
)
);
$this->share( OrdersTableRefundDataStore::class )->addArguments( array( OrdersTableDataStoreMeta::class, DatabaseUtil::class, LegacyProxy::class ) );
$this->share( CustomOrdersTableController::class )->addArguments(
array(

View File

@ -33,14 +33,9 @@ class CacheExceptionTest extends \WC_Unit_Test_Case {
protected function validate( $object ): ?array {
}
/**
* @param $id
*
* @return mixed
*/
protected function get_from_datastore( $id ) {
// TODO: Implement get_from_datastore() method.
}};
}
};
// phpcs:enable Squiz.Commenting
}

View File

@ -11,31 +11,18 @@ class InvalidObjectCacheClass extends ObjectCache {
// phpcs:disable Squiz.Commenting
public function get_object_type(): string {
return '';
}
protected function get_object_id( $object ) {
}
protected function validate( $object ): ?array {
}
protected function get_from_datastore( $id ) {
}
// phpcs:enable Squiz.Commenting
/**
* @inheritDoc
*/
protected function get_object_id( $object ) {
// TODO: Implement get_object_id() method.
}
/**
* @inheritDoc
*/
protected function validate( $object ): ?array {
// TODO: Implement validate() method.
}
/**
* @inheritDoc
*/
protected function get_from_datastore( $id ) {
// TODO: Implement get_from_datastore() method.
}
}

View File

@ -153,8 +153,8 @@ class ObjectCacheTest extends \WC_Unit_Test_Case {
$this->assertTrue( $result );
$expected_prefix = \WC_Cache_Helper::get_cache_prefix( 'the_type' );
$key = $expected_prefix . 'the_id';
$this->assertEquals( $object, wp_cache_get( $key, 'the_type' ) );
}
@ -169,9 +169,9 @@ class ObjectCacheTest extends \WC_Unit_Test_Case {
$prefix = \WC_Cache_Helper::get_cache_prefix( 'the_type' );
$key_1 = $prefix . 'the_id_1';
$key_1 = $prefix . 'the_id_1';
$this->assertEquals( $object_1, wp_cache_get( $key_1, 'the_type' ) );
$key_2 = $prefix . '9999';
$key_2 = $prefix . '9999';
$this->assertEquals( $object_2, wp_cache_get( $key_2, 'the_type' ) );
}
@ -254,8 +254,6 @@ class ObjectCacheTest extends \WC_Unit_Test_Case {
/**
* @testdox 'update_if_cached' updates an already cached object the same way as 'set'.
*
* @param ?int $id Id to pass to update_if_cached.
*/
public function test_update_if_cached_updates_already_cached_object() {
$id = 1234;
@ -317,7 +315,7 @@ class ObjectCacheTest extends \WC_Unit_Test_Case {
*/
public function test_set_with_custom_serialization_that_returns_errors( int $errors_count ) {
$exception = null;
$errors = $errors_count === 1 ? array( 'Foo failed' ) : array( 'Foo failed', 'Bar failed' );
$errors = 1 === $errors_count ? array( 'Foo failed' ) : array( 'Foo failed', 'Bar failed' );
$object = array( 'foo' );
// phpcs:disable Squiz.Commenting
@ -355,7 +353,7 @@ class ObjectCacheTest extends \WC_Unit_Test_Case {
}
$expected_message = 'Object validation/serialization failed';
if ( $errors_count === 1 ) {
if ( 1 === $errors_count ) {
$expected_message .= ': Foo failed';
}
$this->assertEquals( $expected_message, $exception->getMessage() );
@ -503,7 +501,6 @@ class ObjectCacheTest extends \WC_Unit_Test_Case {
$engine = new WPCacheEngine();
// phpcs:disable Squiz.Commenting
$sut = new class($engine) extends ObjectCache {
public function get_object_type(): string {
return 'the_type';
@ -530,7 +527,6 @@ class ObjectCacheTest extends \WC_Unit_Test_Case {
protected function get_from_datastore( $id ) {
}
};
// phpcs:enable Squiz.Commenting
$object = array( 'foo' );
@ -543,11 +539,12 @@ class ObjectCacheTest extends \WC_Unit_Test_Case {
* @testdox A custom cache engine instance can be used via 'wc_object_cache_get_engine' filter.
*/
public function test_custom_cache_engine_via_hook() {
$engine = new class extends WPCacheEngine {};
$engine = new class() extends WPCacheEngine {};
$engine_passed_to_filter = null;
$cache_passed_to_filter = null;
$sut = new class() extends ObjectCache {
// phpcs:disable Squiz.Commenting
public function get_object_type(): string {
return 'the_type';
}
@ -561,6 +558,7 @@ class ObjectCacheTest extends \WC_Unit_Test_Case {
protected function get_from_datastore( $id ) {
}
// phpcs:enable Squiz.Commenting
};
add_filter(