Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php | ||
| 19 | class ProfileExtension extends \Twig_Extension | ||
| 20 | { | ||
| 21 | /** | ||
| 22 | * @var UserRepositoryInterface | ||
| 23 | */ | ||
| 24 | private $userRepository; | ||
| 25 | |||
| 26 | /** | ||
| 27 | * @var ProfileModuleCollector | ||
| 28 | */ | ||
| 29 | private $profileModuleCollector; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * @var TranslatorInterface | ||
| 33 | */ | ||
| 34 | private $translator; | ||
| 35 | |||
| 36 | /** | ||
| 37 | * ProfileExtension constructor. | ||
| 38 | * @param UserRepositoryInterface $userRepository | ||
| 39 | * @param ProfileModuleCollector $profileModuleCollector | ||
| 40 | * @param TranslatorInterface $translator | ||
| 41 | */ | ||
| 42 | public function __construct( | ||
| 51 | |||
| 52 | public function getFunctions() | ||
| 58 | |||
| 59 | public function getFilters() | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Displays the avatar of a given user. | ||
| 69 | * | ||
| 70 | * @param int|string $uid The user's id or name | ||
| 71 | * @param int $width Image width (optional) | ||
| 72 | * @param int $height Image height (optional) | ||
| 73 | * @param int $size Gravatar size (optional) | ||
| 74 | * @param string $rating Gravatar self-rating [g|pg|r|x] see: http://en.gravatar.com/site/implement/images/ (optional) | ||
| 75 | * | ||
| 76 | * @return string | ||
| 77 | */ | ||
| 78 | View Code Duplication | public function getUserAvatar($uid = 0, $width = 0, $height = 0, $size = 0, $rating = '') | |
| 119 | |||
| 120 | /** | ||
| 121 | * Create a link to a users profile from the UID. | ||
| 122 | * | ||
| 123 | * Examples | ||
| 124 | * | ||
| 125 | * Simple version, shows the username | ||
| 126 |      *   {{ uid|profileLinkByUserId() }} | ||
| 127 | * Simple version, shows username, using class="classname" | ||
| 128 |      *   {{ uid|profileLinkByUserId(class='classname') }} | ||
| 129 | * Using profile.gif instead of username, no class | ||
| 130 |      *   {{ uid|profileLinkByUserId(image='images/profile.gif') }} | ||
| 131 | * | ||
| 132 | * @param integer $userId The users uid | ||
| 133 | * @param string $class The class name for the link (optional) | ||
| 134 | * @param string $image Path to the image to show instead of the username (optional) | ||
| 135 | * @param integer $maxLength If set then user names are truncated to x chars | ||
| 136 | * @return string The output | ||
| 137 | */ | ||
| 138 | View Code Duplication | public function profileLinkByUserId($userId, $class = '', $image = '', $maxLength = 0, $title = '') | |
| 146 | |||
| 147 | /** | ||
| 148 | * Create a link to a users profile from the username. | ||
| 149 | * | ||
| 150 | * Examples | ||
| 151 | * | ||
| 152 | * Simple version, shows the username | ||
| 153 |      *   {{ username|profileLinkByUserName() }} | ||
| 154 | * Simple version, shows username, using class="classname" | ||
| 155 |      *   {{ username|profileLinkByUserName(class='classname') }} | ||
| 156 | * Using profile.gif instead of username, no class | ||
| 157 |      *   {{ username|profileLinkByUserName('image'='images/profile.gif') }} | ||
| 158 | * | ||
| 159 | * @param string $userName The users name | ||
| 160 | * @param string $class The class name for the link (optional) | ||
| 161 | * @param string $image Path to the image to show instead of the username (optional) | ||
| 162 | * @param integer $maxLength If set then user names are truncated to x chars | ||
| 163 | * @return string The output | ||
| 164 | */ | ||
| 165 | View Code Duplication | public function profileLinkByUserName($userName, $class = '', $image = '', $maxLength = 0, $title = '') | |
| 173 | |||
| 174 | /** | ||
| 175 | * Internal function used by profileLinkByUserId() and profileLinkByUserName(). | ||
| 176 | * | ||
| 177 | * @param integer $userId The users uid | ||
| 178 | * @param string $userName The users name | ||
| 179 | * @param string $class The class name for the link (optional) | ||
| 180 | * @param string $imagePath Path to the image to show instead of the username (optional) | ||
| 181 | * @param integer $maxLength If set then user names are truncated to x chars | ||
| 182 | * @return string The output | ||
| 183 | */ | ||
| 184 | private function determineProfileLink($userId = null, $userName = null, $class = '', $imagePath = '', $maxLength = 0, $title = '') | ||
| 222 | } | ||
| 223 | 
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.