How to fix Avoid unused parameters PHPMD of protected method in parent class

1.2k views Asked by At

I have two class :

class EntityVoter
{
    protected function canPutToBlockChain($entity, $viewer)
    {
        return false;
    }
}
class VerificationVoter extends EntityVoter
{
    public function canPutToBlockChain($entity, $viewer)
    {
        return $this->canShare($entity, $viewer);
    }
}

PHPMD scan EntityVoter class and throw: Avoid unused parameters such as '$entity', '$viewer'.

My solution is creating a interface:

interface EntityVoterInterface 
{
     function canPutToBlockChain($entity, $viewer);
}

and then add @inhericDoc annotation:

class EntityVoter implements EntityVoterInterface
{
    /**
     * @inheritDoc
     */
    protected function canPutToBlockChain($entity, $viewer)
    {
        return false;
    }
}

Is there any better solution ?

1

There are 1 answers

0
Biapy On

Another option is to suppress PHPMD warning for the rule on the method:

class EntityVoter
{
    /**
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    protected function canPutToBlockChain($entity, $viewer)
    {
        return false;
    }
}

For more information, see PHPMD Suppressing Warnings @ PHP Mess Detector documentation.

I would not use wildcard exclusion such as @SuppressWarnings("unused") but targeted exclusion is OK for warnings linked to third-party code.