CodeService::dumpStruct()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.2
c 0
b 0
f 0
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
12
class CodeService extends AbstractService {
13
14
	/**
15
	 * Adds authors to the docblock of the given struct
16
	 *
17
	 * @param AbstractPhpStruct $struct
18
	 * @param PackageSchema $package
19
	 */
20
	public function addAuthors(AbstractPhpStruct $struct, PackageSchema $package = null) {
21
		if ($package === null) {
22
			$package = $this->packageService->getPackage();
23
		}
24
		$docblock = $struct->getDocblock();
25
26
		foreach ($package->getAuthors() as $author) {
27
			/* @var $author AuthorSchema */
28
			$tag = AuthorTag::create()->setName($author->getName());
29
			$mail = $author->getEmail();
30
31
			if (!empty($mail)) {
32
				$tag->setEmail($mail);
33
			}
34
35
			$docblock->appendTag($tag);
36
		}
37
	}
38
39
	/**
40
	 * Helper to represent an array as php code
41
	 *
42
	 * @param array $array
43
	 * @return string
44
	 */
45
	public function arrayToCode(array $array) {
46
		$fields = '';
47
		foreach ($array as $item) {
48
			$fields .= sprintf("'%s', ", $item);
49
		}
50
51
		if (strlen($fields) > 0) {
52
			$fields = substr($fields, 0, -2);
53
		}
54
55
		return sprintf('[%s]', $fields);
56
	}
57
58
	public function mapToCode(array $array) {
59
		$fields = '';
60
		foreach ($array as $key => $value) {
61
			$fields .= sprintf("\t'%s' => '%s',\n", $key, $value);
62
		}
63
64
		if (strlen($fields) > 0) {
65
			$fields = substr($fields, 0, -2);
66
		}
67
68
		return sprintf("[\n%s\n]", $fields);
69
	}
70
71
	/**
72
	 * Returns the filename for a given struct
73
	 *
74
	 * @param AbstractPhpStruct $struct
75
	 * @return string
76
	 */
77
	public function getFilename(AbstractPhpStruct $struct) {
78
		$package = $this->packageService->getPackage();
79
		$relativeSourcePath = NamespaceResolver::getSourcePath($struct->getNamespace(), $package);
80
81
		if ($relativeSourcePath === null) {
82
			return null;
83
		}
84
85
		$jsonFile = $this->project->getComposerFileName();
86
		$path = new Path(dirname($jsonFile));
87
		$path = $path->append($relativeSourcePath);
88
		$path = $path->append($struct->getName() . '.php');
89
		return $path->toString();
90
	}
91
92
	/**
93
	 * Returns a file object for a given struct
94
	 *
95
	 * @param AbstractPhpStruct $struct
96
	 * @return File
97
	 */
98
	public function getFile(AbstractPhpStruct $struct) {
99
		return new File($this->getFilename($struct));
100
	}
101
102
	public function dumpStruct(AbstractPhpStruct $struct, $overwrite = false) {
103
		$filename = $this->getFilename($struct);
104
		$file = new File($filename);
105
106
		if ($filename !== null && ($file->exists() ? $overwrite : true)) {
107
			// generate code
108
			$generator = new CodeFileGenerator();
109
			$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...
110
111
			// write code to file
112
			$file->write($code);
113
114
			// tell user about
115
			$this->io->writeln(sprintf('Class <info>%s</info> written at <info>%s</info>', $struct->getQualifiedName(), $filename));
116
		}
117
	}
118
}
119