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

CodeGeneratorService::getReadFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 11
nc 3
nop 1
crap 12
1
<?php
2
namespace keeko\tools\services;
3
4
use gossi\codegen\model\AbstractPhpStruct;
5
use gossi\docblock\tags\AuthorTag;
6
use Propel\Generator\Model\Table;
7
use gossi\codegen\generator\CodeFileGenerator;
8
use keeko\tools\utils\NamespaceResolver;
9
use keeko\core\schema\PackageSchema;
10
use keeko\core\schema\AuthorSchema;
11
use phootwork\file\File;
12
use phootwork\file\Path;
13
use keeko\core\schema\CodegenSchema;
14
15
class CodeGeneratorService extends AbstractService {
16
	
17
	private $codegen;
18
	
19 7
	public function getCodegenFile() {
20 7
		$basepath = dirname($this->project->getComposerFileName());
21 7
		return $basepath . '/codegen.json';
22
	}
23
	
24
	/**
25
	 * Loads the contents from codegen.json into a collection
26
	 *
27
	 * @throws JsonEmptyException
28
	 * @throws \RuntimeException
29
	 * @return CodegenSchema
30
	 */
31 12
	public function getCodegen() {
32 7
		if ($this->codegen === null) {
33 7
			$file = new File($this->getCodegenFile());
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 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...
34 7
			$this->codegen = $file->exists() 
35
				? CodegenSchema::fromFile($this->getCodegenFile())
36 7
				: new CodegenSchema();
37 12
		}
38
		
39
		return $this->codegen;
40
	}
41
	
42
// 	/**
43
// 	 * Returns the codegen part for the given action name or an empty map
44
// 	 *
45
// 	 * @param string $name
46
// 	 * @return Map
47
// 	 */
48
// 	public function getCodegenAction($name) {
49
// 		$codegen = $this->getCodegen();
50
	
51
// 		if (isset($codegen['actions'])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
68% 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...
52
// 			$actions = $codegen['actions'];
53
				
54
// 			if (isset($actions[$name])) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
55
// 				return $actions[$name];
56
// 			}
57
// 		}
58
	
59
// 		return null;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% 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...
60
// 	}
61
	
62
	/**
63
	 * Adds authors to the docblock of the given struct
64
	 *
65 15
	 * @param AbstractPhpStruct $struct
66 15
	 * @param array $package
67
	 */
68 15
	public function addAuthors(AbstractPhpStruct $struct, PackageSchema $package) {
69
		$docblock = $struct->getDocblock();
70
		
71 15
		foreach ($package->getAuthors() as $author) {
72 15
			/* @var $author AuthorSchema */
73 15
			$tag = AuthorTag::create()->setName($author->getName());
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...
74
			$mail = $author->getEmail();
75 15
76
			if (!empty($mail)) {
77 15
				$tag->setEmail($mail);
78
			}
79
80
			$docblock->appendTag($tag);
81 15
		}
82 15
	}
83 15
84
	/**
85
	 * Returns code for hydrating a propel model
86
	 *
87
	 * @param string $modelName
88
	 * @return string
89
	 */
90
	public function getWriteFields($modelName) {
91 5
		$codegen = $this->getCodegen();
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...
92 5
		$conversions = $codegen->getWriteConversion($modelName);
93 5
		$filter = $codegen->getWriteFilter($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...
94 5
		$model = $this->modelService->getModel($modelName);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 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...
95 5
		$computed = $this->getComputedFields($model);
1 ignored issue
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...
Bug introduced by
It seems like $model defined by $this->modelService->getModel($modelName) on line 94 can be null; however, keeko\tools\services\Cod...ce::getComputedFields() 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...
96 5
		$filter = array_merge($filter, $computed);
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...
97 5
98
		$fields = '';
99 5
		$cols = $model->getColumns();
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...
100 5
		foreach ($cols as $col) {
101 5
			$prop = $col->getName();
102 5
	
103
			if (!in_array($prop, $filter)) {
104 5
				$fields .= sprintf("'%s'", $prop);
105 5
	
106
				if (isset($conversions[$prop])) {
107 5
					$fields .= ' => function($v) {'."\n\t".'return ' . $conversions[$prop] . ';'."\n".'}';
108
				}
109
	
110
				$fields .= ', ';
111 5
			}
112 5
		}
113 5
	
114
		if (strlen($fields) > 0) {
115 5
			$fields = substr($fields, 0, -2);
116 5
		}
117 5
	
118
		return sprintf('[%s]', $fields);
119 5
	}
120
	
121
	/**
122
	 * Returns the fields for a model
123
	 * 
124
	 * @param string $modelName
125
	 * @return array
126
	 */
127
	public function getReadFields($modelName) {
128
		$codegen = $this->getCodegen();
129
		$model = $this->modelService->getModel($modelName);
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...
130
// 		$computed = $this->getComputedFields($model);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% 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...
131
		$filter = $codegen->getReadFilter($modelName);
132
// 		$filter = array_merge($filter, $computed);
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% 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...
133
		
134
		$fields = [];
135
		$cols = $model->getColumns();
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...
136
		foreach ($cols as $col) {
137
			$prop = $col->getName();
138
		
139
			if (!in_array($prop, $filter)) {
140
				$fields[] = $prop;
141
			}
142
		}
143
		
144
		return $fields;
145
	}
146
	
147
// 	/**
148
// 	 * Returns conversions for model columns
149
// 	 *
150 5
// 	 * @param string $model
151 5
// 	 * @param string $type
152
// 	 * @return array
153
// 	 */
154 5
// 	public function getConversions($model, $type) {
155 5
// 		return $this->getActionProp($model, $type, 'conversion');
156 5
// 	}
157 5
	
158 5
// 	/**
159 5
// 	 * Returns model columns that should be filtered
160
// 	 *
161 5
// 	 * @param string $model
162
// 	 * @param string $type
163
// 	 * @return array
164 5
// 	 */
165 5
// 	public function getFilter($model, $type) {
166
// 		return $this->getActionProp($model, $type, 'filter');
167 5
// 	}
168
	
169
	/**
170
	 * Returns computed model fields
171
	 *
172
	 * @param Table $table
173
	 * @return array<string>
174
	 */
175
	public function getComputedFields(Table $table) {
176 2
		$fields = [];
177 2
	
178 2
		// iterate over behaviors to get their respective columns
179
		foreach ($table->getBehaviors() as $behavior) {
180 2
			switch ($behavior->getName()) {
181
				case 'timestampable':
182 2
					$fields[] = $behavior->getParameter('create_column');
183
					$fields[] = $behavior->getParameter('update_column');
184
					break;
185
	
186 2
				case 'aggregate_column':
187
					$fields[] = $behavior->getParameter('name');
188
					break;
189 15
			}
190 15
		}
191 15
	
192
		return $fields;
193 15
	}
194
	
195
	/**
196
	 * Helper to represent an array as php code
197 15
	 *
198 15
	 * @param array $array
199 15
	 * @return string
200 15
	 */
201 15 View Code Duplication
	public function arrayToCode(array $array) {
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...
202
		$fields = '';
203
		foreach ($array as $item) {
204 15
			$fields .= sprintf("'%s', ", $item);
205 15
		}
206 15
	
207
		if (strlen($fields) > 0) {
208 15
			$fields = substr($fields, 0, -2);
209
		}
210 15
	
211 15
		return sprintf('[%s]', $fields);
212
	}
213
	
214 15 View Code Duplication
	public function mapToCode(array $array) {
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...
215
		$fields = '';
216
		foreach ($array as $k => $item) {
217 15
			$fields .= sprintf("\t'%s' => %s,\n", $k, $this->arrayToCode($item));
218 15
		}
219 15
220
		if (strlen($fields) > 0) {
221
			$fields = substr($fields, 0, -2);
222
		}
223
		
224
		return sprintf("[\n%s\n]", $fields);
225
	}
226
227
	public function getFilename(AbstractPhpStruct $struct) {
228
		$package = $this->packageService->getPackage();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 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...
229
		$relativeSourcePath = NamespaceResolver::getSourcePath($struct->getNamespace(), $package);
230
		
231
		if ($relativeSourcePath === null) {
232
			return null;
233
		}
234
235
		$jsonFile = $this->project->getComposerFileName();
236
		$path = new Path(dirname($jsonFile));
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...
237
		$path = $path->append($relativeSourcePath);
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...
238
		$path = $path->append($struct->getName() . '.php');
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...
239
		return $path->toString();
240
	}
241
	
242
	public function dumpStruct(AbstractPhpStruct $struct, $overwrite = false) {
243
		$filename = $this->getFilename($struct);
244
		$file = new File($filename);
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...
245
246
		if ($filename !== null && $file->exists() ? $overwrite : true) {
247
			// generate code
248
			$generator = new CodeFileGenerator();
249
			$code = $generator->generate($struct);
0 ignored issues
show
Documentation introduced by
$struct is of type object<gossi\codegen\model\AbstractPhpStruct>, but the function expects a object<gossi\codegen\model\GenerateableInterface>.

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...
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...
250
251
			// write code to file
252
			$file->write($code);
253
			
254
			// tell user about
255
			$this->io->writeln(sprintf('Class <info>%s</info> written at <info>%s</info>', $struct->getQualifiedName(), $filename));
256
		}
257
	}
258
}
259