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 |
||
16 | class UsersPresenter extends BasePresenter |
||
17 | { |
||
18 | |||
19 | public function renderChangePass() |
||
20 | { |
||
21 | $navbar = []; |
||
22 | $navbar[] = (object) ['name' => 'Uživatelé', 'link' => 'Users:list']; |
||
23 | $navbar[] = (object) ['name' => 'Detail']; |
||
24 | $navbar[] = (object) ['name' => 'Změna hesla']; |
||
25 | |||
26 | $this->template->navbar = $navbar; |
||
27 | } |
||
28 | |||
29 | public function renderList() |
||
30 | { |
||
31 | $navbar = []; |
||
32 | $navbar[] = (object) ['name' => 'Uživatelé']; |
||
33 | |||
34 | $this->template->navbar = $navbar; |
||
35 | |||
36 | $usersDao = $this->em->getRepository(PModel\User\User::getClassName()); |
||
37 | $users = $usersDao->findBy(['active' => 1]); |
||
38 | |||
39 | $this->template->users = $users; |
||
40 | } |
||
41 | |||
42 | View Code Duplication | public function renderListUnactive() |
|
43 | { |
||
44 | $navbar = []; |
||
45 | $navbar[] = (object) ['name' => 'Uživatelé', 'link' => 'Users:list']; |
||
46 | $navbar[] = (object) ['name' => 'Neaktivní']; |
||
47 | |||
48 | $this->template->navbar = $navbar; |
||
49 | |||
50 | $usersDao = $this->em->getRepository(PModel\User\User::getClassName()); |
||
51 | $users = $usersDao->findBy(['active' => 0]); |
||
52 | |||
53 | $this->template->users = $users; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @Secured |
||
58 | * @Secured\User(loggedIn) |
||
59 | * @Secured\Role(usermanager) |
||
60 | */ |
||
61 | View Code Duplication | public function renderNew() |
|
62 | { |
||
63 | $navbar = []; |
||
64 | $navbar[] = (object) ['name' => 'Uživatelé', 'link' => 'Users:list']; |
||
65 | $navbar[] = (object) ['name' => 'Nový']; |
||
66 | |||
67 | $this->template->navbar = $navbar; |
||
68 | } |
||
69 | |||
70 | View Code Duplication | public function renderChange() |
|
71 | { |
||
72 | $navbar = []; |
||
73 | $navbar[] = (object) ['name' => 'Uživatelé', 'link' => 'Users:list']; |
||
74 | $navbar[] = (object) ['name' => 'Úprava']; |
||
75 | |||
76 | $this->template->navbar = $navbar; |
||
77 | |||
78 | $this->notYetImplemented(); |
||
79 | } |
||
80 | |||
81 | public function renderProfile() |
||
82 | { |
||
83 | $navbar = []; |
||
84 | $navbar[] = (object) ['name' => 'Uživatelé', 'link' => 'Users:list']; |
||
85 | $navbar[] = (object) ['name' => 'Profil']; |
||
86 | |||
87 | $this->template->navbar = $navbar; |
||
88 | |||
89 | $id = ($this->getParameter('id') !== null) ? $this->getParameter('id') : $this->getUser()->getIdentity()->id; |
||
90 | |||
91 | $user = $this->em->getRepository(PModel\User\User::getClassName()); |
||
92 | $profileUser = $user->find($id); |
||
93 | |||
94 | $this->template->profileUser = $profileUser; |
||
95 | |||
96 | $isActual = false; |
||
97 | if ($this->getUser()->isLoggedIn() && $this->getUser()->getIdentity()->id |
||
98 | == $profileUser->id) { |
||
99 | $isActual = true; |
||
100 | } |
||
101 | |||
102 | $this->template->isActual = $isActual; |
||
103 | } |
||
104 | |||
105 | View Code Duplication | public function renderGroups() |
|
106 | { |
||
107 | $navbar = []; |
||
108 | $navbar[] = (object) ['name' => 'Uživatelé', 'link' => 'Users:list']; |
||
109 | $navbar[] = (object) ['name' => 'Skupiny']; |
||
110 | |||
111 | $this->template->navbar = $navbar; |
||
112 | |||
113 | $userGroupsDao = $this->em->getRepository(PModel\User\Group::getClassName()); |
||
114 | $userGroups = $userGroupsDao->findAll(); |
||
115 | |||
116 | $this->template->userGroups = $userGroups; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @Secured |
||
121 | * @Secured\User(loggedIn) |
||
122 | * @Secured\Role(superadmin) |
||
123 | */ |
||
124 | View Code Duplication | public function renderLogActivity() |
|
125 | { |
||
126 | $navbar = []; |
||
127 | $navbar[] = (object) ['link' => 'Users:list', 'name' => 'Uživatelé']; |
||
128 | $navbar[] = (object) ['name' => 'Sledování přihlášení']; |
||
129 | |||
130 | $this->template->navbar = $navbar; |
||
131 | |||
132 | $activityDao = $this->em->getRepository(PModel\User\LogActivity::getClassName()); |
||
133 | $activity = $activityDao->findBy([], ['id' => 'DESC'], 30); |
||
134 | |||
135 | $this->template->activity = $activity; |
||
136 | } |
||
137 | |||
138 | protected function createComponentChangePasswordForm() |
||
139 | { |
||
140 | $form = new Nette\Application\UI\Form; |
||
141 | $form->addPassword('oldpassw', 'Staré heslo:') |
||
142 | ->setRequired('Prosím, zadejte své staré heslo.') |
||
143 | ->getControlPrototype()->class('form-control'); |
||
144 | |||
145 | $form->addPassword('newpassw', 'Nové heslo:') |
||
146 | ->setRequired('Prosím, zadejte své nové heslo.') |
||
147 | ->getControlPrototype()->class('form-control'); |
||
148 | |||
149 | $form->addPassword('newpassw2', 'Nové heslo (znovu):') |
||
150 | ->setRequired('Prosím, zadejte své nové heslo pro kontrolu.') |
||
151 | ->addRule( |
||
152 | Form::EQUAL, |
||
153 | "Zadaná nová hesla se neshodují.", |
||
154 | $form["newpassw"] |
||
155 | ) |
||
156 | ->getControlPrototype()->class('form-control'); |
||
157 | |||
158 | $form->addSubmit('send', 'Změnit heslo') |
||
159 | ->getControlPrototype()->class('btn btn-success'); |
||
160 | |||
161 | $form->onSuccess[] = [$this, 'changePasswordFormSucceeded']; |
||
162 | return $form; |
||
163 | } |
||
164 | |||
165 | protected function createComponentChangeProfileForm() |
||
166 | { |
||
167 | $userDao = $this->em->getRepository(PModel\User\User::getClassName()); |
||
168 | $user = $userDao->find($this->getUser()->id); |
||
169 | |||
170 | $form = new Nette\Application\UI\Form; |
||
171 | $form->addText('firstname', 'Jméno:') |
||
172 | ->setRequired('Prosím, zadejte své své jméno.') |
||
173 | ->setDefaultValue($user->firstName) |
||
174 | ->getControlPrototype()->class('form-control'); |
||
175 | |||
176 | $form->addText('lastname', 'Příjmení:') |
||
177 | ->setRequired('Prosím, zadejte své nové příjmení.') |
||
178 | ->setDefaultValue($user->lastName) |
||
179 | ->getControlPrototype()->class('form-control'); |
||
180 | |||
181 | $form->addText('email', 'Email:') |
||
182 | ->setRequired('Prosím, zadejte svou emailovou adesu.') |
||
183 | ->setDefaultValue($user->email) |
||
184 | ->getControlPrototype()->class('form-control'); |
||
185 | |||
186 | $form->addSubmit('send', 'Uložit změny') |
||
187 | ->getControlPrototype()->class('btn btn-success'); |
||
188 | |||
189 | $form->onSuccess[] = [$this, 'changeProfileFormSucceeded']; |
||
190 | return $form; |
||
191 | } |
||
192 | |||
193 | public function createComponentUsersNewForm() |
||
227 | |||
228 | public function createComponentUsersChangeForm() |
||
262 | |||
263 | public function usersNewFormSucceeded() |
||
266 | |||
267 | View Code Duplication | public function handleActive($id) |
|
277 | |||
278 | View Code Duplication | public function handleDeactive($id) |
|
288 | |||
289 | /** |
||
290 | * @param boolean $status |
||
291 | */ |
||
292 | private function setActiveStatus($id, $status) |
||
303 | |||
304 | public function changeProfileFormSucceeded($form) |
||
305 | { |
||
306 | $values = $form->getValues(true); |
||
307 | |||
308 | $userDao = $this->em->getRepository(PModel\User\User::getClassName()); |
||
309 | $user = $userDao->find($this->getUser()->id); |
||
310 | $user->firstName = $values['firstname']; |
||
311 | $user->lastName = $values['lastname']; |
||
312 | $user->email = $values['email']; |
||
313 | |||
320 | |||
321 | public function changePasswordFormSucceeded($form) |
||
342 | } |
||
343 |
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.