Skip to content

Commit

Permalink
Fixed bug where multiple classes conflicted in the static cache
Browse files Browse the repository at this point in the history
Signed-off-by: RJ Garcia <[email protected]>
  • Loading branch information
ragboyjr committed Sep 15, 2019
1 parent 175fed4 commit 8b354f9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/ADT.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,28 @@ public function matchWithDefault(array $matches, $default = null) {

public static function __callStatic(string $name, array $arguments) {
self::initStatitConstructorMethodCache();
if (!isset(self::$staticConstructorMethodCache[$name])) {
throw new \BadMethodCallException("Method constructor {$name} does not exist for this ADT. Valid static constructors are: " . implode(', ', array_keys(self::$staticConstructorMethodCache)));
$class = get_called_class();
if (!isset(self::$staticConstructorMethodCache[$class][$name])) {
throw new \BadMethodCallException("Method constructor {$name} does not exist for this ADT. Valid static constructors are: " . implode(', ', array_keys(self::$staticConstructorMethodCache[$class])));
}

$className = self::$staticConstructorMethodCache[$name];
$className = self::$staticConstructorMethodCache[$class][$name];
return new $className(...$arguments);
}

private static function initStatitConstructorMethodCache(): void {
if (self::$staticConstructorMethodCache) {
$class = get_called_class();

if (isset(self::$staticConstructorMethodCache[$class])) {
return;
}

foreach (static::types() as $type) {
$finalNsSep = strrpos($type, '\\');
if ($finalNsSep === false) {
self::$staticConstructorMethodCache[lcfirst($type)] = $type;
self::$staticConstructorMethodCache[$class][lcfirst($type)] = $type;
} else {
self::$staticConstructorMethodCache[lcfirst(substr($type, $finalNsSep + 1))] = $type;
self::$staticConstructorMethodCache[$class][lcfirst(substr($type, $finalNsSep + 1))] = $type;
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/adt.spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public function __construct(string $productCode) {
}
}

abstract class Direction extends ADT {
public static function types(): array {
return [North::class, South::class, East::class, West::class];
}
}

final class North extends Direction {}
final class South extends Direction {}
final class East extends Direction {}
final class West extends Direction {}

final class NotRegistered extends Barcode {}

describe('ADT', function() {
Expand Down Expand Up @@ -82,4 +93,10 @@ final class NotRegistered extends Barcode {}
Barcode::bad();
})->throw(\BadMethodCallException::class, 'Method constructor bad does not exist for this ADT. Valid static constructors are: upc, qrCode');
});
it('can support multiple types of ADT static constructors', function() {
$dir = Direction::north();
$barCode = BarCode::qrCode('foo');
expect(get_class($dir))->equal(North::class);
expect(get_class($barCode))->equal(QrCode::class);
});
});

0 comments on commit 8b354f9

Please sign in to comment.