Skip to content

Commit

Permalink
PhpUnitConstructFixer - Fix more use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
SpacePossum authored and keradus committed Oct 16, 2015
1 parent a7199ec commit 0cbf0ad
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Symfony/CS/Fixer/Contrib/Php4ConstructorFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private function fixConstructor(Tokens $tokens, $className, $classStart, $classE
private function fixParent(Tokens $tokens, $classStart, $classEnd)
{
// check calls to the parent constructor
foreach (array_keys($tokens->findGivenKind(T_EXTENDS)) as $index) {
foreach ($tokens->findGivenKind(T_EXTENDS) as $index => $token) {
$parentIndex = $tokens->getNextMeaningfulToken($index);
$parentClass = $tokens[$parentIndex]->getContent();

Expand Down
29 changes: 10 additions & 19 deletions Symfony/CS/Fixer/Contrib/PhpUnitConstructFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,13 @@ public function getPriority()

private function fixAssertNegative(Tokens $tokens, $index, $method)
{
$sequence = $tokens->findSequence(
array(
array(T_VARIABLE, '$this'),
array(T_OBJECT_OPERATOR, '->'),
array(T_STRING, $method),
'(',
array(T_STRING, 'null'),
',',
),
$index
static $map = array(
'false' => 'assertNotFalse',
'null' => 'assertNotNull',
'true' => 'assertNotTrue',
);

if (null === $sequence) {
return;
}

$sequenceIndexes = array_keys($sequence);
$tokens[$sequenceIndexes[2]]->setContent('assertNotNull');
$tokens->clearRange($sequenceIndexes[4], $tokens->getNextNonWhitespace($sequenceIndexes[5]) - 1);

return $sequenceIndexes[5];
return $this->fixAssert($map, $tokens, $index, $method);
}

private function fixAssertPositive(Tokens $tokens, $index, $method)
Expand All @@ -125,6 +111,11 @@ private function fixAssertPositive(Tokens $tokens, $index, $method)
'true' => 'assertTrue',
);

return $this->fixAssert($map, $tokens, $index, $method);
}

private function fixAssert(array $map, Tokens $tokens, $index, $method)
{
$sequence = $tokens->findSequence(
array(
array(T_VARIABLE, '$this'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function fix(\SplFileInfo $file, $content)
{
$tokens = Tokens::fromCode($content);

foreach (array_keys($tokens->findGivenKind(T_DOUBLE_ARROW)) as $index) {
foreach ($tokens->findGivenKind(T_DOUBLE_ARROW) as $index => $token) {
$this->fixWhitespace($tokens[$index - 1]);
// do not move anything about if there is a comment following the whitespace
if (!$tokens[$index + 2]->isComment()) {
Expand Down
2 changes: 1 addition & 1 deletion Symfony/CS/Fixer/Symfony/EmptyReturnFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function fix(\SplFileInfo $file, $content)
{
$tokens = Tokens::fromCode($content);

foreach (array_keys($tokens->findGivenKind(T_RETURN)) as $index) {
foreach ($tokens->findGivenKind(T_RETURN) as $index => $token) {
if ($this->needFixing($tokens, $index)) {
$this->clear($tokens, $index);
}
Expand Down
2 changes: 1 addition & 1 deletion Symfony/CS/Fixer/Symfony/IncludeFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private function findIncludies(Tokens $tokens)
$includies = array();

foreach ($tokens->findGivenKind($includyTokenKinds) as $includyTokens) {
foreach (array_keys($includyTokens) as $index) {
foreach ($includyTokens as $index => $token) {
$includy = array(
'begin' => $index,
'braces' => null,
Expand Down
2 changes: 1 addition & 1 deletion Symfony/CS/Fixer/Symfony/NoEmptyLinesAfterPhpdocsFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function fix(\SplFileInfo $file, $content)

$tokens = Tokens::fromCode($content);

foreach (array_keys($tokens->findGivenKind(T_DOC_COMMENT)) as $index) {
foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $index => $token) {
// get the next non-whitespace token inc comments, provided
// that there is whitespace between it and the current token
$next = $tokens->getNextNonWhitespace($index);
Expand Down
2 changes: 1 addition & 1 deletion Symfony/CS/Fixer/Symfony/ObjectOperatorFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function fix(\SplFileInfo $file, $content)
// [Structure] there should not be space before or after T_OBJECT_OPERATOR
$tokens = Tokens::fromCode($content);

foreach (array_keys($tokens->findGivenKind(T_OBJECT_OPERATOR)) as $index) {
foreach ($tokens->findGivenKind(T_OBJECT_OPERATOR) as $index => $token) {
// clear whitespace before ->
if ($tokens[$index - 1]->isWhitespace(array('whitespaces' => " \t")) && !$tokens[$index - 2]->isComment()) {
$tokens[$index - 1]->clear();
Expand Down
2 changes: 1 addition & 1 deletion Symfony/CS/Fixer/Symfony/PhpdocSeparationFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function fix(\SplFileInfo $file, $content)
{
$tokens = Tokens::fromCode($content);

foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $token) {
foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $index => $token) {
$doc = new DocBlock($token->getContent());
$this->fixDescription($doc);
$this->fixAnnotations($doc);
Expand Down
2 changes: 1 addition & 1 deletion Symfony/CS/Fixer/Symfony/UnusedUseFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private function detectUseUsages($content, array $useDeclarations)
{
$usages = array();

foreach (array_keys($useDeclarations) as $shortName) {
foreach ($useDeclarations as $shortName => $useDeclaration) {
$usages[$shortName] = (bool) preg_match('/(?<![\$\\\\])\b'.preg_quote($shortName).'\b/i', $content);
}

Expand Down
52 changes: 23 additions & 29 deletions Symfony/CS/Tests/Fixer/Contrib/PhpUnitConstructFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,36 +62,8 @@ public function testFix($expected, $input = null)

public function provideTestFixCases()
{
return array(
$cases = array(
array('<?php $sth->assertSame(true, $foo);'),
array(
'<?php $this->assertTrue($a);',
'<?php $this->assertSame(true, $a);',
),
array(
'<?php $this->assertTrue($a , "true" . $bar);',
'<?php $this->assertSame(true , $a , "true" . $bar);',
),
array(
'<?php $this->assertFalse( $a, "false" . $bar);',
'<?php $this->assertSame( false, $a, "false" . $bar);',
),
array(
'<?php $this->assertNull( $a , "null" . $bar);',
'<?php $this->assertSame( null, $a , "null" . $bar);',
),
array(
'<?php $this->assertNotNull( $a , "notNull" . $bar);',
'<?php $this->assertNotSame( null, $a , "notNull" . $bar);',
),
array(
'<?php $this->assertFalse( $a, "false" . $bar);',
'<?php $this->assertEquals( false, $a, "false" . $bar);',
),
array(
'<?php $this->assertNotNull( $a , "notNull" . $bar);',
'<?php $this->assertNotEquals( null, $a , "notNull" . $bar);',
),
array(
'<?php
$this->assertTrue(
Expand All @@ -106,5 +78,27 @@ public function provideTestFixCases()
);',
),
);

return array_merge(
$cases,
$this->generateCases('<?php $this->assert%s%s($a); //%s %s', '<?php $this->assert%s(%s, $a); //%s %s'),
$this->generateCases('<?php $this->assert%s%s($a, "%s", "%s");', '<?php $this->assert%s(%s, $a, "%s", "%s");')
);
}

private function generateCases($expectedTemplate, $inputTemplate)
{
$cases = array();
$functionTypes = array('Same' => true, 'NotSame' => false, 'Equals' => true, 'NotEquals' => false);
foreach (array('true', 'false', 'null') as $type) {
foreach ($functionTypes as $method => $positive) {
$cases[] = array(
sprintf($expectedTemplate, $positive ? '' : 'Not', ucfirst($type), $method, $type),
sprintf($inputTemplate, $method, $type, $method, $type),
);
}
}

return $cases;
}
}
2 changes: 1 addition & 1 deletion Symfony/CS/Tokenizer/Transformer/CurlyClose.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CurlyClose extends AbstractTransformer
*/
public function process(Tokens $tokens)
{
foreach (array_keys($tokens->findGivenKind(T_CURLY_OPEN)) as $index) {
foreach ($tokens->findGivenKind(T_CURLY_OPEN) as $index => $token) {
$level = 1;
$nestIndex = $index;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DollarCloseCurlyBraces extends AbstractTransformer
*/
public function process(Tokens $tokens)
{
foreach (array_keys($tokens->findGivenKind(T_DOLLAR_OPEN_CURLY_BRACES)) as $index) {
foreach ($tokens->findGivenKind(T_DOLLAR_OPEN_CURLY_BRACES) as $index => $token) {
$nextIndex = $tokens->getNextTokenOfKind($index, array('}'));
$tokens[$nextIndex]->override(array(CT_DOLLAR_CLOSE_CURLY_BRACES, '}', $tokens[$nextIndex]->getLine()));
}
Expand Down
2 changes: 1 addition & 1 deletion Symfony/CS/Tokenizer/Transformer/DynamicPropBrace.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DynamicPropBrace extends AbstractTransformer
*/
public function process(Tokens $tokens)
{
foreach (array_keys($tokens->findGivenKind(T_OBJECT_OPERATOR)) as $index) {
foreach ($tokens->findGivenKind(T_OBJECT_OPERATOR) as $index => $token) {
if (!$tokens[$index + 1]->equals('{')) {
continue;
}
Expand Down

0 comments on commit 0cbf0ad

Please sign in to comment.