PageLayoutCompiler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 113
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 113
loc 113
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A compile() 10 10 1
A store() 8 8 1
A setPageTitle() 6 6 1
A setAppName() 6 6 1
A setLinks() 18 18 2
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
10 View Code Duplication
class PageLayoutCompiler 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...
11
{
12
    /**
13
     * Compiles the page layout.
14
     *
15
     * @param      $stub
16
     * @param      $modelName
17
     * @param      $modelData
18
     * @param      $scaffolderConfig
19
     * @param      $hash
20
     * @param null $extra
21
     *
22
     * @return string
23
     */
24
    public function compile($stub, $modelName, $modelData, $scaffolderConfig, $hash, $extra = null)
0 ignored issues
show
Unused Code introduced by
The parameter $modelData 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...
Unused Code introduced by
The parameter $hash 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...
25
    {
26
        $this->stub = $stub;
27
28
        return $this->setPageTitle($scaffolderConfig)
29
            ->setAppName($scaffolderConfig)
30
            ->setLinks($extra['links'], $scaffolderConfig)
31
            ->replaceRoutePrefix($scaffolderConfig->generator->routing->prefix)
32
            ->store($modelName, $scaffolderConfig, $this->stub, new FileToCompile(null, null));
33
    }
34
35
    /**
36
     * Store the compiled stub.
37
     *
38
     * @param               $modelName
39
     * @param               $scaffolderConfig
40
     * @param               $compiled
41
     * @param FileToCompile $fileToCompile
42
     *
43
     * @return string
44
     */
45
    protected function store($modelName, $scaffolderConfig, $compiled, FileToCompile $fileToCompile)
0 ignored issues
show
Unused Code introduced by
The parameter $fileToCompile 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...
46
    {
47
        $path = PathParser::parse($scaffolderConfig->generator->paths->views) . 'layouts/page.blade.php';
48
49
        File::put($path, $compiled);
50
51
        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...
52
    }
53
54
    /**
55
     * Replace the page title.
56
     *
57
     * @param $scaffolderConfig
58
     *
59
     * @return $this
60
     */
61
    private function setPageTitle($scaffolderConfig)
62
    {
63
        $this->stub = str_replace('{{page_title}}', $scaffolderConfig->userInterface->pageTitle, $this->stub);
64
65
        return $this;
66
    }
67
68
    /**
69
     * Replace the app name.
70
     *
71
     * @param $scaffolderConfig
72
     *
73
     * @return $this
74
     */
75
    private function setAppName($scaffolderConfig)
76
    {
77
        $this->stub = str_replace('{{app_name}}', $scaffolderConfig->name, $this->stub);
78
79
        return $this;
80
    }
81
82
    /**
83
     * Add links to the nav.
84
     *
85
     * @param $links
86
     * @param $scaffolderConfig
87
     *
88
     * @return $this
89
     */
90
    private function setLinks($links, $scaffolderConfig)
91
    {
92
        $navLinks = '';
93
94
        foreach ($links as $link)
95
        {
96
            $navLinks .= sprintf("
97
            <li>
98
                <a href='/%s' class='waves-effect'>
99
                    %ss
100
                </a>
101
            </li>", $scaffolderConfig->generator->routing->prefix . '/' . strtolower($link), $link);
102
        }
103
104
        $this->stub = str_replace('{{links}}', $navLinks, $this->stub);
105
106
        return $this;
107
    }
108
109
    /**
110
     * Replace the route 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
}