Skip to content

PHP 8.3: sniff to detect comment between yield and from #1720

@jrfnl

Description

@jrfnl

Is your feature request related to a problem?

Discovered while investigating upstream issue PHPCSStandards/PHP_CodeSniffer#529.

It appears there is a change in PHP 8.3 which makes the PHP tokenizer tolerant of comments between the yield and from keywords in a yield from expression.

Prior to PHP 8.3, this was a parse error in PHP. See: https://3v4l.org/2SI2Q#veol

I have not been able to find documentation/changelogs regarding this change. (though haven't searched extensively yet)

Describe the solution you'd like

A new sniff which will flag yield from with a comment between the keywords.

The sniff would need to take into account that the yield and from keywords might be on different lines which would mean that two subsequent T_YIELD_FROM tokens will need to be examined as one.

This new sniff would currently need to search for:

  • T_YIELD_FROM and check the contents of the token(s) to not contain any comments. I.e. #yield\s+from#i
  • T_YIELD tokens followed by one or more comment tokens, followed by a T_STRING token with a case-insensitive match for from.

This may need to be updated/changed, depending on what action is taken on issue PHPCSStandards/PHP_CodeSniffer#529

<?php
function generator()
{
    // These are OK.
    yield from gen2();

    yield
    from
    gen2();

    // These should be flagged.
    yield /* comment */ from gen2();

    yield // comment
	from gen2();

    yield
    /* comment */
    from
    gen2();
}

Related to #1589

  • I intend to create a pull request to implement this feature.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions