RegisterControllerCompiler::replaceFileUpload()   C
last analyzed

Complexity

Conditions 11
Paths 176

Size

Total Lines 62
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 5.7264
c 0
b 0
f 0
cc 11
eloc 35
nc 176
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
11
class RegisterControllerCompiler extends AbstractCompiler
12
{
13
	protected $cachePrefix 	= 'register_controller_';
14
	protected $stubFilename = 'RegisterController.js' ;
15
16
	public function __construct($scaffolderConfig, $modelData = null)
17
	{
18
		$this->stubsDirectory = __DIR__ . '/../../../../stubs/AngularJs/';
19
		parent::__construct($scaffolderConfig, $modelData);
20
	}
21
22
	/**
23
	 * Replace and store the Stub.
24
	 *
25
	 * @return string
26
	 */
27
	public function replaceAndStore()
28
	{
29
		
30
		return $this
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->replaceFil...modelData->modelHash)); (Scaffolder\Compilers\Ang...isterControllerCompiler) 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...
31
					->replaceFileUpload()
32
					->replaceNewTags()
33
					->store(new FileToCompile(false, $this->modelData->modelHash));
34
		
35
	}
36
37
	public function replaceNewTags()
38
	{
39
		foreach ($this->modelData->fields as $field)
40
		{
41
42
			if($field->type->ui == 'autoComplete' || $field->type->ui == 'multipleAutoComplete')
43
			{
44
				$this->stub = str_replace('{{table_from}}', $field->table_from, $this->stub);
45
			}
46
47
48
		}
49
50
		return $this;
51
	}
52
53
	private function replaceFileUpload()
54
	{
55
		$fileStub = File::get($this->stubsDirectory . '/Register/FileRegisterControllerStub.php');
56
		$this->stub = str_replace('{{file_upload}}', $fileStub, $this->stub);
57
58
		$keyAutoComplete = $keyMultipleAutoComplete = $keyCheckbox = $keyCheckboxTree = false;
59
60
		foreach ($this->modelData->fields as $field)
61
		{
62
63
			//Verifica os tipos de UI no model.json e realiza a troca das tags pelas funções JS se necessário
64
			switch ($field->type->ui) {
65
				case 'autoComplete':
66
					$autoStub = $this->changeStubDataAndTag('AutoComplete', '{{auto_complete}}', $field->table_from);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $autoStub is correct as $this->changeStubDataAnd...}', $field->table_from) (which targets Scaffolder\Compilers\Ang...:changeStubDataAndTag()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$autoStub 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...
67
					$keyAutoComplete = true;
68
					break;
69
				case 'multipleAutoComplete':
70
					$multipleAutoStub = $this->changeStubDataAndTag('MultipleAutoComplete','{{multiple_auto_complete}}',$field->table_from);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $multipleAutoStub is correct as $this->changeStubDataAnd...}', $field->table_from) (which targets Scaffolder\Compilers\Ang...:changeStubDataAndTag()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$multipleAutoStub 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...
71
					$keyMultipleAutoComplete = true;
72
					break;
73
				case 'checkbox':
74
					$checkboxStub = $this->changeStubDataAndTag('Checkbox','{{checkbox}}', $field->table_from);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $checkboxStub is correct as $this->changeStubDataAnd...}', $field->table_from) (which targets Scaffolder\Compilers\Ang...:changeStubDataAndTag()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$checkboxStub 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...
75
					$keyCheckbox = true;
76
					break;
77
				case 'checkboxTree':
78
					$checkboxTreeStub = $this->changeStubDataAndTag('CheckboxTree','{{checkbox_tree}}', $field->table_from);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $checkboxTreeStub is correct as $this->changeStubDataAnd...}', $field->table_from) (which targets Scaffolder\Compilers\Ang...:changeStubDataAndTag()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
$checkboxTreeStub 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...
79
					$keyCheckboxTree = true;
80
					break;
81
				default:
82
					break;
83
			}
84
85
			if($field->foreignKey)
86
			{
87
				$this->stub = str_replace('{{foreign_table}}', $field->foreignKey->table, $this->stub);
88
			}
89
90
91
		}
92
		
93
		if(!$keyAutoComplete)
94
		{
95
			$this->stub = str_replace('{{auto_complete}}', ' ', $this->stub);
96
		}
97
98
		if(!$keyMultipleAutoComplete)
99
		{
100
			$this->stub = str_replace('{{multiple_auto_complete}}', ' ', $this->stub);
101
		}
102
103
		if(!$keyCheckbox)
104
		{
105
			$this->stub = str_replace('{{checkbox}}', ' ', $this->stub);
106
		}			
107
108
		if(!$keyCheckboxTree)
109
		{
110
			$this->stub = str_replace('{{checkbox_tree}}', ' ', $this->stub);
111
		}
112
113
		return $this;
114
	}
115
	
116
117
	public function changeStubDataAndTag($stubName,$stubTag,$table_from)
118
	{
119
		$stubFile = File::get($this->stubsDirectory . '/Register/'.$stubName.'ControllerStub.php');
120
		$stubFile = str_replace('{{table_from}}', $table_from, $stubFile);
121
		$this->stub = str_replace($stubTag, $stubFile, $this->stub);
122
	}
123
124
	/**
125
	 * Get output filename
126
	 *
127
	 *
128
	 * @return $this
129
	 */
130
	protected function getOutputFilename()
131
	{
132
		$folder = PathParser::parse($this->scaffolderConfig->generator->paths->pages).$this->modelData->tableName.'/register/' ;
133
134
		Directory::createIfNotExists($folder, 0755, true);
135
136
		return $folder .$this->modelData->tableName . '_register.controller.js';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $folder . $this->...egister.controller.js'; (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...
137
	}
138
139
}