RegisterTemplateCompiler   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 203
Duplicated Lines 38.42 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 6
dl 78
loc 203
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A replaceAndStore() 0 9 1
A replaceInputFields() 0 8 1
B getInputFields() 36 36 6
A replaceBelongsToManyFields() 0 23 3
A replaceFieldInput() 0 6 1
A replaceFieldValidations() 18 18 3
C getInputStubByField() 24 24 7
A getOutputFilename() 0 8 1

How to fix   Duplicated Code   

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:

1
<?php
2
3
namespace Scaffolder\Compilers\AngularJs;
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\Directory;
10
use Scaffolder\Support\CamelCase;
11
use Scaffolder\Support\Validator;
12
use stdClass ;
13
14
class RegisterTemplateCompiler extends AbstractCompiler
15
{
16
	protected $cachePrefix 	= 'register_template_';
17
	protected $stubFilename = 'RegisterTemplate.html' ;
18
19
	public function __construct($scaffolderConfig, $modelData = null)
20
	{
21
		$this->stubsDirectory = __DIR__ . '/../../../../stubs/AngularJs/';
22
		parent::__construct($scaffolderConfig, $modelData);
23
	}
24
25
	/**
26
	 * Replace and store the Stub.
27
	 *
28
	 * @return string
29
	 */
30
	public function replaceAndStore()
31
	{
32
		
33
		return $this
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->replaceInp...modelData->modelHash)); (Scaffolder\Compilers\Ang...egisterTemplateCompiler) 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...
34
				->replaceInputFields()
35
				->replaceBelongsToManyFields()
36
				->store(new FileToCompile(false, $this->modelData->modelHash));
37
		
38
	}
39
40
	/**
41
	 * Replace input fields
42
	 *
43
	 * @return $this
44
	 */
45
	private function replaceInputFields(){
46
47
		$inputFields = $this->getInputFields();
48
49
		$this->stub = str_replace('{{columns_inputs}}', $inputFields, $this->stub);
50
51
		return $this;
52
	}
53
54
	/**
55
	 * get search conditions
56
	 *
57
	 * @return $this
58
	 */
59 View Code Duplication
	public function getInputFields(){
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...
60
61
		$inputFields = $eagerFields = '';
62
63
		foreach ($this->modelData->fields as $field)
64
		{
65
			$fieldStub = $this->getInputStubByField($field);
66
			
67
			if($field->foreignKey){
68
				
69
				$fieldStub 	= $this->replaceForeingStrings($field, $fieldStub) ;
70
				$fieldStub = str_replace('{{foreign_model_name}}', strtolower(CamelCase::convertToCamelCase($field->foreignKey->table)), $fieldStub);
71
			}
72
73
			$inputFields .= $this->replaceFieldInput($field, $fieldStub) ;
74
75
			// Check foreign key
76
			if ($field->foreignKey && isset($field->foreignKey->eager) && $field->foreignKey->eager)
77
			{
78
				// search eager fields
79
				$foreignModelData = $this->getModelData($field->foreignKey->table);
80
				$foreignControllerCompiler = new RegisterTemplateCompiler($this->scaffolderConfig, $foreignModelData);
81
				$foreignControllerCompiler->setEagerTable($this->modelData->tableName);
82
				$eagerFields 	.= $foreignControllerCompiler->getInputFields();
83
			}
84
85
		}
86
87
		// replace table name
88
		$inputFields = str_replace('{{table_name}}', $this->modelData->tableName, $inputFields);
89
		
90
		$this->stub = str_replace('{{eager_objects_inputs}}', $eagerFields, $this->stub); 
91
92
		return $inputFields ;
93
94
	}
95
96
	/**
97
	 * Replace belongs to many fields
98
	 *
99
	 * @return $this
100
	 */
101
	protected function replaceBelongsToManyFields() {
102
		
103
		$belongToManyFields = '';
104
		
105
		foreach ($this->modelData->reverseRelationships as $relationship)
106
		{
107
			if ($relationship->type == "belongsToMany") {
108
				$fieldStub = File::get($this->stubsDirectory . 'Register/'. CamelCase::convertToCamelCase($relationship->ui). '.html');
109
110
				$fieldStub = str_replace('{{related_table}}',CamelCase::convertToCamelCase($relationship->relatedTable), $fieldStub);
111
				$fieldStub = str_replace('{{related_table_lw}}', strtolower(CamelCase::convertToCamelCase($relationship->relatedTable)), $fieldStub);
112
				$fieldStub = str_replace('{{table_name}}', $this->modelData->tableName, $fieldStub);
113
				$fieldStub = str_replace('{{related_table_lw_pl}}', CamelCase::pluralize(strtolower($relationship->relatedTable)), $fieldStub);
114
				$fieldStub = str_replace('{{foreign_table_lw_pl}}', CamelCase::pluralize(strtolower($relationship->modelName)), $fieldStub);
115
				
116
				$belongToManyFields .= $fieldStub;	
117
			}
118
		}
119
120
		$this->stub = str_replace('{{belongs_to_many_inputs}}', $belongToManyFields, $this->stub); 
121
122
		return $this;
123
	}
124
	
125
	/**
126
	 * replace field stub with fields and validations
127
	 *
128
	 * @param string $field
129
	 * @param string $fieldStub
130
	 *
131
	 * @return $this
132
	 */
133
	protected function replaceFieldInput($field, $fieldStub){
134
		$fieldStub = $this->replaceFieldStrings($field, $fieldStub) ;
0 ignored issues
show
Documentation introduced by
$field is of type string, but the function expects a object<stdClass>.

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...
135
		$fieldStub = $this->replaceFieldValidations($field, $fieldStub) ;
136
137
		return $fieldStub ;
138
	}
139
140
	/**
141
	 * replace field stub with fields and validations
142
	 *
143
	 * @param string $field
144
	 * @param string $fieldStub
145
	 *
146
	 * @return $this
147
	 */
148 View Code Duplication
	protected function replaceFieldValidations($field, $fieldStub){
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...
149
		$fieldStub = $this->replaceFieldStrings($field, $fieldStub) ;
0 ignored issues
show
Documentation introduced by
$field is of type string, but the function expects a object<stdClass>.

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...
150
		
151
		$validationsConverted = Validator::convertValidations($field->validations);
152
153
		$inputValidations = '' ; 
154
155
		foreach ($validationsConverted as $attribute => $value) {
156
			if($value)
157
				$inputValidations .=  ' '.$attribute.'="'. $value.'"' ;
158
			else
159
				$inputValidations .=  ' '.$attribute  ; 
160
		}
161
162
		$fieldStub = str_replace('{{field_validation}}', $inputValidations, $fieldStub);
163
164
		return $fieldStub ;
165
	}
166
167
	
168
	/**
169
	 * get input field stub by ui type
170
	 *
171
	 * @param string $field
172
	 *
173
	 * @return $this
174
	 */
175
	private $inputStub = [];
176 View Code Duplication
	private function getInputStubByField($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...
177
		
178
		if($field->index == 'primary'){
179
			$uiType = 'primary' ;
180
		}
181
		elseif(isset($field->foreignKey) && $field->foreignKey){
182
			if(isset($field->foreignKey->eager) && $field->foreignKey->eager)
183
				$uiType = 'foreign_eager' ;
184
			else 
185
				$uiType = $field->type->ui ;
186
		}
187
		else {
188
			$uiType = $field->type->ui ;
189
		}
190
191
		if(array_key_exists($uiType, $this->inputStub)){
192
			return $this->inputStub[$uiType];
193
		}
194
		else {
195
			$this->inputStub[$uiType] = File::get($this->stubsDirectory . 'Register/'. CamelCase::convertToCamelCase($uiType). '.html');
196
197
			return $this->inputStub[$uiType];
198
		}
199
	}
200
201
	/**
202
	 * Get output filename
203
	 *
204
	 *
205
	 * @return $this
206
	 */
207
	protected function getOutputFilename()
208
	{
209
		$folder = PathParser::parse($this->scaffolderConfig->generator->paths->pages).$this->modelData->tableName.'/register/' ;
210
211
		Directory::createIfNotExists($folder, 0755, true);
212
213
		return $folder .$this->modelData->tableName . '_register.html';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $folder . $this->...ame . '_register.html'; (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...
214
	}
215
216
}