Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class User extends BaseUser implements UserInterface |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | protected $locale; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | protected $displayname; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $confirmationType; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var \DateTime |
||
| 31 | */ |
||
| 32 | protected $confirmedAt; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ArrayCollection|Role[] |
||
| 36 | */ |
||
| 37 | protected $authorizationRoles; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var ImageInterface |
||
| 41 | */ |
||
| 42 | protected $picture; |
||
| 43 | |||
| 44 | public function __construct() |
||
| 45 | { |
||
| 46 | parent::__construct(); |
||
| 47 | |||
| 48 | $this->authorizationRoles = new ArrayCollection(); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | public function getAuthorizationRoles() |
||
| 55 | { |
||
| 56 | return $this->authorizationRoles; |
||
|
|
|||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param ArrayCollection|\Sylius\Component\Rbac\Model\Role[] $authorizationRoles |
||
| 61 | */ |
||
| 62 | public function setAuthorizationRoles($authorizationRoles) |
||
| 63 | { |
||
| 64 | if (!$authorizationRoles instanceof Collection) { |
||
| 65 | $authorizationRoles = new ArrayCollection($authorizationRoles); |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->authorizationRoles = $authorizationRoles; |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritdoc} |
||
| 73 | */ |
||
| 74 | public function resizeSecurityRoles() |
||
| 75 | { |
||
| 76 | $this->roles = array(self::DEFAULT_ROLE); |
||
| 77 | |||
| 78 | foreach($this->authorizationRoles as $role) { |
||
| 79 | foreach($role->getSecurityRoles() as $r) { |
||
| 80 | $this->roles[] = $r; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return bool |
||
| 87 | */ |
||
| 88 | public function isLocked() |
||
| 89 | { |
||
| 90 | return $this->locked; |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function getDisplayName() |
||
| 97 | { |
||
| 98 | $customer = $this->getCustomer(); |
||
| 99 | |||
| 100 | return $this->displayname ?: |
||
| 101 | ( |
||
| 102 | $customer && trim($customer->getFullName()) |
||
| 103 | ? $customer->getFullName() |
||
| 104 | : $this->username |
||
| 105 | ) |
||
| 106 | ; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param null|string $displayname |
||
| 111 | */ |
||
| 112 | public function setDisplayName($displayname = null) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getLocale() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $locale |
||
| 127 | */ |
||
| 128 | public function setLocale($locale) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return string|void |
||
| 135 | */ |
||
| 136 | public function getLang() |
||
| 137 | { |
||
| 138 | if ($this->locale) { |
||
| 139 | if (preg_match('/_([a-z]{2})/i', $this->locale, $match)) { |
||
| 140 | return strtolower($match[1]); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | return; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @inheritdoc |
||
| 149 | */ |
||
| 150 | public function getMediaPath() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * {@inheritdoc} |
||
| 157 | */ |
||
| 158 | public function setPicture(ImageInterface $picture = null) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | public function getPicture() |
||
| 170 | |||
| 171 | /** |
||
| 172 | * {@inheritdoc} |
||
| 173 | */ |
||
| 174 | public function getProfilePicture() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @inheritdoc |
||
| 191 | */ |
||
| 192 | public function getConfirmedAt() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @inheritdoc |
||
| 199 | */ |
||
| 200 | public function setConfirmedAt(\DateTime $confirmedAt = null) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @inheritdoc |
||
| 207 | */ |
||
| 208 | public function confirmed(\DateTime $confirmedAt = null) |
||
| 209 | { |
||
| 210 | $this->setConfirmedAt($confirmedAt ?: new \DateTime()); |
||
| 211 | $this->setEnabled(true); |
||
| 212 | $this->setConfirmationToken(null); |
||
| 213 | $this->setPasswordRequestedAt(null); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * @inheritdoc |
||
| 218 | */ |
||
| 219 | public function isConfirmed() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | public function setEnabled($boolean) |
||
| 228 | { |
||
| 229 | $this->enabled = (Boolean)$boolean; |
||
| 230 | |||
| 231 | if (!$this->isConfirmed()) { |
||
| 232 | $this->enabled = false; |
||
| 233 | } |
||
| 234 | |||
| 235 | return $this; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | public function getConfirmationChannel($propertyPath) |
||
| 242 | { |
||
| 243 | $accessor = PropertyAccess::createPropertyAccessor(); |
||
| 244 | |||
| 245 | return $accessor->getValue($this, $propertyPath); |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * {@inheritdoc} |
||
| 250 | */ |
||
| 251 | public function getConfirmationRequestedAt() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | */ |
||
| 259 | public function setConfirmationRequestedAt(\DateTime $dateTime = null) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | */ |
||
| 267 | public function getConfirmationConfirmedAt() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function setConfirmationConfirmedAt(\DateTime $dateTime = null) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * {@inheritdoc} |
||
| 282 | */ |
||
| 283 | public function isConfirmationConfirmed() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * {@inheritdoc} |
||
| 290 | */ |
||
| 291 | public function confirmationRequest($token) |
||
| 292 | { |
||
| 293 | $this->setConfirmationToken($token); |
||
| 294 | $this->setConfirmationRequestedAt(new \DateTime()); |
||
| 295 | $this->setEnabled(false); |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * {@inheritdoc} |
||
| 300 | */ |
||
| 301 | public function confirmationConfirm() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * {@inheritdoc} |
||
| 308 | */ |
||
| 309 | public function getConfirmationType() |
||
| 313 | |||
| 314 | /** |
||
| 315 | * {@inheritdoc} |
||
| 316 | */ |
||
| 317 | public function setConfirmationType($confirmationType) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * {@inheritdoc} |
||
| 324 | */ |
||
| 325 | public function confirmationDisableAccess() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * {@inheritdoc} |
||
| 332 | */ |
||
| 333 | public function confirmationEnableAccess() |
||
| 337 | } |
||
| 338 |