如何修复避免父类中被保护方法的未使用参数PHPMD



我有两个类:

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

PHPMD扫描EntityVoter类并抛出:避免未使用的参数,如'$entity', '$viewer'。

我的解决方案是创建一个接口:

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

然后添加@inhericDoc注释:

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

有没有更好的解决办法?

另一个选项是对方法上的规则抑制PHPMD警告:

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

有关更多信息,请参阅PHPMD抑制警告@ PHP混乱检测器文档。

我不会使用通配符排除,如@SuppressWarnings("unused"),但有针对性的排除是可以链接到第三方代码的警告。

最新更新