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
This commit is contained in:
Dag 2022-08-06 22:46:28 +02:00 committed by GitHub
parent b042412416
commit 2bbce8ebef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
45 changed files with 679 additions and 827 deletions

View file

@ -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);