refactor(BridgeFactory): make methods only accept valid class names (#2897)

This moves the responsibility for getting a valid class name
to the users of BridgeFactory, avoiding the repeated sanitation.
Improper use can also be checked statically.
This commit is contained in:
Jan Tojnar 2022-07-08 12:54:23 +02:00 committed by GitHub
parent 20bf2aa4fe
commit dbf8c5b7ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 102 additions and 71 deletions

View file

@ -3,7 +3,9 @@
final class BridgeFactory
{
private $folder;
private $bridgeNames = [];
/** @var array<class-string<BridgeInterface>> */
private $bridgeClassNames = [];
/** @var array<class-string<BridgeInterface>> */
private $whitelist = [];
public function __construct(string $folder = PATH_LIB_BRIDGES)
@ -12,8 +14,8 @@ final class BridgeFactory
// create names
foreach (scandir($this->folder) as $file) {
if (preg_match('/^([^.]+)Bridge\.php$/U', $file, $m)) {
$this->bridgeNames[] = $m[1];
if (preg_match('/^([^.]+Bridge)\.php$/U', $file, $m)) {
$this->bridgeClassNames[] = $m[1];
}
}
@ -26,34 +28,48 @@ final class BridgeFactory
$contents = '';
}
if ($contents === '*') { // Whitelist all bridges
$this->whitelist = $this->getBridgeNames();
$this->whitelist = $this->getBridgeClassNames();
} else {
foreach (explode("\n", $contents) as $bridgeName) {
$this->whitelist[] = $this->sanitizeBridgeName($bridgeName);
$bridgeClassName = $this->sanitizeBridgeName($bridgeName);
if ($bridgeClassName !== null) {
$this->whitelist[] = $bridgeClassName;
}
}
}
}
/**
* @param class-string<BridgeInterface> $name
*/
public function create(string $name): BridgeInterface
{
if (preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name)) {
$className = sprintf('%sBridge', $this->sanitizeBridgeName($name));
return new $className();
}
throw new \InvalidArgumentException('Bridge name invalid!');
return new $name();
}
public function getBridgeNames(): array
/**
* @return array<class-string<BridgeInterface>>
*/
public function getBridgeClassNames(): array
{
return $this->bridgeNames;
return $this->bridgeClassNames;
}
public function isWhitelisted($name): bool
/**
* @param class-string<BridgeInterface>|null $name
*/
public function isWhitelisted(string $name): bool
{
return in_array($this->sanitizeBridgeName($name), $this->whitelist);
return in_array($name, $this->whitelist);
}
private function sanitizeBridgeName($name)
/**
* Tries to turn a potentially human produced bridge name into a class name.
*
* @param mixed $name
* @return class-string<BridgeInterface>|null
*/
public function sanitizeBridgeName($name): ?string
{
if (!is_string($name)) {
return null;
@ -64,21 +80,21 @@ final class BridgeFactory
$name = $matches[1];
}
// Trim trailing 'Bridge' if exists
if (preg_match('/(.+)(?:Bridge)/i', $name, $matches)) {
$name = $matches[1];
// Append 'Bridge' suffix if not present.
if (!preg_match('/(Bridge)$/i', $name)) {
$name = sprintf('%sBridge', $name);
}
// Improve performance for correctly written bridge names
if (in_array($name, $this->getBridgeNames())) {
$index = array_search($name, $this->getBridgeNames());
return $this->getBridgeNames()[$index];
if (in_array($name, $this->getBridgeClassNames())) {
$index = array_search($name, $this->getBridgeClassNames());
return $this->getBridgeClassNames()[$index];
}
// The name is valid if a corresponding bridge file is found on disk
if (in_array(strtolower($name), array_map('strtolower', $this->getBridgeNames()))) {
$index = array_search(strtolower($name), array_map('strtolower', $this->getBridgeNames()));
return $this->getBridgeNames()[$index];
if (in_array(strtolower($name), array_map('strtolower', $this->getBridgeClassNames()))) {
$index = array_search(strtolower($name), array_map('strtolower', $this->getBridgeClassNames()));
return $this->getBridgeClassNames()[$index];
}
Debug::log('Invalid bridge name specified: "' . $name . '"!');