Completed
Push — master ( cac1be...eadaa3 )
by Thomas
04:35
created

GenerateResponseCommand::generateResponse()   C

Complexity

Conditions 14
Paths 81

Size

Total Lines 63
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 14

Importance

Changes 7
Bugs 0 Features 0
Metric Value
c 7
b 0
f 0
dl 0
loc 63
ccs 41
cts 41
cp 1
rs 6.0952
cc 14
eloc 32
nc 81
nop 1
crap 14

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\command;
3
4
use gossi\codegen\model\PhpClass;
5
use keeko\tools\generator\BlankHtmlResponseGenerator;
6
use keeko\tools\generator\BlankJsonResponseGenerator;
7
use keeko\tools\generator\GeneratorFactory;
8
use keeko\tools\generator\ModelResponseTraitGenerator;
9
use keeko\tools\generator\TwigHtmlResponseGenerator;
10
use keeko\tools\helpers\QuestionHelperTrait;
11
use Symfony\Component\Console\Input\InputArgument;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Question\ConfirmationQuestion;
16
use Symfony\Component\Console\Question\Question;
17
18
class GenerateResponseCommand extends AbstractGenerateCommand {
19
20
	use QuestionHelperTrait;
21
	
22
	protected $traits = [];
23
	
24 20
	protected function configure() {
25 20
		$this
26 20
			->setName('generate:response')
27 20
			->setDescription('Generates code for a response')
28 20
			->addArgument(
29 20
				'name',
30 20
				InputArgument::OPTIONAL,
31
				'The name of the action, which should be generated. Typically in the form %nomen%-%verb% (e.g. user-create)'
32 20
			)
33 20
			->addOption(
34 20
				'format',
35 20
				'',
36 20
				InputOption::VALUE_OPTIONAL,
37 20
				'The response format to create',
38
				'json'
39 20
			)
40 20
			->addOption(
41 20
				'template',
42 20
				'',
43 20
				InputOption::VALUE_OPTIONAL,
44 20
				'The template for the body method (blank or twig)',
45 1
				'blank'
46 20
			)
47
		;
48
		
49 20
		$this->configureGenerateOptions();
50
51 20
		parent::configure();
52 20
	}
53
54
	/**
55
	 * Checks whether actions can be generated at all by reading composer.json and verify
56
	 * all required information are available
57
	 */
58 7
	private function preCheck() {
59 7
		$module = $this->packageService->getModule();
60 7
		if ($module === null || count($module->getActionNames()) == 0) {
61 1
			throw new \DomainException('No action definition found in composer.json - please run `keeko generate:action`.');
62
		}
63 6
	}
64
65
	protected function interact(InputInterface $input, OutputInterface $output) {
66
		$this->preCheck();
67
		
68
		// check if the dialog can be skipped
69
		$name = $input->getArgument('name');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
70
		$specificAction = false;
71
		
72
		if ($name === null) {
73
			$formatQuestion = new ConfirmationQuestion('Do you want to generate a response for a specific action?');
74
			$specificAction = $this->askConfirmation($formatQuestion);
75
		}
76
		
77
		// ask which action
78
		if ($specificAction) {
79
			$names = [];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
80
			$actions = $this->getKeekoActions();
0 ignored issues
show
Bug introduced by
The method getKeekoActions() does not seem to exist on object<keeko\tools\comma...enerateResponseCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
81
			foreach (array_keys($actions) as $name) {
82
				$names[] = $name;
83
			}
84
			
85
			$formatQuestion = new Question('Which action');
86
			$formatQuestion->setAutocompleterValues($names);
87
			$name = $this->askQuestion($formatQuestion);
88
			$input->setArgument('name', $name);
89
		} 
90
		
91
		
92
		// ask which format
93
		$formatQuestion = new Question('Which format', 'json');
94
		$formatQuestion->setAutocompleterValues(['json', 'html']);
95
		$format = $this->askQuestion($formatQuestion);
96
		$input->setOption('format', $format);
97
		
98
		// ask which template
99
		$templateQuestion = new Question('Which template', 'blank');
100
		$templateQuestion->setAutocompleterValues(['blank', 'twig']);
101
		$template = $this->askQuestion($templateQuestion);
102
		$input->setOption('template', $template);
103
	}
104
105 7
	protected function execute(InputInterface $input, OutputInterface $output) {
106 7
		$this->preCheck();
107
		
108 6
		$name = $input->getArgument('name');
109
110
		// only a specific action
111 6
		if ($name) {
112 5
			$this->generateResponse($name);
113 4
		}
114
		
115
		// anyway all actions
116
		else {
117 1
			$actions = $this->packageService->getModule()->getActionNames();
118
			
119 1
			foreach ($actions as $name) {
120 1
				$this->generateResponse($name);
121 1
			}
122
		}
123
		
124 5
		$this->packageService->savePackage();
125 5
	}
126
	
127 6
	private function generateResponse($actionName) {
128 6
		$module = $this->packageService->getModule();
129
		
130 6
		if (!$module->hasAction($actionName)) {
131 1
			throw new \RuntimeException(sprintf('action (%s) not found', $actionName));
132
		}
133
		
134 5
		$input = $this->io->getInput();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
135 5
		$action = $module->getAction($actionName);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
136 5
		$modelName = $this->modelService->getModelNameByAction($action);
137 5
		$format = $input->getOption('format');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
138 5
		$template = $input->getOption('template');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
139
140 5
		if (!$action->hasResponse($format)) {
141 5
			$action->setResponse($format, str_replace(['Action', 'action'], [ucwords($format) . 'Response', 'response'], $action->getClass()));
142 5
		}
143
144
		// find generator
145 5
		$generator = null;
146 5
		$type = $this->packageService->getActionType($actionName, $modelName);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
147 5
		$isModel = $type && $this->modelService->isModelAction($action); 
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
148
149
		// model given and format is json
150 5
		if ($isModel && $format === 'json') {
151 2
			$generator = GeneratorFactory::createJsonResponseGenerator($type, $this->service);
152 2
		}
153
		
154
		// blank json
155 4
		else if ($format === 'json') {
156 2
			$generator = new BlankJsonResponseGenerator($this->service);
157 2
		}
158
		
159
		// html + twig
160 2
		else if ($format === 'html' && $template == 'twig') {
161 1
			$generator = new TwigHtmlResponseGenerator($this->service);
162 1
		}
163
		
164
		// blank html as default
165 1
		else if ($format == 'html') {
166 1
			$generator = new BlankHtmlResponseGenerator($this->service);
167 1
		}
168
		
169
		// run generation, if generator was chosen
170 5
		if ($generator !== null) {
171
			/* @var $class PhpClass */
172 5
			$class = $generator->generate($action);
173
			
174
			// generate json trait
175 5
			if ($isModel && $format === 'json') {
176 2
				$generator = new ModelResponseTraitGenerator($this->service);
177 2
				$trait = $generator->generate($action);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
178 2
				$class->addTrait($trait);
0 ignored issues
show
Documentation introduced by
$trait is of type object<gossi\codegen\model\PhpClass>, but the function expects a object<gossi\codegen\model\PhpTrait>|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
179
				
180 2
				if (!in_array($trait->getName(), $this->traits)) {
181 2
					$this->codegenService->dumpStruct($trait, true);
182 2
					$this->traits[] = $trait->getName();
183 2
				}
184 2
			}
185
186
			// write to file
187 5
			$this->codegenService->dumpStruct($class, $input->getOption('force'));
188 5
		}
189 5
	}
190
191
}
192