Skip to content
Permalink
Browse files

Merge pull request #176 from kubawerlos/deprecate-single-line-throw

Deprecate SingleLineThrowFixer
  • Loading branch information...
kubawerlos committed Nov 7, 2019
2 parents f263ff1 + 188abc7 commit beb9c531fa37dfde445faaa17197fd2ce43309eb
Showing with 28 additions and 70 deletions.
  1. +1 −1 CHANGELOG.md
  2. +2 −2 README.md
  3. +22 −64 src/Fixer/SingleLineThrowFixer.php
  4. +3 −3 tests/Fixer/SingleLineThrowFixerTest.php
@@ -2,6 +2,7 @@

## [Unreleased]
- Update PHP CS Fixer to v2.16
- Deprecate SingleLineThrowFixer

## v1.16.0 - *2019-10-24*
- Add PhpdocOnlyAllowedAnnotationsFixer
@@ -18,7 +19,6 @@
- Add PhpUnitNoUselessReturnFixer
- Add SingleLineThrowFixer
- Feature: NoCommentedOutCodeFixer - handle class method
- Deprecate SingleLineThrowFixer

## v1.13.0 - *2019-01-11*
- Update PHP CS Fixer to v2.14
@@ -423,8 +423,8 @@ In PHPDoc inside class or interface element `self` should be preferred over the
```

#### SingleLineThrowFixer
`throw` must be single line.
*To be deprecated after [this](https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/4452) is merged and released.*
Throwing exception must be done in single line.
DEPRECATED: use `single_line_throw` instead.
```diff
<?php
-throw new Exception(
@@ -4,95 +4,53 @@
namespace PhpCsFixerCustomFixers\Fixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Token;
use PhpCsFixer\Fixer\DeprecatedFixerInterface;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;
final class SingleLineThrowFixer extends AbstractFixer implements DeprecatingFixerInterface
/**
* @deprecated use "single_line_throw" instead
*/
final class SingleLineThrowFixer extends AbstractFixer implements DeprecatedFixerInterface
{
private const REMOVE_WHITESPACE_AFTER_TOKENS = ['['];
private const REMOVE_WHITESPACE_AROUND_TOKENS = ['.', '(', [T_OBJECT_OPERATOR], [T_DOUBLE_COLON]];
private const REMOVE_WHITESPACE_BEFORE_TOKENS = [')', ']', ',', ';'];
/** @var \PhpCsFixer\Fixer\FunctionNotation\SingleLineThrowFixer */
private $fixer;
public function getDefinition(): FixerDefinition
public function __construct()
{
return new FixerDefinition(
'`throw` must be single line.',
[
new CodeSample("<?php\nthrow new Exception(\n 'Error',\n 500\n);\n"),
]
);
$this->fixer = new \PhpCsFixer\Fixer\FunctionNotation\SingleLineThrowFixer();
}
public function getPriority(): int
public function getDefinition(): FixerDefinitionInterface
{
// must be fun before ConcatSpaceFixer and NoUnneededConcatenationFixer
return 1;
return $this->fixer->getDefinition();
}
public function getPullRequestId(): int
public function getPriority(): int
{
return 4452;
return $this->fixer->getPriority();
}
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_THROW);
return $this->fixer->isCandidate($tokens);
}
public function isRisky(): bool
{
return false;
return $this->fixer->isRisky();
}
public function fix(\SplFileInfo $file, Tokens $tokens): void
{
for ($index = 0, $count = $tokens->count(); $index < $count; $index++) {
if (!$tokens[$index]->isGivenKind(T_THROW)) {
continue;
}
/** @var int $openingBraceCandidateIndex */
$openingBraceCandidateIndex = $tokens->getNextTokenOfKind($index, [';', '(']);
while ($tokens[$openingBraceCandidateIndex]->equals('(')) {
$closingBraceIndex = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openingBraceCandidateIndex);
/** @var int $openingBraceCandidateIndex */
$openingBraceCandidateIndex = $tokens->getNextTokenOfKind($closingBraceIndex, [';', '(']);
}
$this->trimNewLines($tokens, $index, $openingBraceCandidateIndex);
}
$this->fixer->fix($file, $tokens);
}
private function trimNewLines(Tokens $tokens, int $startIndex, int $endIndex): void
/**
* @return string[]
*/
public function getSuccessorsNames(): array
{
for ($index = $startIndex; $index < $endIndex; $index++) {
if (!$tokens[$index]->isGivenKind(T_WHITESPACE)) {
continue;
}
if (Preg::match('/\R/', $tokens[$index]->getContent()) === 0) {
continue;
}
$prevIndex = $tokens->getNonEmptySibling($index, -1);
if ($tokens[$prevIndex]->equalsAny(\array_merge(self::REMOVE_WHITESPACE_AFTER_TOKENS, self::REMOVE_WHITESPACE_AROUND_TOKENS))) {
$tokens->clearAt($index);
continue;
}
$nextIndex = $tokens->getNonEmptySibling($index, 1);
if ($tokens[$nextIndex]->equalsAny(\array_merge(self::REMOVE_WHITESPACE_AROUND_TOKENS, self::REMOVE_WHITESPACE_BEFORE_TOKENS))) {
if (!$tokens[$prevIndex]->isGivenKind(T_FUNCTION)) {
$tokens->clearAt($index);
continue;
}
}
$tokens[$index] = new Token([T_WHITESPACE, ' ']);
}
return [$this->fixer->getName()];
}
}
@@ -25,9 +25,9 @@ public function testIsRisky(): void
static::assertFalse($this->fixer->isRisky());
}
public function testDeprecatingPullRequest(): void
public function testSuccessorName(): void
{
static::assertSame(4452, $this->fixer->getPullRequestId());
static::assertContains('single_line_throw', $this->fixer->getSuccessorsNames());
}
/**
@@ -63,7 +63,7 @@ public function provideFixCases(): iterable
];
yield [
'<?php throw new Exception("Foo"."Bar");',
'<?php throw new Exception("Foo" . "Bar");',
'<?php throw new Exception(
"Foo"
.

0 comments on commit beb9c53

Please sign in to comment.
You can’t perform that action at this time.