TASK: Refactor options access

* No longer use trait but static methods due to lack of Dependency
  Injection.
* Adjust all calls.
* Also don't use yield any longer but return array of file names.

Relates: #42
This commit is contained in:
Daniel Siepmann 2017-04-06 08:56:46 +02:00
parent b99438fff9
commit 20952b7710
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
8 changed files with 35 additions and 35 deletions

View file

@ -24,15 +24,12 @@ use PHP_CodeSniffer as PhpCs;
use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_File as PhpCsFile;
use PHP_CodeSniffer_Sniff as PhpCsSniff; use PHP_CodeSniffer_Sniff as PhpCsSniff;
use Typo3Update\Sniffs\LegacyClassnames\Mapping; use Typo3Update\Sniffs\LegacyClassnames\Mapping;
use Typo3Update\Sniffs\OptionsAccessTrait;
/** /**
* Provide common uses for all sniffs, regarding class name checks. * Provide common uses for all sniffs, regarding class name checks.
*/ */
abstract class AbstractClassnameChecker implements PhpCsSniff abstract class AbstractClassnameChecker implements PhpCsSniff
{ {
use OptionsAccessTrait;
/** /**
* A list of extension names that might contain legacy class names. * A list of extension names that might contain legacy class names.
* Used to check clas names for warnings. * Used to check clas names for warnings.

View file

@ -20,7 +20,7 @@ namespace Typo3Update\Sniffs\LegacyClassnames;
* 02110-1301, USA. * 02110-1301, USA.
*/ */
use Typo3Update\Sniffs\OptionsAccessTrait; use Typo3Update\Sniffs\Options;
/** /**
* Singleton wrapper for mappings. * Singleton wrapper for mappings.
@ -30,8 +30,6 @@ use Typo3Update\Sniffs\OptionsAccessTrait;
*/ */
final class Mapping final class Mapping
{ {
use OptionsAccessTrait;
// Singleton implementation - Start // Singleton implementation - Start
static protected $instance = null; static protected $instance = null;
/** /**
@ -55,7 +53,7 @@ final class Mapping
} }
private function __construct() private function __construct()
{ {
$this->mappings = require $this->getMappingFile(); $this->mappings = require Options::getMappingFile();
} }
// Singleton implementation - End // Singleton implementation - End
@ -116,7 +114,7 @@ final class Mapping
} }
file_put_contents( file_put_contents(
$this->getMappingFile(), Options::getMappingFile(),
'<?php' . PHP_EOL . 'return ' . var_export($this->mappings, true) . ';' '<?php' . PHP_EOL . 'return ' . var_export($this->mappings, true) . ';'
); );
} }

View file

@ -21,6 +21,7 @@
use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_File as PhpCsFile;
use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker; use Typo3Update\Sniffs\LegacyClassnames\AbstractClassnameChecker;
use Typo3Update\Sniffs\Options;
/** /**
* Detect missing namespaces for class definitions. * Detect missing namespaces for class definitions.
@ -153,7 +154,7 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingNamespaceSniff extends Abstract
*/ */
protected function getNamespace($classname) protected function getNamespace($classname)
{ {
$vendor = trim($this->getVendor(), '\\/'); $vendor = trim(Options::getVendor(), '\\/');
$classnameParts = explode('_', $classname); $classnameParts = explode('_', $classname);
unset($classnameParts[0]); // Remove Tx_ unset($classnameParts[0]); // Remove Tx_

View file

@ -22,6 +22,7 @@
use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_File as PhpCsFile;
use PHP_CodeSniffer_Sniff as PhpCsSniff; use PHP_CodeSniffer_Sniff as PhpCsSniff;
use PHP_CodeSniffer_Tokens as Tokens; use PHP_CodeSniffer_Tokens as Tokens;
use Typo3Update\Sniffs\Options;
/** /**
* Detect whether vendor is missing for plugins and modules registrations and configurations. * Detect whether vendor is missing for plugins and modules registrations and configurations.
@ -29,7 +30,6 @@ use PHP_CodeSniffer_Tokens as Tokens;
class Typo3Update_Sniffs_LegacyClassnames_MissingVendorForPluginsAndModulesSniff implements PhpCsSniff class Typo3Update_Sniffs_LegacyClassnames_MissingVendorForPluginsAndModulesSniff implements PhpCsSniff
{ {
use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait;
use \Typo3Update\Sniffs\OptionsAccessTrait;
/** /**
* Returns the token types that this sniff is interested in. * Returns the token types that this sniff is interested in.
@ -72,13 +72,13 @@ class Typo3Update_Sniffs_LegacyClassnames_MissingVendorForPluginsAndModulesSniff
. ' Add vendor before Extensionkey like: "%s." . $_EXTKEY', . ' Add vendor before Extensionkey like: "%s." . $_EXTKEY',
$firstArgument, $firstArgument,
'missingVendor', 'missingVendor',
[$this->getVendor()] [Options::getVendor()]
); );
if ($fix === true) { if ($fix === true) {
$phpcsFile->fixer->replaceToken( $phpcsFile->fixer->replaceToken(
$firstArgument, $firstArgument,
"'{$this->getVendor()}.' . {$tokens[$firstArgument]['content']}" "'{Options::getVendor()}.' . {$tokens[$firstArgument]['content']}"
); );
} }
} }

View file

@ -25,16 +25,16 @@ use PHP_CodeSniffer as PhpCs;
/** /**
* Wrapper to retrieve options from PhpCs with defaults. * Wrapper to retrieve options from PhpCs with defaults.
*/ */
trait OptionsAccessTrait class Options
{ {
/** /**
* Returns the configured vendor, e.g. to generate new namespaces. * Returns the configured vendor, e.g. to generate new namespaces.
* *
* @return string * @return string
*/ */
public function getVendor() public static function getVendor()
{ {
$vendor = $this->getOptionWithDefault( $vendor = static::getOptionWithDefault(
'vendor', 'vendor',
'YourCompany' 'YourCompany'
); );
@ -47,9 +47,9 @@ trait OptionsAccessTrait
* *
* @return string * @return string
*/ */
public function getMappingFile() public static function getMappingFile()
{ {
return (string) $this->getOptionWithDefault( return (string) static::getOptionWithDefault(
'mappingFile', 'mappingFile',
__DIR__ . '/../../../../LegacyClassnames.php' __DIR__ . '/../../../../LegacyClassnames.php'
); );
@ -58,11 +58,11 @@ trait OptionsAccessTrait
/** /**
* Returns an array of absolute file names containing removed function configurations. * Returns an array of absolute file names containing removed function configurations.
* *
* @return \Generator * @return array<string>
*/ */
public function getRemovedFunctionConfigFiles() public static function getRemovedFunctionConfigFiles()
{ {
$this->getOptionFileNames( return static::getOptionFileNames(
'removedFunctionConfigFiles', 'removedFunctionConfigFiles',
__DIR__ . '/../Configuration/Removed/Functions/*.yaml' __DIR__ . '/../Configuration/Removed/Functions/*.yaml'
); );
@ -71,11 +71,11 @@ trait OptionsAccessTrait
/** /**
* Returns an array of absolute file names containing removed constant configurations. * Returns an array of absolute file names containing removed constant configurations.
* *
* @return \Generator * @return array<string>
*/ */
public function getRemovedConstantConfigFiles() public static function getRemovedConstantConfigFiles()
{ {
$this->getOptionFileNames( return static::getOptionFileNames(
'removedConstantConfigFiles', 'removedConstantConfigFiles',
__DIR__ . '/../Configuration/Removed/Constants/*.yaml' __DIR__ . '/../Configuration/Removed/Constants/*.yaml'
); );
@ -89,7 +89,7 @@ trait OptionsAccessTrait
* *
* @return mixed * @return mixed
*/ */
private function getOptionWithDefault($optionName, $default) private static function getOptionWithDefault($optionName, $default)
{ {
$option = PhpCs::getConfigData($optionName); $option = PhpCs::getConfigData($optionName);
if (!$option) { if (!$option) {
@ -102,17 +102,23 @@ trait OptionsAccessTrait
/** /**
* Get file names defined by option using optionName, if not defined, use default. * Get file names defined by option using optionName, if not defined, use default.
* *
* TODO: Multiple files allowed, using glob ...
* to allow splitting per ext (extbase, fluid, ...) and TYPO3 Version 7.1, 7.0, ...
*
* @param string $optionName * @param string $optionName
* @param mixed $default * @param mixed $default
* *
* @return \Generator * @return array<string>
*/ */
private function getOptionFileNames($optionName, $default) protected static function getOptionFileNames($optionName, $default)
{ {
$files = $this->getOptionWithDefault($optionName, $default); $files = static::getOptionWithDefault($optionName, $default);
$fileNames = [];
foreach ((new \GlobIterator($files)) as $file) { foreach ((new \GlobIterator($files)) as $file) {
yield (string) $file; $fileNames[] = (string) $file;
} }
return $fileNames;
} }
} }

View file

@ -24,6 +24,7 @@ use PHP_CodeSniffer_File as PhpCsFile;
use PHP_CodeSniffer_Sniff as PhpCsSniff; use PHP_CodeSniffer_Sniff as PhpCsSniff;
use PHP_CodeSniffer_Tokens as Tokens; use PHP_CodeSniffer_Tokens as Tokens;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
use Typo3Update\Sniffs\Options;
/** /**
* Contains common functionality for removed code like constants or functions. * Contains common functionality for removed code like constants or functions.
@ -34,7 +35,6 @@ use Symfony\Component\Yaml\Yaml;
abstract class AbstractGenericUsage implements PhpCsSniff abstract class AbstractGenericUsage implements PhpCsSniff
{ {
use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait; use \Typo3Update\Sniffs\ExtendedPhpCsSupportTrait;
use \Typo3Update\Sniffs\OptionsAccessTrait;
/** /**
* Configuration to define removed code. * Configuration to define removed code.
@ -49,10 +49,6 @@ abstract class AbstractGenericUsage implements PhpCsSniff
*/ */
protected $removed = []; protected $removed = [];
/**
* TODO: Multiple files allowed, using glob ...
* to allow splitting per ext (extbase, fluid, ...) and TYPO3 Version 7.1, 7.0, ...
*/
public function __construct() public function __construct()
{ {
if ($this->configured === []) { if ($this->configured === []) {

View file

@ -21,6 +21,7 @@
use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_File as PhpCsFile;
use Typo3Update\Sniffs\Removed\AbstractGenericUsage; use Typo3Update\Sniffs\Removed\AbstractGenericUsage;
use Typo3Update\Sniffs\Options;
/** /**
* Sniff that handles all calls to removed constants. * Sniff that handles all calls to removed constants.
@ -34,7 +35,7 @@ class Typo3Update_Sniffs_Removed_GenericConstantUsageSniff extends AbstractGener
*/ */
protected function getRemovedConfigFiles() protected function getRemovedConfigFiles()
{ {
return $this->getRemovedConstantConfigFiles(); return Options::getRemovedConstantConfigFiles();
} }
/** /**

View file

@ -22,6 +22,7 @@
use PHP_CodeSniffer_File as PhpCsFile; use PHP_CodeSniffer_File as PhpCsFile;
use PHP_CodeSniffer_Tokens as Tokens; use PHP_CodeSniffer_Tokens as Tokens;
use Typo3Update\Sniffs\Removed\AbstractGenericUsage; use Typo3Update\Sniffs\Removed\AbstractGenericUsage;
use Typo3Update\Sniffs\Options;
/** /**
* Sniff that handles all calls to removed functions. * Sniff that handles all calls to removed functions.
@ -35,7 +36,7 @@ class Typo3Update_Sniffs_Removed_GenericFunctionCallSniff extends AbstractGeneri
*/ */
protected function getRemovedConfigFiles() protected function getRemovedConfigFiles()
{ {
return $this->getRemovedFunctionConfigFiles(); return Options::getRemovedFunctionConfigFiles();
} }
/** /**