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 |
||
22 | class AccountCommand extends AbstractCommand |
||
23 | { |
||
24 | /** |
||
25 | * The console command name. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $name = 'aimeos:account'; |
||
30 | |||
31 | /** |
||
32 | * The console command description. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $description = 'Creates new (admin) accounts'; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * Execute the console command. |
||
41 | * |
||
42 | * @return mixed |
||
43 | */ |
||
44 | public function handle() |
||
45 | { |
||
46 | $code = $this->argument( 'email' ); |
||
47 | if( ( $password = $this->option( 'password' ) ) === null ) { |
||
48 | $password = $this->secret( 'Password' ); |
||
49 | } |
||
50 | |||
51 | $context = $this->getLaravel()->make( 'Aimeos\Shop\Base\Context' )->get( false, 'command' ); |
||
52 | $context->setEditor( 'aimeos:account' ); |
||
53 | |||
54 | $localeManager = \Aimeos\MShop\Factory::createManager( $context, 'locale' ); |
||
55 | $localeItem = $localeManager->bootstrap( $this->argument( 'site' ), '', '', false ); |
||
56 | $context->setLocale( $localeItem ); |
||
57 | |||
58 | $user = $this->createCustomerItem( $context, $code, $password ); |
||
59 | |||
60 | if( $this->option( 'admin' ) ) { |
||
61 | $this->addGroup( $context, $user, 'admin' ); |
||
62 | } |
||
63 | |||
64 | if( $this->option( 'api' ) ) { |
||
65 | $this->addGroup( $context, $user, 'api' ); |
||
66 | } |
||
67 | |||
68 | if( $this->option( 'editor' ) ) { |
||
69 | $this->addGroup( $context, $user, 'editor' ); |
||
70 | } |
||
71 | |||
72 | if( $this->option( 'viewer' ) ) { |
||
73 | $this->addGroup( $context, $user, 'viewer' ); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | |||
78 | /** |
||
79 | * Associates the user to the group by their given IDs |
||
80 | * |
||
81 | * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
||
82 | * @param string $userid Unique user ID |
||
83 | * @param string $groupid Unique group ID |
||
84 | */ |
||
85 | protected function addListItem( \Aimeos\MShop\Context\Item\Iface $context, $userid, $groupid ) |
||
114 | |||
115 | |||
116 | /** |
||
117 | * Adds the group to the given user |
||
118 | * |
||
119 | * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
||
120 | * @param \Aimeos\MShop\Customer\Item\Iface $user Aimeos customer object |
||
121 | * @param string $group Unique customer group code |
||
122 | */ |
||
123 | protected function addGroup( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Customer\Item\Iface $user, $group ) |
||
131 | |||
132 | |||
133 | /** |
||
134 | * Returns the customer item for the given e-mail and set its password |
||
135 | * |
||
136 | * If the customer doesn't exist yet, it will be created. |
||
137 | * |
||
138 | * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
||
139 | * @param string $email Unique e-mail address |
||
140 | * @param string $password New user password |
||
141 | * @return \Aimeos\MShop\Customer\Item\Iface Aimeos customer item object |
||
142 | */ |
||
143 | protected function createCustomerItem( \Aimeos\MShop\Context\Item\Iface $context, $email, $password ) |
||
163 | |||
164 | |||
165 | /** |
||
166 | * Get the console command arguments. |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | View Code Duplication | protected function getArguments() |
|
177 | |||
178 | |||
179 | /** |
||
180 | * Returns the customer group item for the given code |
||
181 | * |
||
182 | * @param \Aimeos\MShop\Context\Item\Iface $context Aimeos context object |
||
183 | * @param string $code Unique customer group code |
||
184 | * @return \Aimeos\MShop\Customer\Item\Group\Iface Aimeos customer group item object |
||
185 | */ |
||
186 | protected function getGroupItem( \Aimeos\MShop\Context\Item\Iface $context, $code ) |
||
205 | |||
206 | |||
207 | /** |
||
208 | * Get the console command options. |
||
209 | * |
||
210 | * @return array |
||
211 | */ |
||
212 | protected function getOptions() |
||
222 | } |
||
223 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.