Conditions | 3 |
Paths | 5 |
Total Lines | 59 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
52 | protected function execute(InputInterface $input, OutputInterface $output) |
||
53 | { |
||
54 | /** @var Container $container */ |
||
55 | $container = $this->getApplication()->getKernel()->getContainer(); |
||
|
|||
56 | $projectionRepository = $container->get('surfnet_stepup_middleware_api.repository.identity'); |
||
57 | $pipeline = $container->get('surfnet_stepup_middleware_command_handling.pipeline.transaction_aware_pipeline'); |
||
58 | $eventBus = $container->get('surfnet_stepup_middleware_command_handling.event_bus.buffered'); |
||
59 | $connection = $container->get('surfnet_stepup_middleware_middleware.dbal_connection_helper'); |
||
60 | |||
61 | $nameId = new NameId($input->getArgument('name-id')); |
||
62 | $institution = new Institution($input->getArgument('institution')); |
||
63 | |||
64 | if ($projectionRepository->hasIdentityWithNameIdAndInstitution($nameId, $institution)) { |
||
65 | $output->writeln( |
||
66 | sprintf( |
||
67 | '<error>An identity with name ID "%s" from institution "%s" already exists</error>', |
||
68 | $nameId->getNameId(), |
||
69 | $institution->getInstitution() |
||
70 | ) |
||
71 | ); |
||
72 | |||
73 | return 1; |
||
74 | } |
||
75 | |||
76 | $command = new BootstrapIdentityWithYubikeySecondFactorIdentityCommand(); |
||
77 | $command->UUID = (string) Uuid::uuid4(); |
||
78 | $command->identityId = (string) Uuid::uuid4(); |
||
79 | $command->nameId = $input->getArgument('name-id'); |
||
80 | $command->institution = $input->getArgument('institution'); |
||
81 | $command->commonName = $input->getArgument('common-name'); |
||
82 | $command->email = $input->getArgument('email'); |
||
83 | $command->preferredLocale = $input->getArgument('preferred-locale'); |
||
84 | $command->secondFactorId = (string) Uuid::uuid4(); |
||
85 | $command->yubikeyPublicId = $input->getArgument('yubikey'); |
||
86 | |||
87 | $connection->beginTransaction(); |
||
88 | |||
89 | try { |
||
90 | $command = $pipeline->process($command); |
||
91 | $eventBus->flush(); |
||
92 | |||
93 | $connection->commit(); |
||
94 | } catch (Exception $e) { |
||
95 | $output->writeln(sprintf( |
||
96 | '<error>An Error occurred when trying to bootstrap the identity: "%s"</error>', |
||
97 | $e->getMessage() |
||
98 | )); |
||
99 | |||
100 | $connection->rollBack(); |
||
101 | |||
102 | throw $e; |
||
103 | } |
||
104 | |||
105 | $output->writeln(sprintf( |
||
106 | '<info>Successfully created identity with UUID %s and second factor with UUID %s</info>', |
||
107 | $command->identityId, |
||
108 | $command->secondFactorId |
||
109 | )); |
||
110 | } |
||
111 | } |
||
112 |
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: