diff --git a/.github/changelog/fix-stream-typed-connector-properties b/.github/changelog/fix-stream-typed-connector-properties new file mode 100644 index 0000000000..d84bd32e28 --- /dev/null +++ b/.github/changelog/fix-stream-typed-connector-properties @@ -0,0 +1,4 @@ +Significance: patch +Type: fixed + +Fixed a fatal error when Stream 4.5.0 or newer is active. diff --git a/integration/stream/class-connector.php b/integration/stream/class-connector.php index 5aaafda1ea..6c6ef1946e 100644 --- a/integration/stream/class-connector.php +++ b/integration/stream/class-connector.php @@ -22,23 +22,22 @@ */ class Connector extends \WP_Stream\Connector { /** - * Connector slug. + * Set the connector slug and the actions registered for this connector. * - * @var string + * These are assigned here rather than redeclared as properties. Stream 4.5.0 + * added native types to the parent class properties, and PHP requires a + * redeclared property to match the parent's type exactly. Assigning them + * keeps the connector compatible with Stream versions before and after 4.5.0. */ - public $name = 'activitypub'; - - /** - * Actions registered for this connector. - * - * @var array - */ - public $actions = array( - 'activitypub_handled_follow', - 'activitypub_sent_to_inbox', - 'activitypub_outbox_processing_complete', - 'activitypub_outbox_processing_batch_complete', - ); + public function __construct() { + $this->name = 'activitypub'; + $this->actions = array( + 'activitypub_handled_follow', + 'activitypub_sent_to_inbox', + 'activitypub_outbox_processing_complete', + 'activitypub_outbox_processing_batch_complete', + ); + } /** * Return translated connector label. diff --git a/tests/phpunit/tests/integration/class-test-stream-connector.php b/tests/phpunit/tests/integration/class-test-stream-connector.php index 9b12848987..144b69b030 100644 --- a/tests/phpunit/tests/integration/class-test-stream-connector.php +++ b/tests/phpunit/tests/integration/class-test-stream-connector.php @@ -696,6 +696,33 @@ public function test_prepare_outbox_data_for_response_blog_user_url() { public function test_class_hierarchy() { $this->assertInstanceOf( '\WP_Stream\Connector', $this->stream_connector ); } + + /** + * Test that the connector does not redeclare properties of the parent class. + * + * Stream 4.5.0 added native types to the properties of WP_Stream\Connector. + * A child class that redeclares one of them must use the same type, which + * would break older Stream versions where the properties are untyped. The + * connector must assign these values instead of redeclaring them. + * + * @see https://github.com/Automattic/wordpress-activitypub/issues/4066 + * + * @covers ::__construct + */ + public function test_connector_does_not_redeclare_parent_properties() { + $parent_class = \get_parent_class( Connector::class ); + $parent = new \ReflectionClass( $parent_class ); + + foreach ( $parent->getProperties( \ReflectionProperty::IS_PUBLIC | \ReflectionProperty::IS_PROTECTED ) as $parent_property ) { + $property = new \ReflectionProperty( Connector::class, $parent_property->getName() ); + + $this->assertSame( + $parent->getName(), + $property->getDeclaringClass()->getName(), + \sprintf( 'Connector must not redeclare the parent property $%s.', $parent_property->getName() ) + ); + } + } } // Mock WP_Stream\Connector if it doesn't exist.