Skip to content

Commit 465d02f

Browse files
committed
v0.1.0: Raise minimum php version, change some AST representations
Raise the minimum php version from 7.0 to 7.2. Depend on phpunit 8 for local development Unify method names for Node and Token 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. Convert UnsetIntrinsicExpression to UnsetStatement - it can only be used as a statement. Unify previously split up lists into a single list in some AST node properties (initially done that way for backward compatibility) Clean up unnecessary special case for throw - This continues to be parsed as an ExpressionStatement with the same precedence Rename throwStatement to throwExpression in test file names.
1 parent 2a48939 commit 465d02f

File tree

70 files changed

+542
-666
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+542
-666
lines changed

.travis.yml

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
language: php
22

33
php:
4-
- 7.0
5-
- 7.1
6-
- 7.2
7-
- 7.3
8-
- 7.4
9-
# Should be changed to 8.0 once travis officially provides that label.
10-
- nightly
4+
- '7.2'
5+
- '7.3'
6+
- '7.4'
7+
- '8.0'
118

129
env:
1310
- VALIDATION=false
@@ -30,12 +27,7 @@ cache:
3027

3128
before_script:
3229
- if [[ $STATIC_ANALYSIS = true ]]; then composer require phpstan/phpstan --no-update; fi
33-
- |
34-
if php -r 'exit(PHP_MAJOR_VERSION < 8 ? 0 : 1);';
35-
then composer install
36-
else
37-
composer install --ignore-platform-reqs
38-
fi
30+
- composer install
3931
- set -e # Stop on first error.
4032
- phpenv config-rm xdebug.ini || true
4133
- if find . -name "*.php" -path "./src/*" -path "./experiments/*" -path "./tools/*" -path "./syntax-visualizer/server/src/*" -exec php -l {} 2>&1 \; | grep "syntax error, unexpected"; then exit 1; fi

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"php": ">=7.2"
77
},
88
"require-dev": {
9-
"phpunit/phpunit": "^7.5.20"
9+
"phpunit/phpunit": "^8.5.15"
1010
},
1111
"license": "MIT",
1212
"authors": [

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: 17 additions & 17 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
}
@@ -662,7 +662,7 @@ public function getPreviousSibling() {
662662
* Add the alias and resolved name to the corresponding namespace, function, or const import table.
663663
* If the alias already exists, it will get replaced by the most recent using.
664664
*
665-
* TODO - worth throwing an error here in stead?
665+
* TODO - worth throwing an error here instead?
666666
*/
667667
private function addToImportTable($alias, $functionOrConst, $namespaceNameParts, $contents, & $namespaceImportTable, & $functionImportTable, & $constImportTable):array
668668
{

src/Node/MissingMemberDeclaration.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,13 @@ class MissingMemberDeclaration extends Node implements ModifiedTypeInterface {
2020
/** @var Token|null needed along with typeDeclaration for what looked like typed property declarations but was missing VariableName */
2121
public $questionToken;
2222

23-
/** @var QualifiedName|Token|null */
24-
public $typeDeclaration;
25-
2623
/** @var DelimitedList\QualifiedNameList|null */
27-
public $otherTypeDeclarations;
24+
public $typeDeclarationList;
2825

2926
const CHILD_NAMES = [
3027
'attributes',
3128
'modifiers',
3229
'questionToken',
33-
'typeDeclaration',
34-
'otherTypeDeclarations',
30+
'typeDeclarationList',
3531
];
3632
}

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/Node/Expression/UnsetIntrinsicExpression.php renamed to src/Node/Statement/UnsetStatement.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
* Licensed under the MIT License. See License.txt in the project root for license information.
55
*--------------------------------------------------------------------------------------------*/
66

7-
namespace Microsoft\PhpParser\Node\Expression;
7+
namespace Microsoft\PhpParser\Node\Statement;
88

99
use Microsoft\PhpParser\Node\DelimitedList;
1010
use Microsoft\PhpParser\Node\Expression;
1111
use Microsoft\PhpParser\Token;
1212

13-
class UnsetIntrinsicExpression extends Expression {
13+
class UnsetStatement extends Expression {
1414

1515
/** @var Token */
1616
public $unsetKeyword;
@@ -24,10 +24,14 @@ class UnsetIntrinsicExpression extends Expression {
2424
/** @var Token */
2525
public $closeParen;
2626

27+
/** @var Token */
28+
public $semicolon;
29+
2730
const CHILD_NAMES = [
2831
'unsetKeyword',
2932
'openParen',
3033
'expressions',
31-
'closeParen'
34+
'closeParen',
35+
'semicolon',
3236
];
3337
}

0 commit comments

Comments
 (0)