ControllerCompiler   B
last analyzed

Complexity

Total Complexity 54

Size/Duplication

Total Lines 500
Duplicated Lines 73.6 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 54
lcom 1
cbo 4
dl 368
loc 500
rs 7.0642
c 0
b 0
f 0

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 2
A replaceAndStore() 0 17 1
A getOutputFilename() 0 5 1
B replaceEagerCode() 49 49 5
A replaceSortConditions() 22 22 3
A replaceUniqueRules() 6 14 3
A getEagerUniqueRules() 6 12 3
A replaceSearchConditions() 0 8 1
A getSearchConditions() 22 22 2
C replaceSimpleFilter() 54 54 15
B replaceReverseRelationships() 29 29 4
A replaceEnum() 23 23 3
B replaceRelationshipTables() 86 86 3
B replaceCheckbox() 34 34 1
C getConditionStubByField() 30 30 7

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ControllerCompiler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ControllerCompiler, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Scaffolder\Compilers\Core;
4
5
use Illuminate\Support\Facades\File;
6
use Scaffolder\Compilers\AbstractCompiler;
7
use Scaffolder\Compilers\Support\FileToCompile;
8
use Scaffolder\Compilers\Support\PathParser;
9
use Scaffolder\Support\CamelCase;
10
11
class ControllerCompiler extends AbstractCompiler
12
{	
0 ignored issues
show
Coding Style introduced by
The opening class brace should be on a newline by itself.
Loading history...
13
	protected $cachePrefix 	= 'controller_';
14
	protected $stubFilename = 'Controller/Controller.php' ;
15
	
16 View Code Duplication
	public function __construct($scaffolderConfig, $modelData = null, $stubName = null)
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...
17
	{
18
		if ($stubName)
19
			$this->stubFilename = $stubName;
20
		$this->stubsDirectory = __DIR__ . '/../../../../stubs/Api/';
21
		parent::__construct($scaffolderConfig, $modelData);
22
	}
23
24
	/**
25
	 * Replace and store the Stub.
26
	 *
27
	 * @return string
28
	 */
29
	public function replaceAndStore()
30
	{
31
		
32
		return $this->replacePrimaryKey()
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->replacePri...modelData->modelHash)); (Scaffolder\Compilers\Core\ControllerCompiler) is incompatible with the return type declared by the abstract method Scaffolder\Compilers\Abs...mpiler::replaceAndStore of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
33
					->replaceEagerCode()
34
					->replaceUniqueRules()
35
					->replaceSearchConditions()
36
					->replaceSimpleFilter()
37
					->replaceSortConditions()
38
					->replaceRoutePrefix()
39
					->replaceReverseRelationships()
40
					->replaceCheckbox()
41
					->replaceEnum()
42
					->replaceRelationshipTables()
43
					->store(new FileToCompile(false, $this->modelData->modelHash));
44
		
45
	}
46
47
48
	/**
49
	 * Get output filename
50
	 *
51
	 *
52
	 * @return $this
53
	 */
54
	protected function getOutputFilename()
55
	{
56
57
		return PathParser::parse($this->scaffolderConfig->generator->paths->controllers) . $this->modelName . 'Controller.php';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return \Scaffolder\Compi...ame . 'Controller.php'; (string) is incompatible with the return type declared by the abstract method Scaffolder\Compilers\Abs...iler::getOutputFilename of type Scaffolder\Compilers\AbstractCompiler.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
58
	}
59
60
	/**
61
	 * Replace eager code for each foreing key with eager = true 
62
	 *
63
	 * @return $this
64
	 */
65 View Code Duplication
	private function replaceEagerCode()
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...
66
	{
67
		$storeCommands = $updateCommands = $ruleCommands = $useCommands = '';
68
		$eagerConditions = $eagerJoins = '';
69
70
		$storeEagerStubOriginal = File::get($this->stubsDirectory . 'StoreEager.php');
71
		$updateEagerStubOriginal = File::get($this->stubsDirectory . 'UpdateEager.php');
72
		$rulesEagerStubOriginal = File::get($this->stubsDirectory . 'RulesEager.php');
73
		$useEagerStubOriginal = File::get($this->stubsDirectory . 'UseEager.php');
74
		$joinEagerStubOriginal = File::get($this->stubsDirectory . 'SearchConditions/JoinEager.php');
75
76
		foreach ($this->modelData->fields as $field)
77
		{
78
			
79
			// Check foreign key
80
			if ($field->foreignKey && isset($field->foreignKey->eager) && $field->foreignKey->eager)
81
			{
82
83
				$storeCommands 	.= str_replace('{{foreign_model_name}}', CamelCase::convertToCamelCase($field->foreignKey->table), $this->replaceForeingStrings($field, $storeEagerStubOriginal));
84
				$updateCommands	.= str_replace('{{foreign_model_name}}', CamelCase::convertToCamelCase($field->foreignKey->table), $this->replaceForeingStrings($field, $updateEagerStubOriginal)) ;
85
				$ruleCommands	.= str_replace('{{foreign_model_name}}', CamelCase::convertToCamelCase($field->foreignKey->table), $this->replaceForeingStrings($field, $rulesEagerStubOriginal)) ;
86
				$useCommands	.= str_replace('{{foreign_model_name}}', CamelCase::convertToCamelCase($field->foreignKey->table), $this->replaceForeingStrings($field, $useEagerStubOriginal)) ;
87
				$eagerJoins		.= $this->replaceForeingStrings($field, $joinEagerStubOriginal) ;
88
89
				// search eager fields
90
				$foreignModelData = $this->getModelData($field->foreignKey->table);
91
				$foreignControllerCompiler = new ControllerCompiler($this->scaffolderConfig, $foreignModelData);
92
				$foreignControllerCompiler->setEagerTable($this->modelData->tableName);
93
				$eagerConditions 	.= $foreignControllerCompiler->getSearchConditions();
94
				$eagerUniqueRules = $foreignControllerCompiler->getEagerUniqueRules();
95
				$eagerUniqueRules = str_replace("{{class_name_lw}}", strtolower($this->modelName), $eagerUniqueRules);
96
				$eagerUniqueRules = str_replace("{{field}}", $field->name, $eagerUniqueRules);
97
				$ruleCommands = str_replace('{{unique_eager_rules}}', $eagerUniqueRules, $ruleCommands) ;
98
			}
99
100
		}
101
102
		$this->stub = str_replace('{{store_eager_objects}}', $storeCommands, $this->stub);
103
		$this->stub = str_replace('{{update_eager_objects}}', $updateCommands, $this->stub);
104
		$this->stub = str_replace('{{rules_eager}}', $ruleCommands, $this->stub);
105
		$this->stub = str_replace('{{eager_use_classes}}', $useCommands, $this->stub);
106
		$this->stub = str_replace('{{eager_joins}}', $eagerJoins, $this->stub);
107
		$this->stub = str_replace('{{eager_conditions}}', $eagerConditions, $this->stub);
108
		$this->stub = str_replace('{{eager_table}}', $this->eagerTable, $this->stub);
109
		
110
111
112
		return $this;
113
	}
114
115
116 View Code Duplication
	public function replaceSortConditions()	{
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...
117
		$joinSorts = '';
118
119
		foreach ($this->modelData->fields as $field) {
120
			if($field->foreignKey){
121
				$joinSortStub = File::get($this->stubsDirectory . 'SearchConditions/JoinSort.php');
122
				$joinSortStub = str_replace('{{field}}', $field->name, $joinSortStub);
123
				$joinSortStub = str_replace('{{foreign_table}}', $field->foreignKey->table, $joinSortStub);
124
				$joinSortStub = str_replace('{{foreign_key}}', $field->foreignKey->table, $joinSortStub);
125
				$joinSorts .= $joinSortStub;
126
127
			}
128
			
129
	
130
			
131
		}
132
		
133
134
		$this->stub = str_replace('{{relationship_tables_joins_sort}}', $joinSorts, $this->stub);
135
136
		return $this;
137
	}
138
139
	public function replaceUniqueRules() {
140
		$uniqueRules = '';
141
142 View Code Duplication
		foreach ($this->modelData->fields as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
143
			if (strpos($field->validations, 'unique')) {
144
				$rule = sprintf('$rules["%s"] = $rules["%s"] . \',%s,\' . $id;', $field->name, $field->name, $field->name);
145
				$uniqueRules .= $rule . "\n";
146
			}
147
		}
148
149
		$this->stub = str_replace('{{unique_rules}}', $uniqueRules, $this->stub);
150
151
		return $this;
152
	}
153
154
	public function getEagerUniqueRules() {
155
		$uniqueRules = '';
156
157 View Code Duplication
		foreach ($this->modelData->fields as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
158
			if (strpos($field->validations, 'unique')) {
159
				$rule = sprintf('$rules["%s.%s"] .= \',%s,\' . ${{class_name_lw}}->{{field}};', $this->modelData->tableName, $field->name, $field->name);
160
				$uniqueRules .= $rule . "\n";
161
			}
162
		}
163
164
		return $uniqueRules;
165
	}
166
167
	/**
168
	 * Replace search conditions
169
	 *
170
	 * @return $this
171
	 */
172
	private function replaceSearchConditions(){
173
174
		$searchConditions = $this->getSearchConditions();
175
176
		$this->stub = str_replace('{{conditions}}', $searchConditions, $this->stub);
177
178
		return $this;
179
	}
180
181
	/**
182
	 * get search conditions
183
	 *
184
	 * @return $this
185
	 */
186 View Code Duplication
	public function getSearchConditions(){
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...
187
188
		$fieldConditions = '';
189
190
		$searchConditions = File::get($this->stubsDirectory . '/SearchConditions/Conditions.php');
191
192
		foreach ($this->modelData->fields as $field)
193
		{
194
195
			$fieldConditions .= $this->replaceFieldStrings($field, $this->getConditionStubByField($field)) ;
196
197
		}
198
199
		// replace all field conditions
200
		$searchConditions = str_replace('{{field_conditions}}', $fieldConditions, $searchConditions);
201
202
		// replace table name
203
		$searchConditions = str_replace('{{table_name}}', $this->modelData->tableName, $searchConditions);
204
205
		return $searchConditions ;
206
207
	}
208
209
210
	/**
211
	 * Replace simple filter
212
	 *
213
	 * @return $this
214
	 */
215 View Code Duplication
	private function replaceSimpleFilter(){
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...
216
		$i = 0;
0 ignored issues
show
Unused Code introduced by
$i 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...
217
218
		$stubSimpleFilter = '';
219
220
		foreach ($this->modelData->fields as $field)
221
		{
222
			//var_dump($field->name);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
223
			
224
			if($field->index == 'primary'){
225
				$dbType = 'primaryKey' ;
226
			}
227
			elseif($field->foreignKey){
228
				$dbType = 'primary' ;
229
			}
230
			elseif($field->type->db == 'enum'){
231
				$dbType = 'primary' ;
232
			}
233
			elseif($field->type->db == 'boolean'){
234
				$dbType = 'primary' ;
235
			}
236
			elseif($field->type->db == 'text'){
237
				$dbType = 'string' ;
238
			}
239
			else {
240
				$dbType = $field->type->db ;
241
			}
242
			
243
			if($dbType == 'primaryKey')
244
			{
245
				$stubSimpleFilter .= '$query->where("'.$this->modelData->tableName.'.'.$field->name.'", "=", $'.$this->modelData->tableName.'Conditions["'.$field->name.'"])'.PHP_EOL;
246
			}
247
248
			if($dbType == 'primary')
249
			{
250
				$stubSimpleFilter .= '->orWhere("'.$this->modelData->tableName.'.'.$field->name.'", "=", $'.$this->modelData->tableName.'Conditions["'.$field->name.'"])'.PHP_EOL;
251
			}
252
253
			if($dbType == 'string')
254
			{
255
				$stubSimpleFilter .= '->orWhere("'.$this->modelData->tableName.'.'.$field->name.'", "LIKE", "%".$'.$this->modelData->tableName.'Conditions["'.$field->name.'"]."%")'.PHP_EOL;
256
			}
257
258
			if($dbType == 'date' || $dbType == 'datetime' || $dbType == 'float' || $dbType == 'integer' || $dbType == 'number')
259
			{
260
				$stubSimpleFilter .= '->orWhere("'.$this->modelData->tableName.'.'.$field->name.'", "=", $'.$this->modelData->tableName.'Conditions["'.$field->name.'"])'.PHP_EOL;
261
			}
262
263
		}
264
		$stubSimpleFilter .= ';';
265
		$this->stub = str_replace('{{simple_filter}}', $stubSimpleFilter, $this->stub);
266
267
		return $this;
268
	}
269
270
271
	/**
272
	 * replace reverse relationships
273
	 *
274
	 * @return $this
275
	 */
276 View Code Duplication
	public function replaceReverseRelationships(){
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...
277
278
		$functions = '';
279
280
		$method = File::get($this->stubsDirectory . '/Controller/ControllerReverseRelationship.php');
281
282
		foreach ($this->modelData->reverseRelationships as $relationship)
283
		{
284
			$functionName = '';
0 ignored issues
show
Unused Code introduced by
$functionName 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...
285
			if ($relationship->type == "hasOne")
286
				$functionName = strtolower($relationship->modelName);
287
			elseif ($relationship->type == "belongsToMany") 
288
				$functionName = CamelCase::pluralize(strtolower($relationship->relatedTable));
289
			else 
290
				$functionName = CamelCase::pluralize(strtolower($relationship->modelName));
291
292
			$replacedMethod = '';
0 ignored issues
show
Unused Code introduced by
$replacedMethod 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...
293
			$replacedMethod = str_replace('{{function_name}}', $functionName, $method);
294
			$replacedMethod = str_replace('{{class_name_lw}}', $this->modelData->tableName, $replacedMethod);
295
			$replacedMethod = str_replace('{{class_name}}', ucwords($this->modelData->tableName), $replacedMethod);
296
297
			$functions .= $replacedMethod;
298
		}
299
300
		$this->stub = str_replace('{{reverseRelationships}}', $functions, $this->stub);
301
302
		return $this;
303
304
	}
305
306
	/**
307
	 * replace enum fields
308
	 *
309
	 * @return $this
310
	 */
311 View Code Duplication
	public function replaceEnum(){
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...
312
313
		$functions = '';
314
315
		$method = File::get($this->stubsDirectory . '/Controller/ControllerEnum.php');
316
317
		foreach ($this->modelData->fields as $field)
318
		{
319
			if ($field->type->db == "enum") {
320
				$replacedMethod = '';
0 ignored issues
show
Unused Code introduced by
$replacedMethod 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...
321
				$replacedMethod = str_replace('{{field_name_uc}}', CamelCase::convertToCamelCase($field->name), $method);	
322
				$replacedMethod = str_replace('{{field_name}}', $field->name, $replacedMethod);
323
				$replacedMethod = str_replace('{{model_name}}', $this->modelData->modelName, $replacedMethod);
324
				
325
				$functions .= $replacedMethod;
326
			}
327
		}
328
329
		$this->stub = str_replace('{{enum}}', $functions, $this->stub);
330
331
		return $this;
332
333
	}
334
335
	/**
336
	 * replace relationship tables
337
	 *
338
	 * @return $this
339
	 */
340 View Code Duplication
	public function replaceRelationshipTables() {
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...
341
342
		$functions = "";
343
		$functionsCall = "";
344
		$removeAll = "";
345
		$removeAllCall = "";
346
		$includes = "";
347
		$joins = "";
348
		$joinSorts = "";
0 ignored issues
show
Unused Code introduced by
$joinSorts 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...
349
350
		$method = File::get($this->stubsDirectory . '/Controller/ControllerRelationshipTable.php');
351
352
		foreach ($this->modelData->reverseRelationships as $relationship) {
353
354
			if ($relationship->type == "belongsToMany") {
355
				$relatedTablePluralized = CamelCase::pluralize($relationship->relatedTable);
356
				$relatedTablePluralizedUc = CamelCase::pluralize(CamelCase::convertToCamelCase($relationship->relatedTable));
357
358
				$replacedMethod = '';
0 ignored issues
show
Unused Code introduced by
$replacedMethod 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...
359
				$replacedMethod = str_replace('{{related_table_pl_uc}}', $relatedTablePluralizedUc, $method);
360
				$replacedMethod = str_replace('{{class_name_lw}}', $this->modelData->tableName, $replacedMethod);
361
				$replacedMethod = str_replace('{{related_table_pl}}', $relatedTablePluralized, $replacedMethod);
362
				$replacedMethod = str_replace('{{foreign_key}}', $relationship->foreignKey, $replacedMethod);
363
				$replacedMethod = str_replace('{{related_field}}', $relationship->relatedField, $replacedMethod);
364
				$replacedMethod = str_replace('{{foreign_table_lw}}', strtolower($relationship->modelName), $replacedMethod);
365
				$replacedMethod = str_replace('{{foreign_table}}', $relationship->modelName, $replacedMethod);
366
367
				$functions .= $replacedMethod;
368
369
				$methodCall = 'if (array_key_exists(\'{{related_table_pl}}\', $vars))';
370
				$methodCall .= "\n\t\t\t";
371
				$methodCall .= '$this->save{{related_table_pl_uc}}($vars, ${{class_name_lw}});';
372
				$methodCall = str_replace('{{related_table_pl_uc}}', $relatedTablePluralizedUc, $methodCall);
373
				$methodCall = str_replace('{{related_table_pl}}', $relatedTablePluralized, $methodCall);
374
				$methodCall = str_replace('{{class_name_lw}}', $this->modelData->tableName, $methodCall);
375
376
				$functionsCall .= $methodCall . "\n\t\t";
377
				
378
				$removeAllMethod = File::get($this->stubsDirectory . '/Controller/ControllerRemoveAll.php');
379
				$removeAllMethod = str_replace('{{related_table_pl_uc}}', $relatedTablePluralizedUc, $removeAllMethod);
380
				$removeAllMethod = str_replace('{{foreign_key}}', $relationship->foreignKey, $removeAllMethod);
381
				$removeAllMethod = str_replace('{{foreign_table}}', $relationship->modelName, $removeAllMethod);
382
				$removeAllMethod = str_replace('{{foreign_table_lw_pl}}', CamelCase::pluralize(strtolower($relationship->modelName)), $removeAllMethod);
383
				
384
				$removeAll .= $removeAllMethod;
385
386
				$removeAllCallMethod = '$this->deleteAll{{related_table_pl_uc}}(${{class_name_lw}}[\'id\']);';
387
				$removeAllCallMethod = str_replace('{{related_table_pl_uc}}', $relatedTablePluralizedUc, $removeAllCallMethod);
388
				$removeAllCallMethod = str_replace('{{class_name_lw}}', $this->modelData->tableName, $removeAllCallMethod);
389
				
390
				$removeAllCall .= $removeAllCallMethod . "\n\t\t";
391
392
				$joinRelationshipTableStub = File::get($this->stubsDirectory . 'SearchConditions/joinRelationshipTable.php');
393
				$joinRelationshipTableStub = str_replace('{{class_name_lw}}', $this->modelData->tableName, $joinRelationshipTableStub);
394
				$joinRelationshipTableStub = str_replace('{{related_table_pl}}', $relatedTablePluralized, $joinRelationshipTableStub);
395
				$joinRelationshipTableStub = str_replace('{{related_table}}', CamelCase::convertToCamelCase($relationship->relatedTable), $joinRelationshipTableStub);
396
				$joinRelationshipTableStub = str_replace('{{foreign_key}}', $relationship->foreignKey, $joinRelationshipTableStub);
397
				$joinRelationshipTableStub = str_replace('{{related_field}}', $relationship->relatedField, $joinRelationshipTableStub);
398
				$joinRelationshipTableStub = str_replace('{{foreign_table}}', $relationship->tableName, $joinRelationshipTableStub);
399
400
				$joins .= $joinRelationshipTableStub . "\n";
401
402
				$use = 'use App\Models\{{foreign_table}};';
403
				$use = str_replace('{{foreign_table}}', $relationship->modelName, $use);
404
405
				$includes .= $use . "\n";
406
			}
407
		}
408
409
		$this->stub = str_replace('{{relationship_tables_store}}', $functions, $this->stub);
410
411
		$this->stub = str_replace('{{relationship_tables_call}}', $functionsCall, $this->stub);
412
413
		$this->stub = str_replace('{{remove_relationship_objects}}', $removeAll, $this->stub);
414
415
		$this->stub = str_replace('{{remove_relationship_objects_call}}', $removeAllCall, $this->stub);
416
417
		$this->stub = str_replace('{{relationship_tables_classes}}', $includes, $this->stub);
418
419
		$this->stub = str_replace('{{relationship_tables_joins}}', $joins, $this->stub);
420
421
		
422
		
423
		return $this;
424
425
	}
426
427
428
	/**
429
	 * replace checkbox fields
430
	 *
431
	 * @return $this
432
	 */
433 View Code Duplication
	public function replaceCheckbox(){
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...
434
435
		$method = File::get($this->stubsDirectory . '/Controller/ControllerCheckbox.php');
436
		$key = false;
0 ignored issues
show
Unused Code introduced by
$key 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...
437
438
		$method = str_replace('{{class_name_lw}}', $this->modelData->tableName, $method);
439
		$method = str_replace('{{class_name}}', ucwords($this->modelData->tableName), $method);
440
		$method = str_replace('{{model_name}}', $this->modelData->modelName, $method);
441
		$this->stub = str_replace('{{checkbox}}', $method, $this->stub);
442
443
		/*foreach ($this->modelData->fields as $field)
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...
444
		{
445
			if ($field->type->ui == "checkbox") {
446
				
447
				$method = str_replace('{{class_name_lw}}', $this->modelData->tableName, $method);
448
				$method = str_replace('{{class_name}}', ucwords($this->modelData->tableName), $method);
449
450
				$this->stub = str_replace('{{checkbox}}', $method, $this->stub);
451
452
453
454
				$key = true;
455
			}
456
		}
457
458
		if(!$key)
459
		{
460
			$this->stub = str_replace('{{checkbox}}', ' ', $this->stub);
461
		}*/
462
		
463
464
		return $this;
465
466
	}
467
468
469
	/**
470
	 * get search conditions stub by db type
471
	 *
472
	 * @param string $field
473
	 *
474
	 * @return $this
475
	 */
476
	private $conditionsStub = [];
477 View Code Duplication
	private function getConditionStubByField($field){
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...
478
		
479
		if($field->index == 'primary'){
480
			$dbType = 'primary' ;
481
		}
482
		elseif($field->foreignKey){
483
			$dbType = 'primary' ;
484
		}
485
		elseif($field->type->db == 'enum'){
486
			$dbType = 'primary' ;
487
		}
488
		elseif($field->type->db == 'boolean'){
489
			$dbType = 'primary' ;
490
		}
491
		elseif($field->type->db == 'text'){
492
			$dbType = 'string' ;
493
		}
494
		else {
495
			$dbType = $field->type->db ;
496
		}
497
498
		if(array_key_exists($dbType, $this->conditionsStub)){
499
			return $this->conditionsStub[$dbType];
500
		}
501
		else {
502
			$this->conditionsStub[$dbType] = File::get($this->stubsDirectory . 'SearchConditions/'. ucwords($dbType). '.php');;
503
504
			return $this->conditionsStub[$dbType];
505
		}
506
	}
507
	
508
509
510
}