diff options
author | 2022-08-06 22:46:28 +0200 | |
---|---|---|
committer | 2022-08-06 22:46:28 +0200 | |
commit | 2bbce8ebef8cf4f88392431aabe84a15482dc933 (patch) | |
tree | 1f5027ca69b1dfa2364bd9319e8536b86a41e928 /lib/BridgeAbstract.php | |
parent | b042412416cc4ecc71c3f9c13239661a0dd588a6 (diff) | |
download | rss-bridge-2bbce8ebef8cf4f88392431aabe84a15482dc933.tar.gz rss-bridge-2bbce8ebef8cf4f88392431aabe84a15482dc933.tar.zst rss-bridge-2bbce8ebef8cf4f88392431aabe84a15482dc933.zip |
refactor: general code base refactor (#2950)
* refactor
* fix: bug in previous refactor
* chore: exclude phpcompat sniff due to bug in phpcompat
* fix: do not leak absolute paths
* refactor/fix: batch extensions checking, fix DOS issue
Diffstat (limited to 'lib/BridgeAbstract.php')
-rw-r--r-- | lib/BridgeAbstract.php | 52 |
1 files changed, 13 insertions, 39 deletions
diff --git a/lib/BridgeAbstract.php b/lib/BridgeAbstract.php index 16e057c7..e91c8ae9 100644 --- a/lib/BridgeAbstract.php +++ b/lib/BridgeAbstract.php @@ -12,19 +12,6 @@ * @link https://github.com/rss-bridge/rss-bridge */ -/** - * An abstract class for bridges - * - * This class implements {@see BridgeInterface} with most common functions in - * order to reduce code duplication. Bridges should inherit from this class - * instead of implementing the interface manually. - * - * @todo Move constants to the interface (this is supported by PHP) - * @todo Change visibility of constants to protected - * @todo Return `self` on more functions to allow chaining - * @todo Add specification for PARAMETERS () - * @todo Add specification for $items - */ abstract class BridgeAbstract implements BridgeInterface { /** @@ -107,7 +94,7 @@ abstract class BridgeAbstract implements BridgeInterface * * @var array */ - protected $items = []; + protected array $items = []; /** * Holds the list of input parameters used by the bridge @@ -117,7 +104,7 @@ abstract class BridgeAbstract implements BridgeInterface * * @var array */ - protected $inputs = []; + protected array $inputs = []; /** * Holds the name of the queried context @@ -233,7 +220,7 @@ abstract class BridgeAbstract implements BridgeInterface if (empty(static::PARAMETERS)) { if (!empty($inputs)) { - returnClientError('Invalid parameters value(s)'); + throw new \Exception('Invalid parameters value(s)'); } return; @@ -249,10 +236,7 @@ abstract class BridgeAbstract implements BridgeInterface $validator->getInvalidParameters() ); - returnClientError( - 'Invalid parameters value(s): ' - . implode(', ', $parameters) - ); + throw new \Exception(sprintf('Invalid parameters value(s): %s', implode(', ', $parameters))); } // Guess the context from input data @@ -261,9 +245,9 @@ abstract class BridgeAbstract implements BridgeInterface } if (is_null($this->queriedContext)) { - returnClientError('Required parameter(s) missing'); + throw new \Exception('Required parameter(s) missing'); } elseif ($this->queriedContext === false) { - returnClientError('Mixed context parameters'); + throw new \Exception('Mixed context parameters'); } $this->setInputs($inputs, $this->queriedContext); @@ -289,10 +273,7 @@ abstract class BridgeAbstract implements BridgeInterface } if (isset($optionValue['required']) && $optionValue['required'] === true) { - returnServerError( - 'Missing configuration option: ' - . $optionName - ); + throw new \Exception(sprintf('Missing configuration option: %s', $optionName)); } elseif (isset($optionValue['defaultValue'])) { $this->configuration[$optionName] = $optionValue['defaultValue']; } @@ -314,17 +295,11 @@ abstract class BridgeAbstract implements BridgeInterface } /** - * Returns the value for the selected configuration - * - * @param string $input The option name - * @return mixed|null The option value or null if the input is not defined + * Get bridge configuration value */ public function getOption($name) { - if (!isset($this->configuration[$name])) { - return null; - } - return $this->configuration[$name]; + return $this->configuration[$name] ?? null; } /** {@inheritdoc} */ @@ -392,9 +367,8 @@ abstract class BridgeAbstract implements BridgeInterface && $urlMatches[3] === $bridgeUriMatches[3] ) { return []; - } else { - return null; } + return null; } /** @@ -404,13 +378,13 @@ abstract class BridgeAbstract implements BridgeInterface * @param int $duration Cache duration (optional, default: 24 hours) * @return mixed Cached value or null if the key doesn't exist or has expired */ - protected function loadCacheValue($key, $duration = 86400) + protected function loadCacheValue($key, int $duration = 86400) { $cacheFactory = new CacheFactory(); $cache = $cacheFactory->create(); // Create class name without the namespace part - $scope = (new ReflectionClass($this))->getShortName(); + $scope = (new \ReflectionClass($this))->getShortName(); $cache->setScope($scope); $cache->setKey($key); if ($cache->getTime() < time() - $duration) { @@ -430,7 +404,7 @@ abstract class BridgeAbstract implements BridgeInterface $cacheFactory = new CacheFactory(); $cache = $cacheFactory->create(); - $scope = (new ReflectionClass($this))->getShortName(); + $scope = (new \ReflectionClass($this))->getShortName(); $cache->setScope($scope); $cache->setKey($key); $cache->saveData($value); |