RouteCompiler   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 164
Duplicated Lines 4.27 %

Coupling/Cohesion

Components 3
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 3
cbo 4
dl 7
loc 164
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A replaceAndStore() 0 1 1
A compile() 0 1 1
A compileGroup() 0 9 1
A getOutputFilename() 0 6 1
A replaceResource() 0 10 1
B replaceReverseRelationships() 0 27 5
A replaceEnum() 0 20 3
A replaceRoutes() 0 6 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\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 RouteCompiler extends AbstractCompiler
12
{
13
	protected $stubFilename = 'Routes.php' ;
14
15
	protected $stubResourceFilename = 'ResourceRoute.php' ;
16
	protected $stubResource  ;
17
18
19 View Code Duplication
	public function __construct($scaffolderConfig, $modelData = 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...
20
	{
21
		$this->stubsDirectory = __DIR__ . '/../../../../stubs/Api/';
22
		parent::__construct($scaffolderConfig, null);
23
		
24
		$this->stubResource = File::get($this->stubsDirectory . $this->stubResourceFilename );
25
	}
26
27
	/**
28
	 * Replace and store the Stub.
29
	 *
30
	 * @return string
31
	 */
32
	public function replaceAndStore(){}
33
34
	/**
35
	 * Compiles a resource.
36
	 *
37
	 * @param      $hash
38
	 * @param null $extra
39
	 *
40
	 * @return string
41
	 */
42
	public function compile($extra = null) {}
43
44
	/**
45
	 * Compiles a group of routes.
46
	 *
47
	 * @param      $hash
48
	 * @param null $extra
0 ignored issues
show
Bug introduced by
There is no parameter named $extra. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
49
	 *
50
	 * @return mixed
51
	 */
52
	public function compileGroup($compiledRoutes)
53
	{
54
55
		$this->replaceRoutes($compiledRoutes)
56
			->replaceRoutePrefix()
57
			->store(new FileToCompile(null, null));
58
59
		return $this->stub;
60
	}
61
62
63
	/**
64
	 * Get output filename
65
	 *
66
	 *
67
	 * @return $this
68
	 */
69
	protected function getOutputFilename()
70
	{
71
		$folder = PathParser::parse($this->scaffolderConfig->generator->paths->routes);
72
73
		return $folder  . 'routes.php';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $folder . 'routes.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...
74
	}
75
76
77
	/**
78
	 * Replace the resource.
79
	 *
80
	 * @param $this->modelName
81
	 *
82
	 * @return string routeStub
83
	 */
84
	public function replaceResource($modelData)
85
	{
86
		
87
		$routeStub = str_replace('{{resource_lw}}', strtolower($modelData->modelName), $this->stubResource);
88
		$routeStub = str_replace('{{resource}}', $modelData->modelName, $routeStub);
89
		$routeStub = str_replace('{{reverseRelationships}}', $this->replaceReverseRelationships($modelData), $routeStub);
90
		$routeStub = str_replace('{{enum}}', $this->replaceEnum($modelData), $routeStub);
91
92
		return $routeStub;
93
	}
94
95
	/**
96
	 * Replace the reverse relationships.
97
	 *
98
	 * @param $this->modelData
99
	 *
100
	 * @return string functions
101
	 */
102
	public function replaceReverseRelationships($modelData)
103
	{
104
		$functions = '';
105
106
		foreach ($modelData->reverseRelationships as $relationship)
107
		{
108
			if ($relationship)
109
			{
110
				$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...
111
				if ($relationship->type == "hasOne")
112
					$functionName = strtolower($relationship->modelName);
113
				elseif ($relationship->type == "belongsToMany") 
114
					$functionName = CamelCase::pluralize(strtolower($relationship->relatedTable));
115
				else 
116
					$functionName = CamelCase::pluralize(strtolower($relationship->modelName));
117
118
				$method = "\tRoute::get('{{resource_lw}}/{id}/{{function_name}}', '{{resource}}Controller@{{function_name}}');\n";
119
				$method = str_replace('{{resource_lw}}', strtolower($modelData->modelName), $method);
120
				$method = str_replace('{{function_name}}', $functionName, $method);
121
				$method = str_replace('{{resource}}', $modelData->modelName, $method);
122
123
				$functions .= $method;
124
			}
125
		}
126
127
		return $functions;
128
	}
129
130
	/**
131
	 * Replace the enum.
132
	 *
133
	 * @param $this->modelData
134
	 *
135
	 * @return string functions
136
	 */
137
	public function replaceEnum($modelData)
138
	{
139
		$functions = '';
140
141
		foreach ($modelData->fields as $field)
142
		{
143
			if ($field->type->db == "enum")
144
			{
145
				$method = "\tRoute::get('{{resource_lw}}/{{field_name}}', '{{resource}}Controller@get{{field_name_uc}}Options');\n";
146
				$method = str_replace('{{resource_lw}}', strtolower($modelData->modelName), $method);
147
				$method = str_replace('{{field_name_uc}}', CamelCase::convertToCamelCase($field->name), $method);
148
				$method = str_replace('{{field_name}}', $field->name, $method);
149
				$method = str_replace('{{resource}}', $modelData->modelName, $method);
150
151
				$functions .= $method;
152
			}
153
		}
154
155
		return $functions;
156
	}
157
158
159
	/**
160
	 * Replace compiled routes.
161
	 *
162
	 * @param $compiledRoutes
163
	 *
164
	 * @return $this
165
	 */
166
	private function replaceRoutes($compiledRoutes)
167
	{
168
		$this->stub = str_replace('{{routes}}', $compiledRoutes, $this->stub);
169
170
		return $this;
171
	}
172
173
	
174
}
175