Skip to content
This repository was archived by the owner on Feb 21, 2022. It is now read-only.

Add option "group-skip-empty" to skip empty groups #12

Merged
merged 1 commit into from
Dec 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/PHPFormatter/Command/UseSortCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ protected function configure()
InputOption::VALUE_OPTIONAL,
"Type of grouping"
)
->addOption(
'group-skip-empty',
null,
InputOption::VALUE_NONE,
"Skip empty groups"
)
->addOption(
'--config',
'-c',
Expand Down Expand Up @@ -182,12 +188,14 @@ private function getUsableConfig(InputInterface $input)
array(
'group' => $input->getOption('group'),
'group-type' => $input->getOption('group-type'),
'group-skip-empty' => $input->getOption('group-skip-empty'),
'sort-type' => $input->getOption('sort-type'),
'sort-direction' => $input->getOption('sort-direction')
),
array(
'group' => array('_main'),
'group-type' => UseSorter::GROUP_TYPE_EACH,
'group-skip-empty' => false,
'sort-type' => UseSorter::SORT_TYPE_ALPHABETIC,
'sort-direction' => UseSorter::SORT_DIRECTION_ASC
)
Expand Down Expand Up @@ -263,6 +271,11 @@ private function printConfigUsed(
$output->writeln('# --group-type="' . $options['group-type'] . '"');
}

if (!empty($options['group-skip-empty'])) {

$output->writeln('# --group-skip-empty="' . $options['group-skip-empty'] . '"');
}

if (!empty($options['sort-type'])) {

$output->writeln('# --sort-type="' . $options['sort-type'] . '"');
Expand Down Expand Up @@ -344,6 +357,7 @@ private function createUseSorter(array $options)
$useSorter
->setGroups($options['group'])
->setGroupType($options['group-type'])
->setGroupSkipEmpty($options['group-skip-empty'])
->setSortType($options['sort-type'])
->setSortDirection($options['sort-direction']);

Expand Down
26 changes: 26 additions & 0 deletions src/PHPFormatter/Sorter/UseSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ class UseSorter implements SorterInterface
*/
protected $groupType = self::GROUP_TYPE_EACH;

/**
* @var boolean
*
* Skip empty groups
*/
protected $groupSkipEmpty = false;

/**
* Sets Groups
*
Expand Down Expand Up @@ -152,6 +159,21 @@ public function setGroupType($groupType)
return $this;
}

/**
* Sets GroupSkipEmpty
*
* @param boolean $groupSkipEmpty
*
* @return UseSorter Self object
*/
public function setGroupSkipEmpty($groupSkipEmpty)
{
$this->groupSkipEmpty = $groupSkipEmpty;

return $this;
}


/**
* Sort any piece of code given as parameter
*
Expand Down Expand Up @@ -208,6 +230,10 @@ public function sort($data)
* Every block is sorted as desired
*/
foreach ($groups as $groupKey => $group) {
if ($this->groupSkipEmpty && empty($group)) {
unset($groups[$groupKey]);
continue;
}

$groups[$groupKey] = $this->sortGroup($group);
}
Expand Down