diff --git a/src/Changelog/ChangeLogUtil.php b/src/Changelog/ChangeLogUtil.php index cac4c7a..17307d2 100644 --- a/src/Changelog/ChangeLogUtil.php +++ b/src/Changelog/ChangeLogUtil.php @@ -15,16 +15,29 @@ class ChangeLogUtil { /** * @param string $msg + * @param array{startWiths: list, contains: list} $rule * * @return bool */ - public static function isFixMsg(string $msg): bool + public static function matchMsgByRule(string $msg, array $rule): bool { - if (stripos($msg, 'bug') === 0 || stripos($msg, 'close') === 0 || stripos($msg, 'fix') === 0) { - return true; + if (isset($rule['startWiths'])) { + foreach ($rule['startWiths'] as $start) { + if (stripos($msg, $start) === 0) { + return true; + } + } } - return stripos($msg, ' fix') > 0; + if (isset($rule['contains'])) { + foreach ($rule['contains'] as $sub) { + if (stripos($msg, $sub) > 0) { + return true; + } + } + } + + return false; } /** @@ -38,5 +51,4 @@ public static function mkdir(string $path, int $mode = 0775, bool $recursive = t { return (is_dir($path) || !(!@mkdir($path, $mode, $recursive) && !is_dir($path))) && is_writable($path); } - } diff --git a/src/Changelog/Formatter/AbstractFormatter.php b/src/Changelog/Formatter/AbstractFormatter.php index 8a09ea2..6e3e690 100644 --- a/src/Changelog/Formatter/AbstractFormatter.php +++ b/src/Changelog/Formatter/AbstractFormatter.php @@ -5,7 +5,7 @@ use PhpGit\Changelog\ChangeLogUtil; use PhpGit\Changelog\GitChangeLog; use PhpGit\Changelog\ItemFormatterInterface; -use function stripos; +use function array_merge; /** * Class AbstractFormatter @@ -15,22 +15,60 @@ abstract class AbstractFormatter implements ItemFormatterInterface { /** - * @param string $msg + * The group name match rules. key is the group name. * - * @return string + * @var array> + * @see defaultRules() */ - public function matchGroup(string $msg): string + protected array $rules = []; + + /** + * Class constructor. + * + * @param array $rules + */ + public function __construct(array $rules = []) { - if (ChangeLogUtil::isFixMsg($msg)) { - return 'Fixed'; + if ($rules) { + $this->rules = array_merge(self::defaultRules(), $rules); + } else { + $this->rules = self::defaultRules(); } + } - if (stripos($msg, 'up') === 0 || stripos($msg, 'add') === 0 || stripos($msg, 'create') === 0) { - return 'Update'; - } + public static function defaultRules(): array + { + return [ + 'Refactor' => [ + 'startWiths' => ['break', 'refactor'], + 'contains' => [], + ], + 'Update' => [ + 'startWiths' => ['up', 'add', 'create', 'prof', 'perf', 'enhance'], + 'contains' => [], + ], + 'Feature' => [ + 'startWiths' => ['feat', 'support', 'new'], + 'contains' => [], + ], + 'Fixed' => [ + 'startWiths' => ['bug', 'fix', 'close'], + 'contains' => [' fix'] + ] + ]; + } - if (stripos($msg, 'feat') === 0 || stripos($msg, 'support') === 0 || stripos($msg, 'new') === 0) { - return 'Feature'; + /** + * @param string $msg + * + * @return string + */ + public function matchGroup(string $msg): string + { + foreach ($this->rules as $group => $rule) { + if (ChangeLogUtil::matchMsgByRule($msg, $rule)) { + return $group; + } } return GitChangeLog::OTHER_GROUP; @@ -42,4 +80,20 @@ public function matchGroup(string $msg): string * @return string[] returns [group, line string] */ abstract public function format(array $item): array; + + /** + * @return array[] + */ + public function getRules(): array + { + return $this->rules; + } + + /** + * @param array[] $rules + */ + public function setRules(array $rules): void + { + $this->rules = $rules; + } }