Skip to content

Instantly share code, notes, and snippets.

@mcsee
Last active February 20, 2025 22:30
Show Gist options
  • Select an option

  • Save mcsee/91a2d630101ba6137f64195e76c1b266 to your computer and use it in GitHub Desktop.

Select an option

Save mcsee/91a2d630101ba6137f64195e76c1b266 to your computer and use it in GitHub Desktop.
This gist belongs to Clean Code Cookbook http://cleancodecookbook.com By Maximiliano Contieri http://maximilianocontieri.com
<?
final class SocialNetworkProfile {
private $userName;
private $friends;
// friends is a reference to a large collection
private $feed;
// feed references the whole user feed
public function __construct(
$userName,
$friends,
UserFeed $feed) {
$this->assertUsernameIsValid($userName);
$this->assertNoFriendDuplicates($friends);
$this->userName = $userName;
$this->friends = $friends;
$this->feed = $feed;
$this->assertNoFriendOfMyself($friends);
}
// Lots of protocol
}
// If you need to transfer
// to an external system you need
// to duplicate (and maintain) the structure
final class SocialNetworkProfileDTO {
private $userName; // duplicated to be synchronized
private $friends; // duplicated to be synchronized
private $feed; // duplicated to be synchronized
public function __construct() {
// Empty constructor without validations
}
// No protocol, just serializers
}
// If you need to transfer to an external system
// you create an anemic DTO
$janesProfileToTransfer = new SocialNetworkProfileDTO();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment