@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Remove "HeraldRepetitionPolicyConfig" and hide storage details inside HeraldRule

Summary:
Depends on D18925. Ref T13048. Currently, HeraldRule stores policies as integers (0 or 1) in the database.

The application tries to mostly use strings ("first", "every"), but doesn't do a good job of hiding the fact that the values are integers deeper in the stack. So we end up with a lot of code like this:

```lang=php
$stored_int_value = $rule->getRepetitionPolicy();
$equivalent_string = HeraldRepetitionPolicyConfig::getString($stored_int_value);
$is_first = ($equivalent_string === HeraldRepetitionPolicyConfig::FIRST);
```

This happens in several places and is generally awful. Replace it with:

```lang=php
$is_first = $rule->isRepeatFirst();
```

To do this, merge `HeraldRepetitionPolicyConfig` into `HeraldRule` and hide all the mess inside the methods.

(This may let us just get rid of the integers in a later change, although I'm not sure I want to commit to that.)

Test Plan:
- Grepped for `HeraldRepetitionPolicyConfig`, no more hits.
- Grepped for `setRepetitionPolicy(...)` and `getRepetitionPolicy(...)`. There are no remaining callers outside of `HeraldRule`.
- Browed and edited several rules. I'll vet this more convincingly after adding the new repetition rule.

Reviewers: amckinley

Reviewed By: amckinley

Maniphest Tasks: T13048

Differential Revision: https://secure.phabricator.com/D18926

+84 -50
-2
src/__phutil_library_map__.php
··· 1399 1399 'HeraldRelatedFieldGroup' => 'applications/herald/field/HeraldRelatedFieldGroup.php', 1400 1400 'HeraldRemarkupFieldValue' => 'applications/herald/value/HeraldRemarkupFieldValue.php', 1401 1401 'HeraldRemarkupRule' => 'applications/herald/remarkup/HeraldRemarkupRule.php', 1402 - 'HeraldRepetitionPolicyConfig' => 'applications/herald/config/HeraldRepetitionPolicyConfig.php', 1403 1402 'HeraldRule' => 'applications/herald/storage/HeraldRule.php', 1404 1403 'HeraldRuleController' => 'applications/herald/controller/HeraldRuleController.php', 1405 1404 'HeraldRuleDatasource' => 'applications/herald/typeahead/HeraldRuleDatasource.php', ··· 6602 6601 'HeraldRelatedFieldGroup' => 'HeraldFieldGroup', 6603 6602 'HeraldRemarkupFieldValue' => 'HeraldFieldValue', 6604 6603 'HeraldRemarkupRule' => 'PhabricatorObjectRemarkupRule', 6605 - 'HeraldRepetitionPolicyConfig' => 'Phobject', 6606 6604 'HeraldRule' => array( 6607 6605 'HeraldDAO', 6608 6606 'PhabricatorApplicationTransactionInterface',
+3 -6
src/applications/herald/adapter/HeraldAdapter.php
··· 766 766 public function getRepetitionOptions() { 767 767 $options = array(); 768 768 769 - $options[] = HeraldRepetitionPolicyConfig::EVERY; 769 + $options[] = HeraldRule::REPEAT_EVERY; 770 770 771 771 // Some rules, like pre-commit rules, only ever fire once. It doesn't 772 772 // make sense to use state-based repetition policies like "only the first 773 773 // time" for these rules. 774 774 775 775 if (!$this->isSingleEventAdapter()) { 776 - $options[] = HeraldRepetitionPolicyConfig::FIRST; 776 + $options[] = HeraldRule::REPEAT_FIRST; 777 777 } 778 778 779 779 return $options; ··· 897 897 )); 898 898 } 899 899 900 - $integer_code_for_every = HeraldRepetitionPolicyConfig::toInt( 901 - HeraldRepetitionPolicyConfig::EVERY); 902 - 903 - if ($rule->getRepetitionPolicy() == $integer_code_for_every) { 900 + if ($rule->isRepeatEvery()) { 904 901 $action_text = 905 902 pht('Take these actions every time this rule matches:'); 906 903 } else {
-28
src/applications/herald/config/HeraldRepetitionPolicyConfig.php
··· 1 - <?php 2 - 3 - final class HeraldRepetitionPolicyConfig extends Phobject { 4 - 5 - const FIRST = 'first'; // only execute the first time (no repeating) 6 - const EVERY = 'every'; // repeat every time 7 - 8 - private static $policyIntMap = array( 9 - self::FIRST => 0, 10 - self::EVERY => 1, 11 - ); 12 - 13 - public static function getMap() { 14 - return array( 15 - self::EVERY => pht('every time'), 16 - self::FIRST => pht('only the first time'), 17 - ); 18 - } 19 - 20 - public static function toInt($str) { 21 - return idx(self::$policyIntMap, $str, self::$policyIntMap[self::EVERY]); 22 - } 23 - 24 - public static function toString($int) { 25 - return idx(array_flip(self::$policyIntMap), $int, self::EVERY); 26 - } 27 - 28 - }
+3 -5
src/applications/herald/controller/HeraldRuleController.php
··· 373 373 // mutate current rule, so it would be sent to the client in the right state 374 374 $rule->setMustMatchAll((int)$match_all); 375 375 $rule->setName($new_name); 376 - $rule->setRepetitionPolicy( 377 - HeraldRepetitionPolicyConfig::toInt($repetition_policy_param)); 376 + $rule->setRepetitionPolicyStringConstant($repetition_policy_param); 378 377 $rule->attachConditions($conditions); 379 378 $rule->attachActions($actions); 380 379 ··· 594 593 * time) this rule matches..." element. 595 594 */ 596 595 private function renderRepetitionSelector($rule, HeraldAdapter $adapter) { 597 - $repetition_policy = HeraldRepetitionPolicyConfig::toString( 598 - $rule->getRepetitionPolicy()); 596 + $repetition_policy = $rule->getRepetitionPolicyStringConstant(); 599 597 600 598 $repetition_options = $adapter->getRepetitionOptions(); 601 - $repetition_names = HeraldRepetitionPolicyConfig::getMap(); 599 + $repetition_names = HeraldRule::getRepetitionPolicySelectOptionMap(); 602 600 $repetition_map = array_select_keys($repetition_names, $repetition_options); 603 601 604 602 if (count($repetition_map) < 2) {
+4 -2
src/applications/herald/editor/HeraldRuleEditor.php
··· 66 66 $object->setMustMatchAll((int)$new_state['match_all']); 67 67 $object->attachConditions($new_state['conditions']); 68 68 $object->attachActions($new_state['actions']); 69 - $object->setRepetitionPolicy( 70 - HeraldRepetitionPolicyConfig::toInt($new_state['repetition_policy'])); 69 + 70 + $new_repetition = $new_state['repetition_policy']; 71 + $object->setRepetitionPolicyStringConstant($new_repetition); 72 + 71 73 return $object; 72 74 } 73 75
+1 -1
src/applications/herald/editor/HeraldRuleSerializer.php
··· 9 9 (bool)$rule->getMustMatchAll(), 10 10 $rule->getConditions(), 11 11 $rule->getActions(), 12 - HeraldRepetitionPolicyConfig::toString($rule->getRepetitionPolicy())); 12 + $rule->getRepetitionPolicyStringConstant()); 13 13 } 14 14 15 15 public function serializeRuleComponents(
+2 -6
src/applications/herald/engine/HeraldEngine.php
··· 68 68 foreach ($rules as $phid => $rule) { 69 69 $this->stack = array(); 70 70 71 - $policy_first = HeraldRepetitionPolicyConfig::FIRST; 72 - $policy_first_int = HeraldRepetitionPolicyConfig::toInt($policy_first); 73 - $is_first_only = ($rule->getRepetitionPolicy() == $policy_first_int); 71 + $is_first_only = $rule->isRepeatFirst(); 74 72 75 73 try { 76 74 if (!$this->getDryRun() && ··· 175 173 176 174 $rules = mpull($rules, null, 'getID'); 177 175 $applied_ids = array(); 178 - $first_policy = HeraldRepetitionPolicyConfig::toInt( 179 - HeraldRepetitionPolicyConfig::FIRST); 180 176 181 177 // Mark all the rules that have had their effects applied as having been 182 178 // executed for the current object. ··· 194 190 continue; 195 191 } 196 192 197 - if ($rule->getRepetitionPolicy() == $first_policy) { 193 + if ($rule->isRepeatFirst()) { 198 194 $applied_ids[] = $rule_id; 199 195 } 200 196 }
+71
src/applications/herald/storage/HeraldRule.php
··· 30 30 private $actions; 31 31 private $triggerObject = self::ATTACHABLE; 32 32 33 + const REPEAT_EVERY = 'every'; 34 + const REPEAT_FIRST = 'first'; 35 + 33 36 protected function getConfiguration() { 34 37 return array( 35 38 self::CONFIG_AUX_PHID => true, ··· 251 254 252 255 public function getMonogram() { 253 256 return 'H'.$this->getID(); 257 + } 258 + 259 + 260 + /* -( Repetition Policies )------------------------------------------------ */ 261 + 262 + 263 + public function getRepetitionPolicyStringConstant() { 264 + $map = self::getRepetitionPolicyMap(); 265 + $map = ipull($map, 'key.string', 'key.int'); 266 + 267 + return idx($map, $this->getRepetitionPolicyIntegerConstant()); 268 + } 269 + 270 + public function getRepetitionPolicyIntegerConstant() { 271 + $map = self::getRepetitionPolicyMap(); 272 + $map = ipull($map, 'key.int', 'key.int'); 273 + $int = $this->getRepetitionPolicy(); 274 + 275 + if (!isset($map[$int])) { 276 + return head_key($map); 277 + } 278 + 279 + return $int; 280 + } 281 + 282 + public function setRepetitionPolicyStringConstant($value) { 283 + $map = self::getRepetitionPolicyMap(); 284 + $map = ipull($map, 'key.int', 'key.string'); 285 + 286 + if (!isset($map[$value])) { 287 + throw new Exception( 288 + pht( 289 + 'Rule repetition string constant "%s" is unknown.', 290 + $value)); 291 + } 292 + 293 + $int = $map[$value]; 294 + 295 + return $this->setRepetitionPolicy($int); 296 + } 297 + 298 + public function isRepeatEvery() { 299 + return ($this->getRepetitionPolicyStringConstant() === self::REPEAT_EVERY); 300 + } 301 + 302 + public function isRepeatFirst() { 303 + return ($this->getRepetitionPolicyStringConstant() === self::REPEAT_FIRST); 304 + } 305 + 306 + public static function getRepetitionPolicySelectOptionMap() { 307 + $map = self::getRepetitionPolicyMap(); 308 + $map = ipull($map, 'select', 'key.string'); 309 + return $map; 310 + } 311 + 312 + private static function getRepetitionPolicyMap() { 313 + return array( 314 + self::REPEAT_EVERY => array( 315 + 'key.int' => 1, 316 + 'key.string' => self::REPEAT_EVERY, 317 + 'select' => pht('every time'), 318 + ), 319 + self::REPEAT_FIRST => array( 320 + 'key.int' => 0, 321 + 'key.string' => self::REPEAT_FIRST, 322 + 'select' => pht('only the first time'), 323 + ), 324 + ); 254 325 } 255 326 256 327