Skip to content

Question about LCOM4 with Singleton-likes classes #262

Open
@niconoe-

Description

@niconoe-

Hi, still me 😄 ,

I have a question about the LCOM4 value used and even after reseach I wasn't able to find any document or explaination.
Basically, the LCOM4 is used to calculate how many responsabilities a class have.

Now, I have a class that looks like a singleton (but it's NOT a singleton) in its structure and it should have only 1 responsability.
Here is my class:

<?php

class Foo
{
    protected $a;
    protected $b;

    public static function build($a, $b)
    {
        return new self($a, $b);
    }

    protected function __construct($a, $b)
    {
        $this->a = $a;
        $this->b = $b;
    }

    // Only accessors below, it does not matter.
    //...
}

Using both PhpMetrics 1.10.0 and PhpMetrics 2.0, I can see an LCOM4 = 2 for this kind of class.

My question is: "Is LCOM4 = 2 the expected result?".

This is obvious that this class only have 1 responsability, which is build an object with it's properties. But the counter says "2" because Foo::build is related to nothing (nbWorkflow = 1) and the constructor (with all its accessors) is related to all properties (nbWorkflow = 2).

So, what is the position of the "creators" of the LCOM4 method about this? Does static methods must be ignored? In this case, a class with 6 static methods doing 6 unrelated stuffs (understand the class owns 6 responsabilities) should have an LCOM4 = 0, which is also incorrect?

Maybe because of the presence of new self in the Foo::build method, we can add the link between Foo::build and Foo::__construct, but how can we manage inheritance in this case?

I hope you understood what I wanted to explain with this issue, and if you want, I can make the same thread in French for a better understanding between us about this.
Thank you for your great tool.

Regards,

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions