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

CreateViewCompiler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 111
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 17 17 2
A store() 22 22 2
A addFields() 22 22 3
A replaceRoutePrefix() 6 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\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 CreateViewCompiler 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 create 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_create_' . $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 CreateViewCompiler::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
				->replaceRoutePrefix($scaffolderConfig->generator->routing->prefix)
42
				->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
43
		}
44
	}
45
46
	/**
47
	 * Store the compiled stub.
48
	 *
49
	 * @param               $modelName
50
	 * @param               $scaffolderConfig
51
	 * @param               $compiled
52
	 * @param FileToCompile $fileToCompile
53
	 *
54
	 * @return string
55
	 */
56
	protected function store($modelName, $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
57
	{
58
		$folder = PathParser::parse($scaffolderConfig->generator->paths->views) . strtolower($modelName) ;
59
		
60
		// create folder directory
61
		Directory::createIfNotExists($folder, 0755, true);
62
63
		$path = $folder  . '/create.blade.php';
64
65
		// Store in cache
66
		if ($fileToCompile->cached)
67
		{
68
			File::copy(base_path('scaffolder-config/cache/view_create_' . $fileToCompile->hash . self::CACHE_EXT), $path);
69
		}
70
		else
71
		{
72
			File::put(base_path('scaffolder-config/cache/view_create_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
73
			File::copy(base_path('scaffolder-config/cache/view_create_' . $fileToCompile->hash . self::CACHE_EXT), $path);
74
		}
75
76
		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...
77
	}
78
79
	/**
80
	 * Add fields to the create view.
81
	 *
82
	 * @param $modelData
83
	 *
84
	 * @return $this
85
	 */
86
	private function addFields($modelData)
87
	{
88
		$fields = '';
89
		$firstIteration = true;
90
91
		foreach ($modelData->fields as $field)
92
		{
93
			if ($firstIteration)
94
			{
95
				$fields .= sprintf(self::getInputFor($field) . PHP_EOL, $field->name);
96
				$firstIteration = false;
97
			}
98
			else
99
			{
100
				$fields .= sprintf("\t" . self::getInputFor($field) . PHP_EOL, $field->name);
101
			}
102
		}
103
104
		$this->stub = str_replace('{{fields}}', $fields, $this->stub);
105
106
		return $this;
107
	}
108
109
	/**
110
	 * Replace the prefix.
111
	 *
112
	 * @param $prefix
113
	 *
114
	 * @return $this
115
	 */
116
	private function replaceRoutePrefix($prefix)
117
	{
118
		$this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
119
120
		return $this;
121
	}
122
}