FEATURE: Improve handling of missing Vendor

* Check for string concatenation instead of earlier more stupid.

Relates: #36
This commit is contained in:
Daniel Siepmann 2017-03-23 11:27:40 +01:00
parent f9f82b0629
commit 1561c13b46
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
2 changed files with 51 additions and 6 deletions

View file

@ -71,6 +71,21 @@ new ones like ``\TYPO3\Extbase\...``. This is done for:
- Convert old legacy class definitions in extensions to namespace ones. - Convert old legacy class definitions in extensions to namespace ones.
- Add missing vendor to plugin and module registrations and configurations.
You might want to set this to non fixable and warning if you already provide the vendor inside a
single Variable, together with your extension key, as this is not recognized. So the following
will be recognized:
- $_EXTKEY,
- $VENDOR . $_EXTKEY,
- 'VENDOR.' . $_EXTKEY,
While the following will not:
- $key = 'Vendor.' . $_EXTKEY;
Also we check for the following deprecated calls: Also we check for the following deprecated calls:
- Check for ``create`` on ``ObjectManager``, which is "stupid" just all ``create`` calls are marked - Check for ``create`` on ``ObjectManager``, which is "stupid" just all ``create`` calls are marked

View file

@ -68,22 +68,52 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingVendorForPluginsAndModulesSniff
return; return;
} }
$firstParameter = $phpcsFile->findNext([T_WHITESPACE, T_OPEN_PARENTHESIS], $stackPtr + 1, null, true); $firstArgument = $phpcsFile->findNext([T_WHITESPACE, T_OPEN_PARENTHESIS], $stackPtr + 1, null, true);
if ($firstParameter === false || $tokens[$firstParameter]['code'] === T_CONSTANT_ENCAPSED_STRING) { if (! $this->isVendorMissing($phpcsFile, $firstArgument)) {
return; return;
} }
$fix = $phpcsFile->addFixableError( $fix = $phpcsFile->addFixableError(
'No vendor is given, that will break TYPO3 handling for namespaced classes. Add vendor before Extensionkey like: "Vendor." . $_EXTKEY', 'No vendor is given, that will break TYPO3 handling for namespaced classes.'
$firstParameter, . ' Add vendor before Extensionkey like: "Vendor." . $_EXTKEY',
$firstArgument,
'missingVendor' 'missingVendor'
); );
if ($fix === true) { if ($fix === true) {
$phpcsFile->fixer->replaceToken( $phpcsFile->fixer->replaceToken(
$firstParameter, $firstArgument,
"'{$this->getVendor()}.' . " . $tokens[$firstParameter]['content'] "'{$this->getVendor()}.' . " . $tokens[$firstArgument]['content']
); );
} }
} }
/**
* Checks whether vendor is missing in given argument.
*
* @param PhpCsFile $phpcsFile
* @param int|bool $argumentStart
*
* @return bool
*/
protected function isVendorMissing(PhpCsFile $phpcsFile, $argumentStart)
{
if ($argumentStart === false) {
return false;
}
$argumentEnd = $phpcsFile->findNext(T_COMMA, $argumentStart);
if ($argumentEnd === false) {
return false;
}
$stringConcats = array_filter(
array_slice($phpcsFile->getTokens(), $argumentStart, $argumentEnd - $argumentStart),
function (array $token) {
return $token['code'] === 'PHPCS_T_STRING_CONCAT';
}
);
return count($stringConcats) === 0;
}
} }