SonarCloud doen't see changes in my attributes

Template for a good new topic, formatted with Markdown:

  • ALM: Azure DevOps
  • CI system used: Azure DevOps
  • Languages of the repository: PHP

Use Doctrine, Symfony & ApiPlatform

I have an first class which define an id

abstract class AbstractUlidEntity
{
    public function __construct()
    {
        $this->id = new Ulid();
    }

    #[ORM\Id, ORM\Column(type: 'ulid', unique: true)]
    #[ApiProperty(identifier: true)]
    protected Ulid $id;

    public function getId(): string
    {
        return $this->id->__toString();
    }
}

Then, I have a resource that extends this class. This resource is used in ApiPlatform, so it defines its own serialization.

class Resource extends AbstractUlidEntity
{
    #[Groups(['myresource:read'])]
    public function getId(): string
    {
        return parent::getId();
    }
}

My abstract class doesn’t have to know anything about its implementation, so the serialization has nothing to do with it.
Redefining the id in each resource makes the abstract class useless

Each resource has its serialization group, we cannot add all in the abstract class, so redefining only the getter is a good solution, in ApiPlatform point of view.

The problem is that SonarCloud seems not to look at the difference in attributes, and say that I should

Remove this method “getId” to simply inherit it.

Is there a way to remove this warning?

Hello @Guillaume_Wambre and welcome to the community!

Thanks for your report. This is indeed a case that is not currently taken into account by the PHP analyzer. I created a ticket (SONARPHP-1651) to adapt this rule for methods with attributes. Unfortunately, I don’t have any estimates on when it will be fixed.

In the meantime, you can either accept all these issues as FPs, or remove this particular rule from your Quality Profile. If you can identify entity classes by their location, you can also completely exclude them from analysis scope with sonar.exclusions property.

Hope that helps

Peter

Thank you for your response. I will ignore these reports for the moment in my project.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.