Created
October 17, 2015 23:38
-
-
Save Maff-/a0d9f51333269038e8d3 to your computer and use it in GitHub Desktop.
SymfonyCleanupStandard_Sniffs_Form_DeprecatedTypeOptionsSniff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SymfonyCleanupStandard_Sniffs_Form_DeprecatedTypeOptionsSniff extends PHP_CodeSniffer_Standards_AbstractScopeSniff | |
{ | |
/** | |
* @inheritdoc | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(array(T_CLASS), array(T_FUNCTION), false); | |
} | |
/** | |
* @inheritdoc | |
*/ | |
protected function processTokenWithinScope(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $currScope) | |
{ | |
$className = $phpcsFile->getDeclarationName($currScope); | |
if (substr($className, -4) !== 'Type') { | |
return; | |
} | |
$methodName = $phpcsFile->getDeclarationName($stackPtr); | |
if ($methodName !== 'setDefaultOptions') { | |
return; | |
} | |
$methodParams = $phpcsFile->getMethodParameters($stackPtr); | |
if (count($methodParams) != 1 || $methodParams[0]['name'] !== '$resolver' || strpos($methodParams[0]['type_hint'], 'OptionsResolverInterface') === false) { | |
$phpcsFile->addWarning('%s has setDefaultOptions method, but mismatches signature', $stackPtr, 'SignatureMismatch', [$className]); | |
return; | |
} | |
$fix = $phpcsFile->addFixableError('%s uses deprecated setDefaultOptions method', $stackPtr, 'Found', [$className]); | |
if ($fix === true) { | |
$tokens = $phpcsFile->getTokens(); | |
$token = $tokens[$stackPtr]; | |
// Fix method name and parameter type hint | |
$methodNameFixed = false; | |
for ($i = $stackPtr; $i < $phpcsFile->numTokens; $i++) { | |
if (!$methodNameFixed && $tokens[$i]['code'] === T_STRING) { | |
$phpcsFile->fixer->replaceToken($i, 'configureOptions'); | |
$methodNameFixed = true; | |
continue; | |
} | |
if ($i > $token['parenthesis_opener'] && $tokens[$i]['code'] === T_STRING) { | |
$phpcsFile->fixer->replaceToken($i, 'OptionsResolver'); | |
continue; | |
} | |
if ($i == $token['parenthesis_closer']) { | |
break; | |
} | |
} | |
// Fix import | |
$useStatement = false; | |
$useStatementStart = null; | |
$content = ''; | |
for ($i = 0; $i < $currScope; $i++) { | |
if ($tokens[$i]['code'] === T_USE) { | |
$useStatement = true; | |
$content = ''; | |
$i++; | |
$useStatementStart = $i + 1; | |
continue; | |
} | |
if ($useStatement && $tokens[$i]['code'] === T_SEMICOLON) { | |
if ($content === 'Symfony\Component\OptionsResolver\OptionsResolverInterface') { | |
for ($j = $useStatementStart; $j < $i; $j++) { | |
$phpcsFile->fixer->replaceToken($j, ''); | |
} | |
$phpcsFile->fixer->addContentBefore($i, 'Symfony\Component\OptionsResolver\OptionsResolver'); | |
break; | |
} | |
$useStatement = false; | |
continue; | |
} | |
if ($useStatement) { | |
$content .= $tokens[$i]['content']; | |
} | |
} | |
// TODO: replace method DocBlock if set | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment