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