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

EditViewCompiler   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 135
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 135
loc 135
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 18 18 2
A store() 22 22 2
A addFields() 22 22 3
A replaceRoutePrefix() 6 6 1
A replacePrimaryKey() 17 17 3

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\Blade;
4
5
use Illuminate\Support\Facades\File;
6
use Scaffolder\Compilers\AbstractViewCompiler;
7
use Scaffolder\Compilers\Support\FileToCompile;
8
use Scaffolder\Compilers\Support\InputTypeResolverTrait;
9
use Scaffolder\Compilers\Support\PathParser;
10
use Scaffolder\Support\Directory;
11
12 View Code Duplication
class EditViewCompiler extends AbstractViewCompiler
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: getOutputFilename, replaceAndStore
Loading history...
Duplication introduced by
This class 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...
13
{
14
	use InputTypeResolverTrait;
15
16
	/**
17
	 * Compiles the edit view.
18
	 *
19
	 * @param      $stub
20
	 * @param      $modelName
21
	 * @param      $modelData
22
	 * @param      $scaffolderConfig
23
	 * @param      $hash
24
	 * @param null $extra
25
	 *
26
	 * @return string
27
	 */
28
	public function compile($stub, $modelName, $modelData, $scaffolderConfig, $hash, $extra = null)
0 ignored issues
show
Unused Code introduced by
The parameter $extra is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
29
	{
30
		if (File::exists(base_path('scaffolder-config/cache/view_edit_' . $hash . self::CACHE_EXT)))
31
		{
32
			return $this->store($modelName, $scaffolderConfig, '', new FileToCompile(true, $hash));
33
		}
34
		else
35
		{
36
			$this->stub = $stub;
37
38
			return $this->replaceClassName($modelName)
0 ignored issues
show
Unused Code introduced by
The call to EditViewCompiler::replaceClassName() has too many arguments starting with $modelName.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
39
				->replaceBreadcrumb($modelName)
40
				->addFields($modelData)
41
				->replacePrimaryKey($modelData)
42
				->replaceRoutePrefix($scaffolderConfig->generator->routing->prefix)
43
				->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
44
		}
45
	}
46
47
	/**
48
	 * Store the compiled stub.
49
	 *
50
	 * @param               $modelName
51
	 * @param               $scaffolderConfig
52
	 * @param               $compiled
53
	 * @param FileToCompile $fileToCompile
54
	 *
55
	 * @return string
56
	 */
57
	protected function store($modelName, $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
58
	{
59
		$folder = PathParser::parse($scaffolderConfig->generator->paths->views) . strtolower($modelName) ;
60
		
61
		// create folder directory
62
		Directory::createIfNotExists($folder, 0755, true);
63
64
		$path   = $folder . '/edit.blade.php';
65
66
		// Store in cache
67
		if ($fileToCompile->cached)
68
		{
69
			File::copy(base_path('scaffolder-config/cache/view_edit_' . $fileToCompile->hash . self::CACHE_EXT), $path);
70
		}
71
		else
72
		{
73
			File::put(base_path('scaffolder-config/cache/view_edit_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
74
			File::copy(base_path('scaffolder-config/cache/view_edit_' . $fileToCompile->hash . self::CACHE_EXT), $path);
75
		}
76
77
		return $path;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $path; (string) is incompatible with the return type of the parent method Scaffolder\Compilers\AbstractCompiler::store 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...
78
	}
79
80
	/**
81
	 * Add fields to the edit view.
82
	 *
83
	 * @param $modelData
84
	 *
85
	 * @return $this
86
	 */
87
	private function addFields($modelData)
88
	{
89
		$fields = '';
90
		$firstIteration = true;
91
92
		foreach ($modelData->fields as $field)
93
		{
94
			if ($firstIteration)
95
			{
96
				$fields .= sprintf(self::getInputFor($field) . PHP_EOL, $field->name);
97
				$firstIteration = false;
98
			}
99
			else
100
			{
101
				$fields .= sprintf("\t" . self::getInputFor($field) . PHP_EOL, $field->name);
102
			}
103
		}
104
105
		$this->stub = str_replace('{{fields}}', $fields, $this->stub);
106
107
		return $this;
108
	}
109
110
	/**
111
	 * Replace the prefix.
112
	 *
113
	 * @param $prefix
114
	 *
115
	 * @return $this
116
	 */
117
	private function replaceRoutePrefix($prefix)
118
	{
119
		$this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
120
121
		return $this;
122
	}
123
124
	/**
125
	 * Replace the primary key.
126
	 *
127
	 * @param $modelData
128
	 */
129
	private function replacePrimaryKey($modelData)
130
	{
131
		$primaryKey = 'id';
132
133
		foreach ($modelData->fields as $field)
134
		{
135
			if ($field->index == 'primary')
136
			{
137
				$primaryKey = $field->name;
138
				break;
139
			}
140
		}
141
142
		$this->stub = str_replace('{{primaryKey}}', $primaryKey, $this->stub);
143
144
		return $this;
145
	}
146
}