* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace Symfony\CS\Fixer\Contrib; use Symfony\CS\AbstractFixer; use Symfony\CS\Tokenizer\Tokens; /** * @author Dariusz RumiƄski */ final class PhpUnitConstructFixer extends AbstractFixer { private $configuration = array( 'assertSame' => true, 'assertEquals' => true, 'assertNotEquals' => true, 'assertNotSame' => true, ); private $assertionFixers = array( 'assertSame' => 'fixAssertPositive', 'assertEquals' => 'fixAssertPositive', 'assertNotEquals' => 'fixAssertNegative', 'assertNotSame' => 'fixAssertNegative', ); public function configure(array $usingMethods) { foreach ($usingMethods as $method => $fix) { if (!isset($this->configuration[$method])) { throw new \InvalidArgumentException(); } $this->configuration[$method] = $fix; } } /** * {@inheritdoc} */ public function fix(\SplFileInfo $file, $content) { // no assertions to be fixed - fast return if (!in_array(true, $this->configuration, true)) { return $content; } $tokens = Tokens::fromCode($content); foreach ($this->configuration as $assertionMethod => $assertionShouldBeFixed) { if (true !== $assertionShouldBeFixed) { continue; } $assertionFixer = $this->assertionFixers[$assertionMethod]; for ($index = 0, $limit = $tokens->count(); $index < $limit; ++$index) { $index = $this->$assertionFixer($tokens, $index, $assertionMethod); if (null === $index) { break; } } } return $tokens->generateCode(); } /** * {@inheritdoc} */ public function getDescription() { return 'PHPUnit assertion method calls like "->assertSame(true, $foo)" should be written with dedicated method like "->assertTrue($foo)". Warning! This could change code behavior.'; } /** * {@inheritdoc} */ public function getPriority() { // should be run after the PhpUnitStrictFixer return -10; } private function fixAssertNegative(Tokens $tokens, $index, $method) { static $map = array( 'false' => 'assertNotFalse', 'null' => 'assertNotNull', 'true' => 'assertNotTrue', ); return $this->fixAssert($map, $tokens, $index, $method); } private function fixAssertPositive(Tokens $tokens, $index, $method) { static $map = array( 'false' => 'assertFalse', 'null' => 'assertNull', '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'), array(T_OBJECT_OPERATOR, '->'), array(T_STRING, $method), '(', ), $index ); if (null === $sequence) { return; } $sequenceIndexes = array_keys($sequence); $sequenceIndexes[4] = $tokens->getNextMeaningfulToken($sequenceIndexes[3]); $firstParameterToken = $tokens[$sequenceIndexes[4]]; if (!$firstParameterToken->isNativeConstant()) { return; } $sequenceIndexes[5] = $tokens->getNextMeaningfulToken($sequenceIndexes[4]); // return if first method argument is an expression, not value if (!$tokens[$sequenceIndexes[5]]->equals(',')) { return; } $tokens[$sequenceIndexes[2]]->setContent($map[$firstParameterToken->getContent()]); $tokens->clearRange($sequenceIndexes[4], $tokens->getNextNonWhitespace($sequenceIndexes[5]) - 1); return $sequenceIndexes[5]; } }