Conditions | 13 |
Paths | 21 |
Total Lines | 86 |
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 |
||
127 | protected function execute(InputInterface $input, OutputInterface $output) |
||
128 | { |
||
129 | if (($keep = (int) $input->getOption('keep')) < 0) { |
||
130 | throw new InvalidArgumentException( |
||
131 | 'status', |
||
132 | 'Keep value can not be negative.' |
||
133 | ); |
||
134 | } |
||
135 | |||
136 | $status = $input->getOption('status'); |
||
137 | |||
138 | $contentIds = $this->getObjectsIds($keep, $status); |
||
139 | $contentIdsCount = count($contentIds); |
||
140 | |||
141 | if ($contentIdsCount === 0) { |
||
142 | $output->writeln('<info>There is no Content matching given criteria.</info>'); |
||
143 | |||
144 | return; |
||
145 | } |
||
146 | |||
147 | $output->writeln(sprintf( |
||
148 | '<info>Found %d Content IDs matching given criteria.</info>', |
||
149 | $contentIdsCount |
||
150 | )); |
||
151 | |||
152 | $removedVersionsCounter = 0; |
||
153 | |||
154 | $removeAll = $status === self::VERSION_ALL; |
||
155 | $removeDrafts = $status === self::VERSION_DRAFT; |
||
156 | $removeArchived = $status === self::VERSION_ARCHIVED; |
||
157 | |||
158 | foreach ($contentIds as $contentId) { |
||
159 | try { |
||
160 | $contentInfo = $this->contentService->loadContentInfo((int) $contentId); |
||
161 | $versions = $this->contentService->loadVersions($contentInfo); |
||
162 | $versionsCount = count($versions); |
||
163 | |||
164 | $output->writeln(sprintf( |
||
165 | '<info>Content %d has %d version(s)</info>', |
||
166 | (int) $contentId, |
||
167 | $versionsCount |
||
168 | ), Output::VERBOSITY_VERBOSE); |
||
169 | |||
170 | $versions = array_filter($versions, function ($version) use ($removeAll, $removeDrafts, $removeArchived) { |
||
171 | if ( |
||
172 | ($removeAll && $version->status !== VersionInfo::STATUS_PUBLISHED) || |
||
173 | ($removeDrafts && $version->status === VersionInfo::STATUS_DRAFT) || |
||
174 | ($removeArchived && $version->status === VersionInfo::STATUS_ARCHIVED) |
||
175 | ) { |
||
176 | return $version; |
||
177 | } |
||
178 | }); |
||
179 | |||
180 | if ($keep > 0) { |
||
181 | $versions = array_slice($versions, 0, -$keep); |
||
182 | } |
||
183 | |||
184 | $output->writeln(sprintf( |
||
185 | "Found %d content's (%d) version(s) to remove.", |
||
186 | count($versions), |
||
187 | (int) $contentId |
||
188 | ), Output::VERBOSITY_VERBOSE); |
||
189 | |||
190 | /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $version */ |
||
191 | foreach ($versions as $version) { |
||
192 | $this->contentService->deleteVersion($version); |
||
193 | ++$removedVersionsCounter; |
||
194 | $output->writeln(sprintf( |
||
195 | "Content's (%d) version (%d) has been deleted.", |
||
196 | $contentInfo->id, |
||
197 | $version->id |
||
198 | ), Output::VERBOSITY_VERBOSE); |
||
199 | } |
||
200 | } catch (Exception $e) { |
||
201 | $output->writeln(sprintf( |
||
202 | '<error>%s</error>', |
||
203 | $e->getMessage() |
||
204 | )); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | $output->writeln(sprintf( |
||
209 | '<info>Removed %d unwanted contents version(s).</info>', |
||
210 | $removedVersionsCounter |
||
211 | )); |
||
212 | } |
||
213 | |||
267 |
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 implementation 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 interface: