IndexViewCompiler   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 149
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 149
loc 149
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
B addDatatableFields() 26 26 4
B setTableHeaders() 26 26 4
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\PathParser;
9
use Scaffolder\Support\Directory;
10
11 View Code Duplication
class IndexViewCompiler 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...
12
{
13
    /**
14
     * Compiles the index view.
15
     *
16
     * @param      $stub
17
     * @param      $modelName
18
     * @param      $modelData
19
     * @param      $scaffolderConfig
20
     * @param      $hash
21
     * @param null $extra
22
     *
23
     * @return string
24
     */
25
    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...
26
    {
27
        if (File::exists(base_path('scaffolder-config/cache/view_index_' . $hash . self::CACHE_EXT)))
28
        {
29
            return $this->store($modelName, $scaffolderConfig, '', new FileToCompile(true, $hash));
30
        }
31
        else
32
        {
33
            $this->stub = $stub;
34
35
            return $this->replaceClassName($modelName)
0 ignored issues
show
Unused Code introduced by
The call to IndexViewCompiler::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...
36
                ->replaceBreadcrumb($modelName)
37
                ->addDatatableFields($modelName, $modelData)
38
                ->setTableHeaders($modelData)
39
                ->replaceRoutePrefix($scaffolderConfig->generator->routing->prefix)
40
                ->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(false, $hash));
41
        }
42
    }
43
44
    /**
45
     * Store the compiled stub.
46
     *
47
     * @param               $modelName
48
     * @param               $scaffolderConfig
49
     * @param               $compiled
50
     * @param FileToCompile $fileToCompile
51
     *
52
     * @return string
53
     */
54
    protected function store($modelName, $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
55
    {
56
        $folder = PathParser::parse($scaffolderConfig->generator->paths->views) . strtolower($modelName) ;
57
        
58
        // create folder directory
59
        Directory::createIfNotExists($folder, 0755, true);
60
61
        $path = $folder  . '/index.blade.php';
62
63
        // Store in cache
64
        if ($fileToCompile->cached)
65
        {
66
            File::copy(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $path);
67
        }
68
        else
69
        {
70
            File::put(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $compiled);
71
            File::copy(base_path('scaffolder-config/cache/view_index_' . $fileToCompile->hash . self::CACHE_EXT), $path);
72
        }
73
74
        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...
75
    }
76
77
    /**
78
     * Datatable fields.
79
     *
80
     * @param $modelName
81
     * @param $modelData
82
     *
83
     * @return $this
84
     */
85
    private function addDatatableFields($modelName, $modelData)
86
    {
87
        $fields = '';
88
        $firstIteration = true;
89
90
        foreach ($modelData->fields as $field)
91
        {
92
            if ($field->hideInListings == false)
93
            {
94
                if ($firstIteration)
95
                {
96
                    $fields .= sprintf("{ data: '%s', name: '%s' }," . PHP_EOL, $field->name, $field->name);
97
                    $firstIteration = false;
98
                }
99
                else
100
                {
101
                    $fields .= sprintf("\t\t\t\t{ data: '%s', name: '%s' }," . PHP_EOL, $field->name, $field->name);
102
                }
103
            }
104
        }
105
106
        $this->stub = str_replace('{{datatable_fields}}', $fields, $this->stub);
107
        $this->stub = str_replace('{{datatable_url}}', ucfirst($modelName), $this->stub);
108
109
        return $this;
110
    }
111
112
    /**
113
     * Set index table headers.
114
     *
115
     * @param $modelData
116
     *
117
     * @return $this
118
     */
119
    private function setTableHeaders($modelData)
120
    {
121
        $fields = '';
122
        $firstIteration = true;
123
124
        foreach ($modelData->fields as $field)
125
        {
126
            if ($field->hideInListings == false)
127
            {
128
                if ($firstIteration)
129
                {
130
                    $fields .= sprintf("<th>%s</th>" . PHP_EOL, ucfirst($field->name));
131
                    $firstIteration = false;
132
133
                }
134
                else
135
                {
136
                    $fields .= sprintf("\t\t\t<th>%s</th>" . PHP_EOL, ucfirst($field->name));
137
                }
138
            }
139
        }
140
141
        $this->stub = str_replace('{{table_headers}}', $fields, $this->stub);
142
143
        return $this;
144
    }
145
146
    /**
147
     * Replace the prefix.
148
     *
149
     * @param $prefix
150
     *
151
     * @return $this
152
     */
153
    private function replaceRoutePrefix($prefix)
154
    {
155
        $this->stub = str_replace('{{route_prefix}}', $prefix, $this->stub);
156
157
        return $this;
158
    }
159
}