| Conditions | 18 |
| Paths | 352 |
| Total Lines | 117 |
| 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 |
||
| 118 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 119 | { |
||
| 120 | // We don't load repo services or config resolver before execute() to avoid loading before SiteAccess is set. |
||
| 121 | $keep = $input->getOption('keep'); |
||
| 122 | if ($keep === 'config_default') { |
||
| 123 | $config = $this->repositoryConfigurationProvider->getRepositoryConfig(); |
||
| 124 | $keep = $config['options']['default_version_archive_limit']; |
||
| 125 | } |
||
| 126 | |||
| 127 | if (($keep = (int) $keep) < 0) { |
||
| 128 | throw new InvalidArgumentException( |
||
| 129 | 'keep', |
||
| 130 | 'Keep value can not be negative.' |
||
| 131 | ); |
||
| 132 | } |
||
| 133 | |||
| 134 | $userService = $this->repository->getUserService(); |
||
| 135 | $contentService = $this->repository->getContentService(); |
||
| 136 | $permissionResolver = $this->repository->getPermissionResolver(); |
||
| 137 | |||
| 138 | $permissionResolver->setCurrentUserReference( |
||
| 139 | $userService->loadUserByLogin($input->getOption('user')) |
||
| 140 | ); |
||
| 141 | |||
| 142 | $status = $input->getOption('status'); |
||
| 143 | |||
| 144 | $excludedContentTypeIdentifiers = explode(',', $input->getOption('excluded-content-types')); |
||
| 145 | $contentIds = $this->getObjectsIds($keep, $status, $excludedContentTypeIdentifiers); |
||
| 146 | $contentIdsCount = count($contentIds); |
||
| 147 | |||
| 148 | if ($contentIdsCount === 0) { |
||
| 149 | $output->writeln('<info>There is no Content matching given criteria.</info>'); |
||
| 150 | |||
| 151 | return; |
||
| 152 | } |
||
| 153 | |||
| 154 | $output->writeln(sprintf( |
||
| 155 | '<info>Found %d Content IDs matching given criteria.</info>', |
||
| 156 | $contentIdsCount |
||
| 157 | )); |
||
| 158 | |||
| 159 | $displayProgressBar = !($output->isVerbose() || $output->isVeryVerbose() || $output->isDebug()); |
||
| 160 | |||
| 161 | if ($displayProgressBar) { |
||
| 162 | $progressBar = new ProgressBar($output, $contentIdsCount); |
||
| 163 | $progressBar->setFormat( |
||
| 164 | '%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%' . PHP_EOL |
||
| 165 | ); |
||
| 166 | $progressBar->start(); |
||
| 167 | } |
||
| 168 | |||
| 169 | $removedVersionsCounter = 0; |
||
| 170 | |||
| 171 | $removeAll = $status === self::VERSION_ALL; |
||
| 172 | $removeDrafts = $status === self::VERSION_DRAFT; |
||
| 173 | $removeArchived = $status === self::VERSION_ARCHIVED; |
||
| 174 | |||
| 175 | foreach ($contentIds as $contentId) { |
||
| 176 | try { |
||
| 177 | $contentInfo = $contentService->loadContentInfo((int) $contentId); |
||
| 178 | $versions = $contentService->loadVersions($contentInfo); |
||
| 179 | $versionsCount = count($versions); |
||
| 180 | |||
| 181 | $output->writeln(sprintf( |
||
| 182 | '<info>Content %d has %d version(s)</info>', |
||
| 183 | (int) $contentId, |
||
| 184 | $versionsCount |
||
| 185 | ), OutputInterface::VERBOSITY_VERBOSE); |
||
| 186 | |||
| 187 | $versions = array_filter($versions, function ($version) use ($removeAll, $removeDrafts, $removeArchived) { |
||
| 188 | if ( |
||
| 189 | ($removeAll && $version->status !== VersionInfo::STATUS_PUBLISHED) || |
||
| 190 | ($removeDrafts && $version->status === VersionInfo::STATUS_DRAFT) || |
||
| 191 | ($removeArchived && $version->status === VersionInfo::STATUS_ARCHIVED) |
||
| 192 | ) { |
||
| 193 | return true; |
||
| 194 | } |
||
| 195 | }); |
||
| 196 | |||
| 197 | if ($keep > 0) { |
||
| 198 | $versions = array_slice($versions, 0, -$keep); |
||
| 199 | } |
||
| 200 | |||
| 201 | $output->writeln(sprintf( |
||
| 202 | "Found %d content's (%d) version(s) to remove.", |
||
| 203 | count($versions), |
||
| 204 | (int) $contentId |
||
| 205 | ), OutputInterface::VERBOSITY_VERBOSE); |
||
| 206 | |||
| 207 | /** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo $version */ |
||
| 208 | foreach ($versions as $version) { |
||
| 209 | $contentService->deleteVersion($version); |
||
| 210 | ++$removedVersionsCounter; |
||
| 211 | $output->writeln(sprintf( |
||
| 212 | "Content's (%d) version (%d) has been deleted.", |
||
| 213 | $contentInfo->id, |
||
| 214 | $version->id |
||
| 215 | ), OutputInterface::VERBOSITY_VERBOSE); |
||
| 216 | } |
||
| 217 | |||
| 218 | if ($displayProgressBar) { |
||
| 219 | $progressBar->advance(1); |
||
|
|
|||
| 220 | } |
||
| 221 | } catch (Exception $e) { |
||
| 222 | $output->writeln(sprintf( |
||
| 223 | '<error>%s</error>', |
||
| 224 | $e->getMessage() |
||
| 225 | )); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | $output->writeln(sprintf( |
||
| 230 | '<info>Removed %d unwanted contents version(s) from %d content(s).</info>', |
||
| 231 | $removedVersionsCounter, |
||
| 232 | $contentIdsCount |
||
| 233 | )); |
||
| 234 | } |
||
| 235 | |||
| 302 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: