1: <?php
2: namespace Pharborist;
3:
4: /**
5: * Base class for any statement.
6: *
7: * <p>A statement is a single executable unit of PHP code. You can think of a
8: * statement as a single "sentence" of code, usually ending with
9: * a semicolon. A single statement usually (but not always!) occupies a single
10: * line.</p>
11: * <p>Here's an example of a perfectly valid statement:</p>
12: * <pre><code>echo "Let's not go to Camelot. 'Tis a silly place.\n";</code></pre>
13: * <p>Statements can contain other statements, or a block of statements surrounded
14: * by curly braces. A single statement is usually made up of one or more
15: * expressions.</p>
16: * <p>Declarations are also statements. For instance, if/elseif/else and switch/case
17: * structures are statements, including all of their blocks. So is are class and function
18: * declarations. The body of the class or function is a statement block, but it's
19: * contained by the class or function declaration, which is a statement.</p>
20: */
21: abstract class StatementNode extends ParentNode {
22: /**
23: * Gets the number of lines spanned by this statement.
24: *
25: * @return integer
26: * Always returns at least one, because any statement will be at least
27: * one line long.
28: */
29: public function getLineCount() {
30: $count = 1;
31:
32: $this
33: ->find(Filter::isInstanceOf('\Pharborist\WhitespaceNode'))
34: ->each(function(WhitespaceNode $node) use (&$count) {
35: $count += $node->getNewlineCount();
36: });
37:
38: return $count;
39: }
40:
41: /**
42: * Creates a commented-out version of this statement.
43: *
44: * @return \Pharborist\CommentNode|\Pharborist\LineCommentBlockNode
45: */
46: public function toComment() {
47: return CommentNode::create($this->getText());
48: }
49:
50: /**
51: * Adds a line comment block above the statement.
52: *
53: * @param \Pharborist\LineCommentBlockNode|string $comment
54: * The comment to add.
55: *
56: * @return $this
57: *
58: * @throws \InvalidArgumentException
59: */
60: public function addCommentAbove($comment) {
61: if ($comment instanceof LineCommentBlockNode) {
62: $this->before($comment);
63: }
64: elseif (is_string($comment)) {
65: $this->addCommentAbove(LineCommentBlockNode::create($comment));
66: }
67: else {
68: throw new \InvalidArgumentException();
69: }
70: return $this;
71: }
72: }
73: