Completed
Push — master ( dd800d...af5658 )
by Thomas
07:26
created

InitUI::show()   F

Complexity

Conditions 24
Paths 1536

Size

Total Lines 119
Code Lines 81

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 119
rs 2
cc 24
eloc 81
nc 1536
nop 0

How to fix   Long Method    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
2
namespace keeko\tools\ui;
3
4
use keeko\framework\schema\PackageSchema;
5
use keeko\tools\helpers\InitCommandHelperTrait;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Question\Question;
8
9
class InitUI extends AbstractUI {
10
	
11
	use InitCommandHelperTrait;
12
	
13
	/** @var PackageSchema */
14
	private $package;
0 ignored issues
show
Unused Code introduced by
The property $package is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
15
16
	
17
	public function show() {
0 ignored issues
show
Coding Style introduced by
show uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
18
		$input = $this->io->getInput();
19
		$output = $this->io->getOutput();
20
		$force = $input->getOption('force');
21
		$formatter = $this->command->getHelperSet()->get('formatter');
22
		$output->writeln([
23
			'',
24
			$formatter->formatBlock('Welcome to the Keeko initializer', 'bg=blue;fg=white', true),
25
			''
26
		]);
27
		$output->writeln([
28
			'',
29
			'This command will guide you through creating your Keeko composer package.',
30
			'',
31
		]);
32
		
33
		$package = $this->getService()->getPackageService()->getPackage();
34
		
35
		$name = $this->getPackageName();
36
		$askName = $name === null;
37
		if ($name === null) {
38
			$git = $this->getGitConfig();
39
			$cwd = realpath(".");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal . does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
40
			$name = basename($cwd);
41
			$name = preg_replace('{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}', '\\1\\3-\\2\\4', $name);
42
			$name = strtolower($name);
43
			$localName = $package->getFullName();
44
			if (!empty($localName)) {
45
				$name = $package->getFullName();
46
			} else if (isset($git['github.user'])) {
47
				$name = $git['github.user'] . '/' . $name;
48
			} elseif (!empty($_SERVER['USERNAME'])) {
49
				$name = $_SERVER['USERNAME'] . '/' . $name;
50
			} elseif (get_current_user()) {
51
				$name = get_current_user() . '/' . $name;
52
			} else {
53
				// package names must be in the format foo/bar
54
				$name = $name . '/' . $name;
55
			}
56
		} else {
57
			$this->validateName($name);
58
		}
59
		
60
		// asking for the name
61
		if ($askName || $force) {
62
			$name = $this->askQuestion(new Question('Package name (<vendor>/<name>)', $name));
63
			$this->validateName($name);
64
			$input->setOption('name', $name);
65
			$package->setFullName($name);
66
		}
67
		
68
		// asking for a description
69
		$desc = $this->getPackageDescription();
70
		if ($desc === null || $force) {
71
			$desc = $this->askQuestion(new Question('Description', $desc));
72
			$input->setOption('description', $desc);
73
		}
74
		
75
		// asking for the author
76
		if ($package->getAuthors()->isEmpty() || $force) {
77
			$author = $input->getOption('author');
78
			if ($author === null && isset($git['user.name'])) {
79
				$author = $git['user.name'];
80
		
81
				if (isset($git['user.email'])) {
82
					$author = sprintf('%s <%s>', $git['user.name'], $git['user.email']);
83
				}
84
			}
85
		
86
			$author = $this->askQuestion(new Question('Author', $author));
87
			$input->setOption('author', $author);
88
		}
89
		
90
		// asking for the package type
91
		$type = $this->getPackageType();
92
		if ($type === null || $force) {
93
			$types = ['module', 'app'];
94
			$question = new Question('Package type (module|app)', $type);
95
			$question->setAutocompleterValues($types);
96
			$question->setValidator(function($answer) use ($types) {
97
				if (!in_array($answer, $types)) {
98
					throw new \RuntimeException('The name of the type should be one of: ' .
99
						implode(',', $types));
100
				}
101
				return $answer;
102
			});
103
			$question->setMaxAttempts(2);
104
			$type = $this->askQuestion($question);
105
		}
106
		$input->setOption('type', $type);
107
		
108
		// asking for the license
109
		$license = $this->getPackageLicense();
110
		if ($license === null || $force) {
111
			$license = $this->askQuestion(new Question('License', $license));
112
			$input->setOption('license', $license);
113
		}
114
115
		// KEEKO values
116
		$output->writeln([
117
			'',
118
			'Information for Keeko ' . ucfirst($type),
119
			''
120
		]);
121
		
122
		// ask for the title
123
		$title = $this->getPackageTitle();
124
		if ($title === null || $force) {
125
			$title = $this->askQuestion(new Question('Title', $title));
126
			$input->setOption('title', $title);
127
		}
128
		
129
		// ask for the class
130
		$classname = $this->getPackageClass();
131
		if ($classname === null || $force) {
132
			$classname = $this->askQuestion(new Question('Class', $classname));
133
			$input->setOption('classname', $classname);
134
		}
135
	}
136
}