The class Foo has a method that must return a DateTime object.
Foo can be considered a parameter object (it implements an interface)
class Foo implements CustomInterface{
function dateTime():\DateTimeInterface{}
}
The class "Bar" requires a Foo object in the constructor
Does it violate the law of Demeter if Foo's DateTime object is called within Bar constructor?
class Bar{
public function __construct(CustomInterface $interface){
$this->field = $interface->dateTime()->format('Ymd');
}
}
I cannot pass the DateTime object directly in the constructor because Foo is parameter object and has other fields required to make Bar work.
I understand A.B.C violates the Demeter law if C return an object we would work on, but is it still an issue if C returns a "final" result like any string or boolean value?
Update
Here is the exact subtlety I was looking for regarding the law of Demeter:
The data structure behind the Position property is immutable, and that means it’s safe to expose it directly. The client code is unable to mess up with the position’s internal state because it simply doesn’t have any. int positionX = player.Position.X; The law of Demeter is not applicable to the world of purely functional (immutable) data structures.
Source: https://enterprisecraftsmanship.com/posts/law-of-demeter-and-immutability
if in a method you're working with data structures, law of demeter is not applied to them because is natural for a data structure to expose their internal structure.
Source: https://www.fabrizioduroni.it/2018/04/25/clean-code-objects-data-structures-law-demeter/