Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using private method as callable: SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod #1094

Closed
zlodes opened this issue Sep 30, 2020 · 5 comments
Labels

Comments

@zlodes
Copy link

zlodes commented Sep 30, 2020

Hi!

Sniff SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod detects unused private method, but it is using as callable with array synax (e.g. [$this, 'methodName']).

Error:

FILE: /home/vagrant/projects/secret-project/src/PrivateMethodDemo.php
--------------------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
--------------------------------------------------------------------------------
 23 | ERROR | Class PrivateMethodDemo contains unused private method
    |       | privateMethod().
    |       | (SlevomatCodingStandard.Classes.UnusedPrivateElements.UnusedMethod)

Packages versions

squizlabs/php_codesniffer version: 3.5.6
slevomat/coding-standard version 6.4.0

Code example:

<?php

declare(strict_types=1);

namespace AI\B2TraderApi;

use Closure;

class PrivateMethodDemo
{
    public function getClosureFromPrivateMethod(): void
    {
        $closure = Closure::fromCallable([$this, 'privateMethod']);

        $closure();
    }

    public function callPrivateMethodUsingCallUserFuncArray(): void
    {
        call_user_func_array([$this, 'privateMethod'], []);
    }

    private function privateMethod(): void
    {
        // Do nothing
    }
}

P.S.: PhpStorm can detect that the private method is used.
image

image

@kukulich
Copy link
Contributor

This sniff is marked as deprecated

@zlodes
Copy link
Author

zlodes commented Sep 30, 2020

@kukulich what the alternative of it?

@kukulich
Copy link
Contributor

* @deprecated https://phpstan.org/blog/detecting-unused-private-properties-methods-constants

@zlodes
Copy link
Author

zlodes commented Oct 1, 2020

@kukulich thanks!

@github-actions
Copy link

github-actions bot commented Nov 2, 2020

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants