From 09aea8b747a76ebfecd37274d9eb538131d3abae Mon Sep 17 00:00:00 2001 From: Lucas Bickel Date: Wed, 22 Feb 2017 10:12:38 +0100 Subject: [PATCH] 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. --- .../application/common/TaskManager.php | 3 +- airtime_mvc/application/common/enum/Enum.php | 47 ------------------- .../common/enum/HttpRequestType.php | 2 +- .../application/common/enum/MediaType.php | 7 ++- 4 files changed, 7 insertions(+), 52 deletions(-) delete mode 100644 airtime_mvc/application/common/enum/Enum.php diff --git a/airtime_mvc/application/common/TaskManager.php b/airtime_mvc/application/common/TaskManager.php index 61b84f1a0..382f5ad19 100644 --- a/airtime_mvc/application/common/TaskManager.php +++ b/airtime_mvc/application/common/TaskManager.php @@ -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); } /** diff --git a/airtime_mvc/application/common/enum/Enum.php b/airtime_mvc/application/common/enum/Enum.php deleted file mode 100644 index b0d34fbc3..000000000 --- a/airtime_mvc/application/common/enum/Enum.php +++ /dev/null @@ -1,47 +0,0 @@ -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; - } - -} \ No newline at end of file diff --git a/airtime_mvc/application/common/enum/HttpRequestType.php b/airtime_mvc/application/common/enum/HttpRequestType.php index 59b1a25b3..f68652d86 100644 --- a/airtime_mvc/application/common/enum/HttpRequestType.php +++ b/airtime_mvc/application/common/enum/HttpRequestType.php @@ -1,6 +1,6 @@