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

RoutineBuilderPart::generate()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
namespace gossi\codegen\generator\builder\parts;
3
4
use gossi\codegen\model\AbstractModel;
5
use gossi\codegen\model\RoutineInterface;
6
7
Trait RoutineBuilderPart {
8
	
9
	use TypeBuilderPart;
10
	
11
	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...
12
	
13 15
	protected function writeFunctionStatement(RoutineInterface $model) {
14 15
		$this->writer->write('function ');
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...
15
		
16 15
		if ($model->isReferenceReturned()) {
17 2
			$this->writer->write('& ');
18
		}
19
		
20 15
		$this->writer->write($model->getName() . '(');
21 15
		$this->writeParameters($model);
22 15
		$this->writer->write(')');
23 15
		$this->writeFunctionReturnType($model);
24 15
	}
25
	
26 15
	protected function writeParameters(RoutineInterface $model) {
27 15
		$first = true;
28 15
		foreach ($model->getParameters() as $parameter) {
29 5
			if (!$first) {
30 3
				$this->writer->write(', ');
31
			}
32 5
			$first = false;
33
	
34 5
			$this->generate($parameter);
35
		}
36 15
	}
37
	
38 15
	protected function writeFunctionReturnType(RoutineInterface $model) {
39 15
		$type = $this->getType($model, $this->config->getGenerateReturnTypeHints());
2 ignored issues
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...
Documentation introduced by
$model is of type object<gossi\codegen\model\RoutineInterface>, 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...
40 15
		if ($type !== null) {
41 2
			$this->writer->write(': ')->write($type);
42
		}
43 15
	}
44
	
45 14
	protected function writeBody(RoutineInterface $model) {
46 14
		$this->writer->writeln(' {')->indent();
47 14
		$this->writer->writeln(trim($model->getBody()));
48 14
		$this->writer->outdent()->rtrim()->writeln("}");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal } does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
49 14
	}
50
}
51