Skip to content

Commit ce5acf3

Browse files
TysonAndredantleech
authored andcommitted
Use PHPDoc @param string ...$classNames
The `string` is the type of each argument. The `...` indicates that it is an array of that type, like `function (string ...$classNames)` also would indicate. Update code to PHPStan Level 3 Added ReturnTypeWillChange Fix return type for Expression Add type hints for returned variables Add generic parameter for delimted list Fixing type hints Ignore co-variance error Remove TODO - it already must always be a Node Ignore error Add missing types Update phpstan to level 3 Add phpstan to dev reqs No need to install phpstan independently Do not override unary expression operand It seems to me that the operand can be any expression Added token to operand types -- should this be MissingToken? Include tokens in return types ExpressionStatement => EchoStatement It seems this is always an EchoStatement not an ExpressionStatement unaryExpressionOrHigher can return a ThrowExpression Remove overridden property Add Token to union Remove QualifiedName and rename local variable Remove unused import Add return types Remove trailing whitespace Add MissingToken type Ignore "should not happen" statement Bump to level 4 Remove !is_null conditional branch - it always returns Node NamespaceUseDeclaration#useClauses is technically nullable NamespaceUseGroupCluase#functionOrConst can be NULL Add TODO Removed redundant condition (check) Add return type Add retutn type, remove inaccurate docblock Fix bug with get string literal text, as it returned the quotes Ignore assumed false-positive from PHPStan If allowEmptyElements if `false`... ... then it MUST be true in the right hand side of || backslash can be NULL BinaryExpresionOrHigher can return MIssingToken Ignoring error to be safe (as commented) and more type hints Set level to 4 Add explanation
1 parent 6bd3e3b commit ce5acf3

13 files changed

+99
-29
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
],
1919
"autoload": {
2020
"psr-4": { "Microsoft\\PhpParser\\": ["src/"] }
21+
},
22+
"autoload-dev": {
23+
"psr-4": { "Microsoft\\PhpParser\\Tests\\Unit\\": ["tests/unit/"] }
2124
}
2225
}

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 3
2+
level: 4
33
paths:
44
- src/
55
ignoreErrors:

src/DiagnosticsProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public static function checkDiagnostics($node) {
5151
if ($node instanceof Node) {
5252
return $node->getDiagnosticForNode();
5353
}
54+
55+
/** @phpstan-ignore-next-line because it says "unreachable" statement */
5456
return null;
5557
}
5658

src/Node.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,7 @@ public function getLastChild() {
437437
public function getDescendantNodeAtPosition(int $pos) {
438438
foreach ($this->getChildNodes() as $child) {
439439
if ($child->containsPosition($pos)) {
440-
$node = $child->getDescendantNodeAtPosition($pos);
441-
if (!is_null($node)) {
442-
return $node;
443-
}
440+
return $child->getDescendantNodeAtPosition($pos);
444441
}
445442
}
446443

@@ -590,6 +587,7 @@ public function getNamespaceDefinition() {
590587
? $this
591588
: $this->getFirstAncestor(NamespaceDefinition::class, SourceFileNode::class);
592589

590+
/** @phpstan-ignore-next-line TODO: can this happen? test with framework test cases */
593591
if ($namespaceDefinition instanceof NamespaceDefinition && !($namespaceDefinition->parent instanceof SourceFileNode)) {
594592
$namespaceDefinition = $namespaceDefinition->getFirstAncestor(SourceFileNode::class);
595593
}

src/Node/Expression/ArgumentExpression.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Microsoft\PhpParser\Node\Expression;
88

9+
use Microsoft\PhpParser\MissingToken;
910
use Microsoft\PhpParser\Node\Expression;
1011
use Microsoft\PhpParser\Token;
1112

@@ -19,7 +20,7 @@ class ArgumentExpression extends Expression {
1920
/** @var Token|null */
2021
public $dotDotDotToken;
2122

22-
/** @var Expression|null null for first-class callable syntax */
23+
/** @var Expression|MissingToken|null for first-class callable syntax */
2324
public $expression;
2425

2526
const CHILD_NAMES = [

src/Node/InterfaceBaseClause.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use Microsoft\PhpParser\Token;
1111

1212
class InterfaceBaseClause extends Node {
13-
/** @var Token */
13+
/** @var Token|null */
1414
public $extendsKeyword;
1515

1616
/** @var DelimitedList\QualifiedNameList */

src/Node/NamespaceUseGroupClause.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class NamespaceUseGroupClause extends Node {
1313

14-
/** @var Token */
14+
/** @var Token|null */
1515
public $functionOrConst;
1616
/** @var QualifiedName */
1717
public $namespaceName;

src/Node/QualifiedName.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
class QualifiedName extends Node implements NamespacedNameInterface {
2121
use NamespacedNameTrait;
2222

23-
/** @var Token */
23+
/** @var Token|null */
2424
public $globalSpecifier; // \_opt
25-
/** @var RelativeSpecifier */
25+
/** @var RelativeSpecifier|null */
2626
public $relativeSpecifier; // namespace\
2727
/** @var array */
2828
public $nameParts;
@@ -86,9 +86,7 @@ public function getResolvedName($namespaceDefinition = null) {
8686
$this->parent instanceof Node\NamespaceUseClause ||
8787
$this->parent instanceof Node\NamespaceUseGroupClause ||
8888
$this->parent->parent instanceof Node\TraitUseClause ||
89-
$this->parent instanceof Node\TraitSelectOrAliasClause ||
90-
($this->parent instanceof TraitSelectOrAliasClause &&
91-
($this->parent->asOrInsteadOfKeyword == null || $this->parent->asOrInsteadOfKeyword->kind === TokenKind::AsKeyword))
89+
$this->parent instanceof Node\TraitSelectOrAliasClause
9290
) {
9391
return null;
9492
}
@@ -157,10 +155,8 @@ public function getLastNamePart() {
157155

158156
/**
159157
* @param ResolvedName[] $importTable
160-
* @param bool $isCaseSensitive
161-
* @return string|null
162158
*/
163-
private function tryResolveFromImportTable($importTable, bool $isCaseSensitive = false) {
159+
private function tryResolveFromImportTable($importTable, bool $isCaseSensitive = false): ?ResolvedName {
164160
$content = $this->getFileContents();
165161
$index = $this->nameParts[0]->getText($content);
166162
// if (!$isCaseSensitive) {

src/Node/RelativeSpecifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class RelativeSpecifier extends Node {
1313
/** @var Token */
1414
public $namespaceKeyword;
1515

16-
/** @var Token */
16+
/** @var Token|null */
1717
public $backslash;
1818

1919
const CHILD_NAMES = [

src/Node/Statement/NamespaceUseDeclaration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class NamespaceUseDeclaration extends StatementNode {
1818
public $useKeyword;
1919
/** @var Token */
2020
public $functionOrConst;
21-
/** @var DelimitedList\NamespaceUseClauseList */
21+
/** @var DelimitedList\NamespaceUseClauseList|null */
2222
public $useClauses;
2323
/** @var Token */
2424
public $semicolon;

0 commit comments

Comments
 (0)