IndexModuleCompiler::compileGroup()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Scaffolder\Compilers\AngularJs;
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 IndexModuleCompiler extends AbstractCompiler
12
{
13
	protected $stubFilename = 'IndexModule.js' ;
14
15
	protected $stubResourceFilename = 'IndexModuleModel.js' ;
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/AngularJs/';
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($compiledIndexes)
53
	{
54
55
		$this->replaceIndexes($compiledIndexes)
56
			->store(new FileToCompile(null, null));
57
58
		return $this->stub;
59
	}
60
61
62
	/**
63
	 * Get output filename
64
	 *
65
	 *
66
	 * @return $this
67
	 */
68
	protected function getOutputFilename()
69
	{
70
		$folder = PathParser::parse($this->scaffolderConfig->generator->paths->index);
71
72
		return $folder  . 'index.module.js';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $folder . 'index.module.js'; (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...
73
	}
74
75
76
	/**
77
	 * Replace the resource.
78
	 *
79
	 * @param $this->modelName
80
	 *
81
	 * @return string routeStub
82
	 */
83
	public function replaceResource($modelData)
84
	{
85
		
86
		$indexStub = str_replace('{{table_name_uc}}', $modelData->modelName, $this->stubResource);
87
		$indexStub = str_replace('{{table_name}}', $modelData->tableName, $indexStub);
88
		
89
		return $indexStub;
90
	}
91
92
	/**
93
	 * Replace compiled routes.
94
	 *
95
	 * @param $compiledRoutes
96
	 *
97
	 * @return $this
98
	 */
99
	private function replaceIndexes($compiledIndexes)
100
	{
101
		$this->stub = str_replace('{{tables}}', $compiledIndexes, $this->stub);
102
103
		return $this;
104
	}
105
106
	
107
}