Completed
Push — master ( 2e0a22...66405f )
by Thomas
05:23
created

getRelationshipIncludes()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 5
eloc 15
nc 6
nop 3
1
<?php
2
namespace keeko\tools\generator;
3
4
use Propel\Generator\Model\Table;
5
use keeko\tools\utils\NameUtils;
6
7
class AbstractModelJsonResponseGenerator extends AbstractJsonResponseGenerator {
8
	
9
	protected function getRelationshipIncludes(Table $model, $root = '', $processed = []) {
10
		if (in_array($model->getOriginCommonName(), $processed)) {
11
			return [];
12
		}
13
		
14
		$relationships = $this->modelService->getRelationships($model);
15
		$includes = [];
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...
16
	
17
		foreach ($relationships['all'] as $rel) {
18
			$fk = $rel['fk'];
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...
19
			$foreignModel = $fk->getForeignTable();
20
			$processed[] = $foreignModel->getOriginCommonName();
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...
21
			
22
			$typeName = NameUtils::dasherize($fk->getForeignTable()->getOriginCommonName());
23
			if ($rel['type'] == 'many') {
24
				$typeName = NameUtils::pluralize($typeName);
25
			}
26
			$includes[] = (!empty($root) ? $root . '.' : '') . $typeName;
27
			$includes = array_merge($includes, $this->getRelationshipIncludes($foreignModel, $typeName, $processed));
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...
28
		}
29
	
30
		return $includes;
31
	}
32
	
33
	protected function getModelFields(Table $model, $root = '', $processed = []) {
34
		if (in_array($model->getOriginCommonName(), $processed)) {
35
			return [];
36
		}
37
		
38
		$typeName = NameUtils::dasherize($model->getOriginCommonName());
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...
39
		$relationships = $this->modelService->getRelationships($model);
40
		$fields = [$typeName => $model];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
41
42
		foreach ($relationships['all'] as $rel) {
43
			$fk = $rel['fk'];
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...
44
			$foreignModel = $fk->getForeignTable();
45
			$processed[] = $foreignModel->getOriginCommonName();
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...
46
			
47
			$typeName = NameUtils::dasherize($fk->getForeignTable()->getOriginCommonName());
48
			if ($rel['type'] == 'many') {
49
				$typeName = NameUtils::pluralize($typeName);
50
			}
51
			$name = (!empty($root) ? $root . '.' : '') . $typeName;
52
			
53
			$fields[$name] = $foreignModel;
54
			$fields = array_merge($fields, $this->getModelFields($foreignModel, $name, $processed));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 8 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...
55
		}
56
		
57
		return $fields;
58
	}
59
	
60 View Code Duplication
	protected function getFieldsCode(array $fields) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
		$code = '';
62
		foreach ($fields as $typeName => $field) {
63
			$code .= sprintf("\t'%s' => %s::getSerializer()->getFields(),\n", $typeName, $field->getPhpName());
64
		}
65
		
66
		if (strlen($code) > 0) {
67
			$code = substr($code, 0, -2);
68
		}
69
		
70
		return sprintf("[\n%s\n]", $code);
71
	}
72
	
73
}
74