Completed
Push — master ( 55b21a...aec22b )
by Thomas
06:20
created

CodeGenerator::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace gossi\codegen\generator;
3
4
use gossi\codegen\config\CodeGeneratorConfig;
5
use gossi\codegen\model\GenerateableInterface;
6
7
/**
8
 * Code generator
9
 *
10
 * Generates code for any generateable model
11
 *
12
 * @author Thomas Gossmann
13
 */
14
class CodeGenerator {
15
	
16
	const SORT_USESTATEMENTS_DEFAULT = 'default';
17
	
18
	const SORT_CONSTANTS_DEFAULT = 'default';
19
	
20
	const SORT_PROPERTIES_DEFAULT = 'default';
21
	
22
	const SORT_METHODS_DEFAULT = 'default';
23
24
	/** @var CodeGeneratorConfig */
25
	protected $config;
26
27
	/**
28
	 *
29
	 * @param CodeGeneratorConfig|array $config
30
	 */
31 8
	public function __construct($config = null) {
32 8
		$this->configure($config);
33 8
		$this->generator = new ModelGenerator($this->config);
0 ignored issues
show
Bug introduced by
The property generator does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34 8
	}
35
	
36 2
	protected function configure($config = null) {
37 2
		if (is_array($config)) {
38 1
			$this->config = new CodeGeneratorConfig($config);
39 1
		} else if ($config instanceof CodeGeneratorConfig) {
40 1
			$this->config = $config;
41
		} else {
42 1
			$this->config = new CodeGeneratorConfig();
43
		}
44 2
	}
45
46
	/**
47
	 * Returns the used configuration
48
	 *
49
	 * @return CodeGeneratorConfig
50
	 */
51 1
	public function getConfig() {
52 1
		return $this->config;
53
	}
54
55
	/**
56
	 * Generates code from a given model
57
	 *
58
	 * @param GenerateableInterface $model
59
	 * @return string the generated code
60
	 */
61 6
	public function generate(GenerateableInterface $model) {
62
// 		if ($this->config->getGenerateDocblock()) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
63
// 			$model->generateDocblock();
64
// 		}
65
66 6
		$generator = new ModelGenerator($this->config);
67 6
		return $generator->generate($model);
1 ignored issue
show
Documentation introduced by
$model is of type object<gossi\codegen\model\GenerateableInterface>, but the function expects a object<gossi\codegen\model\AbstractModel>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
	}
69
70
}
71