-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPHPFunction.php
More file actions
141 lines (122 loc) · 4.21 KB
/
PHPFunction.php
File metadata and controls
141 lines (122 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace DrupalCodeBuilder\Generator;
use DrupalCodeBuilder\Generator\FormattingTrait\PHPFormattingTrait;
/**
* Generator base class for functions.
*
* (We can't call this 'Function', as that's a reserved word.)
*
* Properties include:
* - 'declaration': The function declaration, including the function name
* and parameters, up to the closing parenthesis. Should not however
* include the opening brace of the function body.
* - 'body' The code of the function. The character '£' is replaced with
* '$' as a convenience to avoid having to keep escaping names of
* variables. This can be in one of the following forms:
* - a string, not including the enclosing function braces or the opening
* or closing newlines.
* TODO: This is not currently working, but doesn't matter as
* Hooks::getTemplates() always returns an array of lines in 'template'
* even if that's just the analysis body code.
* - an array of lines of code. These should not have their newlines.
*/
class PHPFunction extends BaseGenerator {
use PHPFormattingTrait;
/**
* {@inheritdoc}
*/
public static function componentDataDefinition() {
return parent::componentDataDefinition() + [
'function_name' => [
'internal' => TRUE,
],
'docblock_inherit' => [
'internal' => TRUE,
'default' => FALSE,
'processing' => function($value, &$component_data, $property_name, &$property_info) {
if ($value) {
$component_data['function_docblock_lines'] = ['{@inheritdoc}'];
}
},
],
// Deprecated: use function_docblock_lines instead.
'doxygen_first' => [
'internal' => TRUE,
'default' => 'TODO: write function documentation.',
],
// Lines for the class docblock.
// If there is more than one line, a blank link is inserted automatically
// after the first one.
'function_docblock_lines' => [
'format' => 'array',
'internal' => TRUE,
// No default, as most generators don't use this yet.
],
'declaration' => [
'internal' => TRUE,
],
'body' => [
'internal' => TRUE,
],
// Whether code lines in the 'body' property are already indented relative
// to the indentation of function as a whole.
'body_indented' => [
'internal' => TRUE,
'format' => 'boolean',
'default' => FALSE,
],
];
}
/**
* Gets the bare lines to format as the docblock.
*
* @return string[]
* An array of lines.
*/
protected function getFunctionDocBlockLines() {
$lines = [];
if (!empty($this->component_data['function_docblock_lines'])) {
$lines = $this->component_data['function_docblock_lines'];
if (count($lines) > 1) {
// If there is more than one line, splice in a blank line after the
// first one.
array_splice($lines, 1, 0, '');
}
}
elseif (!empty($this->component_data['doxygen_first'])) {
$lines[] = $this->component_data['doxygen_first'];
}
return $lines;
}
/**
* {@inheritdoc}
*/
protected function buildComponentContents($children_contents) {
$function_code = array();
$function_code = array_merge($function_code, $this->docBlock($this->getFunctionDocBlockLines()));
$declaration = str_replace('£', '$', $this->component_data['declaration']);
$function_code[] = $declaration . ' {';
if (isset($this->component_data['body'])) {
$body = is_array($this->component_data['body'])
? $this->component_data['body']
: array($this->component_data['body']);
// Little bit of sugar: to save endless escaping of $ in front of
// variables in code body, you can use £.
$body = array_map(function($line) {
return str_replace('£', '$', $line);
}, $body);
// Add indent.
if (empty($this->component_data['body_indented'])) {
$body = $this->indentCodeLines($body);
}
$function_code = array_merge($function_code, $body);
}
$function_code[] = "}";
return [
'function' => [
'role' => 'function',
'content' => $function_code,
],
];
}
}