Skip to content

Methods/constants with name empty/isset/unset are always reported as error #3609

Closed
@dlakomski

Description

@dlakomski

Describe the bug
PHPCS considers methods with name empty to be incorrect

Code sample

<?php

declare(strict_types=1);

namespace App;

class Foo
{
    public function empty()
    {
        return;
    }
}

Custom ruleset
No custom rules. Just PSR12 standard

To reproduce
Steps to reproduce the behavior:

  1. Create a file called test.php with the code sample above...
  2. Run phpcs --standard=PSR12 test.php
  3. See error message displayed
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 9 | ERROR | Expected "function abc(...)"; found "function abc(...)"
----------------------------------------------------------------------

If you change method name to basically anything else - empty1, createEmpty etc - there is no error

Expected behavior
No complains.

Versions (please complete the following information):

  • OS: Ubuntu 20.04
  • PHP: 8.1.6
  • PHPCS: 3.7.0
  • Standard: PSR12

Activity

jakubkosinski99

jakubkosinski99 commented on Jun 13, 2022

@jakubkosinski99

I noticed that too. Nothing can be done

Arthur-Sk

Arthur-Sk commented on Jun 13, 2022

@Arthur-Sk

Same.

// @codingStandardsIgnoreStart
public function empty() 
{
  // @codingStandardsIgnoreEnd

As a temporary workaround.

dragosprotung

dragosprotung commented on Jun 13, 2022

@dragosprotung

This actually is a problem for more function names:

<?php

declare(strict_types=1);

final class Test
{
    public function unset()
    {
        return;
    }

    public function empty()
    {
        return;
    }

    public function isset()
    {
        return;
    }
}
--------------------------------------------------------------------------
FOUND 3 ERRORS AFFECTING 3 LINES
--------------------------------------------------------------------------
  7 | ERROR | Expected "function abc(...)"; found "function abc(...)"
    |       | (Squiz.Functions.FunctionDeclaration.Found)
 12 | ERROR | Expected "function abc(...)"; found "function abc(...)"
    |       | (Squiz.Functions.FunctionDeclaration.Found)
 17 | ERROR | Expected "function abc(...)"; found "function abc(...)"
    |       | (Squiz.Functions.FunctionDeclaration.Found)
--------------------------------------------------------------------------
jrfnl

jrfnl commented on Jun 13, 2022

@jrfnl
Contributor

Thank you all for reporting this. PR #3610 should fix it. Testing appreciated!

added this to the 3.7.1 milestone on Jun 13, 2022
added a commit that references this issue on Jun 13, 2022
changed the title [-]Method with name `empty` is always reported as error[/-] [+]Methods/constants with name empty/isset/unset are always reported as error[/+] on Jun 13, 2022
added a commit that references this issue on Jun 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Participants

      @dlakomski@gsherwood@jrfnl@dragosprotung@Arthur-Sk

      Issue actions

        Methods/constants with name empty/isset/unset are always reported as error · Issue #3609 · squizlabs/PHP_CodeSniffer