Completed
Push — master ( bbdfc2...ebac7c )
by Thomas
05:30
created

CodeService::dumpStruct()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.2
cc 4
eloc 8
nc 2
nop 2
1
<?php
2
namespace keeko\tools\services;
3
4
use gossi\codegen\generator\CodeFileGenerator;
5
use gossi\codegen\model\AbstractPhpStruct;
6
use gossi\docblock\tags\AuthorTag;
7
use keeko\framework\schema\PackageSchema;
8
use keeko\tools\utils\NamespaceResolver;
9
use phootwork\file\File;
10
use phootwork\file\Path;
11
use Propel\Generator\Model\Table;
12
13
class CodeService extends AbstractService {
14
15
	/**
16
	 * Adds authors to the docblock of the given struct
17
	 *
18
	 * @param AbstractPhpStruct $struct
19
	 * @param PackageSchema $package
20
	 */
21
	public function addAuthors(AbstractPhpStruct $struct, PackageSchema $package = null) {
22
		if ($package === null) {
23
			$package = $this->packageService->getPackage();
24
		}
25
		$docblock = $struct->getDocblock();
26
27
		foreach ($package->getAuthors() as $author) {
28
			/* @var $author AuthorSchema */
29
			$tag = AuthorTag::create()->setName($author->getName());
30
			$mail = $author->getEmail();
31
32
			if (!empty($mail)) {
33
				$tag->setEmail($mail);
34
			}
35
36
			$docblock->appendTag($tag);
37
		}
38
	}
39
40
	/**
41
	 * Helper to represent an array as php code
42
	 *
43
	 * @param array $array
44
	 * @return string
45
	 */
46
	public function arrayToCode(array $array) {
47
		$fields = '';
48
		foreach ($array as $item) {
49
			$fields .= sprintf("'%s', ", $item);
50
		}
51
52
		if (strlen($fields) > 0) {
53
			$fields = substr($fields, 0, -2);
54
		}
55
56
		return sprintf('[%s]', $fields);
57
	}
58
59
	public function mapToCode(array $array) {
60
		$fields = '';
61
		foreach ($array as $key => $value) {
62
			$fields .= sprintf("\t'%s' => '%s',\n", $key, $value);
63
		}
64
65
		if (strlen($fields) > 0) {
66
			$fields = substr($fields, 0, -2);
67
		}
68
69
		return sprintf("[\n%s\n]", $fields);
70
	}
71
72
	/**
73
	 * Returns the filename for a given struct
74
	 *
75
	 * @param AbstractPhpStruct $struct
76
	 * @return string
77
	 */
78
	public function getFilename(AbstractPhpStruct $struct) {
79
		$package = $this->packageService->getPackage();
80
		$relativeSourcePath = NamespaceResolver::getSourcePath($struct->getNamespace(), $package);
81
82
		if ($relativeSourcePath === null) {
83
			return null;
84
		}
85
86
		$jsonFile = $this->project->getComposerFileName();
87
		$path = new Path(dirname($jsonFile));
88
		$path = $path->append($relativeSourcePath);
89
		$path = $path->append($struct->getName() . '.php');
90
		return $path->toString();
91
	}
92
93
	/**
94
	 * Returns a file object for a given struct
95
	 *
96
	 * @param AbstractPhpStruct $struct
97
	 * @return File
98
	 */
99
	public function getFile(AbstractPhpStruct $struct) {
100
		return new File($this->getFilename($struct));
101
	}
102
103
	public function dumpStruct(AbstractPhpStruct $struct, $overwrite = false) {
104
		$filename = $this->getFilename($struct);
105
		$file = new File($filename);
106
107
		if ($filename !== null && ($file->exists() ? $overwrite : true)) {
108
			// generate code
109
			$generator = new CodeFileGenerator();
110
			$code = $generator->generate($struct);
0 ignored issues
show
Documentation introduced by
$struct is of type object<gossi\codegen\model\AbstractPhpStruct>, but the function expects a object<gossi\codegen\model\GenerateableInterface>.

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...
111
112
			// write code to file
113
			$file->write($code);
114
115
			// tell user about
116
			$this->io->writeln(sprintf('Class <info>%s</info> written at <info>%s</info>', $struct->getQualifiedName(), $filename));
117
		}
118
	}
119
}
120