Make WordPress Core

Opened 4 years ago

Last modified 4 years ago

#50110 assigned defect (bug)

Incorrect property docblocks in WP_Block_Type

Reported by: nielsdeblaauw's profile nielsdeblaauw Owned by: peterwilsoncc's profile peterwilsoncc
Milestone: Awaiting Review Priority: normal
Severity: minor Version: 5.0
Component: Editor Keywords:
Focuses: docs, coding-standards Cc:

Description

In the class WP_Block_Type the docblocks declare @var #TYPE# for several properties of the class.

Below I will use attributes as an example, but this counts for all the following properties:

  • render_callback
  • attributes
  • editor_script
  • script
  • editor_style
  • style

The docblock defines that the property must be a certain value ('array' in the case of attributes), but it is not set in the constructor and is optionally set in the set_props() method, if args has been supplied with the correct property.

/**
 * Block type attributes property schemas.
 *
 * @since 5.0.0
 * @var array
 */
public $attributes;

This allows the property to be null, which conflicts with de docblock.

Proposed solution(s)

1. Declare default value:

This might change behavior, where null is expected (see get_attributes() for example)

/**
 * Block type attributes property schemas.
 *
 * @since 5.0.0
 * @var array
 */
public $attributes = array();

2. Add possible null to docblock

This would lead might lead to other conflicts, such as using the the null value as an array without a type check.

/**
 * Block type attributes property schemas.
 *
 * @since 5.0.0
 * @var array|null
 */
public $attributes;

3. Do not declare @var

The set_props() method currently does not do any type checking, so in theory all the properties listed above are mixed.

(intentionally borked example)

new \WP_Block_Type( 'name', array( 
    'attributes' => 'This is not an array.'
) );

This places responsibility of type checking on implementing code, which would be most 'correct' with the current set_props() method.

Change History (2)

This ticket was mentioned in Slack in #core by peterwilsoncc. View the logs.


4 years ago

#2 @peterwilsoncc
4 years ago

  • Owner set to peterwilsoncc
  • Status changed from new to assigned

Hi @nielsdeblaauw,

This was discussed in a Core triage session today.

It looks like the docblocks have been updated in most instances except for render_callback(). I've self assigned this to take a look and update.

Thanks for the report.

Note: See TracTickets for help on using tickets.