Conditions | 11 |
Paths | 133 |
Total Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
31 | protected function execute(InputInterface $input, OutputInterface $output) |
||
32 | { |
||
33 | $this->detectMagento($output); |
||
34 | if ($this->initMagento()) { |
||
35 | |||
36 | // Username |
||
37 | |||
38 | $id = $this->getOrAskForArgument('id', $input, $output, 'Username or Email'); |
||
39 | $user = $this->getUserModel()->loadByUsername($id); |
||
40 | if (!$user->getId()) { |
||
41 | $user = $this->getUserModel()->load($id, 'email'); |
||
42 | } |
||
43 | |||
44 | if (!$user->getId()) { |
||
45 | $output->writeln('<error>User was not found</error>'); |
||
46 | return; |
||
47 | } |
||
48 | |||
49 | try { |
||
50 | $result = $user->validate(); |
||
51 | |||
52 | if (is_array($result)) { |
||
53 | throw new RuntimeException(implode(PHP_EOL, $result)); |
||
54 | } |
||
55 | |||
56 | if ($input->getOption('activate')) { |
||
57 | $user->setIsActive(1); |
||
58 | } |
||
59 | |||
60 | if ($input->getOption('deactivate')) { |
||
61 | $user->setIsActive(0); |
||
62 | } |
||
63 | |||
64 | // toggle is_active |
||
65 | if (!$input->getOption('activate') && !$input->getOption('deactivate')) { |
||
66 | $user->setIsActive(!$user->getIsActive()); // toggle |
||
67 | } |
||
68 | |||
69 | $user->save(); |
||
70 | |||
71 | if ($user->getIsActive() == 1) { |
||
72 | $output->writeln( |
||
73 | '<info>User <comment>' . $user->getUsername() . '</comment>' . |
||
74 | ' is now <comment>active</comment></info>' |
||
75 | ); |
||
76 | } else { |
||
77 | $output->writeln( |
||
78 | '<info>User <comment>' . $user->getUsername() . '</comment>' . |
||
79 | ' is now <comment>inactive</comment></info>' |
||
80 | ); |
||
81 | } |
||
82 | } catch (Exception $e) { |
||
83 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | } |
||
88 |