TASK: Refactor remove duplicate logic

* Make option trait cleaner, remove duplicate logic and use methods for
  same logic.
* Also fix issue in readme pointing to non existing config name.

Relates: #42
This commit is contained in:
Daniel Siepmann 2017-04-06 08:26:13 +02:00
parent 4a7ffaa92d
commit b99438fff9
Signed by: Daniel Siepmann
GPG key ID: 33D6629915560EF4
2 changed files with 49 additions and 22 deletions

View file

@ -110,7 +110,7 @@ available to generate a report of possible issues and during coding through ``ph
- Check for usage of removed constants. - Check for usage of removed constants.
The constants are configured in same way as removed functions. The constants are configured in same way as removed functions.
For configuration options see ``removedConstantsConfigFiles``. For configuration options see ``removedConstantConfigFiles``.
What does it look like? What does it look like?
======================= =======================

View file

@ -34,10 +34,11 @@ trait OptionsAccessTrait
*/ */
public function getVendor() public function getVendor()
{ {
$vendor = PhpCs::getConfigData('vendor'); $vendor = $this->getOptionWithDefault(
if (!$vendor) { 'vendor',
$vendor = 'YourCompany'; 'YourCompany'
} );
return trim($vendor, '\\/'); return trim($vendor, '\\/');
} }
@ -48,11 +49,10 @@ trait OptionsAccessTrait
*/ */
public function getMappingFile() public function getMappingFile()
{ {
$mappingFile = PhpCs::getConfigData('mappingFile'); return (string) $this->getOptionWithDefault(
if (!$mappingFile) { 'mappingFile',
$mappingFile = __DIR__ . '/../../../../LegacyClassnames.php'; __DIR__ . '/../../../../LegacyClassnames.php'
} );
return $mappingFile;
} }
/** /**
@ -62,14 +62,10 @@ trait OptionsAccessTrait
*/ */
public function getRemovedFunctionConfigFiles() public function getRemovedFunctionConfigFiles()
{ {
$configFiles = PhpCs::getConfigData('removedFunctionConfigFiles'); $this->getOptionFileNames(
if (!$configFiles) { 'removedFunctionConfigFiles',
$configFiles = __DIR__ . '/../Configuration/Removed/Functions/*.yaml'; __DIR__ . '/../Configuration/Removed/Functions/*.yaml'
} );
foreach ((new \GlobIterator($configFiles)) as $file) {
yield (string) $file;
}
} }
/** /**
@ -79,12 +75,43 @@ trait OptionsAccessTrait
*/ */
public function getRemovedConstantConfigFiles() public function getRemovedConstantConfigFiles()
{ {
$configFiles = PhpCs::getConfigData('removedConstantConfigFiles'); $this->getOptionFileNames(
if (!$configFiles) { 'removedConstantConfigFiles',
$configFiles = __DIR__ . '/../Configuration/Removed/Constants/*.yaml'; __DIR__ . '/../Configuration/Removed/Constants/*.yaml'
);
}
/**
* Get the option by optionName, if not defined, use default.
*
* @param string $optionName
* @param mixed $default
*
* @return mixed
*/
private function getOptionWithDefault($optionName, $default)
{
$option = PhpCs::getConfigData($optionName);
if (!$option) {
$option = $default;
} }
foreach ((new \GlobIterator($configFiles)) as $file) { return $option;
}
/**
* Get file names defined by option using optionName, if not defined, use default.
*
* @param string $optionName
* @param mixed $default
*
* @return \Generator
*/
private function getOptionFileNames($optionName, $default)
{
$files = $this->getOptionWithDefault($optionName, $default);
foreach ((new \GlobIterator($files)) as $file) {
yield (string) $file; yield (string) $file;
} }
} }