stop using reflection in prooduction code
Problem: Reflection is a debugging tool and not really meant for use on production. There are easier ways to check if a class implements an interface with `is_a()`. Solution: Get rid of reflection use and switch to `is_a()` in TaskManager, refactor the Enum thing that does not make sense since it is not being used. The `is_a()` solution is really straightforward and has been supported in php for ages. The Enum thing was a copy paste hack from stackoverflow and ignored the simple solution mentioned there while not using the features of the advanced one.
This commit is contained in:
parent
4557395a86
commit
09aea8b747
|
@ -350,8 +350,7 @@ class TaskFactory {
|
|||
* @return bool true if the class $c implements AirtimeTask
|
||||
*/
|
||||
private static function _isTask($c) {
|
||||
$reflect = new ReflectionClass($c);
|
||||
return $reflect->implementsInterface('AirtimeTask');
|
||||
return is_a('AirtimeTask', $c, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,47 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Basic enumeration implementation; from http://stackoverflow.com/questions/254514/php-and-enumerations
|
||||
*
|
||||
* Class Enum
|
||||
*/
|
||||
abstract class Enum {
|
||||
|
||||
const __default = NULL;
|
||||
private static $constCacheArray = NULL;
|
||||
|
||||
private function __construct() {}
|
||||
|
||||
private static function getConstants() {
|
||||
if (self::$constCacheArray == NULL) {
|
||||
self::$constCacheArray = [];
|
||||
}
|
||||
$calledClass = get_called_class();
|
||||
if (!array_key_exists($calledClass, self::$constCacheArray)) {
|
||||
$reflect = new ReflectionClass($calledClass);
|
||||
self::$constCacheArray[$calledClass] = $reflect->getConstants();
|
||||
}
|
||||
return self::$constCacheArray[$calledClass];
|
||||
}
|
||||
|
||||
public static function isValidName($name, $strict = false) {
|
||||
$constants = self::getConstants();
|
||||
|
||||
if ($strict) {
|
||||
return array_key_exists($name, $constants);
|
||||
}
|
||||
|
||||
$keys = array_map('strtolower', array_keys($constants));
|
||||
return in_array(strtolower($name), $keys);
|
||||
}
|
||||
|
||||
public static function isValidValue($value) {
|
||||
$values = array_values(self::getConstants());
|
||||
return in_array($value, $values, $strict = true);
|
||||
}
|
||||
|
||||
public static function getDefault() {
|
||||
return static::__default;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
final class HttpRequestType extends Enum {
|
||||
final class HttpRequestType {
|
||||
|
||||
const GET = "GET";
|
||||
const POST = "POST";
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
<?php
|
||||
|
||||
require_once "Enum.php";
|
||||
final class MediaType extends Enum {
|
||||
final class MediaType {
|
||||
|
||||
const __default = self::FILE;
|
||||
|
||||
|
@ -11,4 +10,8 @@ final class MediaType extends Enum {
|
|||
const WEBSTREAM = 4;
|
||||
const PODCAST = 5;
|
||||
|
||||
public static function getDefault() {
|
||||
return static::__default;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue