Completed
Push — fix/parent-selector-for-premiu... ( cac9af...425486 )
by Jeremy
20:54 queued 09:58
created

Application::doRun()   B

Complexity

Conditions 10
Paths 32

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 32
nop 2
dl 0
loc 42
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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:

1
<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
2
/**
3
 * User interface for the changelogger tool.
4
 *
5
 * @package automattic/jetpack-changelogger
6
 */
7
8
namespace Automattic\Jetpack\Changelogger;
9
10
use Symfony\Component\Console\Application as SymfonyApplication;
11
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Question\ConfirmationQuestion;
15
16
/**
17
 * User interface for the changelogger tool.
18
 */
19
class Application extends SymfonyApplication {
20
21
	const VERSION = '1.2.0-alpha';
22
23
	/**
24
	 * Constructor.
25
	 */
26
	public function __construct() {
27
		parent::__construct( 'Jetpack Changelogger', self::VERSION );
28
		$this->setCommandLoader( new CommandLoader() );
29
	}
30
31
	/**
32
	 * Called when the application is run.
33
	 *
34
	 * @param InputInterface  $input InputInterface.
35
	 * @param OutputInterface $output OutputInterface.
36
	 * @return int
37
	 */
38
	public function doRun( InputInterface $input, OutputInterface $output ) {
39
		$output->getFormatter()->setStyle( 'warning', new OutputFormatterStyle( 'black', 'yellow' ) );
40
		$errout = is_callable( array( $output, 'getErrorOutput' ) ) ? $output->getErrorOutput() : $output;
41
42
		// Try to find a composer.json, if COMPOSER isn't set.
43
		if ( ! getenv( 'COMPOSER' ) ) {
44
			$dir = getcwd();
45
			$ok  = false;
46
			do {
47
				$composer = $dir . DIRECTORY_SEPARATOR . 'composer.json';
48
				if ( file_exists( $composer ) ) {
49
					$ok = true;
50
					break;
51
				}
52
53
				$prev = $dir;
54
				$dir  = dirname( $dir );
55
			} while ( $prev !== $dir );
56
57
			// If we found one in a parent directory, ask if it should be used.
58
			if ( getcwd() !== $dir ) {
59
				if ( ! $ok || ! $input->isInteractive() ) {
60
					$errout->writeln( '<error>File composer.json is not found in ' . getcwd() . '.</>' );
61
					$errout->writeln( '<error>Run changelogger from the appropriate directory, or set the environment variable COMPOSER to point to composer.json.</>' );
62
					return -1;
63
				}
64
65
				$question = new ConfirmationQuestion( "<info>No composer.json in current directory, do you want to use the one at $composer? [Y/n]</> ", true );
66
				if ( ! $this->getHelperSet()->get( 'question' )->ask( $input, $output, $question ) ) {
67
					return -1;
68
				}
69
			}
70
			Config::setComposerJsonPath( $composer );
71
		}
72
73
		try {
74
			return parent::doRun( $input, $output );
75
		} catch ( ConfigException $ex ) {
76
			$errout->writeln( "<error>{$ex->getMessage()}</>" );
77
			return -1;
78
		}
79
	}
80
81
}
82