TASK: Migrate MissingNamespaceSniff

* Make MissingNamespaceSniff work again.
* Move to old namespace as it's a custom sniff, not a general one.
* Do not use new feature here, implement logic internal.
* Remove message from ruleset, use it directly.

Relates: #72
This commit is contained in:
Daniel Siepmann 2017-04-25 14:11:40 +02:00
parent d3b612790c
commit efd5d9114f
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
10 changed files with 33 additions and 41 deletions

View file

@ -6,10 +6,10 @@ Typo3Update\Feature\LegacyClassnameFeature:
- Typo3Update_Sniffs_Classname_InstantiationWithNewSniff - Typo3Update_Sniffs_Classname_InstantiationWithNewSniff
- Typo3Update_Sniffs_Classname_InstantiationWithObjectManagerSniff - Typo3Update_Sniffs_Classname_InstantiationWithObjectManagerSniff
- Typo3Update_Sniffs_Classname_IsACallSniff - Typo3Update_Sniffs_Classname_IsACallSniff
- Typo3Update_Sniffs_Classname_MissingNamespaceSniff
- Typo3Update_Sniffs_Classname_MissingVendorForPluginsAndModulesSniff - Typo3Update_Sniffs_Classname_MissingVendorForPluginsAndModulesSniff
- Typo3Update_Sniffs_Classname_PhpDocCommentSniff - Typo3Update_Sniffs_Classname_PhpDocCommentSniff
- Typo3Update_Sniffs_Classname_StaticCallSniff - Typo3Update_Sniffs_Classname_StaticCallSniff
- Typo3Update_Sniffs_Classname_TypeHintCatchExceptionSniff - Typo3Update_Sniffs_Classname_TypeHintCatchExceptionSniff
- Typo3Update_Sniffs_Classname_TypeHintSniff - Typo3Update_Sniffs_Classname_TypeHintSniff
- Typo3Update_Sniffs_Classname_UseSniff - Typo3Update_Sniffs_Classname_UseSniff
- Typo3Update_Sniffs_LegacyClassname_MissingNamespaceSniff

View file

@ -20,13 +20,14 @@
*/ */
use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_File as PhpCsFile;
use Typo3Update\Sniffs\Classname\AbstractClassnameChecker; use PHP_CodeSniffer_Sniff as PhpCsSniff;
use Typo3Update\Feature\LegacyClassnameMapping;
use Typo3Update\Options; use Typo3Update\Options;
/** /**
* Detect missing namespaces for class definitions. * Detect missing namespaces for class definitions.
*/ */
class Typo3Update_Sniffs_Classname_MissingNamespaceSniff extends AbstractClassnameChecker class Typo3Update_Sniffs_LegacyClassname_MissingNamespaceSniff implements PhpCsSniff
{ {
/** /**
* Returns the token types that this sniff is interested in. * Returns the token types that this sniff is interested in.
@ -66,28 +67,16 @@ class Typo3Update_Sniffs_Classname_MissingNamespaceSniff extends AbstractClassna
} }
$classname = $tokens[$classnamePosition]['content']; $classname = $tokens[$classnamePosition]['content'];
// TODO: Migrate class, use custom feature as some parts are different! $fix = $phpcsFile->addFixableError(
// $this->addFixableError($phpcsFile, $classnamePosition, $classname); 'Legacy class definitions are not allowed; found "%s".'
} . ' Wrap your class inside a namespace.',
$classnamePosition,
/** 'legacyClassname',
* Overwrite as we don't look up the classname, but check whether the style is legacy. [$classname]
* );
* @param string $classname if ($fix === true) {
* @return bool $this->replaceLegacyClassname($phpcsFile, $classnamePosition, $classname);
*/ }
protected function isLegacyClassname($classname)
{
return strpos($classname, 'Tx_') === 0;
}
/**
* @param string $classname
* @return string
*/
protected function getNewClassname($classname)
{
return substr($classname, strrpos($classname, '_') + 1);
} }
/** /**
@ -95,18 +84,12 @@ class Typo3Update_Sniffs_Classname_MissingNamespaceSniff extends AbstractClassna
* @param PhpCsFile $phpcsFile * @param PhpCsFile $phpcsFile
* @param int $classnamePosition * @param int $classnamePosition
* @param string $classname * @param string $classname
* @param bool $forceEmptyPrefix Defines whether '\\' prefix should be checked or always be left out.
*
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/ */
protected function replaceLegacyClassname( protected function replaceLegacyClassname(
PhpCsFile $phpcsFile, PhpCsFile $phpcsFile,
$classnamePosition, $classnamePosition,
$classname, $classname
$forceEmptyPrefix = true
) { ) {
parent::replaceLegacyClassname($phpcsFile, $classnamePosition, $classname, $forceEmptyPrefix);
$tokens = $phpcsFile->getTokens(); $tokens = $phpcsFile->getTokens();
$lineEndings = PhpCsFile::detectLineEndings($phpcsFile->getFilename()); $lineEndings = PhpCsFile::detectLineEndings($phpcsFile->getFilename());
$suffix = $lineEndings; $suffix = $lineEndings;
@ -115,16 +98,29 @@ class Typo3Update_Sniffs_Classname_MissingNamespaceSniff extends AbstractClassna
$suffix .= $lineEndings; $suffix .= $lineEndings;
} }
$phpcsFile->fixer->replaceToken(
$classnamePosition,
substr($classname, strrpos($classname, '_') + 1)
);
$phpcsFile->fixer->replaceToken( $phpcsFile->fixer->replaceToken(
$this->getNamespacePosition($phpcsFile), $this->getNamespacePosition($phpcsFile),
'<?php' . $lineEndings . $this->getNamespaceDefinition($classname) . $suffix '<?php' . $lineEndings . $this->getNamespaceDefinition($classname) . $suffix
); );
$this->addLegacyClassname( LegacyClassnameMapping::getInstance()->addLegacyClassname(
$classname, $classname,
$this->getNamespace($classname) . '\\' . $this->getNewClassname($classname) $this->getNamespace($classname) . '\\' . $this->getNewClassname($classname)
); );
} }
/**
* @param string $classname
* @return string
*/
protected function getNewClassname($classname)
{
return substr($classname, strrpos($classname, '_') + 1);
}
/** /**
* @param PhpCsFile $phpcsFile * @param PhpCsFile $phpcsFile
* @return int|false * @return int|false

View file

@ -12,8 +12,4 @@
<property name="allowedTags" type="array" value="@param,@return,@var,@see,@throws"/> <property name="allowedTags" type="array" value="@param,@return,@var,@see,@throws"/>
</properties> </properties>
</rule> </rule>
<rule ref="Typo3Update.Classname.MissingNamespace.legacyClassname">
<message>Legacy class definitions are not allowed; found "%s". Wrap your class inside a namespace.</message>
</rule>
</ruleset> </ruleset>

View file

@ -1,4 +1,4 @@
--- tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/MissingNamespaceSniff/customVendor/InputFileForIssues.php --- tests/Fixtures/Standards/Typo3Update/Sniffs/LegacyClassname/MissingNamespaceSniff/customVendor/InputFileForIssues.php
+++ PHP_CodeSniffer +++ PHP_CodeSniffer
@@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
<?php <?php

View file

@ -9,7 +9,7 @@
"line": 24, "line": 24,
"message": "Legacy class definitions are not allowed; found \"Tx_ExtName_Controller_Frontendcontroller\". Wrap your class inside a namespace.", "message": "Legacy class definitions are not allowed; found \"Tx_ExtName_Controller_Frontendcontroller\". Wrap your class inside a namespace.",
"severity": 5, "severity": 5,
"source": "Typo3Update.Classname.MissingNamespace.legacyClassname", "source": "Typo3Update.LegacyClassname.MissingNamespace.legacyClassname",
"type": "ERROR" "type": "ERROR"
} }
], ],

View file

@ -1,4 +1,4 @@
--- tests/Fixtures/Standards/Typo3Update/Sniffs/Classname/MissingNamespaceSniff/defaultVendor/InputFileForIssues.php --- tests/Fixtures/Standards/Typo3Update/Sniffs/LegacyClassname/MissingNamespaceSniff/defaultVendor/InputFileForIssues.php
+++ PHP_CodeSniffer +++ PHP_CodeSniffer
@@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
<?php <?php

View file

@ -9,7 +9,7 @@
"line": 24, "line": 24,
"message": "Legacy class definitions are not allowed; found \"Tx_ExtName_Controller_Frontendcontroller\". Wrap your class inside a namespace.", "message": "Legacy class definitions are not allowed; found \"Tx_ExtName_Controller_Frontendcontroller\". Wrap your class inside a namespace.",
"severity": 5, "severity": 5,
"source": "Typo3Update.Classname.MissingNamespace.legacyClassname", "source": "Typo3Update.LegacyClassname.MissingNamespace.legacyClassname",
"type": "ERROR" "type": "ERROR"
} }
], ],