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

Commit 93e44ec

Browse files
committed
Merge pull request #12 from hacfi/skip_blank_groups
Add option "group-skip-empty" to skip empty groups
2 parents 05908d0 + 95f8483 commit 93e44ec

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/PHPFormatter/Command/UseSortCommand.php

+14
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ protected function configure()
7878
InputOption::VALUE_OPTIONAL,
7979
"Type of grouping"
8080
)
81+
->addOption(
82+
'group-skip-empty',
83+
null,
84+
InputOption::VALUE_NONE,
85+
"Skip empty groups"
86+
)
8187
->addOption(
8288
'--config',
8389
'-c',
@@ -182,12 +188,14 @@ private function getUsableConfig(InputInterface $input)
182188
array(
183189
'group' => $input->getOption('group'),
184190
'group-type' => $input->getOption('group-type'),
191+
'group-skip-empty' => $input->getOption('group-skip-empty'),
185192
'sort-type' => $input->getOption('sort-type'),
186193
'sort-direction' => $input->getOption('sort-direction')
187194
),
188195
array(
189196
'group' => array('_main'),
190197
'group-type' => UseSorter::GROUP_TYPE_EACH,
198+
'group-skip-empty' => false,
191199
'sort-type' => UseSorter::SORT_TYPE_ALPHABETIC,
192200
'sort-direction' => UseSorter::SORT_DIRECTION_ASC
193201
)
@@ -263,6 +271,11 @@ private function printConfigUsed(
263271
$output->writeln('# --group-type="' . $options['group-type'] . '"');
264272
}
265273

274+
if (!empty($options['group-skip-empty'])) {
275+
276+
$output->writeln('# --group-skip-empty="' . $options['group-skip-empty'] . '"');
277+
}
278+
266279
if (!empty($options['sort-type'])) {
267280

268281
$output->writeln('# --sort-type="' . $options['sort-type'] . '"');
@@ -344,6 +357,7 @@ private function createUseSorter(array $options)
344357
$useSorter
345358
->setGroups($options['group'])
346359
->setGroupType($options['group-type'])
360+
->setGroupSkipEmpty($options['group-skip-empty'])
347361
->setSortType($options['sort-type'])
348362
->setSortDirection($options['sort-direction']);
349363

src/PHPFormatter/Sorter/UseSorter.php

+26
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ class UseSorter implements SorterInterface
9696
*/
9797
protected $groupType = self::GROUP_TYPE_EACH;
9898

99+
/**
100+
* @var boolean
101+
*
102+
* Skip empty groups
103+
*/
104+
protected $groupSkipEmpty = false;
105+
99106
/**
100107
* Sets Groups
101108
*
@@ -152,6 +159,21 @@ public function setGroupType($groupType)
152159
return $this;
153160
}
154161

162+
/**
163+
* Sets GroupSkipEmpty
164+
*
165+
* @param boolean $groupSkipEmpty
166+
*
167+
* @return UseSorter Self object
168+
*/
169+
public function setGroupSkipEmpty($groupSkipEmpty)
170+
{
171+
$this->groupSkipEmpty = $groupSkipEmpty;
172+
173+
return $this;
174+
}
175+
176+
155177
/**
156178
* Sort any piece of code given as parameter
157179
*
@@ -208,6 +230,10 @@ public function sort($data)
208230
* Every block is sorted as desired
209231
*/
210232
foreach ($groups as $groupKey => $group) {
233+
if ($this->groupSkipEmpty && empty($group)) {
234+
unset($groups[$groupKey]);
235+
continue;
236+
}
211237

212238
$groups[$groupKey] = $this->sortGroup($group);
213239
}

0 commit comments

Comments
 (0)