Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG for PHP CS Fixer: custom fixers

## v3.31.0
- Update minimum PHP CS Fixer version to 3.84.0

## v3.30.0
- Update minimum PHP CS Fixer version to 3.82.0

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"php": "^7.4 || ^8.0",
"ext-filter": "*",
"ext-tokenizer": "*",
"friendsofphp/php-cs-fixer": "^3.82"
"friendsofphp/php-cs-fixer": "^3.84"
},
"require-dev": {
"phpunit/phpunit": "^9.6.22 || 10.5.45 || ^11.5.7"
Expand Down
47 changes: 38 additions & 9 deletions src/Fixer/PhpdocVarAnnotationToAssertFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace PhpCsFixerCustomFixers\Fixer;

use PhpCsFixer\AbstractPhpdocToTypeDeclarationFixer;
use PhpCsFixer\DocBlock\Annotation;
use PhpCsFixer\DocBlock\DocBlock;
use PhpCsFixer\FixerDefinition\CodeSample;
Expand Down Expand Up @@ -57,17 +58,18 @@ public function isRisky(): bool

public function fix(\SplFileInfo $file, Tokens $tokens): void
{
for ($docCommentIndex = $tokens->count() - 1; $docCommentIndex > 0; $docCommentIndex--) {
if (!$tokens[$docCommentIndex]->isGivenKind([\T_DOC_COMMENT])) {
continue;
}
$tokensToInsert = [];
$typesToExclude = [];

foreach ($tokens->findGivenKind(\T_DOC_COMMENT) as $index => $token) {
$typesToExclude = \array_merge($typesToExclude, self::getTypesToExclude($token->getContent()));

$variableIndex = self::getVariableIndex($tokens, $docCommentIndex);
$variableIndex = self::getVariableIndex($tokens, $index);
if ($variableIndex === null) {
continue;
}

$assertTokens = self::getAssertTokens($tokens, $docCommentIndex, $tokens[$variableIndex]->getContent());
$assertTokens = self::getAssertTokens($tokens, $index, $tokens[$variableIndex]->getContent(), $typesToExclude);
if ($assertTokens === null) {
continue;
}
Expand All @@ -82,10 +84,32 @@ public function fix(\SplFileInfo $file, Tokens $tokens): void
\array_unshift($assertTokens, new Token([\T_WHITESPACE, $tokens[$variableIndex - 1]->getContent()]));
}

$tokens->insertAt($expressionEndIndex + 1, $assertTokens);
$tokensToInsert[$expressionEndIndex + 1] = $assertTokens;

TokenRemover::removeWithLinesIfPossible($tokens, $index);
}

$tokens->insertSlices($tokensToInsert);
}

TokenRemover::removeWithLinesIfPossible($tokens, $docCommentIndex);
/**
* @return list<string>
*/
private static function getTypesToExclude(string $content): array
{
/** @var null|\Closure(string): list<string> $getTypesToExclude */
static $getTypesToExclude = null;

if ($getTypesToExclude === null) {
/** @var \Closure(string): list<string> $getTypesToExclude */
$getTypesToExclude = \Closure::bind(
static fn (string $content): array => AbstractPhpdocToTypeDeclarationFixer::getTypesToExclude($content),
null,
AbstractPhpdocToTypeDeclarationFixer::class,
);
}

return $getTypesToExclude($content);
}

private static function getVariableIndex(Tokens $tokens, int $docCommentIndex): ?int
Expand Down Expand Up @@ -114,9 +138,11 @@ private static function getVariableIndex(Tokens $tokens, int $docCommentIndex):
}

/**
* @param list<string> $typesToExclude
*
* @return null|list<Token>
*/
private static function getAssertTokens(Tokens $tokens, int $docCommentIndex, string $variableName): ?array
private static function getAssertTokens(Tokens $tokens, int $docCommentIndex, string $variableName, array $typesToExclude): ?array
{
$annotation = self::getAnnotationForVariable($tokens, $docCommentIndex, $variableName);
if ($annotation === null) {
Expand All @@ -136,6 +162,9 @@ private static function getAssertTokens(Tokens $tokens, int $docCommentIndex, st
$assertions['null'] = self::getCodeForType('null', $variableName);
$type = \substr($type, 1);
}
if (\in_array($type, $typesToExclude, true)) {
return null;
}
$assertions[$type] = self::getCodeForType($type, $variableName);
}

Expand Down
162 changes: 162 additions & 0 deletions tests/Fixer/PhpdocVarAnnotationToAssertFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,5 +399,167 @@ function ($x) { $x++; return $x + 6; },
$z = 4;
',
];

yield 'types defined with `phpstan-type`' => [
<<<'PHP'
<?php
/**
* @phpstan-type _Pair array{int, int}
*/
class Foo {
public function bar() {
/** @var _Pair $x */
$x = getValue();
}
}
/**
* @phpstan-type _Trio array{int, int, int}
*/
class Bar {
public function bar() {
/** @var _Trio $x */
$x = getValue();
}
}
PHP,
];

yield 'types defined with `phpstan-import-type`' => [
<<<'PHP'
<?php
/**
* @phpstan-import-type _Pair from FooFoo
*/
class Foo {
public function bar() {
/** @var _Pair $x */
$x = getValue();
}
}
/**
* @phpstan-import-type _Trio from BarBar
*/
class Bar {
public function bar() {
/** @var _Trio $x */
$x = getValue();
}
}
PHP,
];

yield 'types defined with `phpstan-import-type` with alias' => [
<<<'PHP'
<?php
/**
* @phpstan-import-type ConflictingType from FooFoo as Not_ConflictingType
*/
class Foo {
public function bar() {
$x = getValue();
assert($x instanceof ConflictingType);

/** @var Not_ConflictingType $y */
$y = getValue();
}
}
PHP,
<<<'PHP'
<?php
/**
* @phpstan-import-type ConflictingType from FooFoo as Not_ConflictingType
*/
class Foo {
public function bar() {
/** @var ConflictingType $x */
$x = getValue();

/** @var Not_ConflictingType $y */
$y = getValue();
}
}
PHP,
];

yield 'types defined with `psalm-type`' => [
<<<'PHP'
<?php
/**
* @psalm-type _Pair = array{int, int}
*/
class Foo {
public function bar() {
/** @var _Pair $x */
$x = getValue();
}
}
/**
* @psalm-type _Trio array{int, int, int}
*/
class Bar {
public function bar() {
/** @var _Trio $x */
$x = getValue();
}
}
PHP,
];

yield 'types defined with `psalm-import-type`' => [
<<<'PHP'
<?php
/**
* @psalm-import-type _Pair from FooFoo
*/
class Foo {
public function bar() {
/** @var _Pair $x */
$x = getValue();
}
}
/**
* @psalm-import-type _Trio from BarBar
*/
class Bar {
public function bar() {
/** @var _Trio $x */
$x = getValue();
}
}
PHP,
];

yield 'types defined with `psalm-import-type` with alias' => [
<<<'PHP'
<?php
/**
* @psalm-import-type Bar from FooFoo as BarAliased
*/
class Foo {
public function bar() {
$x = getValue();
assert($x instanceof Bar);

/** @var BarAliased $y */
$y = getValue();
}
}
PHP,
<<<'PHP'
<?php
/**
* @psalm-import-type Bar from FooFoo as BarAliased
*/
class Foo {
public function bar() {
/** @var Bar $x */
$x = getValue();

/** @var BarAliased $y */
$y = getValue();
}
}
PHP,
];
}
}
Loading