I wrote the following lines of code:
$this->validate($group);
$this->em->persist($group);
$this->em->flush();
Method "validate" will throw an exception if $group is not valid. The issue is, it seems kind of "fragile". If another developer changed this code, maybe he would accidently move the validate method and the entity manager would save the object into the database without validating it.
Do you think the following lines of code are better or am I just overthinking it?
$validGroup = $this->validate($group);
$this->em->persist($validGroup);
$this->em->flush();
Are there any patterns for validation?