Skip to content

Commit

Permalink
fix(config): possibile issues with empty data
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Cesarato committed Jan 22, 2021
1 parent c3bffa7 commit 179c1fa
Showing 1 changed file with 47 additions and 18 deletions.
65 changes: 47 additions & 18 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,19 @@ class Configuration
'/^chore\(release\):/i',
];

/**
* Ignore types.
*
* @var string[]
*/
public $ignoreTypes = ['build', 'chore', 'ci', 'docs', 'refactor', 'revert', 'style', 'test'];

/**
* Constructor.
*/
public function __construct(array $settings = [])
{
$this->setTypes($this->preset);
$this->fromArray($settings);
}

Expand All @@ -95,7 +103,8 @@ public function fromArray(array $array)
'path' => $this->getPath(),
'preset' => $this->getPreset(),
'types' => [],
'ignoreTypes' => ['build', 'chore', 'ci', 'docs', 'refactor', 'revert', 'style', 'test'],
'ignoreTypes' => $this->getIgnoreTypes(),
'ignorePatterns' => $this->getIgnorePatterns(),
];

$params = array_replace_recursive($defaults, $array);
Expand All @@ -104,21 +113,6 @@ public function fromArray(array $array)
if (!empty($array['ignoreTypes'])) {
$params['ignoreTypes'] = $array['ignoreTypes']; // Overwrite ignored types
}
if (is_array($params['ignoreTypes'])) {
foreach ($params['ignoreTypes'] as $type) {
unset($params['preset'][$type]); // Unset excluded types
}
}

// Ignore Patterns
if (!empty($array['ignorePatterns'])) {
$params['ignorePatterns'] = array_merge($this->ignorePatterns, $array['ignorePatterns']);
}
foreach ($params['ignorePatterns'] as $key => $pattern) {
if (!$this->isRegex($pattern)) { // Check ignore patterns
$params['ignorePatterns'][$key] = '#' . preg_quote($pattern, '#') . '#i';
}
}

// Set Types (overwrite ignored types)
if (!empty($array['types'])) {
Expand All @@ -136,11 +130,12 @@ public function fromArray(array $array)
// Add breaking changes
$params['preset'] = array_merge($this->breakingPreset, $params['preset']);

$this->setPath($params['path']);
$this->setIgnorePatterns($params['ignorePatterns']);
$this->setIgnoreTypes($params['ignoreTypes']);
$this->setTypes($params['preset']);
$this->setHeaderTitle($params['headerTitle']);
$this->setHeaderDescription($params['headerDescription']);
$this->setPath($params['path']);
$this->setIgnorePatterns($params['ignorePatterns']);
}

/**
Expand Down Expand Up @@ -195,6 +190,11 @@ public function getTypeDescription(string $type): string
*/
public function setTypes(array $types): Configuration
{
$ignoreTypes = $this->getIgnoreTypes();
foreach ($ignoreTypes as $type) {
unset($types[$type]); // Unset excluded types
}

$this->types = $types;

return $this;
Expand Down Expand Up @@ -249,6 +249,11 @@ public function getIgnorePatterns(): array
*/
public function setIgnorePatterns(array $ignorePatterns): Configuration
{
foreach ($ignorePatterns as $key => $pattern) {
if (!$this->isRegex($pattern)) {
$ignorePatterns[$key] = '#' . preg_quote($pattern, '#') . '#i';
}
}
$this->ignorePatterns = $ignorePatterns;

return $this;
Expand All @@ -261,4 +266,28 @@ public function getPreset(): array
{
return array_merge($this->breakingPreset, $this->preset);
}

/**
* @param string[] $ignoreTypes
*/
public function setIgnoreTypes(array $ignoreTypes): Configuration
{
$types = $this->getTypes();
foreach ($ignoreTypes as $type) {
unset($types[$type]); // Unset excluded types
}

$this->ignoreTypes = $ignoreTypes;
$this->types = $types;

return $this;
}

/**
* @return string[]
*/
public function getIgnoreTypes(): array
{
return $this->ignoreTypes;
}
}

0 comments on commit 179c1fa

Please sign in to comment.