Skip to content

Commit d536a80

Browse files
committed
Rename to getFullStartPosition and getStartPosition
Consistently end public API methods with Position. Avoid the need for callers to check if an object is a Node/Token before calling a method.
1 parent 79bb01f commit d536a80

File tree

6 files changed

+27
-59
lines changed

6 files changed

+27
-59
lines changed

docs/ApiDocumentation.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
```php
1111
public function getNodeKindName ( ) : string
1212
```
13-
### Node::getStart
13+
### Node::getStartPosition
1414
Gets start position of Node, not including leading comments and whitespace.
1515
```php
16-
public function getStart ( ) : int
16+
public function getStartPosition ( ) : int
1717
```
18-
### Node::getFullStart
18+
### Node::getFullStartPosition
1919
Gets start position of Node, including leading comments and whitespace
2020
```php
21-
public function getFullStart ( ) : int
21+
public function getFullStartPosition ( ) : int
2222
```
2323
### Node::getParent
2424
Gets parent of current node (returns null if has no parent)
@@ -198,11 +198,11 @@ public function getFullText ( string & $document ) : string
198198
```php
199199
public function getStartPosition ( )
200200
```
201-
### Token::getFullStart
201+
### Token::getFullStartPosition
202202
> TODO: add doc comment
203203
204204
```php
205-
public function getFullStart ( )
205+
public function getFullStartPosition ( )
206206
```
207207
### Token::getWidth
208208
> TODO: add doc comment

src/FilePositionMap.php

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,46 +34,19 @@ public function __construct(string $file_contents) {
3434
$this->lineForCurrentOffset = 1;
3535
}
3636

37-
/**
38-
* @param Node $node the node to get the start line for.
39-
* TODO deprecate and merge this and getTokenStartLine into getStartLine
40-
* if https://github.com/Microsoft/tolerant-php-parser/issues/166 is fixed,
41-
* (i.e. if there is a consistent way to get the start offset)
42-
*/
43-
public function getNodeStartLine(Node $node) : int {
44-
return $this->getLineNumberForOffset($node->getStart());
45-
}
46-
47-
/**
48-
* @param Token $token the token to get the start line for.
49-
*/
50-
public function getTokenStartLine(Token $token) : int {
51-
return $this->getLineNumberForOffset($token->start);
52-
}
53-
5437
/**
5538
* @param Node|Token $node
5639
*/
5740
public function getStartLine($node) : int {
58-
if ($node instanceof Token) {
59-
$offset = $node->start;
60-
} else {
61-
$offset = $node->getStart();
62-
}
63-
return $this->getLineNumberForOffset($offset);
41+
return $this->getLineNumberForOffset($node->getStartPosition());
6442
}
6543

6644
/**
6745
* @param Node|Token $node
6846
* Similar to getStartLine but includes the column
6947
*/
7048
public function getStartLineCharacterPositionForOffset($node) : LineCharacterPosition {
71-
if ($node instanceof Token) {
72-
$offset = $node->start;
73-
} else {
74-
$offset = $node->getStart();
75-
}
76-
return $this->getLineCharacterPositionForOffset($offset);
49+
return $this->getLineCharacterPositionForOffset($node->getStartPosition());
7750
}
7851

7952
/** @param Node|Token $node */

src/Node.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ public function getNodeKindName() : string {
3131
* @return int
3232
* @throws \Exception
3333
*/
34-
public function getStart() : int {
34+
public function getStartPosition() : int {
3535
$child = $this->getChildNodesAndTokens()->current();
3636
if ($child instanceof Node) {
37-
return $child->getStart();
37+
return $child->getStartPosition();
3838
} elseif ($child instanceof Token) {
3939
return $child->start;
4040
}
@@ -46,7 +46,7 @@ public function getStart() : int {
4646
* @return int
4747
* @throws \Exception
4848
*/
49-
public function getFullStart() : int {
49+
public function getFullStartPosition() : int {
5050
foreach($this::CHILD_NAMES as $name) {
5151

5252
if (($child = $this->$name) !== null) {
@@ -59,7 +59,7 @@ public function getFullStart() : int {
5959
}
6060

6161
if ($child instanceof Node) {
62-
return $child->getFullStart();
62+
return $child->getFullStartPosition();
6363
}
6464

6565
if ($child instanceof Token) {
@@ -330,7 +330,7 @@ public function getChildNames() {
330330
* @return int
331331
*/
332332
public function getWidth() : int {
333-
$first = $this->getStart();
333+
$first = $this->getStartPosition();
334334
$last = $this->getEndPosition();
335335

336336
return $last - $first;
@@ -342,7 +342,7 @@ public function getWidth() : int {
342342
* @return int
343343
*/
344344
public function getFullWidth() : int {
345-
$first = $this->getFullStart();
345+
$first = $this->getFullStartPosition();
346346
$last = $this->getEndPosition();
347347

348348
return $last - $first;
@@ -353,7 +353,7 @@ public function getFullWidth() : int {
353353
* @return string
354354
*/
355355
public function getText() : string {
356-
$start = $this->getStart();
356+
$start = $this->getStartPosition();
357357
$end = $this->getEndPosition();
358358

359359
$fileContents = $this->getFileContents();
@@ -365,7 +365,7 @@ public function getText() : string {
365365
* @return string
366366
*/
367367
public function getFullText() : string {
368-
$start = $this->getFullStart();
368+
$start = $this->getFullStartPosition();
369369
$end = $this->getEndPosition();
370370

371371
$fileContents = $this->getFileContents();
@@ -463,7 +463,7 @@ public function getDescendantNodeAtPosition(int $pos) {
463463
* @return bool
464464
*/
465465
private function containsPosition(int $pos): bool {
466-
return $this->getStart() <= $pos && $pos <= $this->getEndPosition();
466+
return $this->getStartPosition() <= $pos && $pos <= $this->getEndPosition();
467467
}
468468

469469
/**
@@ -476,7 +476,7 @@ private function containsPosition(int $pos): bool {
476476
public function getDocCommentText() {
477477
$leadingTriviaText = $this->getLeadingCommentAndWhitespaceText();
478478
$leadingTriviaTokens = PhpTokenizer::getTokensArrayFromContent(
479-
$leadingTriviaText, ParseContext::SourceElements, $this->getFullStart(), false
479+
$leadingTriviaText, ParseContext::SourceElements, $this->getFullStartPosition(), false
480480
);
481481
for ($i = \count($leadingTriviaTokens) - 1; $i >= 0; $i--) {
482482
$token = $leadingTriviaTokens[$i];
@@ -509,13 +509,13 @@ public function getImportTablesForCurrentScope() {
509509
$topLevelNamespaceStatements = $namespaceDefinition->compoundStatementOrSemicolon instanceof Token
510510
? $namespaceDefinition->parent->statementList // we need to start from the namespace definition.
511511
: $namespaceDefinition->compoundStatementOrSemicolon->statements;
512-
$namespaceFullStart = $namespaceDefinition->getFullStart();
512+
$namespaceFullStart = $namespaceDefinition->getFullStartPosition();
513513
} else {
514514
$topLevelNamespaceStatements = $this->getRoot()->statementList;
515515
$namespaceFullStart = 0;
516516
}
517517

518-
$nodeFullStart = $this->getFullStart();
518+
$nodeFullStart = $this->getFullStartPosition();
519519

520520
// TODO optimize performance
521521
// Currently we rebuild the import tables on every call (and therefore every name resolution operation)
@@ -535,10 +535,10 @@ public function getImportTablesForCurrentScope() {
535535
$contents = $this->getFileContents();
536536

537537
foreach ($topLevelNamespaceStatements as $useDeclaration) {
538-
if ($useDeclaration->getFullStart() <= $namespaceFullStart) {
538+
if ($useDeclaration->getFullStartPosition() <= $namespaceFullStart) {
539539
continue;
540540
}
541-
if ($useDeclaration->getFullStart() > $nodeFullStart) {
541+
if ($useDeclaration->getFullStartPosition() > $nodeFullStart) {
542542
break;
543543
} elseif (!($useDeclaration instanceof NamespaceUseDeclaration)) {
544544
continue;
@@ -609,11 +609,11 @@ public function getNamespaceDefinition() {
609609
throw new \Exception("Invalid tree - SourceFileNode must always exist at root of tree.");
610610
}
611611

612-
$fullStart = $this->getFullStart();
612+
$fullStart = $this->getFullStartPosition();
613613
$lastNamespaceDefinition = null;
614614
if ($namespaceDefinition instanceof SourceFileNode) {
615615
foreach ($namespaceDefinition->getChildNodes() as $childNode) {
616-
if ($childNode instanceof NamespaceDefinition && $childNode->getFullStart() < $fullStart) {
616+
if ($childNode instanceof NamespaceDefinition && $childNode->getFullStartPosition() < $fullStart) {
617617
$lastNamespaceDefinition = $childNode;
618618
}
619619
}

src/Node/QualifiedName.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function isUnqualifiedName() : bool {
8181
*/
8282
public function getResolvedName($namespaceDefinition = null) {
8383
// Name resolution not applicable to constructs that define symbol names or aliases.
84-
if (($this->parent instanceof Node\Statement\NamespaceDefinition && $this->parent->name->getStart() === $this->getStart()) ||
84+
if (($this->parent instanceof Node\Statement\NamespaceDefinition && $this->parent->name->getStartPosition() === $this->getStartPosition()) ||
8585
$this->parent instanceof Node\Statement\NamespaceUseDeclaration ||
8686
$this->parent instanceof Node\NamespaceUseClause ||
8787
$this->parent instanceof Node\NamespaceUseGroupClause ||

src/Node/Statement/BreakOrContinueStatement.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,7 @@ public function getDiagnosticForNode() {
6060
}
6161
}
6262

63-
if ($breakoutLevel instanceof Token) {
64-
$start = $breakoutLevel->getStartPosition();
65-
}
66-
else {
67-
$start = $breakoutLevel->getStart();
68-
}
63+
$start = $breakoutLevel->getStartPosition();
6964
$end = $breakoutLevel->getEndPosition();
7065

7166
return new Diagnostic(

src/Token.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function getStartPosition() {
6161
/**
6262
* @return int
6363
*/
64-
public function getFullStart() {
64+
public function getFullStartPosition() {
6565
return $this->fullStart;
6666
}
6767

0 commit comments

Comments
 (0)