PHP_CodeSniffer icon indicating copy to clipboard operation
PHP_CodeSniffer copied to clipboard

PHP 8.2 - DNF Support

Open auroraeosrose opened this issue 3 years ago • 5 comments

Describe the bug DNF in properties and as return types creates spurious formatting errors

Code sample

<?php

declare(strict_types=1);

namespace Test;

interface A
{
    public function foo(): void;
}
interface B
{
    public function bar(): void;
}

class C implements A, B
{
    public function foo(): void
    {
        echo 'foo';
    }
    public function bar(): void
    {
        echo 'bar';
    }
}

class Dnf
{
    protected (A&B)|null $property;

    public function getProperty(): (A&B)|null
    {
        return $this->property;
    }
}


To reproduce Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs test.php ...
  3. See error message displayed
FILE: test.php
-------------------------------------------------------------------------------------------------------
FOUND 12 ERRORS AFFECTING 5 LINES
-------------------------------------------------------------------------------------------------------
 11 | ERROR | [ ] Each interface must be in a file by itself
 16 | ERROR | [ ] Each interface must be in a file by itself
 28 | ERROR | [ ] Each class must be in a file by itself
 30 | ERROR | [x] Expected at least 1 space before "&"; 0 found
 30 | ERROR | [x] Expected at least 1 space after "&"; 0 found
 30 | ERROR | [x] Expected at least 1 space before "|"; 0 found
 30 | ERROR | [x] Expected at least 1 space after "|"; 0 found
 32 | ERROR | [ ] There must be a single space between the colon and type in a return type declaration
 32 | ERROR | [x] Expected at least 1 space before "&"; 0 found
 32 | ERROR | [x] Expected at least 1 space after "&"; 0 found
 32 | ERROR | [x] Expected at least 1 space before "|"; 0 found
 32 | ERROR | [x] Expected at least 1 space after "|"; 0 found
-------------------------------------------------------------------------------------------------------
PHPCBF CAN FIX THE 8 MARKED SNIFF VIOLATIONS AUTOMATICALLY
-------------------------------------------------------------------------------------------------------

Expected behavior Ignoring the "each interface/class must be in a file by itself" all the rest of the errors marked are NOT errors and definitely should not be autocorrecting

The single space between colon and type declaration doesn't apparently recognize the parentheses () as part of the type declaration

Versions (please complete the following information):

  • OS: all
  • PHP: 8.2
  • PHPCS: version 4.0.0 (alpha)
  • Standard: PSR12

auroraeosrose avatar Dec 14 '22 17:12 auroraeosrose

PHPCS does not support DNF (at all) yet. PHP 8.2 support has yet to be added. Also see a number of open PRs covering bits and pieces of PHP 8.2 support (but not DNF yet).

jrfnl avatar Dec 14 '22 17:12 jrfnl

Well at least I'm not crazy :) Is there an 8.2 checklist issue open yet like there was for 8.1? Definitely have to change the tokenizing, probably similarly to what was done for union types then..

auroraeosrose avatar Dec 14 '22 18:12 auroraeosrose

//phpcs:disable PSR12.Operators.OperatorSpacing
//phpcs:disable PSR12.Functions.ReturnTypeDeclaration.SpaceBeforeReturnType

disables for anyone who finds this ticket and needs them

@jrfnl I'm going to retitle this ticket to make it easier to find

auroraeosrose avatar Dec 14 '22 18:12 auroraeosrose

Well at least I'm not crazy :) Is there an 8.2 checklist issue open yet like there was for 8.1? Definitely have to change the tokenizing, probably similarly to what was done for union types then..

No, there's no PHP 8.2 checklist (yet), then again, for the Tokenizer, PHP 8.2 is finally a quite manageble release after three years of onslaught with new syntaxes, so I don't think a checklist is needed.

AFAICS, DNF is the only thing which necessitates changes to the Tokenizer\PHP class. Everything else just needs some tweaks to utility functions and sniffs.

A preliminary discussion about how to handle DNF for the tokenizer is in this (quite unrelated) ticket: https://github.com/squizlabs/PHP_CodeSniffer/pull/3667#issuecomment-1286599659 (as that's when the build started failing).

jrfnl avatar Dec 14 '22 19:12 jrfnl

I'm doing this right now:

// phpcs:ignore Squiz.WhiteSpace.OperatorSpacing.NoSpaceAfter, Squiz.WhiteSpace.OperatorSpacing.NoSpaceBefore
public function foo(): (A&B)|null
{
...

simPod avatar Feb 24 '23 14:02 simPod

FYI: DNF support will be included in the next PHPCS release (3.10.0).

jrfnl avatar Apr 29 '24 20:04 jrfnl