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

StructBuilderPart::buildMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
namespace gossi\codegen\generator\builder\parts;
3
4
use gossi\codegen\model\AbstractModel;
5
use gossi\codegen\model\AbstractPhpStruct;
6
use gossi\codegen\model\ConstantsInterface;
7
use gossi\codegen\model\NamespaceInterface;
8
use gossi\codegen\model\PropertiesInterface;
9
use gossi\codegen\model\TraitsInterface;
10
use gossi\codegen\generator\ComparatorFactory;
11
12
trait StructBuilderPart {
13
	
14
	protected abstract function ensureBlankLine();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
15
	protected abstract function generate(AbstractModel $model);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
16
	
17 14
	protected function buildHeader(AbstractPhpStruct $model) {
18 14
		$this->buildNamespace($model);
19 14
		$this->buildRequiredFiles($model);
20 14
		$this->buildUseStatements($model);
21 14
		$this->buildDocblock($model);
0 ignored issues
show
Bug introduced by
It seems like buildDocblock() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
22 14
	}
23
	
24 14
	protected function buildNamespace(NamespaceInterface $model) {
25 14
		if ($namespace = $model->getNamespace()) {
26 2
			$this->writer->writeln('namespace ' . $namespace . ';');
1 ignored issue
show
Bug introduced by
The property writer 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...
27
		}
28 14
	}
29
	
30 14
	protected function buildRequiredFiles(AbstractPhpStruct $model) {
31 14
		if ($files = $model->getRequiredFiles()) {
32 14
			$this->ensureBlankLine();
33 14
			foreach ($files as $file) {
34 1
				$this->writer->writeln('require_once ' . var_export($file, true) . ';');
35
			}
36
		}
37 14
	}
38
	
39 14
	protected function buildUseStatements(AbstractPhpStruct $model) {
40 14
		if ($useStatements = $model->getUseStatements()) {
41 14
			$this->ensureBlankLine();
42 14
			foreach ($useStatements as $alias => $namespace) {
43 1
				if (false === strpos($namespace, '\\')) {
44 1
					$commonName = $namespace;
45
				} else {
46 1
					$commonName = substr($namespace, strrpos($namespace, '\\') + 1);
47
				}
48
	
49 1
				if (false === strpos($namespace, '\\') && !$model->getNamespace()) {
50
					//avoid fatal 'The use statement with non-compound name '$commonName' has no effect'
1 ignored issue
show
Unused Code Comprehensibility introduced by
43% 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...
51 1
					continue;
52
				}
53
	
54 1
				$this->writer->write('use ' . $namespace);
55
	
56 1
				if ($commonName !== $alias) {
57 1
					$this->writer->write(' as ' . $alias);
58
				}
59
	
60 1
				$this->writer->writeln(';');
61
			}
62 14
			$this->ensureBlankLine();
63
		}
64 14
	}
65
	
66 12
	protected function buildTraits(TraitsInterface $model) {
67 12
		foreach ($model->getTraits() as $trait) {
68 1
			$this->writer->write('use ');
69 1
			$this->writer->writeln($trait . ';');
70
		}
71 12
	}
72
	
73 13
	protected function buildConstants(ConstantsInterface $model) {
74 13
		foreach ($model->getConstants() as $constant) {
75 2
			$this->generate($constant);
76
		}
77 13
	}
78
	
79 12
	protected function buildProperties(PropertiesInterface $model) {
80 12
		foreach ($model->getProperties() as $property) {
81 3
			$this->generate($property);
82
		}
83 12
	}
84
	
85 14
	protected function buildMethods(AbstractPhpStruct $model) {
86 14
		foreach ($model->getMethods() as $method) {
87 3
			$this->generate($method);
88
		}
89 14
	}
90
	
91 14
	private function sortUseStatements(AbstractPhpStruct $model) {
92 14
		if ($this->config->isSortingEnabled() 
1 ignored issue
show
Bug introduced by
The property config 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...
93 14
				&& ($useStatementSorting = $this->config->getUseStatementSorting()) !== false) {
94 14
			if (is_string($useStatementSorting)) {
95 14
				$useStatementSorting = ComparatorFactory::createUseStatementComparator($useStatementSorting);
96
			}
97 14
			$model->getUseStatements()->sort($useStatementSorting);
98
		}
99 14
	}
100
	
101 13
	private function sortConstants(ConstantsInterface $model) {
102 13
		if ($this->config->isSortingEnabled()
103 13
				&& ($constantSorting = $this->config->getConstantSorting()) !== false) {
104 13
			if (is_string($constantSorting)) {
105 13
				$constantSorting = ComparatorFactory::createConstantComparator($constantSorting);
106
			}
107 13
			$model->getConstants()->sort($constantSorting);
1 ignored issue
show
Bug introduced by
The method sort cannot be called on $model->getConstants() (of type array<integer,object<gos...gen\model\PhpConstant>>).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
108
		}
109 13
	}
110
	
111 12
	private function sortProperties(PropertiesInterface $model) {
112 12
		if ($this->config->isSortingEnabled()
113 12
				&& ($propertySorting = $this->config->getPropertySorting()) !== false) {
114 12
			if (is_string($propertySorting)) {
115 12
				$propertySorting = ComparatorFactory::createPropertyComparator($propertySorting);
116
			}
117 12
			$model->getProperties()->sort($propertySorting);
118
		}	
119 12
	}
120
		
121 14
	private function sortMethods(AbstractPhpStruct $model) {
122 14
		if ($this->config->isSortingEnabled()
123 14
				&& ($methodSorting = $this->config->getMethodSorting()) !== false) {
124 14
			if (is_string($methodSorting)) {
125 14
				$methodSorting = ComparatorFactory::createMethodComparator($methodSorting);
126
			}
127 14
			$model->getMethods()->sort($methodSorting);
128
		}
129 14
	}
130
}
131