Conditions | 8 |
Paths | 31 |
Total Lines | 89 |
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 |
||
58 | protected function execute(InputInterface $input, OutputInterface $output) |
||
59 | { |
||
60 | $registrationStatus = $input->getArgument('registration-status'); |
||
61 | $this->validRegistrationStatus($registrationStatus); |
||
|
|||
62 | |||
63 | $this->tokenStorage->setToken( |
||
64 | new AnonymousToken('cli.bootstrap-gssp-token', 'cli', ['ROLE_SS', 'ROLE_RA']) |
||
65 | ); |
||
66 | $nameId = new NameId($input->getArgument('name-id')); |
||
67 | $institutionText = $input->getArgument('institution'); |
||
68 | $institution = new Institution($institutionText); |
||
69 | $mailVerificationRequired = $this->requiresMailVerification($institutionText); |
||
70 | $tokenType = $input->getArgument('gssp-token-type'); |
||
71 | $tokenIdentifier = $input->getArgument('gssp-token-identifier'); |
||
72 | $actorId = $input->getArgument('actor-id'); |
||
73 | $this->enrichEventMetadata($actorId); |
||
74 | if (!$this->tokenBootstrapService->hasIdentityWithNameIdAndInstitution($nameId, $institution)) { |
||
75 | $output->writeln( |
||
76 | sprintf( |
||
77 | '<error>An identity with name ID "%s" from institution "%s" does not exist, create it first.</error>', |
||
78 | $nameId->getNameId(), |
||
79 | $institution->getInstitution() |
||
80 | ) |
||
81 | ); |
||
82 | |||
83 | return; |
||
84 | } |
||
85 | $identity = $this->tokenBootstrapService->findOneByNameIdAndInstitution($nameId, $institution); |
||
86 | $output->writeln(sprintf('<comment>Adding a %s %s GSSP token for %s</comment>', $registrationStatus, $tokenType, $identity->commonName)); |
||
87 | $this->beginTransaction(); |
||
88 | $secondFactorId = Uuid::uuid4()->toString(); |
||
89 | |||
90 | try { |
||
91 | switch ($registrationStatus) { |
||
92 | case "unverified": |
||
93 | $output->writeln(sprintf('<comment>Creating an unverified %s token</comment>', $tokenType)); |
||
94 | $this->provePossession($secondFactorId, $identity, $tokenType, $tokenIdentifier); |
||
95 | break; |
||
96 | case "verified": |
||
97 | $output->writeln(sprintf('<comment>Creating an unverified %s token</comment>', $tokenType)); |
||
98 | $this->provePossession($secondFactorId, $identity, $tokenType, $tokenIdentifier); |
||
99 | $unverifiedSecondFactor = $this->tokenBootstrapService->findUnverifiedToken($identity->id, $tokenType); |
||
100 | if ($mailVerificationRequired) { |
||
101 | $output->writeln(sprintf('<comment>Creating an verified %s token</comment>', $tokenType)); |
||
102 | $this->verifyEmail($identity, $unverifiedSecondFactor); |
||
103 | } |
||
104 | break; |
||
105 | case "vetted": |
||
106 | $output->writeln(sprintf('<comment>Creating an unverified %s token</comment>', $tokenType)); |
||
107 | $this->provePossession($secondFactorId, $identity, $tokenType, $tokenIdentifier); |
||
108 | /** @var UnverifiedSecondFactor $unverifiedSecondFactor */ |
||
109 | $unverifiedSecondFactor = $this->tokenBootstrapService->findUnverifiedToken($identity->id, $tokenType); |
||
110 | if ($mailVerificationRequired) { |
||
111 | $output->writeln(sprintf('<comment>Creating an verified %s token</comment>', $tokenType)); |
||
112 | $this->verifyEmail($identity, $unverifiedSecondFactor); |
||
113 | } |
||
114 | $verifiedSecondFactor = $this->tokenBootstrapService->findVerifiedToken($identity->id, $tokenType); |
||
115 | $output->writeln(sprintf('<comment>Vetting the verified %s token</comment>', $tokenType)); |
||
116 | $this->vetSecondFactor( |
||
117 | $tokenType, |
||
118 | $actorId, |
||
119 | $identity, |
||
120 | $secondFactorId, |
||
121 | $verifiedSecondFactor, |
||
122 | $tokenIdentifier |
||
123 | ); |
||
124 | break; |
||
125 | } |
||
126 | $this->finishTransaction(); |
||
127 | } catch (Exception $e) { |
||
128 | $output->writeln( |
||
129 | sprintf( |
||
130 | '<error>An Error occurred when trying to bootstrap the %s token: "%s"</error>', |
||
131 | $tokenType, |
||
132 | $e->getMessage() |
||
133 | ) |
||
134 | ); |
||
135 | $this->rollback(); |
||
136 | throw $e; |
||
137 | } |
||
138 | $output->writeln( |
||
139 | sprintf( |
||
140 | '<info>Successfully %s %s second factor with UUID %s</info>', |
||
141 | $registrationStatus, |
||
142 | $tokenType, |
||
143 | $secondFactorId |
||
144 | ) |
||
145 | ); |
||
146 | } |
||
147 | |||
159 |
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.