Skip to content

Commit

Permalink
bug PHP-CS-Fixer#4963 PhpdocToReturnTypeFixer - fix for breaking PHP …
Browse files Browse the repository at this point in the history
…syntax for type having reserved name (kubawerlos, Slamdunk)

This PR was merged into the 2.15 branch.

Discussion
----------

PhpdocToReturnTypeFixer - fix for breaking PHP syntax for type having reserved name

This test tries all tokens as return type which results with 73 failures.

Limiting test to PHP 7.4 is only because of laziness - finally, it should run for all PHP versions.

A question to @keradus, @SpacePossum and @julienfalque: do you think we
- can have "internal" repository like `php-cs-fixer/all-token` to provide us with all tokens to test like this
- should I make the currently used Packagist/release ready and we can use the stable version of it
- should we put this somewhere in PHP CS Fixer repository?

Commits
-------

1893af2 Update naming
a8ff8f0 Cache reserved keyword list (PHP-CS-Fixer#4)
e8dbfc0 Add one more test
5f5853d PhpdocToReturnTypeFixer - tests against all tokens
  • Loading branch information
SpacePossum committed Jun 14, 2020
2 parents 2920da6 + 1893af2 commit 6ce78a0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/Fixer/FunctionNotation/PhpdocToReturnTypeFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ final class PhpdocToReturnTypeFixer extends AbstractFixer implements Configurati
*/
private $classRegex = '/^\\\\?[a-zA-Z_\\x7f-\\xff](?:\\\\?[a-zA-Z0-9_\\x7f-\\xff]+)*(?<array>\[\])*$/';

/**
* @var array<string, bool>
*/
private $returnTypeCache = [];

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -251,6 +256,10 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens)
continue;
}

if (!$this->isValidType($returnType)) {
continue;
}

$this->fixFunctionDefinition($tokens, $startIndex, $isNullable, $returnType);
}
}
Expand Down Expand Up @@ -337,4 +346,23 @@ private function findReturnAnnotations(Tokens $tokens, $index)

return $doc->getAnnotationsOfType('return');
}

/**
* @param string $returnType
*
* @return bool
*/
private function isValidType($returnType)
{
if (!\array_key_exists($returnType, $this->returnTypeCache)) {
try {
Tokens::fromCode(sprintf('<?php function f():%s {}', $returnType));
$this->returnTypeCache[$returnType] = true;
} catch (\ParseError $e) {
$this->returnTypeCache[$returnType] = false;
}
}

return $this->returnTypeCache[$returnType];
}
}
9 changes: 9 additions & 0 deletions tests/Fixer/FunctionNotation/PhpdocToReturnTypeFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function provideFixCases()
'invalid class 2' => [
'<?php /** @return \\Foo\\\\Bar */ function my_foo() {}',
],
'invalid class 3' => [
'<?php /** @return Break */ function my_foo() {}',
],
'invalid class 4' => [
'<?php /** @return __CLASS__ */ function my_foo() {}',
],
'invalid class 5' => [
'<?php /** @return I\Want\To\Break\Free */ function queen() {}',
],
'blacklisted class methods' => [
'<?php
Expand Down

0 comments on commit 6ce78a0

Please sign in to comment.