Skip to content

Backwards compatibility break in PHP/Messages constructors #100

Open
@ciaranmcnulty

Description

@ciaranmcnulty

Relevant PHP concepts

PHP supports 'optional arguments' (arguments given an explicit default). This means a signature can change from:

foo(string $bar)

to

foo(string $bar, int $baz=0)

and the calling code foo('hello') still works as expected

Modern PHP additionally supports 'named arguments' so the same call can be written as foo(name: 'hello') but because this is a relatively recent addition, positional arguments are often used by developers.

The problem in Messages 19.0.0

For Messages, all arguments are optional by design and in the documentation we encourage using named arguments

The new KeywordType field has changed the constructor of Step from:

    public function __construct( 
        Location $location = new Location(),
        string $keyword = '',
        string $text = '',
        ?DocString $docString = null,
        ?DataTable $dataTable = null,
        string $id = '',
    ) {

to

    public function __construct( 
        Location $location = new Location(),
        string $keyword = '',
        ?KeywordType $keywordType = null,
        string $text = '',
        ?DocString $docString = null,
        ?DataTable $dataTable = null,
        string $id = '',
    ) {

This means that while named arguments still work:

$step = new Step(
    keyword: 'Given',
    text: 'I have an apple',
)

The equivalent with positional arguments is now a syntax error:

$step = new Step(
    new Location(),
    'Given',
    'I have an apple', # error because this is not a KeywordType
)

Solutions

We could

  1. Explicitly state that positional arguments are not supported. This is easy for us but relies on the developer reading the documentation and will lead to some friction
  2. Only ever add new fields at the end of the schema; this could be enforced by adding some tests that check the objects are all constructable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions