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