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

Commit 20c16cd

Browse files
matt-usurpmmoreram
authored andcommitted
Added failing tests and the fix for extra whitespace being left with multiple empty groups.
1 parent 0bc5d38 commit 20c16cd

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

src/PHPFormatter/Sorter/UseSorter.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,6 @@ public function sort($data)
229229
* Every block is sorted as desired
230230
*/
231231
foreach ($groups as $groupKey => $group) {
232-
if ($this->groupSkipEmpty && empty($group)) {
233-
unset($groups[$groupKey]);
234-
continue;
235-
}
236-
237232
if (is_int($groupKey)) {
238233
$subGroupSorted = [];
239234
foreach ($group as $subGroupKey => $subGroup) {
@@ -244,6 +239,12 @@ public function sort($data)
244239
} else {
245240
$groups[$groupKey] = $this->sortGroup($group);
246241
}
242+
243+
// Remove empty groups (if configured) after the sorting has happened.
244+
// @see https://github.com/mmoreram/php-formatter/issues/24
245+
if ($this->groupSkipEmpty && (0 === count($groups[$groupKey]))) {
246+
unset($groups[$groupKey]);
247+
}
247248
}
248249

249250
$doubleEOL = PHP_EOL . PHP_EOL;
@@ -363,6 +364,9 @@ private function sortGroup(array $group)
363364
*/
364365
private function renderGroup(array $group)
365366
{
367+
if (empty($group)) {
368+
return '';
369+
}
366370
if ($this->groupType === self::GROUP_TYPE_EACH) {
367371
return implode(PHP_EOL, array_map(function ($namespace) {
368372

tests/PHPFormatter/Sorter/UseSorterTest.php

+68
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,25 @@ public function dataSort()
326326
use Test1\\Myclass1;
327327
use Test1\\Myclass2;
328328
329+
use Test3\\File;
330+
use Test3\\MyFolder\\Myclass;
331+
use Test4\\Myclass3;
332+
",
333+
],
334+
[
335+
['Test1', ['TestEmpty1', 'TestEmpty2'], '_main'],
336+
UseSorter::SORT_TYPE_ALPHABETIC,
337+
UseSorter::SORT_DIRECTION_ASC,
338+
UseSorter::GROUP_TYPE_EACH,
339+
"
340+
use Test1\\Myclass1;
341+
use Test1\\Myclass2;
342+
use Test1\\MyFolder5\\File as MyFile;
343+
344+
345+
346+
use Test2\\Myclass3;
347+
use Test2\\Myclass4;
329348
use Test3\\File;
330349
use Test3\\MyFolder\\Myclass;
331350
use Test4\\Myclass3;
@@ -368,6 +387,55 @@ public function testGroupSkip()
368387
* Copyright
369388
*/
370389
390+
namespace PHPFormatter\\Tests\\Mocks;
391+
$result
392+
/**
393+
* Class SimpleMock
394+
*/
395+
class SimpleMock
396+
{}";
397+
398+
$this->assertEquals(
399+
$realResult,
400+
$parsedData
401+
);
402+
}
403+
404+
/**
405+
* Test skip empty where consecutive empty groups are between used groups.
406+
*
407+
* @see https://github.com/mmoreram/php-formatter/issues/24
408+
*/
409+
public function testGroupSkipWithMissingGroups()
410+
{
411+
$parsedData = $this
412+
->useSorter
413+
->setGroups(['Test1', 'TestEmpty1', ['TestEmpty2', 'TestEmpty3'], '_main'])
414+
->setSortType(UseSorter::SORT_TYPE_ALPHABETIC)
415+
->setSortDirection(UseSorter::SORT_DIRECTION_ASC)
416+
->setGroupType(UseSorter::GROUP_TYPE_EACH)
417+
->setGroupSkipEmpty(true)
418+
->sort($this->data);
419+
420+
$result =
421+
"
422+
use Test1\\Myclass1;
423+
use Test1\\Myclass2;
424+
use Test1\\MyFolder5\\File as MyFile;
425+
426+
use Test2\\Myclass3;
427+
use Test2\\Myclass4;
428+
use Test3\\File;
429+
use Test3\\MyFolder\\Myclass;
430+
use Test4\\Myclass3;
431+
";
432+
$realResult =
433+
"<?php
434+
435+
/**
436+
* Copyright
437+
*/
438+
371439
namespace PHPFormatter\\Tests\\Mocks;
372440
$result
373441
/**

0 commit comments

Comments
 (0)