Skip to content

Commit 2f2e2d5

Browse files
committed
Ast: added FunctionNode
1 parent f99b450 commit 2f2e2d5

9 files changed

+596
-23
lines changed

src/Ast/FunctionNode.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CzProject\PhpSimpleAst\Ast;
6+
7+
8+
class FunctionNode implements INode
9+
{
10+
/** @var PhpDocNode|NULL */
11+
private $phpDocNode;
12+
13+
/** @var string */
14+
private $indentation;
15+
16+
/** @var string */
17+
private $keyword;
18+
19+
/** @var Name|NULL */
20+
private $name;
21+
22+
/** @var Parameters */
23+
private $parameters;
24+
25+
/** @var InheritedVariables|NULL */
26+
private $inheritedVariables;
27+
28+
/** @var FunctionReturnType|NULL */
29+
private $returnType;
30+
31+
/** @var FunctionBody */
32+
private $body;
33+
34+
35+
public function __construct(
36+
?PhpDocNode $phpDocNode,
37+
string $indentation,
38+
string $keyword,
39+
?Name $name,
40+
Parameters $parameters,
41+
?InheritedVariables $inheritedVariables,
42+
?FunctionReturnType $returnType,
43+
FunctionBody $body
44+
)
45+
{
46+
$this->phpDocNode = $phpDocNode;
47+
$this->indentation = $indentation;
48+
$this->keyword = $keyword;
49+
$this->name = $name;
50+
$this->parameters = $parameters;
51+
$this->returnType = $returnType;
52+
$this->inheritedVariables = $inheritedVariables;
53+
$this->body = $body;
54+
}
55+
56+
57+
public function getName(): ?string
58+
{
59+
return $this->name !== NULL
60+
? $this->name->getName()
61+
: NULL;
62+
}
63+
64+
65+
public function getDocComment(): ?string
66+
{
67+
return $this->phpDocNode !== NULL ? $this->phpDocNode->getContent() : NULL;
68+
}
69+
70+
71+
/**
72+
* @return Parameter[]
73+
*/
74+
public function getParameters(): array
75+
{
76+
return $this->parameters->getParameters();
77+
}
78+
79+
80+
public function hasReturnType(): bool
81+
{
82+
return $this->returnType !== NULL;
83+
}
84+
85+
86+
public function toString(): string
87+
{
88+
$s = $this->phpDocNode !== NULL ? $this->phpDocNode->toString() : '';
89+
$s .= $this->indentation;
90+
$s .= $this->keyword;
91+
92+
if ($this->name !== NULL) {
93+
$s .= $this->name->toString();
94+
}
95+
96+
$s .= $this->parameters->toString();
97+
98+
if ($this->inheritedVariables !== NULL) {
99+
$s .= $this->inheritedVariables->toString();
100+
}
101+
102+
if ($this->returnType !== NULL) {
103+
$s .= $this->returnType->toString();
104+
}
105+
106+
$s .= $this->body->toString();
107+
return $s;
108+
}
109+
110+
111+
/**
112+
* @return self
113+
*/
114+
public static function parse(
115+
?PhpDocNode $phpDocNode,
116+
NodeParser $parser
117+
)
118+
{
119+
$nodeIndentation = $parser->consumeNodeIndentation();
120+
$keyword = $parser->consumeTokenAsText(T_FUNCTION);
121+
$parser->consumeWhitespace();
122+
$name = Name::tryParseFunctionName($parser->createSubParser());
123+
$parser->tryConsumeWhitespace();
124+
$parameters = Parameters::parse($parser->createSubParser());
125+
$parser->tryConsumeWhitespace();
126+
$inheritedVariables = NULL;
127+
$returnType = NULL;
128+
$body = NULL;
129+
130+
if ($name === NULL && $parser->isCurrent(T_USE)) {
131+
$inheritedVariables = InheritedVariables::parse($parser->createSubParser());
132+
$parser->tryConsumeWhitespace();
133+
}
134+
135+
if ($parser->isCurrent(':')) {
136+
$returnType = FunctionReturnType::parse($parser->createSubParser());
137+
$parser->tryConsumeWhitespace();
138+
}
139+
140+
if ($parser->isCurrent(T_COMMENT)) {
141+
$parser->consumeAsIndentation(T_COMMENT);
142+
$parser->tryConsumeWhitespace();
143+
}
144+
145+
$body = FunctionBody::parse($parser->createSubParser());
146+
$parser->close();
147+
148+
return new self(
149+
$phpDocNode,
150+
$nodeIndentation,
151+
$keyword,
152+
$name,
153+
$parameters,
154+
$inheritedVariables,
155+
$returnType,
156+
$body
157+
);
158+
}
159+
}

src/Ast/InheritedVariable.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CzProject\PhpSimpleAst\Ast;
6+
7+
8+
class InheritedVariable
9+
{
10+
/** @var VariableName */
11+
private $variable;
12+
13+
/** @var Literal|NULL */
14+
private $suffix;
15+
16+
17+
public function __construct(
18+
VariableName $variable,
19+
?Literal $suffix
20+
)
21+
{
22+
$this->variable = $variable;
23+
$this->suffix = $suffix;
24+
}
25+
26+
27+
public function toString(): string
28+
{
29+
$s = $this->variable->toString();
30+
31+
if ($this->suffix !== NULL) {
32+
$s .= $this->suffix->toString();
33+
}
34+
35+
return $s;
36+
}
37+
38+
39+
/**
40+
* @return self
41+
*/
42+
public static function parse(NodeParser $parser)
43+
{
44+
$variable = VariableName::parse($parser->createSubParser());
45+
$parser->tryConsumeWhitespace();
46+
$suffix = NULL;
47+
48+
if ($parser->isCurrent(',')) {
49+
$suffix = Literal::parseToken($parser->createSubParser(), ',');
50+
}
51+
52+
$parser->close();
53+
54+
return new self(
55+
$variable,
56+
$suffix
57+
);
58+
}
59+
}

src/Ast/InheritedVariables.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CzProject\PhpSimpleAst\Ast;
6+
7+
8+
class InheritedVariables
9+
{
10+
/** @var Literal */
11+
private $keyword;
12+
13+
/** @var Literal */
14+
private $opener;
15+
16+
/** @var InheritedVariable[] */
17+
private $variables;
18+
19+
/** @var Literal */
20+
private $closer;
21+
22+
23+
/**
24+
* @param InheritedVariable[] $variables
25+
*/
26+
public function __construct(
27+
Literal $keyword,
28+
Literal $opener,
29+
array $variables,
30+
Literal $closer
31+
)
32+
{
33+
$this->keyword = $keyword;
34+
$this->opener = $opener;
35+
$this->variables = $variables;
36+
$this->closer = $closer;
37+
}
38+
39+
40+
public function toString(): string
41+
{
42+
$s = $this->keyword->toString();
43+
$s .= $this->opener->toString();
44+
45+
foreach ($this->variables as $variable) {
46+
$s .= $variable->toString();
47+
}
48+
49+
$s .= $this->closer->toString();
50+
return $s;
51+
}
52+
53+
54+
/**
55+
* @return self
56+
*/
57+
public static function parse(NodeParser $parser)
58+
{
59+
$keyword = Literal::parseToken($parser->createSubParser(), T_USE);
60+
$parser->tryConsumeWhitespace();
61+
$opener = Literal::parseToken($parser->createSubParser(), '(');
62+
$inheritedVariables = [];
63+
64+
do {
65+
$parser->tryConsumeWhitespace();
66+
$inheritedVariables[] = InheritedVariable::parse($parser->createSubParser());
67+
$parser->tryConsumeWhitespace();
68+
69+
} while (!$parser->isCurrent(')'));
70+
71+
$closer = Literal::parseToken($parser->createSubParser(), ')');
72+
$parser->close();
73+
74+
return new self(
75+
$keyword,
76+
$opener,
77+
$inheritedVariables,
78+
$closer
79+
);
80+
}
81+
}

src/Ast/Name.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,22 @@ public static function tryParseClassName(NodeParser $parser)
161161
$parser->close();
162162
return $name !== '' ? new self($nodeIndentation, $name) : NULL;
163163
}
164+
165+
166+
/**
167+
* @return self|NULL
168+
*/
169+
public static function tryParseFunctionName(NodeParser $parser)
170+
{
171+
$nodeIndentation = '';
172+
$name = '';
173+
174+
if ($parser->isCurrent(T_STRING)) {
175+
$nodeIndentation = $parser->consumeNodeIndentation();
176+
$name = $parser->consumeTokenAsText(T_STRING);
177+
}
178+
179+
$parser->close();
180+
return $name !== '' ? new self($nodeIndentation, $name) : NULL;
181+
}
164182
}

src/Ast/NamespaceNode.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ public static function parse(NodeParser $parser)
148148
$parser->consumeAsUnknowContent(T_DOUBLE_COLON);
149149
$parser->tryConsumeAsUnknowContent(T_CLASS);
150150

151+
} elseif ($parser->isCurrent(T_FUNCTION)) {
152+
$child = FunctionNode::parse(NULL, $parser->createSubParser());
153+
151154
} elseif ($parser->isCurrent(T_USE)) {
152155
$child = UseNode::parse($parser->createSubParser());
153156

src/Ast/PhpNode.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public static function parse(NodeParser $parser)
7979
$parser->consumeAsUnknowContent(T_DOUBLE_COLON);
8080
$parser->tryConsumeAsUnknowContent(T_CLASS);
8181

82+
} elseif ($parser->isCurrent(T_FUNCTION)) {
83+
$child = FunctionNode::parse(NULL, $parser->createSubParser());
84+
8285
} elseif ($parser->isCurrent(T_USE)) {
8386
$child = UseNode::parse($parser->createSubParser());
8487

0 commit comments

Comments
 (0)