Skip to content

Commit

Permalink
fix: handle comments in classes when parsing types imports
Browse files Browse the repository at this point in the history
  • Loading branch information
nickvergessen committed Oct 18, 2023
1 parent 004eb16 commit 3b663a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Utility/Reflection/DocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,16 @@ private static function splitStringBy(string $string, string ...$cases): array
$result = [$string];

foreach ($cases as $case) {
foreach ($result as $value) {
$previousResult = $result;
$result = [];
foreach ($previousResult as $value) {
$result = array_merge($result, explode($case, $value));
}
}

// Remove the first segment of the docs before the first `$cases` string
array_shift($result);

return $result;
}
}
12 changes: 12 additions & 0 deletions tests/Integration/Mapping/Object/LocalTypeAliasMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ class GenericObjectWithPhpStanLocalAlias
}

/**
* Comment:
* Some comment before types
*
* @phpstan-type AliasWithEqualsSign = int
* @phpstan-type AliasWithoutEqualsSign int
* @phpstan-type AliasShapedArray = array{foo: string, bar: int}
Expand Down Expand Up @@ -111,6 +114,9 @@ class AnotherPhpStanLocalAlias
}

/**
* Comment:
* Some comment before import
*
* @phpstan-import-type AliasWithEqualsSign from PhpStanLocalAliases
* @phpstan-import-type AliasWithoutEqualsSign from AnotherPhpStanLocalAlias
*/
Expand All @@ -134,6 +140,9 @@ class GenericObjectWithPsalmLocalAlias
}

/**
* Comment:
* Some comment before types
*
* @psalm-type AliasWithEqualsSign = int
* @psalm-type AliasWithoutEqualsSign int
* @psalm-type AliasShapedArray = array{foo: string, bar: int}
Expand Down Expand Up @@ -171,6 +180,9 @@ class AnotherPsalmLocalAliases
}

/**
* Comment:
* Some comment before import
*
* @psalm-import-type AliasWithEqualsSign from PsalmLocalAliases
* @psalm-import-type AliasWithoutEqualsSign from AnotherPsalmLocalAliases
*/
Expand Down

0 comments on commit 3b663a9

Please sign in to comment.