Completed
Push — master ( 2fb143...d99a08 )
by Thomas
09:36
created

ModelReadJsonResponderGenerator::addMethods()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 2
eloc 17
nc 2
nop 2
1
<?php
2
namespace keeko\tools\generator\responder;
3
4
use gossi\codegen\model\PhpClass;
5
use keeko\framework\schema\ActionSchema;
6
7
class ModelReadJsonResponderGenerator extends AbstractModelJsonResponderGenerator {
8
9
	protected function addMethods(PhpClass $class, ActionSchema $action) {
10
		$this->generateGetPayloadMethods($class, $this->twig->render('getPayloadMethods-read.twig'));
11
		$this->generateNotFound($class);
12
		
13
		// method: found(Request $request, PayloadInterface $payload)
1 ignored issue
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
14
		$modelName = $this->modelService->getModelNameByAction($action);
15
		$model = $this->modelService->getModel($modelName);
16
17
		$fields = $this->getModelFields($model);
1 ignored issue
show
Bug introduced by
It seems like $model defined by $this->modelService->getModel($modelName) on line 15 can be null; however, keeko\tools\generator\re...rator::getModelFields() 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...
18
		foreach ($fields as $field) {
19
			$class->addUseStatement($field->getNamespace() . '\\' . $field->getPhpName());
20
		}
21
		
22
		$found = $this->generatePayloadMethod('found', $this->twig->render('model-read.twig', [
23
			'class' => $model->getPhpName(),
24
			'includes' => $this->codegenService->arrayToCode($this->getRelationshipIncludes($model)),
1 ignored issue
show
Bug introduced by
It seems like $model defined by $this->modelService->getModel($modelName) on line 15 can be null; however, keeko\tools\generator\re...tRelationshipIncludes() 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...
25
			'fields' => $this->getFieldsCode($fields)
26
		]));
27
		
28
		$class->setMethod($found);
29
		$class->addUseStatement('Tobscure\\JsonApi\\Document');
30
		$class->addUseStatement('Tobscure\\JsonApi\\Resource');
31
		$class->addUseStatement('Tobscure\\JsonApi\\Parameters');
32
		$class->addUseStatement($model->getNamespace() . '\\' . $model->getPhpName());
33
	}
34
}