Skip to content

Commit

Permalink
DX: Narrow docblock types in Tokenizer (#6293)
Browse files Browse the repository at this point in the history
narrow docblock types in tokenizer
  • Loading branch information
jrmajor authored Jul 19, 2022
1 parent 76bd49d commit fab645b
Show file tree
Hide file tree
Showing 26 changed files with 156 additions and 132 deletions.
6 changes: 3 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ parameters:
-
message: '#^Method .+ has parameter .+ with no value type specified in iterable type array\.$#'
path: *
count: 379
count: 363
-
message: '#^Method .+ has parameter .+ with no value type specified in iterable type iterable\.$#'
path: *
count: 4
-
message: '#^Method .+ return type has no value type specified in iterable type array\.$#'
path: *
count: 163
count: 153
-
message: '#^Method .+ return type has no value type specified in iterable type iterable\.$#'
path: *
Expand All @@ -43,6 +43,6 @@ parameters:
-
message: '#^Property .+ type has no value type specified in iterable type array\.$#'
path: *
count: 38
count: 37
tipsOfTheDay: false
tmpDir: dev-tools/phpstan/cache
3 changes: 3 additions & 0 deletions src/Fixer/Alias/ModernizeStrposFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ private function fixCall(Tokens $tokens, int $functionIndex, array $operatorIndi
}
}

/**
* @param -1|1 $direction
*/
private function getCompareTokens(Tokens $tokens, int $offsetIndex, int $direction): ?array
{
$operatorIndex = $tokens->getMeaningfulTokenSibling($offsetIndex, $direction);
Expand Down
1 change: 0 additions & 1 deletion src/Fixer/FunctionNotation/StaticLambdaFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ private function findExpressionEnd(Tokens $tokens, int $index): int
break;
}

/** @var null|array{isStart: bool, type: int} $blockType */
$blockType = Tokens::detectBlockType($nextToken);

if (null !== $blockType && $blockType['isStart']) {
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Import/OrderedImportsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ private function getNewOrder(array $uses, Tokens $tokens): array
}

$namespaceTokens = [];
$nextPartIndex = $tokens->getTokenNotOfKindSibling($index, 1, [[','], [T_WHITESPACE]]);
$nextPartIndex = $tokens->getTokenNotOfKindSibling($index, 1, [',', [T_WHITESPACE]]);
$startIndex = $nextPartIndex;
$index = $nextPartIndex;

Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/LanguageConstruct/ErrorSuppressionFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private function isDeprecationErrorCall(Tokens $tokens, int $index): bool
return false;
}

$endBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $tokens->getNextTokenOfKind($index, [T_STRING, '(']));
$endBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $tokens->getNextTokenOfKind($index, [[T_STRING], '(']));
$prevIndex = $tokens->getPrevMeaningfulToken($endBraceIndex);

if ($tokens[$prevIndex]->equals(',')) {
Expand Down
3 changes: 3 additions & 0 deletions src/Fixer/Operator/NoSpaceAroundDoubleColonFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}
}

/**
* @param -1|1 $direction
*/
private function removeSpace(Tokens $tokens, int $index, int $direction): void
{
if (!$tokens[$index + $direction]->isWhitespace()) {
Expand Down
9 changes: 4 additions & 5 deletions src/Fixer/Operator/OperatorLinebreakFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,16 @@ private function getExcludedIndices(Tokens $tokens): array
{
$colonIndices = [];

/** @var SwitchAnalysis $analysis */
foreach (ControlCaseStructuresAnalyzer::findControlStructures($tokens, [T_SWITCH]) as $analysis) {
foreach ($analysis->getCases() as $case) {
$colonIndices[] = $case->getColonIndex();
}

if ($analysis instanceof SwitchAnalysis) {
$defaultAnalysis = $analysis->getDefaultAnalysis();
$defaultAnalysis = $analysis->getDefaultAnalysis();

if (null !== $defaultAnalysis) {
$colonIndices[] = $defaultAnalysis->getColonIndex();
}
if (null !== $defaultAnalysis) {
$colonIndices[] = $defaultAnalysis->getColonIndex();
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/Fixer/Operator/TernaryOperatorSpacesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,16 @@ private function getColonIndicesForSwitch(Tokens $tokens): array
{
$colonIndices = [];

/** @var SwitchAnalysis $analysis */
foreach (ControlCaseStructuresAnalyzer::findControlStructures($tokens, [T_SWITCH]) as $analysis) {
foreach ($analysis->getCases() as $case) {
$colonIndices[] = $case->getColonIndex();
}

if ($analysis instanceof SwitchAnalysis) {
$defaultAnalysis = $analysis->getDefaultAnalysis();
$defaultAnalysis = $analysis->getDefaultAnalysis();

if (null !== $defaultAnalysis) {
$colonIndices[] = $defaultAnalysis->getColonIndex();
}
if (null !== $defaultAnalysis) {
$colonIndices[] = $defaultAnalysis->getColonIndex();
}
}

Expand Down
1 change: 0 additions & 1 deletion src/Fixer/StringNotation/StringLengthToEmptyFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ private function keepParentheses(Tokens $tokens, int $openParenthesisIndex, int
continue;
}

/** @var null|array{isStart: bool, type: int} $blockType */
$blockType = Tokens::detectBlockType($token);

if (null !== $blockType && $blockType['isStart']) {
Expand Down
6 changes: 3 additions & 3 deletions src/Tokenizer/Analyzer/Analysis/EnumAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
final class EnumAnalysis extends AbstractControlCaseStructuresAnalysis
{
/**
* @var CaseAnalysis[]
* @var list<CaseAnalysis>
*/
private array $cases;

/**
* @param CaseAnalysis[] $cases
* @param list<CaseAnalysis> $cases
*/
public function __construct(int $index, int $open, int $close, array $cases)
{
Expand All @@ -35,7 +35,7 @@ public function __construct(int $index, int $open, int $close, array $cases)
}

/**
* @return CaseAnalysis[]
* @return list<CaseAnalysis>
*/
public function getCases(): array
{
Expand Down
6 changes: 3 additions & 3 deletions src/Tokenizer/Analyzer/Analysis/SwitchAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
final class SwitchAnalysis extends AbstractControlCaseStructuresAnalysis
{
/**
* @var CaseAnalysis[]
* @var list<CaseAnalysis>
*/
private array $cases;

private ?DefaultAnalysis $defaultAnalysis;

/**
* @param CaseAnalysis[] $cases
* @param list<CaseAnalysis> $cases
*/
public function __construct(int $index, int $open, int $close, array $cases, ?DefaultAnalysis $defaultAnalysis)
{
Expand All @@ -38,7 +38,7 @@ public function __construct(int $index, int $open, int $close, array $cases, ?De
}

/**
* @return CaseAnalysis[]
* @return list<CaseAnalysis>
*/
public function getCases(): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizer/Analyzer/Analysis/TypeAnalysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class TypeAnalysis implements StartEndTokenAwareAnalysis
* @see https://php.net/manual/en/reserved.other-reserved-words.php
* @see https://php.net/manual/en/language.pseudo-types.php
*
* @var string[]
* @var list<string>
*/
private static array $reservedTypes = [
'array',
Expand Down
3 changes: 3 additions & 0 deletions src/Tokenizer/Analyzer/BlocksAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public function isBlock(Tokens $tokens, ?int $openIndex, ?int $closeIndex): bool
return $closeIndex === $tokens->findBlockEnd($blockType, $openIndex);
}

/**
* @return Tokens::BLOCK_TYPE_*
*/
private function getBlockType(Token $token): ?int
{
foreach (Tokens::getBlockEdgeDefinitions() as $blockType => $definition) {
Expand Down
4 changes: 3 additions & 1 deletion src/Tokenizer/Analyzer/CommentsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ public function isBeforeStructuralElement(Tokens $tokens, int $index): bool
* Return array of indices that are part of a comment started at given index.
*
* @param int $index T_COMMENT index
*
* @return list<int>
*/
public function getCommentBlockIndices(Tokens $tokens, int $index): ?array
public function getCommentBlockIndices(Tokens $tokens, int $index): array
{
if (!$tokens[$index]->isGivenKind(T_COMMENT)) {
throw new \InvalidArgumentException('Given index must point to a comment.');
Expand Down
7 changes: 6 additions & 1 deletion src/Tokenizer/Analyzer/ControlCaseStructuresAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
final class ControlCaseStructuresAnalyzer
{
/**
* @param int[] $types Token types of interest of which analyzes must be returned
* @param list<int> $types Token types of interest of which analyzes must be returned
*
* @return \Generator<int, AbstractControlCaseStructuresAnalysis>
*/
public static function findControlStructures(Tokens $tokens, array $types): \Generator
{
Expand Down Expand Up @@ -267,6 +269,9 @@ private static function findDefaultOpen(Tokens $tokens, int $kind, int $index):
throw new \InvalidArgumentException(sprintf('Unexpected default for type "%d".', $kind));
}

/**
* @return list<int>
*/
private static function getTypesWithCaseOrDefault(): array
{
$supportedTypes = [T_SWITCH];
Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizer/Analyzer/FunctionsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function isGlobalFunctionCall(Tokens $tokens, int $index): bool
}

/**
* @return ArgumentAnalysis[]
* @return array<string, ArgumentAnalysis>
*/
public function getFunctionArguments(Tokens $tokens, int $functionIndex): array
{
Expand Down
6 changes: 3 additions & 3 deletions src/Tokenizer/Analyzer/NamespaceUsesAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
final class NamespaceUsesAnalyzer
{
/**
* @return NamespaceUseAnalysis[]
* @return list<NamespaceUseAnalysis>
*/
public function getDeclarationsFromTokens(Tokens $tokens): array
{
Expand All @@ -37,7 +37,7 @@ public function getDeclarationsFromTokens(Tokens $tokens): array
}

/**
* @return NamespaceUseAnalysis[]
* @return list<NamespaceUseAnalysis>
*/
public function getDeclarationsInNamespace(Tokens $tokens, NamespaceAnalysis $namespace): array
{
Expand All @@ -53,7 +53,7 @@ public function getDeclarationsInNamespace(Tokens $tokens, NamespaceAnalysis $na
}

/**
* @return NamespaceUseAnalysis[]
* @return list<NamespaceUseAnalysis>
*/
private function getDeclarations(Tokens $tokens, array $useIndices): array
{
Expand Down
2 changes: 1 addition & 1 deletion src/Tokenizer/Analyzer/NamespacesAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
final class NamespacesAnalyzer
{
/**
* @return NamespaceAnalysis[]
* @return list<NamespaceAnalysis>
*/
public function getDeclarations(Tokens $tokens): array
{
Expand Down
3 changes: 3 additions & 0 deletions src/Tokenizer/Analyzer/RangeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ private function __construct()

/**
* Meaningful compare of tokens within ranges.
*
* @param array{start: int, end: int} $range1
* @param array{start: int, end: int} $range2
*/
public static function rangeEqualsRange(Tokens $tokens, array $range1, array $range2): bool
{
Expand Down
3 changes: 3 additions & 0 deletions src/Tokenizer/CT.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ public static function has(int $value): bool
return isset($tokens[$value]);
}

/**
* @return array<self::T_*, string>
*/
private static function getMapById(): array
{
static $constants;
Expand Down
Loading

0 comments on commit fab645b

Please sign in to comment.