From: Philipp Holzer Date: Thu, 5 Jul 2018 05:59:56 +0000 (+0200) Subject: code standards / simplifications X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=906bb25972bf3f23e6cc3b98a50c0f3fa0990964;p=friendica.git code standards / simplifications --- diff --git a/src/Core/Cache/ArrayCache.php b/src/Core/Cache/ArrayCache.php index d4fe8bc7f7..71610c39df 100644 --- a/src/Core/Cache/ArrayCache.php +++ b/src/Core/Cache/ArrayCache.php @@ -6,7 +6,7 @@ namespace Friendica\Core\Cache; use Friendica\Core\Cache; /** - * @brief Implementation of the IMemoryCacheDriver mainly for testing purpose + * Implementation of the IMemoryCacheDriver mainly for testing purpose * * Class ArrayCache * @@ -80,4 +80,4 @@ class ArrayCache implements IMemoryCacheDriver return false; } } -} \ No newline at end of file +} diff --git a/src/Core/Cache/CacheDriverFactory.php b/src/Core/Cache/CacheDriverFactory.php index 45cc17a52f..8fbdc1549e 100644 --- a/src/Core/Cache/CacheDriverFactory.php +++ b/src/Core/Cache/CacheDriverFactory.php @@ -9,12 +9,12 @@ use Friendica\Core\Config; * * @package Friendica\Core\Cache * - * @brief A basic class to generate a CacheDriver + * A basic class to generate a CacheDriver */ class CacheDriverFactory { /** - * @brief This method creates a CacheDriver for the given cache driver name + * This method creates a CacheDriver for the given cache driver name * * @param string $driver The name of the cache driver * @return ICacheDriver The instance of the CacheDriver diff --git a/src/Core/Cache/ICacheDriver.php b/src/Core/Cache/ICacheDriver.php index ff329f34eb..ced7b4e216 100644 --- a/src/Core/Cache/ICacheDriver.php +++ b/src/Core/Cache/ICacheDriver.php @@ -12,7 +12,7 @@ use Friendica\Core\Cache; interface ICacheDriver { /** - * @brief Fetches cached data according to the key + * Fetches cached data according to the key * * @param string $key The key to the cached data * @@ -21,18 +21,18 @@ interface ICacheDriver public function get($key); /** - * @brief Stores data in the cache identified by the key. The input $value can have multiple formats. + * Stores data in the cache identified by the key. The input $value can have multiple formats. * * @param string $key The cache key * @param mixed $value The value to store - * @param integer $ttl The cache lifespan, must be one of the Cache constants + * @param integer $ttl The cache lifespan, must be one of the Cache constants * * @return bool */ public function set($key, $value, $ttl = Cache::FIVE_MINUTES); /** - * @brief Delete a key from the cache + * Delete a key from the cache * * @param string $key The cache key * @@ -41,7 +41,7 @@ interface ICacheDriver public function delete($key); /** - * @brief Remove outdated data from the cache + * Remove outdated data from the cache * * @return bool */ diff --git a/src/Core/Cache/IMemoryCacheDriver.php b/src/Core/Cache/IMemoryCacheDriver.php index 7843ca7b5f..a50e2d1d48 100644 --- a/src/Core/Cache/IMemoryCacheDriver.php +++ b/src/Core/Cache/IMemoryCacheDriver.php @@ -4,7 +4,7 @@ namespace Friendica\Core\Cache; use Friendica\Core\Cache; /** - * @brief This interface defines methods for Memory-Caches only + * This interface defines methods for Memory-Caches only * * Interface IMemoryCacheDriver * @@ -13,7 +13,7 @@ use Friendica\Core\Cache; interface IMemoryCacheDriver extends ICacheDriver { /** - * @brief Sets a value if it's not already stored + * Sets a value if it's not already stored * * @param string $key The cache key * @param mixed $value The old value we know from the cache @@ -23,7 +23,7 @@ interface IMemoryCacheDriver extends ICacheDriver public function add($key, $value, $ttl = Cache::FIVE_MINUTES); /** - * @brief Compares if the old value is set and sets the new value + * Compares if the old value is set and sets the new value * * @param string $key The cache key * @param mixed $oldValue The old value we know from the cache @@ -35,11 +35,11 @@ interface IMemoryCacheDriver extends ICacheDriver public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES); /** - * @brief Compares if the old value is set and removes it + * Compares if the old value is set and removes it * * @param string $key The cache key * @param mixed $value The old value we know and want to delete * @return bool */ public function compareDelete($key, $value); -} \ No newline at end of file +} diff --git a/src/Core/Cache/MemcacheCacheDriver.php b/src/Core/Cache/MemcacheCacheDriver.php index 0b1ca3cec9..8eb45d907e 100644 --- a/src/Core/Cache/MemcacheCacheDriver.php +++ b/src/Core/Cache/MemcacheCacheDriver.php @@ -90,6 +90,9 @@ class MemcacheCacheDriver extends BaseObject implements IMemoryCacheDriver return $this->memcache->delete($key); } + /** + * (@inheritdoc) + */ public function clear() { return $this->memcache->flush(); diff --git a/src/Core/Cache/RedisCacheDriver.php b/src/Core/Cache/RedisCacheDriver.php index d23fa2697b..25c18aa6b6 100644 --- a/src/Core/Cache/RedisCacheDriver.php +++ b/src/Core/Cache/RedisCacheDriver.php @@ -84,12 +84,7 @@ class RedisCacheDriver extends BaseObject implements IMemoryCacheDriver /** - * @brief Sets a value if it's not already stored - * - * @param string $key The cache key - * @param mixed $value The old value we know from the cache - * @param int $ttl The cache lifespan, must be one of the Cache constants - * @return bool + * (@inheritdoc) */ public function add($key, $value, $ttl = Cache::FIVE_MINUTES) { @@ -101,13 +96,7 @@ class RedisCacheDriver extends BaseObject implements IMemoryCacheDriver } /** - * @brief Compares if the old value is set and sets the new value - * - * @param string $key The cache key - * @param mixed $oldValue The old value we know - * @param mixed $newValue The new value we want to set - * @param int $ttl The cache lifespan, must be one of the Cache constants - * @return bool + * (@inheritdoc) */ public function compareSet($key, $oldValue, $newValue, $ttl = Cache::FIVE_MINUTES) { @@ -133,11 +122,7 @@ class RedisCacheDriver extends BaseObject implements IMemoryCacheDriver return false; } /** - * @brief Compares if the old value is set and removes it - * - * @param string $key The cache key - * @param mixed $value The old value we know and want to delete - * @return bool + * (@inheritdoc) */ public function compareDelete($key, $value) { diff --git a/src/Core/Cache/TraitCompareDelete.php b/src/Core/Cache/TraitCompareDelete.php index 898e39aecc..ef59f69cd1 100644 --- a/src/Core/Cache/TraitCompareDelete.php +++ b/src/Core/Cache/TraitCompareDelete.php @@ -7,7 +7,7 @@ use Friendica\Core\Cache; /** * Trait TraitCompareSetDelete * - * @brief This Trait is to compensate non native "exclusive" sets/deletes in caches + * This Trait is to compensate non native "exclusive" sets/deletes in caches * * @package Friendica\Core\Cache */ @@ -22,7 +22,7 @@ trait TraitCompareDelete abstract public function add($key, $value, $ttl = Cache::FIVE_MINUTES); /** - * @brief NonNative - Compares if the old value is set and removes it + * NonNative - Compares if the old value is set and removes it * * @param string $key The cache key * @param mixed $value The old value we know and want to delete @@ -42,4 +42,4 @@ trait TraitCompareDelete return false; } } -} \ No newline at end of file +} diff --git a/src/Core/Cache/TraitCompareSet.php b/src/Core/Cache/TraitCompareSet.php index 55193b7567..77a6028355 100644 --- a/src/Core/Cache/TraitCompareSet.php +++ b/src/Core/Cache/TraitCompareSet.php @@ -7,7 +7,7 @@ use Friendica\Core\Cache; /** * Trait TraitCompareSetDelete * - * @brief This Trait is to compensate non native "exclusive" sets/deletes in caches + * This Trait is to compensate non native "exclusive" sets/deletes in caches * * @package Friendica\Core\Cache */ @@ -22,7 +22,7 @@ trait TraitCompareSet abstract public function add($key, $value, $ttl = Cache::FIVE_MINUTES); /** - * @brief NonNative - Compares if the old value is set and sets the new value + * NonNative - Compares if the old value is set and sets the new value * * @param string $key The cache key * @param mixed $oldValue The old value we know from the cache @@ -45,4 +45,4 @@ trait TraitCompareSet return false; } } -} \ No newline at end of file +} diff --git a/src/Core/Lock.php b/src/Core/Lock.php index 7235c64a98..47a1f9b4fe 100644 --- a/src/Core/Lock.php +++ b/src/Core/Lock.php @@ -29,21 +29,9 @@ class Lock try { switch ($lock_driver) { case 'memcache': - $cache_driver = CacheDriverFactory::create('memcache'); - if ($cache_driver instanceof IMemoryCacheDriver) { - self::$driver = new Lock\CacheLockDriver($cache_driver); - } - break; - case 'memcached': - $cache_driver = CacheDriverFactory::create('memcached'); - if ($cache_driver instanceof IMemoryCacheDriver) { - self::$driver = new Lock\CacheLockDriver($cache_driver); - } - break; - case 'redis': - $cache_driver = CacheDriverFactory::create('redis'); + $cache_driver = CacheDriverFactory::create($lock_driver); if ($cache_driver instanceof IMemoryCacheDriver) { self::$driver = new Lock\CacheLockDriver($cache_driver); } @@ -129,7 +117,7 @@ class Lock */ public static function acquireLock($key, $timeout = 120) { - return self::getDriver()->acquireLock($key, $timeout); + return self::getDriver()->acquire($key, $timeout); } /** @@ -140,7 +128,7 @@ class Lock */ public static function releaseLock($key) { - return self::getDriver()->releaseLock($key); + return self::getDriver()->release($key); } /** diff --git a/src/Core/Lock/AbstractLockDriver.php b/src/Core/Lock/AbstractLockDriver.php index 09549c50bf..53597d45fc 100644 --- a/src/Core/Lock/AbstractLockDriver.php +++ b/src/Core/Lock/AbstractLockDriver.php @@ -8,7 +8,7 @@ use Friendica\BaseObject; * * @package Friendica\Core\Lock * - * @brief Basic class for Locking with common functions (local acquired locks, releaseAll, ..) + * Basic class for Locking with common functions (local acquired locks, releaseAll, ..) */ abstract class AbstractLockDriver extends BaseObject implements ILockDriver { @@ -18,7 +18,7 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver protected $acquiredLocks = []; /** - * @brief Check if we've locally acquired a lock + * Check if we've locally acquired a lock * * @param string key The Name of the lock * @return bool Returns true if the lock is set @@ -28,7 +28,7 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver } /** - * @brief Mark a locally acquired lock + * Mark a locally acquired lock * * @param string $key The Name of the lock */ @@ -37,7 +37,7 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver } /** - * @brief Mark a release of a locally acquired lock + * Mark a release of a locally acquired lock * * @param string $key The Name of the lock */ @@ -46,13 +46,13 @@ abstract class AbstractLockDriver extends BaseObject implements ILockDriver } /** - * @brief Releases all lock that were set by us + * Releases all lock that were set by us * * @return void */ public function releaseAll() { foreach ($this->acquiredLocks as $acquiredLock => $hasLock) { - $this->releaseLock($acquiredLock); + $this->release($acquiredLock); } } } diff --git a/src/Core/Lock/CacheLockDriver.php b/src/Core/Lock/CacheLockDriver.php index 13d912c1e2..57627acecf 100644 --- a/src/Core/Lock/CacheLockDriver.php +++ b/src/Core/Lock/CacheLockDriver.php @@ -22,15 +22,9 @@ class CacheLockDriver extends AbstractLockDriver } /** - * - * @brief Sets a lock for a given name - * - * @param string $key The Name of the lock - * @param integer $timeout Seconds until we give up - * - * @return boolean Was the lock successful? + * (@inheritdoc) */ - public function acquireLock($key, $timeout = 120) + public function acquire($key, $timeout = 120) { $got_lock = false; $start = time(); @@ -64,11 +58,9 @@ class CacheLockDriver extends AbstractLockDriver } /** - * @brief Removes a lock if it was set by us - * - * @param string $key Name of the lock + * (@inheritdoc) */ - public function releaseLock($key) + public function release($key) { $cachekey = self::getCacheKey($key); @@ -77,10 +69,7 @@ class CacheLockDriver extends AbstractLockDriver } /** - * @brief Checks, if a key is currently locked to a process - * - * @param string $key The name of the lock - * @return bool + * (@inheritdoc) */ public function isLocked($key) { diff --git a/src/Core/Lock/DatabaseLockDriver.php b/src/Core/Lock/DatabaseLockDriver.php index 6f4b942a4d..8f8e174214 100644 --- a/src/Core/Lock/DatabaseLockDriver.php +++ b/src/Core/Lock/DatabaseLockDriver.php @@ -14,7 +14,7 @@ class DatabaseLockDriver extends AbstractLockDriver /** * (@inheritdoc) */ - public function acquireLock($key, $timeout = 120) + public function acquire($key, $timeout = 120) { $got_lock = false; $start = time(); @@ -55,7 +55,7 @@ class DatabaseLockDriver extends AbstractLockDriver /** * (@inheritdoc) */ - public function releaseLock($key) + public function release($key) { dba::delete('locks', ['name' => $key, 'pid' => getmypid()]); diff --git a/src/Core/Lock/ILockDriver.php b/src/Core/Lock/ILockDriver.php index 3fbe049d37..af8a1d56ae 100644 --- a/src/Core/Lock/ILockDriver.php +++ b/src/Core/Lock/ILockDriver.php @@ -10,7 +10,7 @@ namespace Friendica\Core\Lock; interface ILockDriver { /** - * @brief Checks, if a key is currently locked to a or my process + * Checks, if a key is currently locked to a or my process * * @param string $key The name of the lock * @return bool @@ -19,26 +19,26 @@ interface ILockDriver /** * - * @brief Acquires a lock for a given name + * Acquires a lock for a given name * * @param string $key The Name of the lock * @param integer $timeout Seconds until we give up * * @return boolean Was the lock successful? */ - public function acquireLock($key, $timeout = 120); + public function acquire($key, $timeout = 120); /** - * @brief Releases a lock if it was set by us + * Releases a lock if it was set by us * * @param string $key The Name of the lock * * @return void */ - public function releaseLock($key); + public function release($key); /** - * @brief Releases all lock that were set by us + * Releases all lock that were set by us * * @return void */ diff --git a/src/Core/Lock/SemaphoreLockDriver.php b/src/Core/Lock/SemaphoreLockDriver.php index b4439743c8..250a75fbfe 100644 --- a/src/Core/Lock/SemaphoreLockDriver.php +++ b/src/Core/Lock/SemaphoreLockDriver.php @@ -14,11 +14,7 @@ class SemaphoreLockDriver extends AbstractLockDriver } /** - * @brief Creates a semaphore key - * - * @param string $key Name of the lock - * - * @return integer the semaphore key + * (@inheritdoc) */ private static function semaphoreKey($key) { @@ -35,14 +31,9 @@ class SemaphoreLockDriver extends AbstractLockDriver /** * - * @brief Sets a lock for a given name - * - * @param string $key The Name of the lock - * @param integer $timeout Seconds until we give up - * - * @return boolean Was the lock successful? + * (@inheritdoc) */ - public function acquireLock($key, $timeout = 120) + public function acquire($key, $timeout = 120) { self::$semaphore[$key] = sem_get(self::semaphoreKey($key)); if (self::$semaphore[$key]) { @@ -56,13 +47,9 @@ class SemaphoreLockDriver extends AbstractLockDriver } /** - * @brief Removes a lock if it was set by us - * - * @param string $key Name of the lock - * - * @return mixed + * (@inheritdoc) */ - public function releaseLock($key) + public function release($key) { if (empty(self::$semaphore[$key])) { return false; @@ -75,10 +62,7 @@ class SemaphoreLockDriver extends AbstractLockDriver } /** - * @brief Checks, if a key is currently locked to a process - * - * @param string $key The name of the lock - * @return bool + * (@inheritdoc) */ public function isLocked($key) { diff --git a/tests/src/Core/Lock/CacheLockDriverTest.php b/tests/src/Core/Lock/CacheLockDriverTest.php index a089059725..b39000e119 100644 --- a/tests/src/Core/Lock/CacheLockDriverTest.php +++ b/tests/src/Core/Lock/CacheLockDriverTest.php @@ -1,6 +1,6 @@ instance->acquireLock('foo', 1); + $this->instance->acquire('foo', 1); $this->assertTrue($this->instance->isLocked('foo')); $this->assertFalse($this->instance->isLocked('bar')); } public function testDoubleLock() { - $this->instance->acquireLock('foo', 1); + $this->instance->acquire('foo', 1); $this->assertTrue($this->instance->isLocked('foo')); // We already locked it - $this->assertTrue($this->instance->acquireLock('foo', 1)); + $this->assertTrue($this->instance->acquire('foo', 1)); } public function testReleaseLock() { - $this->instance->acquireLock('foo', 1); + $this->instance->acquire('foo', 1); $this->assertTrue($this->instance->isLocked('foo')); - $this->instance->releaseLock('foo'); + $this->instance->release('foo'); $this->assertFalse($this->instance->isLocked('foo')); } public function testReleaseAll() { - $this->instance->acquireLock('foo', 1); - $this->instance->acquireLock('bar', 1); - $this->instance->acquireLock('#/$%§', 1); + $this->instance->acquire('foo', 1); + $this->instance->acquire('bar', 1); + $this->instance->acquire('#/$%§', 1); $this->instance->releaseAll(); @@ -66,11 +66,11 @@ abstract class LockTest extends TestCase } public function testReleaseAfterUnlock() { - $this->instance->acquireLock('foo', 1); - $this->instance->acquireLock('bar', 1); - $this->instance->acquireLock('#/$%§', 1); + $this->instance->acquire('foo', 1); + $this->instance->acquire('bar', 1); + $this->instance->acquire('#/$%§', 1); - $this->instance->releaseLock('foo'); + $this->instance->release('foo'); $this->instance->releaseAll(); diff --git a/tests/src/Core/Lock/SemaphoreLockDriverTest.php b/tests/src/Core/Lock/SemaphoreLockDriverTest.php index fb7efd6584..0fcf789e61 100644 --- a/tests/src/Core/Lock/SemaphoreLockDriverTest.php +++ b/tests/src/Core/Lock/SemaphoreLockDriverTest.php @@ -1,6 +1,6 @@