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.
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?
=======================

View file

@ -34,10 +34,11 @@ trait OptionsAccessTrait
*/
public function getVendor()
{
$vendor = PhpCs::getConfigData('vendor');
if (!$vendor) {
$vendor = 'YourCompany';
}
$vendor = $this->getOptionWithDefault(
'vendor',
'YourCompany'
);
return trim($vendor, '\\/');
}
@ -48,11 +49,10 @@ trait OptionsAccessTrait
*/
public function getMappingFile()
{
$mappingFile = PhpCs::getConfigData('mappingFile');
if (!$mappingFile) {
$mappingFile = __DIR__ . '/../../../../LegacyClassnames.php';
}
return $mappingFile;
return (string) $this->getOptionWithDefault(
'mappingFile',
__DIR__ . '/../../../../LegacyClassnames.php'
);
}
/**
@ -62,14 +62,10 @@ trait OptionsAccessTrait
*/
public function getRemovedFunctionConfigFiles()
{
$configFiles = PhpCs::getConfigData('removedFunctionConfigFiles');
if (!$configFiles) {
$configFiles = __DIR__ . '/../Configuration/Removed/Functions/*.yaml';
}
foreach ((new \GlobIterator($configFiles)) as $file) {
yield (string) $file;
}
$this->getOptionFileNames(
'removedFunctionConfigFiles',
__DIR__ . '/../Configuration/Removed/Functions/*.yaml'
);
}
/**
@ -79,12 +75,43 @@ trait OptionsAccessTrait
*/
public function getRemovedConstantConfigFiles()
{
$configFiles = PhpCs::getConfigData('removedConstantConfigFiles');
if (!$configFiles) {
$configFiles = __DIR__ . '/../Configuration/Removed/Constants/*.yaml';
$this->getOptionFileNames(
'removedConstantConfigFiles',
__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;
}
}