Completed
Push — master ( 63d8d6...28be9d )
by Raphael
11:25
created

ListTemplateCompiler::getInputStubByField()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 24
Code Lines 15

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
dl 24
loc 24
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 8
nop 1
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 ListTemplateCompiler extends AbstractCompiler
15
{
16
	protected $cachePrefix 	= 'list_template_';
17
	protected $stubFilename = 'ListTemplate.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->replaceInputFields()
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->replaceInp...modelData->modelHash)); (Scaffolder\Compilers\Ang...Js\ListTemplateCompiler) 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
					->replaceBelongsToManyFields()
35
					->store(new FileToCompile(false, $this->modelData->modelHash));
36
		
37
	}
38
39
	/**
40
	 * Replace input fields
41
	 *
42
	 * @return $this
43
	 */
44
	private function replaceInputFields(){
45
46
		$inputFields = $this->getInputFields();
47
48
		$this->stub = str_replace('{{columns_inputs}}', $inputFields, $this->stub);
49
50
		return $this;
51
	}
52
53
	/**
54
	 * get search conditions
55
	 *
56
	 * @return $this
57
	 */
58 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...
59
60
		$inputFields = $eagerFields = '';
61
62
		foreach ($this->modelData->fields as $field)
63
		{
64
			$fieldStub = $this->getInputStubByField($field);
65
66
			if($field->foreignKey){
67
				$fieldStub 	= $this->replaceForeingStrings($field, $fieldStub) ;
68
				$fieldStub = str_replace('{{foreign_model_name}}', strtolower(CamelCase::convertToCamelCase($field->foreignKey->table)), $fieldStub);
69
			}
70
71
			$inputFields .= $this->replaceFieldInput($field, $fieldStub) ;
72
73
			// Check foreign key
74
			if ($field->foreignKey && isset($field->foreignKey->eager) && $field->foreignKey->eager)
75
			{
76
				// search eager fields
77
				$foreignModelData = $this->getModelData($field->foreignKey->table);
78
				$foreignControllerCompiler = new ListTemplateCompiler($this->scaffolderConfig, $foreignModelData);
79
				$foreignControllerCompiler->setEagerTable($this->modelData->tableName);
80
				$eagerFields 	.= $foreignControllerCompiler->getInputFields();
81
			}
82
83
		}
84
85
		// replace table name
86
		$inputFields = str_replace('{{table_name}}', $this->modelData->tableName, $inputFields);
87
		
88
		$this->stub = str_replace('{{eager_objects_inputs}}', $eagerFields, $this->stub); 
89
90
		return $inputFields ;
91
92
	}
93
94
	/**
95
	 * replace field stub with fields and validations
96
	 *
97
	 * @param string $field
98
	 * @param string $fieldStub
99
	 *
100
	 * @return $this
101
	 */
102
	protected function replaceFieldInput($field, $fieldStub){
103
		$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...
104
		$fieldStub = $this->replaceFieldValidations($field, $fieldStub) ;
105
106
		return $fieldStub ;
107
	}
108
109
	/**
110
	 * replace field stub with fields and validations
111
	 *
112
	 * @param string $field
113
	 * @param string $fieldStub
114
	 *
115
	 * @return $this
116
	 */
117 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...
118
		$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...
119
		
120
		$validationsConverted = Validator::convertValidations($field->validations, true);
121
122
		$inputValidations = '' ; 
123
124
		foreach ($validationsConverted as $attribute => $value) {
125
			if($value)
126
				$inputValidations .=  ' '.$attribute.'="'. $value.'"' ;
127
			else
128
				$inputValidations .=  ' '.$attribute  ; 
129
		}
130
131
		$fieldStub = str_replace('{{field_validation}}', $inputValidations, $fieldStub);
132
133
		return $fieldStub ;
134
	}
135
136
	/**
137
	 * Replace belongs to many fields
138
	 *
139
	 * @return $this
140
	 */
141
	protected function replaceBelongsToManyFields() {
142
		
143
		$belongToManyFields = '';
144
		
145
		foreach ($this->modelData->reverseRelationships as $relationship)
146
		{
147
			if ($relationship->type == "belongsToMany") {
148
				$fieldStub = File::get($this->stubsDirectory . 'List/'. CamelCase::convertToCamelCase($relationship->ui). '.html');
149
150
				$fieldStub = str_replace('{{related_table}}',CamelCase::convertToCamelCase($relationship->relatedTable), $fieldStub);
151
				$fieldStub = str_replace('{{related_table_lw}}', strtolower(CamelCase::convertToCamelCase($relationship->relatedTable)), $fieldStub);
152
				$fieldStub = str_replace('{{table_name}}', $this->modelData->tableName, $fieldStub);
153
				$fieldStub = str_replace('{{related_table_lw_pl}}', CamelCase::pluralize(strtolower($relationship->relatedTable)), $fieldStub);
154
				
155
				$belongToManyFields .= $fieldStub;	
156
			}
157
		}
158
159
		$this->stub = str_replace('{{belongs_to_many_inputs}}', $belongToManyFields, $this->stub); 
160
161
		return $this;
162
	}
163
164
	
165
	/**
166
	 * get input field stub by ui type
167
	 *
168
	 * @param string $field
169
	 *
170
	 * @return $this
171
	 */
172
	private $inputStub = [];
173 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...
174
		
175
		if($field->index == 'primary'){
176
			$uiType = 'primary' ;
177
		}
178
		elseif(isset($field->foreignKey) && $field->foreignKey){
179
			if(isset($field->foreignKey->eager) && $field->foreignKey->eager)
180
				$uiType = 'foreign_eager' ;
181
			else 
182
				$uiType = $field->type->ui ;
183
		}
184
		else {
185
			$uiType = $field->type->ui ;
186
		}
187
188
		if(array_key_exists($uiType, $this->inputStub)){
189
			return $this->inputStub[$uiType];
190
		}
191
		else {
192
			$this->inputStub[$uiType] = File::get($this->stubsDirectory . 'List/'. CamelCase::convertToCamelCase($uiType). '.html');
193
194
			return $this->inputStub[$uiType];
195
		}
196
	}
197
	
198
	/**
199
	 * Get output filename
200
	 *
201
	 *
202
	 * @return $this
203
	 */
204
	protected function getOutputFilename()
205
	{
206
		$folder = PathParser::parse($this->scaffolderConfig->generator->paths->pages).$this->modelData->tableName.'/list/' ;
207
208
		Directory::createIfNotExists($folder, 0755, true);
209
210
		return $folder .$this->modelData->tableName . '_list.html';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $folder . $this->...bleName . '_list.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...
211
	}
212
213
}