Conditions | 7 |
Paths | 157 |
Total Lines | 72 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
41 | public function execute(InputInterface $input, OutputInterface $output) |
||
42 | { |
||
43 | /** @var QuestionHelper $questionHelper */ |
||
44 | $questionHelper = $this->getHelper('question'); |
||
45 | |||
46 | $output->writeln([ |
||
47 | '<error>WARNING: you are about to migrate institution configurations' |
||
48 | . ' to work with identifiers based on insitutions with normalized casing.</error>', |
||
49 | '<error>This command is intended to be run only once.</error>', |
||
50 | '' |
||
51 | ]); |
||
52 | |||
53 | $confirmationQuestion = new ConfirmationQuestion( |
||
54 | 'Are you sure you want to run the institution configuration migrations? [y/N]', |
||
55 | false |
||
56 | ); |
||
57 | $confirmed = $questionHelper->ask($input, $output, $confirmationQuestion); |
||
58 | |||
59 | if (!$confirmed) { |
||
60 | $output->writeln('<info>Exiting without running migrations.</info>'); |
||
61 | return; |
||
62 | } |
||
63 | |||
64 | /** @var Container $container */ |
||
65 | $container = $this->getApplication()->getKernel()->getContainer(); |
||
|
|||
66 | |||
67 | $tokenStorage = $container->get('security.token_storage'); |
||
68 | $connectionHelper = $container->get('surfnet_stepup_middleware_middleware.dbal_connection_helper'); |
||
69 | $provider = $container->get('surfnet_stepup_middleware_middleware.institution_configuration_provider'); |
||
70 | $pipeline = $container->get('pipeline'); |
||
71 | $eventBus = $container->get('surfnet_stepup_middleware_command_handling.event_bus.buffered'); |
||
72 | |||
73 | // The InstitutionConfiguration commands require ROLE_MANAGEMENT |
||
74 | // Note that the new events will not have any actor metadata associated with them |
||
75 | $tokenStorage->setToken(new AnonymousToken('console', 'console', ['ROLE_MANAGEMENT'])); |
||
76 | |||
77 | $connectionHelper->beginTransaction(); |
||
78 | |||
79 | try { |
||
80 | $state = $provider->loadData(); |
||
81 | |||
82 | foreach ($state->inferRemovalCommands() as $removalCommand) { |
||
83 | $pipeline->process($removalCommand); |
||
84 | } |
||
85 | $eventBus->flush(); |
||
86 | |||
87 | foreach ($state->inferCreateCommands() as $createCommand) { |
||
88 | $pipeline->process($createCommand); |
||
89 | } |
||
90 | $eventBus->flush(); |
||
91 | |||
92 | foreach ($state->inferReconfigureCommands() as $reconfigureCommand) { |
||
93 | $pipeline->process($reconfigureCommand); |
||
94 | } |
||
95 | $eventBus->flush(); |
||
96 | |||
97 | foreach ($state->inferAddRaLocationCommands() as $addRaLocationCommand) { |
||
98 | $pipeline->process($addRaLocationCommand); |
||
99 | } |
||
100 | $eventBus->flush(); |
||
101 | |||
102 | $connectionHelper->commit(); |
||
103 | $output->writeln('<info>Successfully migrated institution configurations.</info>'); |
||
104 | } catch (Exception $exception) { |
||
105 | $output->writeln( |
||
106 | sprintf('<error>Could not migrate institution configurations: %s</error>', $exception->getMessage()) |
||
107 | ); |
||
108 | $connectionHelper->rollBack(); |
||
109 | |||
110 | throw $exception; |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: