Conditions | 13 |
Paths | 46 |
Total Lines | 113 |
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 |
||
85 | protected function execute(InputInterface $input, OutputInterface $output) |
||
86 | { |
||
87 | // $passwd = $this->promptSilent($output); |
||
88 | // $output->writeln('fertig für heute:'.$passwd.'#'); |
||
89 | |||
90 | // get the ticket |
||
91 | $ticket = $input->getArgument('ticket'); |
||
92 | |||
93 | // we might need framing dates |
||
94 | $toDate = null; |
||
95 | $fromDate = null; |
||
96 | |||
97 | // there might be a limit |
||
98 | $limit = null; |
||
99 | |||
100 | // prepare all the input options |
||
101 | $this->prepareInputParams($input, $ticket, $fromDate, $toDate, $limit); |
||
|
|||
102 | |||
103 | // filter by ticket if given |
||
104 | /** @var \Wicked\Timely\Storage\StorageInterface $storage */ |
||
105 | $storage = StorageFactory::getStorage(); |
||
106 | $bookings = $storage->retrieve($ticket, $toDate, $fromDate, $limit); |
||
107 | |||
108 | // return if we did not find any bookings |
||
109 | if (empty($bookings)) { |
||
110 | $output->write('No bookings found, nothing to push ...', true); |
||
111 | return; |
||
112 | } |
||
113 | |||
114 | // get tasks from bookings |
||
115 | $tasks = TaskFactory::getTasksFromBookings($bookings); |
||
116 | |||
117 | // retrieve configuration |
||
118 | $configuration = new DotEnvConfiguration(); |
||
119 | $password = $configuration->getJiraPassword(); |
||
120 | if (empty($password) || strtolower($password) === self::KEYCHAIN_NAME) { |
||
121 | $password = $this->getPasswordFromKeychain($output, $configuration); |
||
122 | if (empty($password)) { |
||
123 | return; |
||
124 | } |
||
125 | $configuration->setJiraPassword($password); |
||
126 | |||
127 | } |
||
128 | unset($password); |
||
129 | $bookingsPushed = array(); |
||
130 | // get our issue service and push the tasks |
||
131 | $issueService = new IssueService($configuration); |
||
132 | foreach ($tasks as $task) { |
||
133 | // Allready pushed to jira? take next one |
||
134 | if ($task->isPushed()) { |
||
135 | continue; |
||
136 | } |
||
137 | // create a worklog from the task |
||
138 | $workLog = new Worklog(); |
||
139 | $workLog->setComment($task->getComment()) |
||
140 | ->setStarted($task->getStartTime()) |
||
141 | ->setTimeSpent(Date::secondsToUnits(Date::roundByInterval($task->getDuration(), 900))); |
||
142 | |||
143 | // check the sanity of our worklog and discard it if there is something missing |
||
144 | if (!$task->getTicketId() || empty($workLog->timeSpent) || empty($workLog->comment)) { |
||
145 | $output->writeln('Not pushing one worklog as it misses vital information'); |
||
146 | continue; |
||
147 | } |
||
148 | |||
149 | // log the worklog about to being pushed if output is verbose |
||
150 | if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) { |
||
151 | $output->writeln( |
||
152 | sprintf( |
||
153 | '%s : %s > %s', |
||
154 | $task->getTicketId(), |
||
155 | $workLog->timeSpent, |
||
156 | $workLog->comment |
||
157 | ) |
||
158 | ); |
||
159 | } |
||
160 | |||
161 | try { |
||
162 | // push to remote |
||
163 | $issueService->addWorklog($task->getTicketId(), $workLog); |
||
164 | |||
165 | $output->writeln( |
||
166 | sprintf( |
||
167 | 'PUSHED %s : %s > %s', |
||
168 | $task->getTicketId(), |
||
169 | $workLog->timeSpent, |
||
170 | $workLog->comment |
||
171 | ) |
||
172 | ); |
||
173 | |||
174 | $bookingsPushed[] = $task->getStartBooking(); |
||
175 | |||
176 | } catch (JiraException $e) { |
||
177 | $output->write( |
||
178 | sprintf( |
||
179 | 'Error while pushing. Status %s, with message: "%s"', |
||
180 | $e->getCode(), |
||
181 | $e->getMessage() |
||
182 | ) |
||
183 | ); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | foreach ($bookingsPushed as $booking) { |
||
188 | $storage->storePush($booking); |
||
189 | $formatter = FormatterFactory::getFormatter(); |
||
190 | $bookString = $formatter->toString($booking); |
||
191 | $output->write($bookString, true); |
||
192 | } |
||
193 | |||
194 | // write output |
||
195 | $output->write(sprintf('Successfully pushed %s tasks.', count($bookingsPushed)), true); |
||
196 | |||
197 | } |
||
198 | |||
250 |
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.