ResponseUI::getLabel()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace keeko\tools\ui;
3
4
use keeko\tools\ui\ModelSkeletonUI;
5
use Symfony\Component\Console\Question\Question;
6
use keeko\framework\utils\NameUtils;
7
8
class ResponseUI extends ModelSkeletonUI {
9
10
	protected function getLabel() {
11
		return 'responder';
12
	}
13
	
14
	protected function showSkeleton() {
15
		$packageService = $this->getService()->getPackageService();
16
		$modelService = $this->getService()->getModelService();
17
		$input = $this->getService()->getIOService()->getInput();
18
		$name = $input->getArgument('name');
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
19
		
20
		$names = [];
21
		$module = $packageService->getModule();
22
		foreach ($module->getActionNames() as $actionName) {
23
			$names[] = $actionName;
24
		}
25
			
26
		$actionQuestion = new Question('Which action');
27
		$actionQuestion->setAutocompleterValues($names);
28
		$name = $this->askQuestion($actionQuestion);
29
		$input->setArgument('name', $name);
30
		
31
		// ask which format
32
		$formatQuestion = new Question('Which format', 'json');
33
		$formatQuestion->setAutocompleterValues(['json', 'html']);
34
		$format = $this->askQuestion($formatQuestion);
35
		$input->setOption('format', $format);
36
		
37
		// ask which template
38
		$action = $packageService->getAction($name);
39
		if (!($format == 'json' && $modelService->isModelAction($action))) {
0 ignored issues
show
Bug introduced by
It seems like $action defined by $packageService->getAction($name) on line 38 can be null; however, keeko\tools\services\ModelService::isModelAction() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
40
			$templates = [
41
				'html' => ['twig', 'payload', 'blank'],
42
				'json' => ['api', 'payload', 'blank']
43
			];
44
45
			$suggestions = isset($templates[$format]) ? $templates[$format] : [];
46
			$default = count($suggestions) ? $suggestions[0] : '';
47
			$templateQuestion = new Question('Which template', $default);
48
			$templateQuestion->setAutocompleterValues($suggestions);
49
			$template = $this->askQuestion($templateQuestion);
50
			$input->setOption('template', $template);
51
			
52
			// aks for serializer
53
			if ($format == 'json' && $template == 'api') {
54
				$guessedSerializer = NameUtils::toStudlyCase($name) . 'Serializer';
55
				$serializerQuestion = new Question('Which format', $guessedSerializer);
56
				$serializer = $this->askQuestion($serializerQuestion);
57
				$input->setOption('serializer', $serializer);
58
			}
59
		}
60
	}
61
}