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

TranslateCompiler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A replaceAndStore() 0 8 1
A replaceArea() 0 5 1
A replaceColumns() 0 14 2
A getOutputFilename() 0 8 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\Directory;
10
use Scaffolder\Support\CamelCase;
11
use stdClass ;
12
13
14
class TranslateCompiler extends AbstractCompiler
15
{
16
	protected $cachePrefix 	= 'translate_';
17
	protected $stubFilename = 'Translate.js' ;
18
19
	public function __construct($scaffolderConfig, $modelData = null)
20
	{
21
		$this->stubsDirectory = __DIR__ . '/../../../../stubs/AngularJs/';
22
		parent::__construct($scaffolderConfig, $modelData);
23
	}
24
25
	/**
26
	 * Replace and store the Stub.
27
	 *
28
	 * @return string
29
	 */
30
	public function replaceAndStore()
31
	{
32
		
33
		return $this->replaceColumns()
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->replaceCol...modelData->modelHash)); (Scaffolder\Compilers\AngularJs\TranslateCompiler) is incompatible with the return type declared by the abstract method Scaffolder\Compilers\Abs...mpiler::replaceAndStore of type string.

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...
34
					->replaceArea()
35
					->store(new FileToCompile(false, $this->modelData->modelHash));
36
		
37
	}
38
39
	/**
40
	 * replace columns
41
	 *
42
	 * @return $this
43
	 */
44
	public function replaceArea(){
45
		$this->stub = str_replace('{{singular}}', ucwords($this->modelData->tableName), $this->stub); 
46
		$this->stub = str_replace('{{plural}}', ucwords(CamelCase::pluralize($this->modelData->tableName)), $this->stub); 
47
		return $this;
48
	}
49
50
	/**
51
	 * replace columns
52
	 *
53
	 * @return $this
54
	 */
55
	public function replaceColumns(){
56
57
		$columns = [];
58
59
		foreach ($this->modelData->fields as $field)
60
		{
61
			array_push($columns,  "\t\t".sprintf('"%s" : "%s"',  $field->name,  CamelCase::convertToCamelCase($field->name) ));
62
		}
63
64
		$this->stub = str_replace('{{columns}}', join(",\n", $columns), $this->stub); 
65
66
		return $this ;
67
68
	}
69
	
70
	/**
71
	 * Get output filename
72
	 *
73
	 *
74
	 * @return $this
75
	 */
76
	protected function getOutputFilename()
77
	{
78
		$folder = PathParser::parse($this->scaffolderConfig->generator->paths->pages).$this->modelData->tableName.'/i18n/' ;
79
80
		Directory::createIfNotExists($folder, 0755, true);
81
82
		return $folder . 'en.json';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $folder . 'en.json'; (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...
83
	}
84
85
}