Closed basz closed 7 years ago
@ojhaujjwal
Why not just pass that option as a constructor argument?
This ok by you?
cc/@bakura10
@basz thanks for tagging. looks great :+1:
@basz since we're breaking BC anyway and now ServerOptions
instance is stored everywhere, I think it would be nice to make ServerOptions
immutable to avoid side effects.
@danizord agreed. setters are a big NO.
+1
• done •
@bakura10 this what yo mean?
plus a fail; but I don't see why; tests/src/Options/ServerOptionsTest.php:64
@bakura10 @danizord I'm not sure I like the current implemetation of ServerOptions very much.
Main point; It introduces a dependency for a problem that isn't there. Bloats; The class is lightweight enough for a few setters, which when private still ensure immutability. (class is also marked final). Now we I check if a option is set twice... And get awkward checks for combinations that the assertion library doesn't provide for (null or callable or string). Private setters are more expressive and guard values in logical way. Bahavior; With private setters it is still immutable.
The named constructor does seem helpful, but I think it would be more elegant to keep the setters in this case?
Actually as suggested @danizord an alternative to remove teh dependency to Assertion would be to do that:
private function construct(
bool $param1,
int $param2
) {
$this->param1 = $param1;
$this->param2 = $param2;
}
static public function fromArray(array $options)
{
return new self(
$options['param1'] ?? true,
$options['param2'] ?? 400
);
}
Therefore you keep the built-in typehinting and provide true immutability. What do you think?
I think that's better - (until (many) more options are added)
still don't see the 'true immutability' argument. private setters provide true mutability...
I will refactor...
@basz the point of using constructor instead of private setters is to ensure that an object is always in consistent state after construction.
@danizord i get that, just saying that a mega constructor is not always nice.
i tend to create ValueObjects with the following method
static withSomething : returns mutated copy) private setSomething : from (named) constructors, may do guarding public getSomething : get value
I see, so it's a matter of preference. I think setters are more verbose and PHPStorm generates the constructor automatically anyway :P
Before I continue, do you think approach is correct?
IMO this is ok... But looking at the test, it is possible to change behaviour by mutating the ServerOptions. When we do this everywhere that might lead to unexpected results?