From e431ab2189239487ebf37ba0cee6e5392194ec7c Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 1 Oct 2015 16:56:21 -0700 Subject: [PATCH] Use getPhobjectClassConstant() to access class constants Summary: Ref T9494. Depends on D14216. Remove 10 copies of this code. Test Plan: Ran `arc unit --everything`, browsed Config > Modules, clicked around Herald / etc. Reviewers: chad Reviewed By: chad Maniphest Tasks: T9494 Differential Revision: https://secure.phabricator.com/D14217 --- .../drydock/logtype/DrydockLogType.php | 30 +------------------ .../artifact/HarbormasterArtifact.php | 30 +------------------ .../stepgroup/HarbormasterBuildStepGroup.php | 14 +-------- .../herald/action/HeraldAction.php | 30 +------------------ .../herald/action/HeraldActionGroup.php | 14 +-------- src/applications/herald/field/HeraldField.php | 28 ++--------------- .../herald/field/HeraldFieldGroup.php | 14 +-------- .../phid/type/PhabricatorPHIDType.php | 12 +------- .../PhabricatorPolicyCapability.php | 24 +-------------- .../edges/type/PhabricatorEdgeType.php | 12 +------- 10 files changed, 12 insertions(+), 196 deletions(-) diff --git a/src/applications/drydock/logtype/DrydockLogType.php b/src/applications/drydock/logtype/DrydockLogType.php index aa1f4fc4e0..7faab42dfe 100644 --- a/src/applications/drydock/logtype/DrydockLogType.php +++ b/src/applications/drydock/logtype/DrydockLogType.php @@ -28,35 +28,7 @@ abstract class DrydockLogType extends Phobject { } final public function getLogTypeConstant() { - $class = new ReflectionClass($this); - - $const = $class->getConstant('LOGCONST'); - if ($const === false) { - throw new Exception( - pht( - '"%s" class "%s" must define a "%s" property.', - __CLASS__, - get_class($this), - 'LOGCONST')); - } - - $limit = self::getLogTypeConstantByteLimit(); - if (!is_string($const) || (strlen($const) > $limit)) { - throw new Exception( - pht( - '"%s" class "%s" has an invalid "%s" property. Field constants '. - 'must be strings and no more than %s bytes in length.', - __CLASS__, - get_class($this), - 'LOGCONST', - new PhutilNumber($limit))); - } - - return $const; - } - - final private static function getLogTypeConstantByteLimit() { - return 64; + return $this->getPhobjectClassConstant('LOGCONST', 64); } final public static function getAllLogTypes() { diff --git a/src/applications/harbormaster/artifact/HarbormasterArtifact.php b/src/applications/harbormaster/artifact/HarbormasterArtifact.php index a0d3a86101..8d3d8dd169 100644 --- a/src/applications/harbormaster/artifact/HarbormasterArtifact.php +++ b/src/applications/harbormaster/artifact/HarbormasterArtifact.php @@ -43,35 +43,7 @@ abstract class HarbormasterArtifact extends Phobject { } final public function getArtifactConstant() { - $class = new ReflectionClass($this); - - $const = $class->getConstant('ARTIFACTCONST'); - if ($const === false) { - throw new Exception( - pht( - '"%s" class "%s" must define a "%s" property.', - __CLASS__, - get_class($this), - 'ARTIFACTCONST')); - } - - $limit = self::getArtifactConstantByteLimit(); - if (!is_string($const) || (strlen($const) > $limit)) { - throw new Exception( - pht( - '"%s" class "%s" has an invalid "%s" property. Action constants '. - 'must be strings and no more than %s bytes in length.', - __CLASS__, - get_class($this), - 'ARTIFACTCONST', - new PhutilNumber($limit))); - } - - return $const; - } - - final public static function getArtifactConstantByteLimit() { - return 32; + return $this->getPhobjectClassConstant('ARTIFACTCONST', 32); } final public static function getAllArtifactTypes() { diff --git a/src/applications/harbormaster/stepgroup/HarbormasterBuildStepGroup.php b/src/applications/harbormaster/stepgroup/HarbormasterBuildStepGroup.php index bb439b75b4..5806861836 100644 --- a/src/applications/harbormaster/stepgroup/HarbormasterBuildStepGroup.php +++ b/src/applications/harbormaster/stepgroup/HarbormasterBuildStepGroup.php @@ -14,19 +14,7 @@ abstract class HarbormasterBuildStepGroup extends Phobject { } final public function getGroupKey() { - $class = new ReflectionClass($this); - - $const = $class->getConstant('GROUPKEY'); - if ($const === false) { - throw new Exception( - pht( - '"%s" class "%s" must define a "%s" property.', - __CLASS__, - get_class($this), - 'GROUPKEY')); - } - - return $const; + return $this->getPhobjectClassConstant('GROUPKEY'); } final public static function getAllGroups() { diff --git a/src/applications/herald/action/HeraldAction.php b/src/applications/herald/action/HeraldAction.php index 02a9dfa60a..f4217cd4db 100644 --- a/src/applications/herald/action/HeraldAction.php +++ b/src/applications/herald/action/HeraldAction.php @@ -122,35 +122,7 @@ abstract class HeraldAction extends Phobject { } final public function getActionConstant() { - $class = new ReflectionClass($this); - - $const = $class->getConstant('ACTIONCONST'); - if ($const === false) { - throw new Exception( - pht( - '"%s" class "%s" must define a "%s" property.', - __CLASS__, - get_class($this), - 'ACTIONCONST')); - } - - $limit = self::getActionConstantByteLimit(); - if (!is_string($const) || (strlen($const) > $limit)) { - throw new Exception( - pht( - '"%s" class "%s" has an invalid "%s" property. Action constants '. - 'must be strings and no more than %s bytes in length.', - __CLASS__, - get_class($this), - 'ACTIONCONST', - new PhutilNumber($limit))); - } - - return $const; - } - - final public static function getActionConstantByteLimit() { - return 64; + return $this->getPhobjectClassConstant('ACTIONCONST', 64); } final public static function getAllActions() { diff --git a/src/applications/herald/action/HeraldActionGroup.php b/src/applications/herald/action/HeraldActionGroup.php index a087909609..ad4fecd6b6 100644 --- a/src/applications/herald/action/HeraldActionGroup.php +++ b/src/applications/herald/action/HeraldActionGroup.php @@ -3,19 +3,7 @@ abstract class HeraldActionGroup extends HeraldGroup { final public function getGroupKey() { - $class = new ReflectionClass($this); - - $const = $class->getConstant('ACTIONGROUPKEY'); - if ($const === false) { - throw new Exception( - pht( - '"%s" class "%s" must define a "%s" property.', - __CLASS__, - get_class($this), - 'ACTIONGROUPKEY')); - } - - return $const; + return $this->getPhobjectClassConstant('ACTIONGROUPKEY'); } final public static function getAllActionGroups() { diff --git a/src/applications/herald/field/HeraldField.php b/src/applications/herald/field/HeraldField.php index a6d2e580c2..2aba443077 100644 --- a/src/applications/herald/field/HeraldField.php +++ b/src/applications/herald/field/HeraldField.php @@ -169,31 +169,9 @@ abstract class HeraldField extends Phobject { } final public function getFieldConstant() { - $class = new ReflectionClass($this); - - $const = $class->getConstant('FIELDCONST'); - if ($const === false) { - throw new Exception( - pht( - '"%s" class "%s" must define a "%s" property.', - __CLASS__, - get_class($this), - 'FIELDCONST')); - } - - $limit = self::getFieldConstantByteLimit(); - if (!is_string($const) || (strlen($const) > $limit)) { - throw new Exception( - pht( - '"%s" class "%s" has an invalid "%s" property. Field constants '. - 'must be strings and no more than %s bytes in length.', - __CLASS__, - get_class($this), - 'FIELDCONST', - new PhutilNumber($limit))); - } - - return $const; + return $this->getPhobjectClassConstant( + 'FIELDCONST', + self::getFieldConstantByteLimit()); } final public static function getFieldConstantByteLimit() { diff --git a/src/applications/herald/field/HeraldFieldGroup.php b/src/applications/herald/field/HeraldFieldGroup.php index adb7fbe372..9e13f17cb7 100644 --- a/src/applications/herald/field/HeraldFieldGroup.php +++ b/src/applications/herald/field/HeraldFieldGroup.php @@ -3,19 +3,7 @@ abstract class HeraldFieldGroup extends HeraldGroup { final public function getGroupKey() { - $class = new ReflectionClass($this); - - $const = $class->getConstant('FIELDGROUPKEY'); - if ($const === false) { - throw new Exception( - pht( - '"%s" class "%s" must define a "%s" property.', - __CLASS__, - get_class($this), - 'FIELDGROUPKEY')); - } - - return $const; + return $this->getPhobjectClassConstant('FIELDGROUPKEY'); } final public static function getAllFieldGroups() { diff --git a/src/applications/phid/type/PhabricatorPHIDType.php b/src/applications/phid/type/PhabricatorPHIDType.php index c69075bdad..a8502b12f7 100644 --- a/src/applications/phid/type/PhabricatorPHIDType.php +++ b/src/applications/phid/type/PhabricatorPHIDType.php @@ -3,17 +3,7 @@ abstract class PhabricatorPHIDType extends Phobject { final public function getTypeConstant() { - $class = new ReflectionClass($this); - - $const = $class->getConstant('TYPECONST'); - if ($const === false) { - throw new Exception( - pht( - '%s class "%s" must define a %s property.', - __CLASS__, - get_class($this), - 'TYPECONST')); - } + $const = $this->getPhobjectClassConstant('TYPECONST'); if (!is_string($const) || !preg_match('/^[A-Z]{4}$/', $const)) { throw new Exception( diff --git a/src/applications/policy/capability/PhabricatorPolicyCapability.php b/src/applications/policy/capability/PhabricatorPolicyCapability.php index b7ff6a060b..36b8ea87c0 100644 --- a/src/applications/policy/capability/PhabricatorPolicyCapability.php +++ b/src/applications/policy/capability/PhabricatorPolicyCapability.php @@ -15,29 +15,7 @@ abstract class PhabricatorPolicyCapability extends Phobject { * @return string Globally unique capability key. */ final public function getCapabilityKey() { - $class = new ReflectionClass($this); - - $const = $class->getConstant('CAPABILITY'); - if ($const === false) { - throw new Exception( - pht( - '%s class "%s" must define a %s property.', - __CLASS__, - get_class($this), - 'CAPABILITY')); - } - - if (!is_string($const)) { - throw new Exception( - pht( - '%s class "%s" has an invalid %s property. '. - 'Capability constants must be a string.', - __CLASS__, - get_class($this), - 'CAPABILITY')); - } - - return $const; + return $this->getPhobjectClassConstant('CAPABILITY'); } diff --git a/src/infrastructure/edges/type/PhabricatorEdgeType.php b/src/infrastructure/edges/type/PhabricatorEdgeType.php index c86af5c027..1729256050 100644 --- a/src/infrastructure/edges/type/PhabricatorEdgeType.php +++ b/src/infrastructure/edges/type/PhabricatorEdgeType.php @@ -12,17 +12,7 @@ abstract class PhabricatorEdgeType extends Phobject { final public function getEdgeConstant() { - $class = new ReflectionClass($this); - - $const = $class->getConstant('EDGECONST'); - if ($const === false) { - throw new Exception( - pht( - '%s class "%s" must define an %s property.', - __CLASS__, - get_class($this), - 'EDGECONST')); - } + $const = $this->getPhobjectClassConstant('EDGECONST'); if (!is_int($const) || ($const <= 0)) { throw new Exception(