| Conditions | 18 |
| Paths | 130 |
| Total Lines | 101 |
| 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 // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase |
||
| 66 | protected function execute( InputInterface $input, OutputInterface $output ) { |
||
| 67 | try { |
||
| 68 | $formatter = Config::formatterPlugin(); |
||
| 69 | $formatter->setIO( $input, $output ); |
||
| 70 | $versioning = Config::versioningPlugin(); |
||
| 71 | $versioning->setIO( $input, $output ); |
||
| 72 | } catch ( \Exception $ex ) { |
||
| 73 | $output->writeln( "<error>{$ex->getMessage()}</>" ); |
||
| 74 | return 1; |
||
| 75 | } |
||
| 76 | |||
| 77 | $which = (string) $input->getArgument( 'which' ); |
||
| 78 | $l = '' === $which ? 1 : strlen( $which ); |
||
| 79 | $ok = false; |
||
| 80 | foreach ( array( 'previous', 'current', 'next' ) as $w ) { |
||
| 81 | if ( substr( $w, 0, $l ) === $which ) { |
||
| 82 | $which = $w; |
||
| 83 | $ok = true; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | if ( ! $ok ) { |
||
| 87 | $output->writeln( "<error>Don't know how to fetch the \"$which\" version</>" ); |
||
| 88 | return 1; |
||
| 89 | } |
||
| 90 | |||
| 91 | if ( 'next' === $which && $input->getOption( 'use-version' ) !== null ) { |
||
| 92 | $versions = array( $input->getOption( 'use-version' ) ); |
||
| 93 | } else { |
||
| 94 | $file = Config::changelogFile(); |
||
| 95 | if ( ! file_exists( $file ) ) { |
||
| 96 | $output->writeln( "<error>Changelog file $file does not exist</>" ); |
||
| 97 | return 1; |
||
| 98 | } |
||
| 99 | |||
| 100 | Utils::error_clear_last(); |
||
| 101 | $contents = quietCall( 'file_get_contents', $file ); |
||
| 102 | // @codeCoverageIgnoreStart |
||
| 103 | if ( ! is_string( $contents ) ) { |
||
| 104 | $err = error_get_last(); |
||
| 105 | $output->writeln( "<error>Failed to read $file: {$err['message']}</>" ); |
||
| 106 | return 1; |
||
| 107 | } |
||
| 108 | // @codeCoverageIgnoreEnd |
||
| 109 | |||
| 110 | try { |
||
| 111 | $versions = $formatter->parse( $contents )->getVersions(); |
||
| 112 | } catch ( \Exception $ex ) { |
||
| 113 | $output->writeln( "<error>Failed to parse changelog: {$ex->getMessage()}</>" ); |
||
| 114 | return 1; |
||
| 115 | } |
||
| 116 | |||
| 117 | if ( count( $versions ) === 0 ) { |
||
| 118 | $output->writeln( '<error>Changelog file contains no entries</>' ); |
||
| 119 | return 1; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | if ( 'previous' === $which ) { |
||
| 124 | if ( count( $versions ) < 2 ) { |
||
| 125 | $output->writeln( '<error>Changelog file contains no previous version</>' ); |
||
| 126 | return 1; |
||
| 127 | } |
||
| 128 | $output->writeln( $versions[1], OutputInterface::VERBOSITY_QUIET ); |
||
| 129 | return 0; |
||
| 130 | } |
||
| 131 | if ( 'current' === $which ) { |
||
| 132 | $output->writeln( $versions[0], OutputInterface::VERBOSITY_QUIET ); |
||
| 133 | return 0; |
||
| 134 | } |
||
| 135 | |||
| 136 | if ( $input->getOption( 'use-significance' ) ) { |
||
| 137 | try { |
||
| 138 | $changes = array( |
||
| 139 | $formatter->newChangeEntry( |
||
| 140 | array( |
||
| 141 | 'significance' => $input->getOption( 'use-significance' ), |
||
| 142 | 'content' => 'Dummy', |
||
| 143 | ) |
||
| 144 | ), |
||
| 145 | ); |
||
| 146 | } catch ( \Exception $ex ) { |
||
| 147 | $output->writeln( "<error>{$ex->getMessage()}</>" ); |
||
| 148 | return 1; |
||
| 149 | } |
||
| 150 | } else { |
||
| 151 | $changes = Utils::loadAllChanges( Config::changesDir(), Config::types(), $formatter, $output ); |
||
| 152 | } |
||
| 153 | $extra = array_filter( |
||
| 154 | array( |
||
| 155 | 'prerelease' => $input->getOption( 'prerelease' ), |
||
| 156 | 'buildinfo' => $input->getOption( 'buildinfo' ), |
||
| 157 | ) |
||
| 158 | ); |
||
| 159 | try { |
||
| 160 | $output->writeln( $versioning->nextVersion( $versions[0], $changes, $extra ), OutputInterface::VERBOSITY_QUIET ); |
||
| 161 | return 0; |
||
| 162 | } catch ( \Exception $ex ) { |
||
| 163 | $output->writeln( "<error>{$ex->getMessage()}</>" ); |
||
| 164 | return 1; |
||
| 165 | } |
||
| 166 | } |
||
| 167 | } |
||
| 168 |