diff --git a/CHANGELOG.md b/CHANGELOG.md index a81bbde..a8c966f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed - unit tests for PHPUnit 8 +### Fixed +- error when formatting empty hook. + ## 3.2 - 2019-01-30 ### Added diff --git a/src/Formatter/Hook_Formatter.php b/src/Formatter/Hook_Formatter.php index 89ed11a..6051e2e 100644 --- a/src/Formatter/Hook_Formatter.php +++ b/src/Formatter/Hook_Formatter.php @@ -16,6 +16,7 @@ class Hook_Formatter { */ public function __construct() { + // TODO inject paths instead of reaching for global constants. $this->truncate_paths = [ wp_normalize_path( WP_CONTENT_DIR ), wp_normalize_path( ABSPATH ), @@ -35,6 +36,11 @@ class Hook_Formatter { if ( $hook instanceof \WP_Hook ) { $hook = $hook->callbacks; } + + if ( empty( $hook ) ) { + return $callbacks; + } + ksort( $hook ); $functions = array_merge( ...$hook ); diff --git a/tests/Formatter/HookTest.php b/tests/Formatter/HookTest.php index 246781b..7843ff0 100644 --- a/tests/Formatter/HookTest.php +++ b/tests/Formatter/HookTest.php @@ -55,6 +55,8 @@ class HookTest extends LapsTestCase { $this->assertEquals( 'class::method', $result[1] ); $this->assertStringStartsWith( 'closure from', $result[2] ); $this->assertStringStartsWith( 'anonymous class from', $result[3] ); + + $this->assertEquals( [], $formatter->format( [] ) ); } }