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 |
||
17 | View Code Duplication | class AccessToken implements AccessTokenEntityInterface |
|
|
|||
18 | { |
||
19 | use AccessTokenTrait; |
||
20 | |||
21 | /** |
||
22 | * @var ArrayCollection $scopes |
||
23 | * @ORM\ManyToMany(targetEntity="Scope", inversedBy="accessTokens") |
||
24 | * @ORM\JoinTable(name="AccessToken_Scope", |
||
25 | * joinColumns={@ORM\JoinColumn(name="token_id", referencedColumnName="identifier")}, |
||
26 | * inverseJoinColumns={@ORM\JoinColumn(name="scope_id", referencedColumnName="identifier")} |
||
27 | * ) |
||
28 | */ |
||
29 | protected $scopes; |
||
30 | |||
31 | /** |
||
32 | * @var DateTime |
||
33 | * @ORM\Column(type="date",nullable=true) |
||
34 | */ |
||
35 | protected $expiryDateTime; |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | * @ORM\Column(type="integer", length=11) |
||
40 | */ |
||
41 | protected $userIdentifier; |
||
42 | |||
43 | /** |
||
44 | * @var ClientEntityInterface |
||
45 | * @ORM\ManyToOne(targetEntity="OAuth\Client") |
||
46 | * @ORM\JoinColumn(name="client", referencedColumnName="id") |
||
47 | */ |
||
48 | protected $client; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | * @ORM\Id |
||
53 | * @ORM\Column(type="string", length=40) |
||
54 | */ |
||
55 | protected $identifier; |
||
56 | |||
57 | 5 | public function __construct() |
|
61 | |||
62 | /** |
||
63 | * Set token |
||
64 | * |
||
65 | * @param string $token |
||
66 | * @return AccessToken |
||
67 | */ |
||
68 | public function setToken($token) |
||
73 | |||
74 | /** |
||
75 | * Get token |
||
76 | * |
||
77 | * @return string |
||
78 | */ |
||
79 | 2 | public function getIdentifier() |
|
83 | |||
84 | /** |
||
85 | * @param string $identifier |
||
86 | */ |
||
87 | 2 | public function setIdentifier($identifier) |
|
91 | |||
92 | /** |
||
93 | * @param ScopeEntityInterface $scope |
||
94 | * @return $this |
||
95 | */ |
||
96 | 1 | public function addScope(ScopeEntityInterface $scope) |
|
101 | |||
102 | /** |
||
103 | * Return an array of scopes associated with the token. |
||
104 | * |
||
105 | * @return ScopeEntityInterface[] |
||
106 | */ |
||
107 | 1 | public function getScopes() |
|
111 | |||
112 | /** |
||
113 | * Get the token's expiry date time. |
||
114 | * |
||
115 | * @return DateTime |
||
116 | */ |
||
117 | 1 | public function getExpiryDateTime() |
|
121 | |||
122 | /** |
||
123 | * Set the date time when the token expires. |
||
124 | * |
||
125 | * @param DateTime $dateTime |
||
126 | */ |
||
127 | 1 | public function setExpiryDateTime(DateTime $dateTime) |
|
131 | |||
132 | /** |
||
133 | * @param int $identifier |
||
134 | * @return $this |
||
135 | */ |
||
136 | public function setUserIdentifier($identifier) |
||
141 | |||
142 | /** |
||
143 | * Get the token user's identifier. |
||
144 | * |
||
145 | * @return int |
||
146 | */ |
||
147 | public function getUserIdentifier() |
||
151 | |||
152 | /** |
||
153 | * Get the client that the token was issued to. |
||
154 | * |
||
155 | * @return ClientEntityInterface |
||
156 | */ |
||
157 | 1 | public function getClient() |
|
161 | |||
162 | /** |
||
163 | * Set the client that the token was issued to. |
||
164 | * |
||
165 | * @param ClientEntityInterface $client |
||
166 | */ |
||
167 | 1 | public function setClient(ClientEntityInterface $client) |
|
171 | } |
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.