Permalink
Browse files

feature(events): Adds static methods for returning common values

These functions are particularly useful as handlers for events/hooks.
  • Loading branch information...
mrclay committed Jul 2, 2015
1 parent 2dde839 commit f080fed1dfbd1a982cc2dda3110cc74f58ad028d
@@ -0,0 +1,48 @@
+<?php
+namespace Elgg;
+
+/**
+ * Functions for use as plugin hook/event handlers or other situations where you need a
+ * globally accessible callable.
+ */
+class Values {
+
+ /**
+ * Return true
+ *
+ * @return true
+ * @since 1.12.0
+ */
+ public static function getTrue() {
+ return true;
+ }
+
+ /**
+ * Return false
+ *
+ * @return false
+ * @since 1.12.0
+ */
+ public static function getFalse() {
+ return false;
+ }
+
+ /**
+ * Return null
+ *
+ * @return null
+ * @since 1.12.0
+ */
+ public static function getNull() {
+ }
+
+ /**
+ * Return empty array
+ *
+ * @return array
+ * @since 1.12.0
+ */
+ public static function getArray() {
+ return [];
+ }
+}
@@ -23,7 +23,7 @@ public function testTriggerCallsRegisteredHandlersAndReturnsTrue() {
public function testFalseStopsPropagationAndReturnsFalse() {
$events = new \Elgg\EventsService();
- $events->registerHandler('foo', 'bar', array('\Elgg\EventsServiceTest', 'returnFalse'));
+ $events->registerHandler('foo', 'bar', 'Elgg\Values::getFalse');
$events->registerHandler('foo', 'bar', array($this, 'incrementCounter'));
$this->assertFalse($events->trigger('foo', 'bar'));
@@ -33,7 +33,7 @@ public function testFalseStopsPropagationAndReturnsFalse() {
public function testNullDoesNotStopPropagation() {
$events = new \Elgg\EventsService();
- $events->registerHandler('foo', 'bar', array('\Elgg\EventsServiceTest', 'returnNull'));
+ $events->registerHandler('foo', 'bar', 'Elgg\Values::getNull');
$events->registerHandler('foo', 'bar', array($this, 'incrementCounter'));
$this->assertTrue($events->trigger('foo', 'bar'));
@@ -43,7 +43,7 @@ public function testNullDoesNotStopPropagation() {
public function testUnstoppableEventsCantBeStoppedAndReturnTrue() {
$events = new \Elgg\EventsService();
- $events->registerHandler('foo', 'bar', array('\Elgg\EventsServiceTest', 'returnFalse'));
+ $events->registerHandler('foo', 'bar', 'Elgg\Values::getFalse');
$events->registerHandler('foo', 'bar', array($this, 'incrementCounter'));
$this->assertTrue($events->trigger('foo', 'bar', null, array(
@@ -69,17 +69,5 @@ public function incrementCounter() {
$this->counter++;
return true;
}
-
- public static function returnTrue() {
- return true;
- }
-
- public static function returnFalse() {
- return false;
- }
-
- public static function returnNull() {
- return;
- }
}
@@ -27,7 +27,7 @@ public function testCanPassParamsAndChangeReturnValue() {
public function testNullReturnDoesntChangeValue() {
$hooks = new \Elgg\PluginHooksService();
- $hooks->registerHandler('foo', 'bar', array('\Elgg\PluginHooksServiceTest', 'returnNull'));
+ $hooks->registerHandler('foo', 'bar', 'Elgg\Values::getNull');
$returnval = $hooks->trigger('foo', 'bar', array(), 1);
@@ -46,10 +46,6 @@ public function testUncallableHandlersAreLogged() {
$hooks->trigger('foo', 'bar');
}
-
- public static function returnTwo() {
- return 2;
- }
public static function changeReturn($foo, $bar, $returnval, $params) {
$testCase = $params['testCase'];
@@ -59,10 +55,6 @@ public static function changeReturn($foo, $bar, $returnval, $params) {
return 2;
}
- public static function returnNull() {
- return;
- }
-
public static function throwInvalidArg() {
throw new \InvalidArgumentException();
}

0 comments on commit f080fed

Please sign in to comment.